Add events for current_content and connection_status

This commit is contained in:
kiragrammel
2023-09-13 17:37:51 +02:00
committed by Sebastian Serth
parent 5ae306997b
commit 49e3fa6176
2 changed files with 22 additions and 3 deletions

View File

@ -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

View File

@ -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:,