From 31d4660947445fca2aef2d4002e2f2652ca45d41 Mon Sep 17 00:00:00 2001 From: mgabdev <> Date: Wed, 28 Aug 2019 01:26:34 -0400 Subject: [PATCH] Added streaming of updated status from fetch_link_card_service Sends updated status down after adding/updating/removing preview card. --- app/javascript/gabsocial/actions/streaming.js | 16 ++++++++++ app/services/fetch_link_card_service.rb | 30 +++++++++++++++++-- streaming/index.js | 9 ++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/app/javascript/gabsocial/actions/streaming.js b/app/javascript/gabsocial/actions/streaming.js index e599805e..0baf42c9 100644 --- a/app/javascript/gabsocial/actions/streaming.js +++ b/app/javascript/gabsocial/actions/streaming.js @@ -10,6 +10,7 @@ import { updateNotificationsQueue, expandNotifications } from './notifications'; import { updateConversations } from './conversations'; import { fetchFilters } from './filters'; import { getLocale } from '../locales'; +import { handleComposeSubmit } from './compose'; const { messages } = getLocale(); @@ -61,3 +62,18 @@ export const connectHashtagStream = (id, tag, accept) => connectTimelineStream export const connectDirectStream = () => connectTimelineStream('direct', 'direct'); export const connectListStream = id => connectTimelineStream(`list:${id}`, `list&list=${id}`); export const connectGroupStream = id => connectTimelineStream(`group:${id}`, `group&group=${id}`); + +export const connectStatusUpdateStream = () => { + return connectStream('statuscard', null, (dispatch, getState) => { + return { + onConnect() {}, + onDisconnect() {}, + onReceive (data) { + if (!data['event'] || !data['payload']) return; + if (data.event === 'update') { + handleComposeSubmit(dispatch, getState, {data: JSON.parse(data.payload)}, null) + } + }, + }; + }); +} \ No newline at end of file diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb index e7df52dc..7fc0689a 100644 --- a/app/services/fetch_link_card_service.rb +++ b/app/services/fetch_link_card_service.rb @@ -15,7 +15,15 @@ class FetchLinkCardService < BaseService @status = status @url = parse_urls - return if @url.nil? || @status.preview_cards.any? + if @status.preview_cards.any? + if @url.nil? + detach_card + return + end + return if @status.preview_cards.first.url == @url + end + + return if @url.nil? @url = @url.to_s @@ -49,13 +57,23 @@ class FetchLinkCardService < BaseService end end - return if @html.nil? + if @html.nil? + detach_card + return + end attempt_oembed || attempt_opengraph end def attach_card - @status.preview_cards << @card + @status.preview_cards = [@card] + send_status_update_payload(@status) + Rails.cache.delete(@status) + end + + def detach_card + @status.preview_cards = [] + send_status_update_payload(@status) Rails.cache.delete(@status) end @@ -165,4 +183,10 @@ class FetchLinkCardService < BaseService def lock_options { redis: Redis.current, key: "fetch:#{@url}" } end + + def send_status_update_payload(status) + @payload = InlineRenderer.render(status, nil, :status) + @payload = Oj.dump(event: :update, payload: @payload) + Redis.current.publish('statuscard', @payload) + end end diff --git a/streaming/index.js b/streaming/index.js index 38d9b28f..7f212850 100644 --- a/streaming/index.js +++ b/streaming/index.js @@ -534,6 +534,11 @@ const startWorker = (workerId) => { app.use(authenticationMiddleware); app.use(errorMiddleware); + app.get('/api/v1/streaming/statuscard', (req, res) => { + const channel = `statuscard`; + streamFrom(channel, req, streamToHttp(req, res), streamHttpEnd(req, subscriptionHeartbeat(channel))); + }); + app.get('/api/v1/streaming/user', (req, res) => { const channel = `timeline:${req.accountId}`; streamFrom(channel, req, streamToHttp(req, res), streamHttpEnd(req, subscriptionHeartbeat(channel))); @@ -608,6 +613,10 @@ const startWorker = (workerId) => { let channel; switch(location.query.stream) { + case 'statuscard': + channel = `statuscard`; + streamFrom(channel, req, streamToWs(req, ws), streamWsEnd(req, ws, subscriptionHeartbeat(channel))); + break; case 'user': channel = `timeline:${req.accountId}`; streamFrom(channel, req, streamToWs(req, ws), streamWsEnd(req, ws, subscriptionHeartbeat(channel)));