# frozen_string_literal: true module CommandTag::Command::StatusTools def handle_boost_once_at_start(args) return unless @parent.present? && StatusPolicy.new(@account, @parent).reblog? status = ReblogService.new.call( @account, @parent, visibility: @status.visibility, spoiler_text: args.join(' ').presence || @status.spoiler_text ) end alias handle_reblog_at_start handle_boost_once_at_start alias handle_rb_at_start handle_boost_once_at_start alias handle_rt_at_start handle_boost_once_at_start def handle_article_before_save(args) return unless author_of_status? && args.present? case args.shift.downcase when 'title', 'name', 't' status.title = args.join(' ') when 'summary', 'abstract', 'cw', 'cn', 's', 'a' @status.title = @status.spoiler_text if @status.title.blank? @status.spoiler_text = args.join(' ') end end def handle_title_before_save(args) args.unshift('title') handle_article_before_save(args) end def handle_summary_before_save(args) args.unshift('summary') handle_article_before_save(args) end alias handle_abstract_before_save handle_summary_before_save def handle_visibility_before_save(args) return unless author_of_status? && args[0].present? args[0] = read_visibility_from(args[0]) return if args[0].blank? if args[1].blank? @status.visibility = args[0].to_sym elsif args[0] == @status.visibility.to_s domains = args[1..-1].map { |domain| normalize_domain(domain) unless domain == '*' }.uniq.compact @status.domain_permissions.where(domain: domains).destroy_all if domains.present? elsif args[0] == 'cc' expect_list = false args[1..-1].uniq.each do |target| if expect_list expect_list = false address_to_list(target) elsif %w(list list:).include?(target.downcase) expect_list = true else mention(resolve_mention(target)) end end elsif args[0] == 'community' @status.visibility = :public @status.domain_permissions.create_or_update(domain: '*', visibility: :unlisted) else args[1..-1].flat_map(&:split).uniq.each do |domain| domain = normalize_domain(domain) unless domain == '*' @status.domain_permissions.create_or_update(domain: domain, visibility: args[0]) if domain.present? end end end alias handle_v_before_save handle_visibility_before_save alias handle_p_before_save handle_visibility_before_save alias handle_privacy_before_save handle_visibility_before_save def handle_notify_before_save(args) return if args[0].blank? @status.notify = read_boolean_from(args[0]) end alias handle_notice_before_save handle_notify_before_save def handle_tags_before_save(args) return if args.blank? cmd = args.shift.downcase args.select! { |tag| tag =~ /\A(#{Tag::HASHTAG_NAME_RE})\z/i } case cmd when 'add', 'a', '+' ProcessHashtagsService.new.call(@status, args) when 'del', 'remove', 'rm', 'r', 'd', '-' RemoveHashtagsService.new.call(@status, args) end end def handle_tag_before_save(args) args.unshift('add') handle_tags_before_save(args) end def handle_untag_before_save(args) args.unshift('del') handle_tags_before_save(args) end private def resolve_mention(mention_text) return unless (match = mention_text.match(Account::MENTION_RE)) username, domain = match[1].split('@') domain = begin if TagManager.instance.local_domain?(domain) nil else TagManager.instance.normalize_domain(domain) end end Account.find_remote(username, domain) end def mention(target_account) return if target_account.blank? || target_account.mentions.where(status: @status).exists? target_account.mentions.create(status: @status, silent: true) end def address_to_list(list_name) return if list_name.blank? list_accounts = ListAccount.joins(:list).where(lists: { account: @account }).where('LOWER(lists.title) = ?', list_name.mb_chars.downcase).includes(:account).map(&:account) list_accounts.each { |target_account| mention(target_account) } end end