From 49e3fa6176330d43bc1e04017b62af892d398db2 Mon Sep 17 00:00:00 2001 From: kiragrammel Date: Wed, 13 Sep 2023 17:37:51 +0200 Subject: [PATCH] Add events for current_content and connection_status --- app/channels/synchronized_editor_channel.rb | 6 +++++- app/models/event/synchronized_editor.rb | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/channels/synchronized_editor_channel.rb b/app/channels/synchronized_editor_channel.rb index e390b509..c4692fab 100644 --- a/app/channels/synchronized_editor_channel.rb +++ b/app/channels/synchronized_editor_channel.rb @@ -43,10 +43,14 @@ class SynchronizedEditorChannel < ApplicationCable::Channel end def connection_status - ActionCable.server.broadcast(specific_channel, create_message('connection_status', 'connected')) + message = create_message('connection_status', 'connected') + + Event::SynchronizedEditor.create_for_connection_change(message, current_user, programming_group) + ActionCable.server.broadcast(specific_channel, message) end def current_content(message) + Event::SynchronizedEditor.create_for_current_content(message, current_user, programming_group) ActionCable.server.broadcast(specific_channel, message) end diff --git a/app/models/event/synchronized_editor.rb b/app/models/event/synchronized_editor.rb index aba87161..a58aeadb 100644 --- a/app/models/event/synchronized_editor.rb +++ b/app/models/event/synchronized_editor.rb @@ -13,6 +13,7 @@ class Event::SynchronizedEditor < ApplicationRecord editor_change: 0, connection_change: 1, connection_status: 2, + current_content: 3, }, _prefix: true enum status: { @@ -27,13 +28,13 @@ class Event::SynchronizedEditor < ApplicationRecord validates :session_id, presence: true validates :status, presence: true, if: -> { action_connection_change? } - validates :file_id, presence: true, if: -> { action_editor_change? } + validates :file_id, presence: true, if: -> { action_editor_change? || action_current_content? } validates :editor_action, presence: true, if: -> { action_editor_change? } validates :range_start_row, numericality: {only_integer: true, greater_than_or_equal_to: 0}, if: -> { action_editor_change? } validates :range_start_column, numericality: {only_integer: true, greater_than_or_equal_to: 0}, if: -> { action_editor_change? } validates :range_end_row, numericality: {only_integer: true, greater_than_or_equal_to: 0}, if: -> { action_editor_change? } validates :range_end_column, numericality: {only_integer: true, greater_than_or_equal_to: 0}, if: -> { action_editor_change? } - validates :lines, presence: true, if: -> { action_editor_change? } + validates :lines, presence: true, if: -> { action_editor_change? || action_current_content? } def self.create_for_editor_change(event, user, programming_group) event_copy = event.deep_dup @@ -59,6 +60,20 @@ class Event::SynchronizedEditor < ApplicationRecord ) end + def self.create_for_current_content(message, user, programming_group) + message['files'].each do |file| + create!( + user:, + programming_group:, + study_group_id: user.current_study_group_id, + action: message['action'], + file_id: file['file_id'], + session_id: message['session_id'], + lines: file['content'].split("\n") + ) + end + end + def self.create_for_connection_change(message, user, programming_group) create!( user:,