First draft to support Oembed protocol as defined http://oembed.com/
Currently only support soundcloud (futur providers will be add)pull/1241/head
parent
f6d3817f47
commit
93e293a5b2
@ -1,7 +1,11 @@ |
||||
<template name="oembedSoundcloudWidget"> |
||||
<template name="oembedFrameWidget"> |
||||
{{#if parsedUrl}} |
||||
<blockquote> |
||||
<a href="https://www.soundcloud.com" style="color: #9e9ea6">SoundCloud</a><br/> |
||||
{{#if meta.oembedProviderName}} |
||||
{{#if meta.oembedProviderUrl}} |
||||
<a href="{{meta.oembedProviderUrl}}" style="color: #9e9ea6">{{meta.oembedProviderName}}</a><br/> |
||||
{{/if}} |
||||
{{/if}} |
||||
{{#if meta.oembedAuthorName}} |
||||
{{#if meta.oembedAuthorUrl}} |
||||
<a href="meta.oembedAuthorUrl">{{meta.oembedAuthorName}}</a><br/> |
||||
@ -0,0 +1,55 @@ |
||||
URL = Npm.require('url') |
||||
|
||||
class Providers |
||||
providers: [] |
||||
|
||||
@getConsumerUrl: (provider, url) -> |
||||
urlObj = URL.parse provider.endPoint, true |
||||
urlObj.query['url'] = url |
||||
delete urlObj.search |
||||
return URL.format urlObj |
||||
|
||||
registerProvider: (provider) -> |
||||
this.providers.push(provider) |
||||
|
||||
getProviders: () -> |
||||
return this.providers |
||||
|
||||
getProviderForUrl: (url) -> |
||||
return _.find this.providers, (provider) -> |
||||
candidate = _.find provider.urls, (re) -> |
||||
return re.test url |
||||
return candidate? |
||||
|
||||
providers = new Providers() |
||||
providers.registerProvider |
||||
urls: [new RegExp('https?://soundcloud.com/\\S+')] |
||||
endPoint: 'https://soundcloud.com/oembed?format=json&maxheight=150' |
||||
|
||||
RocketChat.oembed = {} |
||||
RocketChat.oembed.providers = providers |
||||
|
||||
RocketChat.callbacks.add 'oembed:beforeGetUrlContent', (data) -> |
||||
if data.parsedUrl? |
||||
url = URL.format data.parsedUrl |
||||
provider = providers.getProviderForUrl url |
||||
if provider? |
||||
consumerUrl = Providers.getConsumerUrl provider, url |
||||
consumerUrl = URL.parse consumerUrl, true |
||||
_.extend data.parsedUrl, consumerUrl |
||||
data.requestOptions.port = consumerUrl.port |
||||
data.requestOptions.hostname = consumerUrl.hostname |
||||
data.requestOptions.path = consumerUrl.path |
||||
|
||||
RocketChat.callbacks.add 'oembed:afterParseContent', (data) -> |
||||
if data.parsedUrl? |
||||
url = URL.format data.parsedUrl |
||||
provider = providers.getProviderForUrl url |
||||
if provider? |
||||
if data.content?.body? |
||||
metas = JSON.parse data.content.body; |
||||
_.each metas, (value, key) -> |
||||
if _.isString value |
||||
data.meta[changeCase.camelCase('oembed_' + key)] = value |
||||
if data.parsedUrl?.query?.url? |
||||
data.meta['oembedUrl'] = data.parsedUrl.query.url |
||||
@ -1,3 +0,0 @@ |
||||
Template.oembedBaseWidget.onCreated () -> |
||||
if this.data?.parsedUrl?.host is 'soundcloud.com' and this.data?.meta?.oembedHtml? |
||||
this.data._overrideTemplate = 'oembedSoundcloudWidget' |
||||
@ -1,19 +0,0 @@ |
||||
URL = Npm.require('url') |
||||
|
||||
RocketChat.callbacks.add 'oembed:beforeGetUrlContent', (data) -> |
||||
if data.parsedUrl.host is 'soundcloud.com' |
||||
newUrlObj = URL.format data.parsedUrl |
||||
newUrlObj = URL.parse "https://soundcloud.com/oembed?url=" + encodeURIComponent newUrlObj + "&format=json&maxheight=150" |
||||
data.requestOptions.port = newUrlObj.port |
||||
data.requestOptions.hostname = newUrlObj.hostname |
||||
data.requestOptions.path = newUrlObj.path |
||||
|
||||
RocketChat.callbacks.add 'oembed:afterParseContent', (data) -> |
||||
if data.parsedUrl.host is 'soundcloud.com' |
||||
if data.content?.body? |
||||
metas = JSON.parse data.content.body; |
||||
_.each metas, (value, key) -> |
||||
if _.isString value |
||||
data.meta[changeCase.camelCase('oembed_' + key)] = value |
||||
if data.parsedUrl? |
||||
data.meta['oembedUrl'] = URL.format data.parsedUrl |
||||
@ -1,28 +0,0 @@ |
||||
Package.describe({ |
||||
name: 'rocketchat:soundcloud', |
||||
version: '0.0.1', |
||||
summary: 'Soundcloud integration', |
||||
git: '' |
||||
}); |
||||
|
||||
Package.onUse(function(api) { |
||||
api.versionsFrom('1.0'); |
||||
|
||||
api.use([ |
||||
'coffeescript', |
||||
'templating', |
||||
'underscore', |
||||
'konecty:change-case', |
||||
'rocketchat:lib', |
||||
'rocketchat:oembed@0.0.1' |
||||
]); |
||||
|
||||
api.addFiles('lib/client/widget.coffee', 'client'); |
||||
api.addFiles('lib/client/oembedSoundcloudWidget.html', 'client'); |
||||
|
||||
api.addFiles('lib/server/server.coffee', 'server'); |
||||
}); |
||||
|
||||
Package.onTest(function(api) { |
||||
|
||||
}); |
||||
Loading…
Reference in new issue