This commit is contained in:
mgabdev
2020-12-15 19:31:30 -05:00
parent de0c977950
commit 75d52c841e
129 changed files with 2559 additions and 910 deletions

View File

@@ -0,0 +1,12 @@
# frozen_string_literal: true
class DeleteChatMessageService < BaseService
def call(account, chatMessageId)
@chat = ChatMessage.where(from_account: account).find(chatMessageId)
# : todo :
# make sure last_chat_message_id in chat_account_conversation gets set to last
@chat.destroy!
end
end

View File

@@ -6,12 +6,7 @@ class FanOutOnWriteService < BaseService
# @param [Status] status
def call(status)
raise GabSocial::RaceConditionError if status.visibility.nil?
if status.direct_visibility? || status.limited_visibility?
#
else
deliver_to_self(status) if status.account.local?
end
deliver_to_self(status) if status.account.local?
end
private

View File

@@ -0,0 +1,104 @@
# frozen_string_literal: true
class PostChatMessageService < BaseService
def call(account, options = {})
@account = account
@options = options
@text = @options[:text] || ''
@chat_conversation = @options[:chat_conversation]
preprocess_attributes!
validate_text!
validate_links!
set_chat_conversation_recipients!
set_message_expiration_date!
process_chat!
postprocess_chat!
@chat
end
def preprocess_attributes!
@text = ActionController::Base.helpers.strip_tags(@text)
unless @chat_conversation
raise ActiveRecord::RecordInvalid
end
rescue ArgumentError
raise ActiveRecord::RecordInvalid
end
def validate_links!
raise GabSocial::NotPermittedError if LinkBlock.block?(@text)
end
def validate_text!
raise GabSocial::NotPermittedError if @text.nil? || @text.strip.length == 0
end
def process_chat!
@chat = ChatMessage.create!(
from_account: @account,
chat_conversation: @chat_conversation,
text: @text
expires_at: @expires_at
)
end
def postprocess_chat!
@chat_conversation_recipients_accounts = ChatConversationAccount.where(chat_conversation: @chat_conversation)
@chat_conversation_recipients_accounts.each do |recipient|
recipient.last_chat_message_id = @chat.id
recipient.is_hidden = false # reset to show unless blocked
# Get not mine
if @account_conversation.id != recipient.id
recipient.unread_count = recipient.unread_count + 1
# : todo :
# check if muting, redis
payload = InlineRenderer.render(@chat, recipient.account, :chat_message)
Redis.current.publish("chat_messages:#{recipient.account.id}", Oj.dump(event: :notification, payload: payload))
else
recipient.unread_count = 0
end
recipient.save
end
end
def set_chat_conversation_recipients!
# : todo :
# check if chat blocked
# check if normal blocked
@account_conversation = ChatConversationAccount.where(account: @account, chat_conversation: @chat_conversation).first
rescue ArgumentError
raise ActiveRecord::RecordInvalid
end
def set_message_expiration_date
case @account_conversation.expiration_policy
when :five_minutes
@expires_at = 5.minutes
when :sixty_minutes
@expires_at = 1.hour
when :six_hours
@expires_at = 6.hours
when :one_day
@expires_at = 1.day
when :three_days
@expires_at = 3.days
when :one_week
@expires_at = 1.week
else
@expires_at = nil
end
@expires_at
end
end

View File

@@ -0,0 +1,28 @@
# frozen_string_literal: true
class PurgeChatMessagesService < BaseService
def call(account, chat_conversation)
unless account.is_pro
raise GabSocial::NotPermittedError
end
# Destroy all
ChatMessage.where(from_account: account, chat_conversation: chat_conversation).in_batches.destroy_all
@last_chat_in_conversation = ChatMessage.where(chat_conversation: chat_conversation).first
@chat_conversation_recipients_accounts = ChatConversationAccount.where(chat_conversation: chat_conversation)
@chat_conversation_recipients_accounts.each do |recipient|
# make sure last_chat_message_id in chat_account_conversation gets set to last
unless @last_chat_in_conversation.nil?
recipient.last_chat_message_id = @last_chat_in_conversation.id
else
recipient.last_chat_message_id = nil
end
# Reset and save
recipient.unread_count = 0
recipient.save
end
end
end

View File

@@ -5,14 +5,20 @@ class SuspendAccountService < BaseService
active_relationships
block_relationships
blocked_by_relationships
chat_block_relationships
chat_blocked_by_relationships
conversations
chat_conversations
chat_messages
custom_filters
favourites
follow_requests
list_accounts
media_attachments
mute_relationships
chat_mute_relationships
muted_by_relationships
chat_muted_by_relationships
notifications
owned_lists
passive_relationships
@@ -21,6 +27,10 @@ class SuspendAccountService < BaseService
status_bookmarks
status_pins
subscriptions
group_accounts
group_join_requests
group_removed_accounts
shortcuts
).freeze
ASSOCIATIONS_ON_DESTROY = %w(

View File

@@ -5,6 +5,10 @@ class UpdateAccountService < BaseService
was_locked = account.locked
update_method = raise_error ? :update! : :update
# : todo :
# check if link blocking
# set account.is_flagged_as_spam
account.send(update_method, params).tap do |ret|
next unless ret