-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
34 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,39 @@ | ||
class CollaboratorsChannel < ApplicationCable::Channel | ||
def subscribed | ||
puts "subscribing user id #{params["user_id"]} to #{params["channel_name"]}" | ||
puts params | ||
stream_from params["channel_name"] | ||
collaborators = Rails.cache.read(params["channel_name"]) | ||
user = User.find(params["user_id"]) | ||
|
||
if collaborators.nil? || collaborators.empty? | ||
string = "#{user.id}:#{user.email}:#{user.full_name}:EDITOR" | ||
Rails.cache.write(params["channel_name"], string) | ||
collaborators = string | ||
elsif collaborators.is_a?(String) && !collaborators.include?(params["user_id"]) # will not work with just a user id | ||
string = collaborators += "/#{user.id}:#{user.email}:#{user.full_name}" | ||
Rails.cache.write(params["channel_name"], string) | ||
collaborators = string | ||
if collaborators.blank? | ||
collaborators = "#{user.id}:#{params["current_tab"]}:#{user.email}:#{user.full_name}:EDITOR" | ||
else | ||
collaborators += "/#{user.id}:#{params["current_tab"]}:#{user.email}:#{user.full_name}" | ||
end | ||
|
||
puts "subscribed - collaborator list: #{collaborators}" | ||
|
||
# ActionCable.server.broadcast(params["channel_name"], { collaborators: collaborators }) unless collaborators.nil? | ||
Rails.cache.write(params["channel_name"], collaborators) | ||
|
||
Collaborators::BroadcastCollabWorker.perform_async(params["channel_name"], collaborators) | ||
end | ||
|
||
def unsubscribed | ||
puts "unsubscribing user_id #{params["user_id"]} from channel: #{params["channel_name"]}" | ||
|
||
user_id = params["user_id"] | ||
collaborators = Rails.cache.read(params["channel_name"]) | ||
new_collaborators = "" | ||
collaborators = Rails.cache.read(params["channel_name"]) | ||
|
||
collaborators.split("/").each do |collaborator| | ||
new_collaborators += collaborator unless collaborator.split(":")[0] == user_id | ||
# remove the unsubscribing user | ||
new_collaborators += collaborator unless collaborator.split(":")[0] == user_id && collaborator.split(":")[1] == params["current_tab"] | ||
end | ||
|
||
if !new_collaborators.empty? && !new_collaborators.include?("EDITOR") | ||
if !new_collaborators.empty? && new_collaborators.exclude?("EDITOR") | ||
# editor has left the channel, so we update the next in line to be editor | ||
temp_collabs = new_collaborators.split("/") | ||
temp_collabs[0] += ":EDITOR" | ||
new_collaborators = temp_collabs.join("/") | ||
end | ||
|
||
if new_collaborators.empty? | ||
puts "new collabs will be empty" | ||
else | ||
puts "new collabs will be: #{new_collaborators}" | ||
end | ||
|
||
Rails.cache.write(params["channel_name"], new_collaborators) | ||
|
||
# ActionCable.server.broadcast(params["channel_name"], { collaborators: new_collaborators }) | ||
Collaborators::BroadcastCollabWorker.perform_async(params["channel_name"], new_collaborators) | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters