FIX: Add editing user ids to ChatMessage and ChatMessageRevision (#18877)

This commit adds last_editor_id to ChatMessage for parity with Post in
core, as well as adding user_id to the ChatMessageRevision record since
we need to know who is making edits and revisions to messages, in case
in future we want to allow more than just the current user to edit chat
messages. The backfill for data here simply uses the record's creating
user ID, but in future if we allow other people to edit the messages it
will use their ID.
This commit is contained in:
Martin Brennan
2022-11-07 09:04:47 +10:00
committed by GitHub
parent 5a7b478fee
commit 766bcbc684
9 changed files with 102 additions and 7 deletions

View File

@ -1,4 +1,5 @@
# frozen_string_literal: true
class Chat::ChatMessageUpdater
attr_reader :error
@ -8,7 +9,9 @@ class Chat::ChatMessageUpdater
instance
end
def initialize(chat_message:, new_content:, upload_ids: nil)
def initialize(guardian:, chat_message:, new_content:, upload_ids: nil)
@guardian = guardian
@user = guardian.user
@chat_message = chat_message
@old_message_content = chat_message.message
@chat_channel = @chat_message.chat_channel
@ -23,6 +26,7 @@ class Chat::ChatMessageUpdater
begin
validate_channel_status!
@chat_message.message = @new_content
@chat_message.last_editor_id = @user.id
upload_info = get_upload_info
validate_message!(has_uploads: upload_info[:uploads].any?)
@chat_message.cook
@ -43,6 +47,10 @@ class Chat::ChatMessageUpdater
private
# TODO (martin) Since we have guardian here now we should move
# guardian.ensure_can_edit_chat!(@message) from the controller into
# this class.
def validate_channel_status!
return if @guardian.can_modify_channel_message?(@chat_channel)
raise StandardError.new(
@ -86,6 +94,7 @@ class Chat::ChatMessageUpdater
@chat_message.revisions.create!(
old_message: @old_message_content,
new_message: @chat_message.message,
user_id: @user.id,
)
end
end