Skip to content

Commit

Permalink
Chore: Migrate spotify to ts (#25507)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman authored May 16, 2022
1 parent 5bd2660 commit 3b9254e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
38 changes: 0 additions & 38 deletions apps/meteor/app/spotify/lib/spotify.js

This file was deleted.

46 changes: 46 additions & 0 deletions apps/meteor/app/spotify/lib/spotify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { IMessage } from '@rocket.chat/core-typings';

const process = (
message: IMessage,
source: string,
callback: (msg: IMessage, msgParts: string[], index: number, part: string) => void,
): void => {
if (!source?.trim()) {
return;
}

const msgParts = source.split(/(```\w*[\n ]?[\s\S]*?```+?)|(`(?:[^`]+)`)/);
for (let index = 0; index < msgParts.length; index++) {
const part = msgParts[index];
if (!/(?:```(\w*)[\n ]?([\s\S]*?)```+?)|(?:`(?:[^`]+)`)/.test(part)) {
callback(message, msgParts, index, part);
}
}
};

export const createSpotifyBeforeSaveMessageHandler =
(): ((msg: IMessage) => IMessage) =>
(message: IMessage): IMessage => {
const urls = Array.isArray(message.urls) ? message.urls : [];

let changed = false;

process(message, message.msg, (_message: IMessage, _msgParts: string[], _index: number, part: string) => {
const re = /(?:^|\s)spotify:([^:\s]+):([^:\s]+)(?::([^:\s]+))?(?::(\S+))?(?:\s|$)/g;

let match;
while ((match = re.exec(part)) != null) {
const data = match.slice(1).filter(Boolean);
const path = data.map((value) => encodeURI(value)).join('/');
const url = `https://open.spotify.com/${path}`;
urls.push({ url, source: `spotify:${data.join(':')}`, meta: {} });
changed = true;
}
});

if (changed) {
message.urls = urls;
}

return message;
};
File renamed without changes.
1 change: 1 addition & 0 deletions packages/core-typings/src/IMessage/IMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type MentionType = 'user' | 'team';

type MessageUrl = {
url: string;
source?: string;
meta: Record<string, string>;
headers?: { contentLength: string; contentType: string };
};
Expand Down

0 comments on commit 3b9254e

Please sign in to comment.