Make pusher channels private (#410)

This commit is contained in:
Ben Halpern 2018-06-08 18:49:59 -04:00 committed by GitHub
parent 645de032df
commit 1a85bddd2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 9 additions and 8 deletions

View file

@ -42,7 +42,7 @@ class ChatChannelsController < ApplicationController
if banned_user
banned_user.add_role :banned
banned_user.messages.each(&:destroy!)
notification_channels = @chat_channel.chat_channel_memberships.pluck(:user_id).map { |id| "message-notifications-#{id}"}
notification_channels = @chat_channel.chat_channel_memberships.pluck(:user_id).map { |id| "private-message-notifications-#{id}"}
Pusher.trigger(notification_channels, "user-banned", { userId: banned_user.id }.to_json)
render json: { status: "success", message: "banned!" }, status: 200
else

View file

@ -3,13 +3,14 @@ class MessagesController < ApplicationController
def create
@message = Message.new(message_params)
@message.user_id = current_user.id
authorize @message
success = false
if @message.save
begin
message_json = create_pusher_payload(@message)
notification_channels = @message.chat_channel.chat_channel_memberships.pluck(:user_id).map { |id| "message-notifications-#{id}"}
notification_channels = @message.chat_channel.chat_channel_memberships.pluck(:user_id).map { |id| "private-message-notifications-#{id}"}
Pusher.trigger(notification_channels, "message-created", message_json)
success = true
rescue Pusher::Error => e

View file

@ -1,8 +1,8 @@
class PusherController < ApplicationController
def auth
if current_user
response = pusher_client.authenticate(params[:channel_name], params[:socket_id], {
if current_user && params[:channel_name] == "private-message-notifications-#{current_user.id}"
response = Pusher.authenticate(params[:channel_name], params[:socket_id], {
user_id: current_user.id, # => required
})
render json: response

View file

@ -39,7 +39,7 @@ export default class Chat extends Component {
});
setupObserver(this.observerCallback);
setupPusher(this.props.pusherKey, {
channelId: `message-notifications-${window.currentUser.id}`,
channelId: `private-message-notifications-${window.currentUser.id}`,
messageCreated: this.receiveNewMessage,
channelCleared: this.clearChannel,
redactUserMessages: this.redactUserMessages,

View file

@ -15,7 +15,7 @@ class UnopenedChannelNotice extends Component {
componentDidMount() {
setupPusher(this.props.pusherKey, {
channelId: `message-notifications-${window.currentUser.id}`,
channelId: `private-message-notifications-${window.currentUser.id}`,
messageCreated: this.receiveNewMessage,
});
}

View file

@ -10,7 +10,7 @@ class ChatChannel < ApplicationRecord
def clear_channel
messages.each(&:destroy!)
notification_channels = chat_channel_memberships.pluck(:user_id).map { |id| "message-notifications-#{id}"}
notification_channels = chat_channel_memberships.pluck(:user_id).map { |id| "private-message-notifications-#{id}"}
Pusher.trigger(notification_channels, "channel-cleared", { chat_channel_id: id }.to_json)
true
rescue Pusher::Error => e

View file

@ -102,7 +102,7 @@ Rails.application.routes.draw do
get "/💌" => "chat_channels#index"
get "/💌/:slug" => "chat_channels#index"
post "/pusher/auth" => "chat_channels#index"
post "/pusher/auth" => "pusher#auth"
# resources :users