Skip to content

Commit

Permalink
implement per tab lock controls
Browse files Browse the repository at this point in the history
  • Loading branch information
sammo1235 committed Oct 14, 2024
1 parent d8f44c1 commit d63f78a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ window.ApplicationCollaboratorsAccessManager =
# CollaboratorsLog.log("[TAB SWITCH] ------------- Log out from (" + room + ") to " + current_step + " --------------------")

# Set new pusher section
window.pusher_section = current_step;
window.form_section = current_step;

# Set new login timestamp for user
timestamp = new Date().getTime();
Expand All @@ -58,7 +58,8 @@ window.ApplicationCollaboratorsAccessManager =
ApplicationCollaboratorsConnectionManager.init_room()

i_am_current_editor: () ->
ApplicationCollaboratorsAccessManager.current_editor_id() == window.user_id
ApplicationCollaboratorsAccessManager.current_editor_id() == window.user_id &&
window.tab_ident == ApplicationCollaboratorsAccessManager.current_editor().tab_ident

im_in_viewer_mode: () ->
!ApplicationCollaboratorsAccessManager.i_am_current_editor()
Expand All @@ -75,8 +76,9 @@ window.ApplicationCollaboratorsAccessManager =
editor = window.current_channel_members.split("/").find((el) => el.includes("EDITOR")).split(":")
editor_info = {
id: editor[0],
email: editor[1],
name: editor[2]
tab_ident: editor[1],
email: editor[2],
name: editor[3]
}

editor_info
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
window.ApplicationCollaboratorsConnectionManager =

init: (form_id, user_id, p_host, p_port, p_key, rails_env, timestamp) ->
init: (form_id, user_id, rails_env) ->

#
# Checking if browser supports WebSockets technology
Expand All @@ -9,69 +9,34 @@ window.ApplicationCollaboratorsConnectionManager =

window.form_id = form_id

window.pusher_host = p_host
window.pusher_port = p_port

window.pusher_key = p_key
window.rails_env = rails_env
window.user_id = user_id

if rails_env == "staging" || rails_env == "production"
window.pusher_encrypted = true
else
window.pusher_encrypted = false
window.tab_ident = ApplicationCollaboratorsConnectionManager.get_tab_ident()

window.pusher_section = $(".js-step-link.step-current").attr('data-step')
window.form_section = $(".js-step-link.step-current").attr('data-step')

# ApplicationCollaboratorsConnectionManager.init_pusher(timestamp)
ApplicationCollaboratorsConnectionManager.init_room()
# ApplicationCollaboratorsGeneralRoomTracking.login()

init_room: () ->
# Introduce new channel
channel_name = 'presence-chat-' + window.rails_env + "-" + window.form_id + '-sep-' + window.pusher_section

CollaboratorsLog.log("[PUSHER INIT ROOM] ------------------------ channel_name: " + channel_name)
channel_name = 'presence-chat-' + window.rails_env + "-" + window.form_id + '-sep-' + window.form_section

# window.pusher_current_channel = window.pusher.subscribe(channel_name)
CollaboratorsLog.log("[INIT ROOM] ------------------------ channel_name: " + channel_name)

window.App.collaborators = App.cable.subscriptions.create { channel: "CollaboratorsChannel", channel_name: channel_name, user_id: window.user_id },
window.App.collaborators = App.cable.subscriptions.create { channel: "CollaboratorsChannel", channel_name: channel_name, user_id: window.user_id, current_tab: window.tab_ident },
connected: ->
console.log("successfully connected")
console.log("connected")

received: (data) ->
console.log("receieved data", data.collaborators)
# console.log("Member count: " + data.collaborators.split("/").length)

window.current_channel_members = data.collaborators
ApplicationCollaboratorsAccessManager.set_access_mode()

disconnected: ->
console.log("disconnected")

# App.collaborators.unsubscribed()

# Check if subscription was successful
# window.pusher_current_channel.bind 'pusher:subscription_succeeded', (members) ->
# CollaboratorsLog.log('[subscription_succeeded] Count ' + members.count)

# ApplicationCollaboratorsAccessManager.set_access_mode()
# members.each (member) =>
# ApplicationCollaboratorsAccessManager.register_member(member)

# return

# # Handle member removed
# window.pusher_current_channel.bind 'pusher:member_removed', (member) ->
# CollaboratorsLog.log('[member_removed] Count ' + window.pusher_current_channel.members.count)

# ApplicationCollaboratorsAccessManager.try_mark_as_editor()

# return

# # Handle member added
# window.pusher_current_channel.bind 'pusher:member_added', (member) ->
# CollaboratorsLog.log('[member_added] Count ' + window.pusher_current_channel.members.count)
# ApplicationCollaboratorsAccessManager.register_member(member)
get_tab_ident: () ->
document.cookie.split('; ').find((c) => c.split("=")[0] == 'public_tab_ident').split('=')[1]

# return
37 changes: 11 additions & 26 deletions app/channels/collaborators_channel.rb
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
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ApplicationController < ActionController::Base
before_action :set_current_attributes
before_action :set_paper_trail_whodunnit
before_action :disable_browser_caching!
before_action :set_session_identifier

self.responder = AppResponder
respond_to :html
Expand Down Expand Up @@ -93,6 +94,10 @@ def should_enable_js?
helper_method "#{award}_submission_started_deadline"
end

def set_session_identifier
cookies["public_tab_ident"] = cookies["_qae_session#{"_development" if Rails.env.development?}"].first(8)
end

protected

def settings
Expand Down
2 changes: 1 addition & 1 deletion app/views/qae_form/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ form.qae-form.award-form data-autosave-url=save_form_url(@form_answer) action=sa

- if application_collaborator_group_mode?(@form_answer)
= content_for(:javascript_code) do
| ApplicationCollaboratorsConnectionManager.init('#{@form_answer.id}', '#{current_user.id}', '#{Pusher.host}', #{Pusher.port}, '#{Pusher.key}', '#{Rails.env}', #{Time.now.utc.to_i});
| ApplicationCollaboratorsConnectionManager.init('#{@form_answer.id}', '#{current_user.id}', '#{Rails.env}');
2 changes: 1 addition & 1 deletion app/workers/collaborators/broadcast_collab_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ class Collaborators::BroadcastCollabWorker
def perform(channel_name, collaborators)
ActionCable.server.broadcast(channel_name, { collaborators: collaborators }) unless collaborators.nil?
end
end
end
2 changes: 1 addition & 1 deletion spec/channels/collaborators_channel_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'rails_helper'
require "rails_helper"

RSpec.describe CollaboratorsChannel, type: :channel do
pending "add some examples to (or delete) #{__FILE__}"

Check warning on line 4 in spec/channels/collaborators_channel_spec.rb

View workflow job for this annotation

GitHub Actions / test

CollaboratorsChannel add some examples to (or delete) /home/runner/work/qae/qae/spec/channels/collaborators_channel_spec.rb Skipped: Not yet implemented
Expand Down

0 comments on commit d63f78a

Please sign in to comment.