Skip to content

Commit

Permalink
[FIX] Spotify oEmbed (#19825)
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan authored Dec 10, 2020
1 parent 7a21974 commit 3460771
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 129 deletions.
18 changes: 5 additions & 13 deletions app/oembed/client/baseWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,19 @@ createCollapseable(Template.oembedBaseWidget, (instance) => instance.data.settin
Template.oembedBaseWidget.helpers({
template() {
const { collapsedMedia } = Template.instance();
this.collapsedMediaVar = function() { return collapsedMedia; };
this.collapsedMediaVar = () => collapsedMedia;
this.collapsed = collapsedMedia.get();

let contentType;
if (this.headers) {
contentType = this.headers.contentType;
}

if (this._overrideTemplate) {
return this._overrideTemplate;
}
if (this.headers && contentType && contentType.match(/image\/.*/)) {
if (this.headers?.contentType?.match(/image\/.*/)) {
return 'oembedImageWidget';
}
if (this.headers && contentType && contentType.match(/audio\/.*/)) {
if (this.headers?.contentType?.match(/audio\/.*/)) {
return 'oembedAudioWidget';
}
if ((this.headers && contentType && contentType.match(/video\/.*/)) || (this.meta && this.meta.twitterPlayerStreamContentType && this.meta.twitterPlayerStreamContentType.match(/video\/.*/))) {
if (this.headers?.contentType?.match(/video\/.*/) || this.meta?.twitterPlayerStreamContentType?.match(/video\/.*/)) {
return 'oembedVideoWidget';
}
if (this.meta && this.meta.oembedHtml) {
if (this.meta?.oembedHtml) {
return 'oembedFrameWidget';
}
return 'oembedUrlWidget';
Expand Down
5 changes: 5 additions & 0 deletions app/oembed/server/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ providers.registerProvider({
endPoint: 'https://publish.twitter.com/oembed',
});

providers.registerProvider({
urls: [new RegExp('https?://(play|open)\\.spotify\\.com/(track|album|playlist|show)/\\S+')],
endPoint: 'https://open.spotify.com/oembed',
});

export const oembed = {};

oembed.providers = providers;
Expand Down
4 changes: 0 additions & 4 deletions app/spotify/client/index.js

This file was deleted.

16 changes: 0 additions & 16 deletions app/spotify/client/oembedSpotifyWidget.html

This file was deleted.

16 changes: 0 additions & 16 deletions app/spotify/client/oembedSpotifyWidget.js

This file was deleted.

69 changes: 17 additions & 52 deletions app/spotify/lib/spotify.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
/*
* Spotify a named function that will process Spotify links or syntaxes (ex: spotify:track:1q6IK1l4qpYykOaWaLJkWG)
* @param {Object} message - The message object
*/
import _ from 'underscore';

const process = (message, source, callback) => {
if (!source?.trim()) {
return;
}

// Separate text in code blocks and non code blocks
const msgParts = source.split(/(```\w*[\n ]?[\s\S]*?```+?)|(`(?:[^`]+)`)/);
for (let index = 0; index < msgParts.length; index++) {
const part = msgParts[index];
Expand All @@ -19,55 +12,27 @@ const process = (message, source, callback) => {
}
};

class Spotify {
static transform(message) {
let urls = [];
if (Array.isArray(message.urls)) {
urls = urls.concat(message.urls);
}

let changed = false;
export const createSpotifyBeforeSaveMessageHandler = () => (message) => {
const urls = Array.isArray(message.urls) ? message.urls : [];

process(message, message.msg, function(message, msgParts, index, part) {
const re = /(?:^|\s)spotify:([^:\s]+):([^:\s]+)(?::([^:\s]+))?(?::(\S+))?(?:\s|$)/g;
let changed = false;

let match;
while ((match = re.exec(part)) != null) {
const data = _.filter(match.slice(1), (value) => value != null);
const path = _.map(data, (value) => _.escape(value)).join('/');
const url = `https://open.spotify.com/${ path }`;
urls.push({ url, source: `spotify:${ data.join(':') }` });
changed = true;
}
});
process(message, message.msg, (message, msgParts, index, part) => {
const re = /(?:^|\s)spotify:([^:\s]+):([^:\s]+)(?::([^:\s]+))?(?::(\S+))?(?:\s|$)/g;

// Re-mount message
if (changed) {
message.urls = urls;
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;
}
});

return message;
}

static render(message) {
process(message, message.html, function(message, msgParts, index, part) {
if (Array.isArray(message.urls)) {
for (const item of Array.from(message.urls)) {
if (item.source) {
const quotedSource = item.source.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
const re = new RegExp(`(^|\\s)${ quotedSource }(\\s|$)`, 'g');
msgParts[index] = part.replace(re, `$1<a href="${ item.url }" target="_blank">${ item.source }</a>$2`);
}
}
message.html = msgParts.join('');
return message.html;
}
});

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

export const createSpotifyMessageRenderer = () => Spotify.render;

export const createSpotifyBeforeSaveMessageHandler = () => Spotify.transform;
return message;
};
4 changes: 1 addition & 3 deletions app/spotify/server/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Meteor } from 'meteor/meteor';

import { callbacks } from '../../callbacks';
import { createSpotifyBeforeSaveMessageHandler, createSpotifyMessageRenderer } from '../lib/spotify';
import { createSpotifyBeforeSaveMessageHandler } from '../lib/spotify';

Meteor.startup(() => {
const beforeSaveMessage = createSpotifyBeforeSaveMessageHandler();
const renderMessage = createSpotifyMessageRenderer();

callbacks.add('beforeSaveMessage', beforeSaveMessage, callbacks.priority.LOW, 'spotify-save');
callbacks.add('renderMessage', renderMessage, callbacks.priority.MEDIUM, 'spotify-render');
});
1 change: 0 additions & 1 deletion client/importPackages.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import '../app/slashcommands-kick/client';
import '../app/slashcommands-open';
import '../app/slashcommands-topic/client';
import '../app/slashcommands-unarchiveroom/client';
import '../app/spotify/client';
import '../app/tokenpass/client';
import '../app/ui';
import '../app/ui-account';
Expand Down
1 change: 0 additions & 1 deletion client/startup/beforeSaveMessage/index.js

This file was deleted.

10 changes: 0 additions & 10 deletions client/startup/beforeSaveMessage/spotify.js

This file was deleted.

1 change: 0 additions & 1 deletion client/startup/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import './beforeSaveMessage';
import './contextualBar';
import './emailVerification';
import './i18n';
Expand Down
1 change: 0 additions & 1 deletion client/startup/renderMessage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ import './issuelink';
import './katex';
import './markdown';
import './mentionsMessage';
import './spotify';
11 changes: 0 additions & 11 deletions client/startup/renderMessage/spotify.js

This file was deleted.

0 comments on commit 3460771

Please sign in to comment.