From 35719f60407adfcc12f59cb9448789a01fbaf2b5 Mon Sep 17 00:00:00 2001 From: Tdxdxoz Date: Thu, 23 Mar 2023 16:52:33 +0800 Subject: [PATCH] feat: support anonymous --- app/controllers/api/v1/statuses_controller.rb | 21 +++++++++++++++++-- config/initializers/new_feature.rb | 10 +++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 config/initializers/new_feature.rb diff --git a/app/controllers/api/v1/statuses_controller.rb b/app/controllers/api/v1/statuses_controller.rb index 8dcf6331e..8310632ad 100644 --- a/app/controllers/api/v1/statuses_controller.rb +++ b/app/controllers/api/v1/statuses_controller.rb @@ -50,10 +50,27 @@ class Api::V1::StatusesController < Api::BaseController render json: @context, serializer: REST::ContextSerializer, relationships: StatusRelationshipsPresenter.new(statuses, current_user&.account_id) end + def generate_anon_name(input, namelist, note) + name = namelist[Digest::SHA2.hexdigest(input).to_i(16) % namelist.size] + name.in?(note) ? nil : name + end + def create + ori_text = status_params[:status] + anon = Rails.configuration.x.anon + anon_name = anon.acc && + ori_text.strip.end_with?(anon.tag, "#{anon.tag} 👁️") && + generate_anon_name( + current_user.account.username + anon.salt + 5.hours.ago.strftime('%D'), + anon.namelist, + Account.find(anon.acc).note + ) + sender = anon_name ? Account.find(anon.acc) : current_user.account + st_text = anon_name ? "[#{anon_name}]:\n#{ori_text}" : ori_text + @status = PostStatusService.new.call( - current_user.account, - text: status_params[:status], + sender, + text: st_text, thread: @thread, media_ids: status_params[:media_ids], sensitive: status_params[:sensitive], diff --git a/config/initializers/new_feature.rb b/config/initializers/new_feature.rb new file mode 100644 index 000000000..6beb571e7 --- /dev/null +++ b/config/initializers/new_feature.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +Rails.application.configure do + config.x.email_default_domain = ENV.fetch('EMAIL_DEFAULT_DOMAIN', '') + config.x.email_regex = ENV.fetch('EMAIL_REGEX', nil) + config.x.anon.tag = ENV.fetch('ANON_TAG', '[mask]') + config.x.anon.acc = ENV.fetch('ANON_ACC', nil) + config.x.anon.namelist = ENV['ANON_NAME_LIST'] ? File.readlines(ENV['ANON_NAME_LIST']).collect(&:strip) : ['Alice', 'Bob', 'Carol', 'Dave'] + config.x.anon.salt = (1..42).map { ('a'..'z').to_a.sample }.join +end