From 48b9619439818ecb344ae33c9c31a55ecb1aa27a Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 5 Nov 2016 15:20:05 +0100 Subject: Adding hashtags --- app/channels/application_cable/channel.rb | 13 +++++++++++++ app/channels/hashtag_channel.rb | 11 +++++++++++ app/channels/public_channel.rb | 14 ++------------ app/channels/timeline_channel.rb | 4 ---- 4 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 app/channels/hashtag_channel.rb (limited to 'app/channels') diff --git a/app/channels/application_cable/channel.rb b/app/channels/application_cable/channel.rb index d67269728..d27b058fb 100644 --- a/app/channels/application_cable/channel.rb +++ b/app/channels/application_cable/channel.rb @@ -1,4 +1,17 @@ module ApplicationCable class Channel < ActionCable::Channel::Base + protected + + def hydrate_status(encoded_message) + message = ActiveSupport::JSON.decode(encoded_message) + status = Status.find_by(id: message['id']) + message['message'] = FeedManager.instance.inline_render(current_user.account, status) + + [status, message] + end + + def filter?(status) + status.nil? || current_user.account.blocking?(status.account) || (status.reblog? && current_user.account.blocking?(status.reblog.account)) + end end end diff --git a/app/channels/hashtag_channel.rb b/app/channels/hashtag_channel.rb new file mode 100644 index 000000000..5be8d94cd --- /dev/null +++ b/app/channels/hashtag_channel.rb @@ -0,0 +1,11 @@ +class HashtagChannel < ApplicationCable::Channel + def subscribed + tag = params[:tag].downcase + + stream_from "timeline:hashtag:#{tag}", lambda { |encoded_message| + status, message = hydrate_status(encoded_message) + next if filter?(status) + transmit message + } + end +end diff --git a/app/channels/public_channel.rb b/app/channels/public_channel.rb index 708eff055..41e21611d 100644 --- a/app/channels/public_channel.rb +++ b/app/channels/public_channel.rb @@ -1,19 +1,9 @@ -# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading. class PublicChannel < ApplicationCable::Channel def subscribed stream_from 'timeline:public', lambda { |encoded_message| - message = ActiveSupport::JSON.decode(encoded_message) - - status = Status.find_by(id: message['id']) - next if status.nil? || current_user.account.blocking?(status.account) || (status.reblog? && current_user.account.blocking?(status.reblog.account)) - - message['message'] = FeedManager.instance.inline_render(current_user.account, status) - + status, message = hydrate_status(encoded_message) + next if filter?(status) transmit message } end - - def unsubscribed - # Any cleanup needed when channel is unsubscribed - end end diff --git a/app/channels/timeline_channel.rb b/app/channels/timeline_channel.rb index 9e5a81188..f2a9636fd 100644 --- a/app/channels/timeline_channel.rb +++ b/app/channels/timeline_channel.rb @@ -2,8 +2,4 @@ class TimelineChannel < ApplicationCable::Channel def subscribed stream_from "timeline:#{current_user.account_id}" end - - def unsubscribed - # Any cleanup needed when channel is unsubscribed - end end -- cgit