-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: Migrate spotify to ts (#25507)
- Loading branch information
Showing
4 changed files
with
47 additions
and
38 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters