Merge pull request #904 from kakawait/spotify-integration

Spotify integrations
pull/926/head
Rodrigo Nascimento 11 years ago
commit 6b24877aef
  1. 7
      packages/rocketchat-oembed/client/baseWidget.coffee
  2. 13
      packages/rocketchat-spotify/client/oembedSpotifyWidget.html
  3. 11
      packages/rocketchat-spotify/client/widget.coffee
  4. 27
      packages/rocketchat-spotify/package.js
  5. 48
      packages/rocketchat-spotify/spotify.coffee

@ -1,6 +1,9 @@
Template.oembedBaseWidget.helpers
template: ->
# console.log this
# console.log this
if this._overrideTemplate
return this._overrideTemplate
if this.headers?.contentType?.match(/image\/.*/)?
return 'oembedImageWidget'
@ -10,4 +13,4 @@ Template.oembedBaseWidget.helpers
if this.parsedUrl?.host is 'www.youtube.com' and this.meta?.twitterPlayer?
return 'oembedYoutubeWidget'
return 'oembedUrlWidget'
return 'oembedUrlWidget'

@ -0,0 +1,13 @@
<template name="oembedSpotifyWidget">
{{#if parsedUrl}}
<blockquote>
<a href="https://www.spotify.com" style="color: #9e9ea6">Spotify</a><br/>
{{#if match meta.ogAudio "spotify:artist:\\S+"}}
<a href="{{url}}">{{{meta.ogTitle}}}</a><br/>
{{else}}
<a href="{{url}}">{{{replace meta.ogDescription ", an? (?:song|album) by (.+?) on Spotify" " - $1" regex=true}}}</a><br/>
{{/if}}
<iframe width="300" height="380" src="https://embed.spotify.com/?uri={{meta.ogAudio}}" frameborder="0"></iframe><br/>
</blockquote>
{{/if}}
</template>

@ -0,0 +1,11 @@
Template.registerHelper 'replace', (source, find, replace, option) ->
if option.hash.regex is true
find = new RegExp(find)
return source.replace(find, replace)
Template.registerHelper 'match', (source, regex) ->
return new RegExp(regex).test(source)
Template.oembedBaseWidget.onCreated () ->
if this.data?.parsedUrl?.host is 'open.spotify.com' and this.data?.meta?.ogAudio?
this.data._overrideTemplate = 'oembedSpotifyWidget'

@ -0,0 +1,27 @@
Package.describe({
name: 'rocketchat:spotify',
version: '0.0.1',
summary: 'Message pre-processor that will translate spotify on messages',
git: ''
});
Package.onUse(function(api) {
api.versionsFrom('1.0');
api.use([
'coffeescript',
'templating',
'underscore',
'rocketchat:oembed@0.0.1',
'rocketchat:lib@0.0.1'
]);
api.addFiles('client/widget.coffee', 'client');
api.addFiles('client/oembedSpotifyWidget.html', 'client');
api.addFiles('spotify.coffee', ['server','client']);
});
Package.onTest(function(api) {
});

@ -0,0 +1,48 @@
###
# Spotify a named function that will process Spotify links or syntaxes (ex: spotify:track:1q6IK1l4qpYykOaWaLJkWG)
# @param {Object} message - The message object
###
class Spotify
process = (message, source, callback) ->
if _.trim source
# Separate text in code blocks and non code blocks
msgParts = source.split(/(```\w*[\n\ ]?[\s\S]*?```+?)/)
for part, index in msgParts
# Verify if this part is code
codeMatch = part.match(/```(\w*)[\n\ ]?([\s\S]*?)```+?/)
if not codeMatch?
callback message, msgParts, part, index
@transform: (message) ->
urls = []
if Array.isArray message.urls
urls = urls.concat message.urls
changed = false
process message, message.msg, (message, msgParts, part) ->
re = /(?:^|\s)spotify:([^:]+):(\S+)(?:\s|$)/g
while match = re.exec(part)
url = "https://open.spotify.com/" + _.escape match[1] + "/" + _.escape match[2]
urls.push {'url': url}
changed = true
# Re-mount message
if changed
message.urls = urls
return message
@render: (message) ->
process message, message.html, (message, msgParts, part, index) ->
msgParts[index] = part.replace /(^|\s)spotify:([^:]+):(\S+)(\s|$)/g, (match, before, type, id, after) ->
url = 'https://open.spotify.com/' + _.escape type + '/' + _.escape id
return before + '<a href="' + url + '" target="_blank">spotify:' + type + ':' + id + '</a>' + after
message.html = msgParts.join('')
return message
RocketChat.callbacks.add 'beforeSaveMessage', Spotify.transform, RocketChat.callbacks.priority.LOW
RocketChat.callbacks.add 'renderMessage', Spotify.render
Loading…
Cancel
Save