diff --git a/apps/meteor/app/spotify/lib/spotify.js b/apps/meteor/app/spotify/lib/spotify.js deleted file mode 100644 index 9ff6aa3037dc..000000000000 --- a/apps/meteor/app/spotify/lib/spotify.js +++ /dev/null @@ -1,38 +0,0 @@ -const process = (message, source, callback) => { - 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 = () => (message) => { - const urls = Array.isArray(message.urls) ? message.urls : []; - - let changed = false; - - process(message, message.msg, (message, msgParts, index, part) => { - 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(':')}` }); - changed = true; - } - }); - - if (changed) { - message.urls = urls; - } - - return message; -}; diff --git a/apps/meteor/app/spotify/lib/spotify.ts b/apps/meteor/app/spotify/lib/spotify.ts new file mode 100644 index 000000000000..718be19dcb90 --- /dev/null +++ b/apps/meteor/app/spotify/lib/spotify.ts @@ -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; + }; diff --git a/apps/meteor/app/spotify/server/index.js b/apps/meteor/app/spotify/server/index.ts similarity index 100% rename from apps/meteor/app/spotify/server/index.js rename to apps/meteor/app/spotify/server/index.ts diff --git a/packages/core-typings/src/IMessage/IMessage.ts b/packages/core-typings/src/IMessage/IMessage.ts index 2a0e738fdd1c..19fc3f51ed09 100644 --- a/packages/core-typings/src/IMessage/IMessage.ts +++ b/packages/core-typings/src/IMessage/IMessage.ts @@ -12,6 +12,7 @@ type MentionType = 'user' | 'team'; type MessageUrl = { url: string; + source?: string; meta: Record; headers?: { contentLength: string; contentType: string }; };