From 329f61ff6838edf3a052a07616c9d2b8f36004a0 Mon Sep 17 00:00:00 2001
From: Teal <git@teal.is>
Date: Mon, 27 May 2024 11:55:19 +0200
Subject: [PATCH] broadcast?

---
 app/channels/conference_channel.rb            | 10 ++++++++++
 app/controllers/assignments_controller.rb     | 17 ++++++++--------
 app/javascript/channels/conference_channel.js | 16 +++++++++++++++
 app/javascript/channels/index.js              |  1 +
 app/javascript/channels/session_channel.js    |  1 +
 app/models/assignment.rb                      |  8 ++++++++
 app/models/session.rb                         | 20 ++++++++++++-------
 app/views/conferences/show.html.erb           |  4 +++-
 app/views/sessions/_session.html.erb          |  3 +--
 test/channels/conference_channel_test.rb      |  8 ++++++++
 10 files changed, 69 insertions(+), 19 deletions(-)
 create mode 100644 app/channels/conference_channel.rb
 create mode 100644 app/javascript/channels/conference_channel.js
 create mode 100644 test/channels/conference_channel_test.rb

diff --git a/app/channels/conference_channel.rb b/app/channels/conference_channel.rb
new file mode 100644
index 0000000..d967ed8
--- /dev/null
+++ b/app/channels/conference_channel.rb
@@ -0,0 +1,10 @@
+class ConferenceChannel < ApplicationCable::Channel
+  def subscribed
+    # conference = Conference.find(params[:id])
+    # stream_for conference
+  end
+
+  def unsubscribed
+    # Any cleanup needed when channel is unsubscribed
+  end
+end
diff --git a/app/controllers/assignments_controller.rb b/app/controllers/assignments_controller.rb
index f7ab1eb..14ad1bc 100644
--- a/app/controllers/assignments_controller.rb
+++ b/app/controllers/assignments_controller.rb
@@ -14,26 +14,23 @@ class AssignmentsController < ApplicationController
     params[:user_id] ||= params[:assignment][:user_id]
     if params[:user_id].nil? or params[:user_id].empty?
       flash.now[:alert] = 'Please select a user to assign.'
-      Turbo::StreamsChannel.broadcast_replace_later_to(
-        @session,
-        target: dom_id(@session),
-        partial: "sessions/session",
-        locals: { session: @session }
-      )
       respond_to do |format|
         format.turbo_stream { render turbo_stream: turbo_stream.replace(helpers.dom_id(@session), partial: "sessions/session", locals: { session: @session }), status: :unprocessable_entity }
         format.html { redirect_to conference_session_path(@session.conference, @session), alert: 'Please select a user to assign.' }
       end
       return
     end
+
     @session = Session.find_by(ref_id: params[:session_ref_id])
     @conference = Conference.find_by(slug: params[:conference_slug])
     @user = User.find(params[:user_id])
     @assignment = Assignment.new(user: @user, session: @session)
 
     if @assignment.save
-      Turbo::StreamsChannel.broadcast_replace_later_to(
-        @session,
+      Rails.logger.debug("Saved assignment #{@assignment.inspect}")
+      @session = Session.find_by(ref_id: params[:session_ref_id])
+      Turbo::StreamsChannel.broadcast_replace_to(
+        @session.conference,
         target: helpers.dom_id(@session),
         partial: "sessions/session",
         locals: { session: @session }
@@ -57,8 +54,10 @@ class AssignmentsController < ApplicationController
     @session = @assignment.session
 
     if @assignment&.destroy
+      @session = @assignment.session
+      Rails.logger.debug("destroyed assignment")
       Turbo::StreamsChannel.broadcast_replace_later_to(
-        @session,
+        @session.conference,
         target: helpers.dom_id(@session),
         partial: "sessions/session",
         locals: { session: @session }
diff --git a/app/javascript/channels/conference_channel.js b/app/javascript/channels/conference_channel.js
new file mode 100644
index 0000000..560c3f2
--- /dev/null
+++ b/app/javascript/channels/conference_channel.js
@@ -0,0 +1,16 @@
+import consumer from "channels/consumer"
+
+consumer.subscriptions.create("ConferenceChannel", {
+  connected() {
+    // Called when the subscription is ready for use on the server
+  },
+
+  disconnected() {
+    // Called when the subscription has been terminated by the server
+  },
+
+  received(data) {
+    // Called when there's incoming data on the websocket for this channel
+    console.log("received data on ConferenceChannel: ", data)
+  }
+});
diff --git a/app/javascript/channels/index.js b/app/javascript/channels/index.js
index 7b67b92..79c2648 100644
--- a/app/javascript/channels/index.js
+++ b/app/javascript/channels/index.js
@@ -1,2 +1,3 @@
 // Import all the channels to be used by Action Cable
 import "channels/session_channel"
+import "channels/conference_channel"
diff --git a/app/javascript/channels/session_channel.js b/app/javascript/channels/session_channel.js
index 7041654..32b8bd8 100644
--- a/app/javascript/channels/session_channel.js
+++ b/app/javascript/channels/session_channel.js
@@ -11,5 +11,6 @@ consumer.subscriptions.create("SessionChannel", {
 
   received(data) {
     // Called when there's incoming data on the websocket for this channel
+    console.log("received data on SessionChannel: ", data)
   }
 });
diff --git a/app/models/assignment.rb b/app/models/assignment.rb
index 20d75f3..20fabd0 100644
--- a/app/models/assignment.rb
+++ b/app/models/assignment.rb
@@ -9,6 +9,14 @@ class Assignment < ApplicationRecord
   after_create_commit :notify_assignment_created
   after_destroy_commit :notify_assignment_destroyed
 
+  after_create_commit -> {
+    Rails.logger.debug('Created assignment, broadcasting')
+    broadcast_replace_to "sessions",
+      target: session,
+      partial: "sessions/session",
+      locals: { session: session }
+  }
+
   private
 
   def no_overlapping_assignments
diff --git a/app/models/session.rb b/app/models/session.rb
index 2c718d8..5506da8 100644
--- a/app/models/session.rb
+++ b/app/models/session.rb
@@ -7,12 +7,18 @@ class Session < ApplicationRecord
   has_many :speakers, through: :session_speakers
 
   scope :scheduled, -> { where(status: 'scheduled') }
-  
+
   validates :ref_id, uniqueness: { scope: :conference_id }
-  
+
   after_update :notify_if_changed
-  after_update_commit -> { broadcast_replace_to "sessions" }
-  
+  after_update_commit -> {
+    Rails.logger.debug("session update commit")
+    broadcast_replace_to "sessions",
+      target: helpers.dom_id(@session),
+      partial: "sessions/session",
+      locals: { session: @session }
+  }
+
   self.implicit_order_column = :starts_at
 
   def to_param
@@ -38,11 +44,11 @@ class Session < ApplicationRecord
 
   def backup_needed?
     return false
-    is_interpreted && assignments.length < 4
+    # is_interpreted && assignments.length < 4
   end
 
-  def has_assignees?
-    assignments.length > 0
+  def assignees?
+    assignments.length.positive?
   end
 
   private
diff --git a/app/views/conferences/show.html.erb b/app/views/conferences/show.html.erb
index 054bb99..97ed2bb 100644
--- a/app/views/conferences/show.html.erb
+++ b/app/views/conferences/show.html.erb
@@ -7,6 +7,7 @@ current_time = Time.zone.now.in_time_zone(@conference.time_zone)
 # current_time = Time.parse(@conference.days.first.strftime("%Y-%m-%d 11:00"))
 #current_time = @sessions_by_date[@conference.days.first].first.starts_at.advance(minutes: 5)
 %>
+<%= turbo_frame_tag dom_id(@conference) do %>
 <div>
   <h1 class="text-2xl font-bold my-2"><%= @conference.name %></h1>
   <p class="text-xs mb-6">
@@ -133,4 +134,5 @@ current_time = Time.zone.now.in_time_zone(@conference.time_zone)
     </div>
   </div>
   <% end %>
-</div>
\ No newline at end of file
+</div>
+<% end %>
diff --git a/app/views/sessions/_session.html.erb b/app/views/sessions/_session.html.erb
index 4a49a8a..80a61d3 100644
--- a/app/views/sessions/_session.html.erb
+++ b/app/views/sessions/_session.html.erb
@@ -1,7 +1,6 @@
 <% unassigned_users = User.all - session.assignments.collect(&:user) %>
-<%= turbo_stream_from session %>
 <%= turbo_frame_tag dom_id(session) do %>
-  <div class="session shadow hover:shadow-lg overflow-scroll text-sm w-full !h-full min-h-full hover:!min-h-max <%= session.translators_needed? ? "translators-needed" : "no-translators-needed" %> <%= session.backup_needed? ? "backup-needed" : "no-backup-needed" %> <%= session.has_assignees? ? "has-assignees" : "no-assignees" %>">
+  <div class="session shadow hover:shadow-lg overflow-scroll text-sm w-full !h-full min-h-full hover:!min-h-max <%= session.translators_needed? ? "translators-needed" : "no-translators-needed" %> <%= session.backup_needed? ? "backup-needed" : "no-backup-needed" %> <%= session.assignees? ? "has-assignees" : "no-assignees" %>">
     <h4>
       <small class="text-2xs uppercase font-light bg-black/10 rounded-sm p-1 mr-1 lang-<%= session.language %>"><%= session.language %></small>
       <%= link_to session.title, session.url, target: "_top" %>
diff --git a/test/channels/conference_channel_test.rb b/test/channels/conference_channel_test.rb
new file mode 100644
index 0000000..6500d21
--- /dev/null
+++ b/test/channels/conference_channel_test.rb
@@ -0,0 +1,8 @@
+require "test_helper"
+
+class ConferenceChannelTest < ActionCable::Channel::TestCase
+  # test "subscribes" do
+  #   subscribe
+  #   assert subscription.confirmed?
+  # end
+end
-- 
GitLab