Browse Source

fix bug (static variables are not thread safe)

pull/4/head
欧醚 4 years ago
parent
commit
57f5128fc6
2 changed files with 13 additions and 30 deletions
  1. +9
    -27
      app/controllers/api/v1/statuses_controller.rb
  2. +4
    -3
      config/initializers/new_features.rb

+ 9
- 27
app/controllers/api/v1/statuses_controller.rb View File

@ -1,29 +1,5 @@
# frozen_string_literal: true
class AnonTag
@@namelist = (f = Rails.configuration.x.anon_namelist) ? File.readlines(f).collect(&:strip) : ['Alice', 'Bob', 'Carol', 'Dave']
@@used_name = []
@@map = {}
@@last_use = Time.now
def self.get_or_generate(pid, note)
if Time.now - @@last_use > 1.day
@@map = {}
@@used_name = []
end
@@last_use = Time.now
pre = @@map.empty? ? '*' : ''
if !@@map[pid]
@@map[pid] = (@@namelist - @@used_name).sample()
@@used_name.append(@@map[pid])
end
@@map[pid].in?(note) ? nil : pre + @@map[pid]
end
end
class Api::V1::StatusesController < Api::BaseController
include Authorization
@ -63,9 +39,10 @@ class Api::V1::StatusesController < Api::BaseController
end
def create
anon = Rails.configuration.x.anon_acc && status_params[:status].end_with?(Rails.configuration.x.anon_tag) && AnonTag.get_or_generate(current_user.account_id, Account.find(Rails.configuration.x.anon_acc).note)
sender = anon ? Account.find(Rails.configuration.x.anon_acc) : current_user.account
st_text = anon ? ("[#{anon}]:\n#{status_params[:status]}"[0..5000]) : status_params[:status]
anon = Rails.configuration.x.anon
anon_name = anon.acc && status_params[:status].end_with?(anon.tag) && generate_anon_name(current_user.account.username + anon.salt, anon.namelist, Account.find(anon.acc).note)
sender = anon_name ? Account.find(anon.acc) : current_user.account
st_text = anon_name ? ("[#{anon_name}]:\n#{status_params[:status]}"[0..5000]) : status_params[:status]
@status = PostStatusService.new.call(sender,
text: st_text,
@ -129,4 +106,9 @@ class Api::V1::StatusesController < Api::BaseController
def pagination_params(core_params)
params.slice(:limit).permit(:limit).merge(core_params)
end
def generate_anon_name(k, namelist, note)
name = namelist[Digest::SHA2.hexdigest(k).to_i(16) % namelist.size]
name.in?(note) ? nil : name
end
end

+ 4
- 3
config/initializers/new_features.rb View File

@ -4,8 +4,9 @@ Rails.application.configure do
config.x.email_default_domain = ENV.fetch('EMAIL_DEFAULT_DOMAIN') { '???.edu.cn' }
config.x.tree_address = ENV.fetch('TREE_ADDRESS') {''}
config.x.tree_acc = ENV.fetch('TREE_ACC') {'0'}
config.x.anon_tag = ENV.fetch('ANON_TAG') {'[mask]'}
config.x.anon_acc = ENV.fetch('ANON_ACC') {nil}
config.x.anon_namelist = ENV.fetch('ANON_NAME_LIST') {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

Loading…
Cancel
Save