Added streaming of updated status from fetch_link_card_service

Sends updated status down after adding/updating/removing preview card.
This commit is contained in:
mgabdev 2019-08-28 01:26:34 -04:00
parent dc87184ef0
commit 31d4660947
3 changed files with 52 additions and 3 deletions

View File

@ -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)
}
},
};
});
}

View File

@ -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

View File

@ -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)));