<commit_before>require! {
	'../../utils/publish-redis-streaming'
	'../../models/talk-message': TalkMessage
}

module.exports = (app, user, message-id, text) ->
	resolve, reject <- new Promise!
	
	function throw-error(code, message)
		reject {code, message}
	
	if null-or-empty message-id
		throw-error \empty-message-id 'Empty message-id.'
	else
		text .= trim!
		(err, message) <- TalkMessage.find-by-id message-id
		switch
		| err? => throw-error \message-find-error err
		| !message? => throw-error \message-not-found 'Message not found.'
		| not message.is-image-attached and empty text => throw-error \empty-text 'Empty text.'
		| message.user-id.to-string! != user.id.to-string! => throw-error \not-author 'Message that you have sent only can not be modified.'
		| message.is-deleted => throw-error \message-has-been-deleted 'Message has been deleted.'
		| _ =>
			message
				..text = text
				..is-edited = yes
			err <- message.save
			if err?
				throw-error \message-save-error err
			else
				resolve message

				# Streaming events
				publish-redis-streaming "talkStream:#{message.otherparty-id}-#{user.id}" to-json do
					type: \otherpartyMessageUpdate
					value: obj
				publish-redis-streaming "talkStream:#{user.id}-#{message.otherparty-id}" to-json do
					type: \meMessageUpdate
					value: obj<commit_msg>Fix bug<commit_after>require! {
	'../../utils/publish-redis-streaming'
	'../../models/talk-message': TalkMessage
	'../../models/utils/filter-talk-message-for-response'
}

module.exports = (app, user, message-id, text) ->
	resolve, reject <- new Promise!
	
	function throw-error(code, message)
		reject {code, message}
	
	if null-or-empty message-id
		throw-error \empty-message-id 'Empty message-id.'
	else
		text .= trim!
		(err, message) <- TalkMessage.find-by-id message-id
		switch
		| err? => throw-error \message-find-error err
		| !message? => throw-error \message-not-found 'Message not found.'
		| not message.is-image-attached and empty text => throw-error \empty-text 'Empty text.'
		| message.user-id.to-string! != user.id.to-string! => throw-error \not-author 'Message that you have sent only can not be modified.'
		| message.is-deleted => throw-error \message-has-been-deleted 'Message has been deleted.'
		| _ =>
			message
				..text = text
				..is-edited = yes
			err <- message.save
			if err?
				throw-error \message-save-error err
			else
				resolve message

				# Streaming events
				obj <- filter-talk-message-for-response message
				publish-redis-streaming "talkStream:#{message.otherparty-id}-#{user.id}" to-json do
					type: \otherpartyMessageUpdate
					value: obj
				publish-redis-streaming "talkStream:#{user.id}-#{message.otherparty-id}" to-json do
					type: \meMessageUpdate
					value: obj
{'fraction_non_alphanumeric': 0.14798694232861806, 'fraction_numerical': 0.0, 'mean_word_length': 3.073855243722304, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5436860', 'n_tokens_mistral': 1014, 'n_tokens_neox': 885, 'n_words': 218}