You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

534 lines
16 KiB

Account domain blocks (#2381) * Add <ostatus:conversation /> tag to Atom input/output Only uses ref attribute (not href) because href would be the alternate link that's always included also. Creates new conversation for every non-reply status. Carries over conversation for every reply. Keeps remote URIs verbatim, generates local URIs on the fly like the rest of them. * Conversation muting - prevents notifications that reference a conversation (including replies, favourites, reblogs) from being created. API endpoints /api/v1/statuses/:id/mute and /api/v1/statuses/:id/unmute Currently no way to tell when a status/conversation is muted, so the web UI only has a "disable notifications" button, doesn't work as a toggle * Display "Dismiss notifications" on all statuses in notifications column, not just own * Add "muted" as a boolean attribute on statuses JSON For now always false on contained reblogs, since it's only relevant for statuses returned from the notifications endpoint, which are not nested Remove "Disable notifications" from detailed status view, since it's only relevant in the notifications column * Up max class length * Remove pending test for conversation mute * Add tests, clean up * Rename to "mute conversation" and "unmute conversation" * Raise validation error when trying to mute/unmute status without conversation * Adding account domain blocks that filter notifications and public timelines * Add tests for domain blocks in notifications, public timelines Filter reblogs of blocked domains from home * Add API for listing and creating account domain blocks * API for creating/deleting domain blocks, tests for Status#ancestors and Status#descendants, filter domain blocks from them * Filter domains in streaming API * Update account_domain_block_spec.rb
7 years ago
Fix Account model deprecation warnings (#3689) ``` DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:61) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:62) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:63) ``` Here's PR describing changes to Dirty API https://github.com/rails/rails/pull/25337
7 years ago
Fix Account model deprecation warnings (#3689) ``` DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:61) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:62) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:63) ``` Here's PR describing changes to Dirty API https://github.com/rails/rails/pull/25337
7 years ago
8 years ago
8 years ago
Account domain blocks (#2381) * Add <ostatus:conversation /> tag to Atom input/output Only uses ref attribute (not href) because href would be the alternate link that's always included also. Creates new conversation for every non-reply status. Carries over conversation for every reply. Keeps remote URIs verbatim, generates local URIs on the fly like the rest of them. * Conversation muting - prevents notifications that reference a conversation (including replies, favourites, reblogs) from being created. API endpoints /api/v1/statuses/:id/mute and /api/v1/statuses/:id/unmute Currently no way to tell when a status/conversation is muted, so the web UI only has a "disable notifications" button, doesn't work as a toggle * Display "Dismiss notifications" on all statuses in notifications column, not just own * Add "muted" as a boolean attribute on statuses JSON For now always false on contained reblogs, since it's only relevant for statuses returned from the notifications endpoint, which are not nested Remove "Disable notifications" from detailed status view, since it's only relevant in the notifications column * Up max class length * Remove pending test for conversation mute * Add tests, clean up * Rename to "mute conversation" and "unmute conversation" * Raise validation error when trying to mute/unmute status without conversation * Adding account domain blocks that filter notifications and public timelines * Add tests for domain blocks in notifications, public timelines Filter reblogs of blocked domains from home * Add API for listing and creating account domain blocks * API for creating/deleting domain blocks, tests for Status#ancestors and Status#descendants, filter domain blocks from them * Filter domains in streaming API * Update account_domain_block_spec.rb
7 years ago
8 years ago
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: accounts
  5. #
  6. # id :bigint(8) not null, primary key
  7. # username :string default(""), not null
  8. # domain :string
  9. # secret :string default(""), not null
  10. # private_key :text
  11. # public_key :text default(""), not null
  12. # remote_url :string default(""), not null
  13. # salmon_url :string default(""), not null
  14. # hub_url :string default(""), not null
  15. # created_at :datetime not null
  16. # updated_at :datetime not null
  17. # note :text default(""), not null
  18. # display_name :string default(""), not null
  19. # uri :string default(""), not null
  20. # url :string
  21. # avatar_file_name :string
  22. # avatar_content_type :string
  23. # avatar_file_size :integer
  24. # avatar_updated_at :datetime
  25. # header_file_name :string
  26. # header_content_type :string
  27. # header_file_size :integer
  28. # header_updated_at :datetime
  29. # avatar_remote_url :string
  30. # subscription_expires_at :datetime
  31. # locked :boolean default(FALSE), not null
  32. # header_remote_url :string default(""), not null
  33. # last_webfingered_at :datetime
  34. # inbox_url :string default(""), not null
  35. # outbox_url :string default(""), not null
  36. # shared_inbox_url :string default(""), not null
  37. # followers_url :string default(""), not null
  38. # protocol :integer default("ostatus"), not null
  39. # memorial :boolean default(FALSE), not null
  40. # moved_to_account_id :bigint(8)
  41. # featured_collection_url :string
  42. # fields :jsonb
  43. # actor_type :string
  44. # discoverable :boolean
  45. # also_known_as :string is an Array
  46. # silenced_at :datetime
  47. # suspended_at :datetime
  48. # trust_level :integer
  49. #
  50. class Account < ApplicationRecord
  51. USERNAME_RE = /[a-z0-9_]+([a-z0-9_\.-]+[a-z0-9_]+)?/i
  52. MENTION_RE = /(?<=^|[^\/[:word:]])@((#{USERNAME_RE})(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i
  53. include AccountAssociations
  54. include AccountAvatar
  55. include AccountFinderConcern
  56. include AccountHeader
  57. include AccountInteractions
  58. include Attachmentable
  59. include Paginable
  60. include AccountCounters
  61. include DomainNormalizable
  62. TRUST_LEVELS = {
  63. untrusted: 0,
  64. trusted: 1,
  65. }.freeze
  66. enum protocol: [:ostatus, :activitypub]
  67. validates :username, presence: true
  68. # Remote user validations
  69. validates :username, uniqueness: { scope: :domain, case_sensitive: true }, if: -> { !local? && will_save_change_to_username? }
  70. validates :username, format: { with: /\A#{USERNAME_RE}\z/i }, if: -> { !local? && will_save_change_to_username? }
  71. # Local user validations
  72. validates :username, format: { with: /\A[a-z0-9_]+\z/i }, length: { maximum: 30 }, if: -> { local? && will_save_change_to_username? && actor_type != 'Application' }
  73. validates_with UniqueUsernameValidator, if: -> { local? && will_save_change_to_username? }
  74. validates_with UnreservedUsernameValidator, if: -> { local? && will_save_change_to_username? }
  75. validates :display_name, length: { maximum: 30 }, if: -> { local? && will_save_change_to_display_name? }
  76. validates :note, note_length: { maximum: 500 }, if: -> { local? && will_save_change_to_note? }
  77. validates :fields, length: { maximum: 4 }, if: -> { local? && will_save_change_to_fields? }
  78. scope :remote, -> { where.not(domain: nil) }
  79. scope :local, -> { where(domain: nil) }
  80. scope :expiring, ->(time) { remote.where.not(subscription_expires_at: nil).where('subscription_expires_at < ?', time) }
  81. scope :partitioned, -> { order(Arel.sql('row_number() over (partition by domain)')) }
  82. scope :silenced, -> { where.not(silenced_at: nil) }
  83. scope :suspended, -> { where.not(suspended_at: nil) }
  84. scope :without_suspended, -> { where(suspended_at: nil) }
  85. scope :without_silenced, -> { where(silenced_at: nil) }
  86. scope :recent, -> { reorder(id: :desc) }
  87. scope :bots, -> { where(actor_type: %w(Application Service)) }
  88. scope :alphabetic, -> { order(domain: :asc, username: :asc) }
  89. scope :by_domain_accounts, -> { group(:domain).select(:domain, 'COUNT(*) AS accounts_count').order('accounts_count desc') }
  90. scope :matches_username, ->(value) { where(arel_table[:username].matches("#{value}%")) }
  91. scope :matches_display_name, ->(value) { where(arel_table[:display_name].matches("#{value}%")) }
  92. scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
  93. scope :searchable, -> { without_suspended.where(moved_to_account_id: nil) }
  94. scope :discoverable, -> { searchable.without_silenced.where(discoverable: true).left_outer_joins(:account_stat) }
  95. scope :tagged_with, ->(tag) { joins(:accounts_tags).where(accounts_tags: { tag_id: tag }) }
  96. scope :by_recent_status, -> { order(Arel.sql('(case when account_stats.last_status_at is null then 1 else 0 end) asc, account_stats.last_status_at desc, accounts.id desc')) }
  97. scope :popular, -> { order('account_stats.followers_count desc') }
  98. scope :by_domain_and_subdomains, ->(domain) { where(domain: domain).or(where(arel_table[:domain].matches('%.' + domain))) }
  99. scope :not_excluded_by_account, ->(account) { where.not(id: account.excluded_from_timeline_account_ids) }
  100. scope :not_domain_blocked_by_account, ->(account) { where(arel_table[:domain].eq(nil).or(arel_table[:domain].not_in(account.excluded_from_timeline_domains))) }
  101. delegate :email,
  102. :unconfirmed_email,
  103. :current_sign_in_ip,
  104. :current_sign_in_at,
  105. :confirmed?,
  106. :approved?,
  107. :pending?,
  108. :disabled?,
  109. :unconfirmed_or_pending?,
  110. :role,
  111. :admin?,
  112. :moderator?,
  113. :staff?,
  114. :locale,
  115. :hides_network?,
  116. :shows_application?,
  117. to: :user,
  118. prefix: true,
  119. allow_nil: true
  120. delegate :chosen_languages, to: :user, prefix: false, allow_nil: true
  121. update_index('accounts#account', :self)
  122. def local?
  123. domain.nil?
  124. end
  125. def moved?
  126. moved_to_account_id.present?
  127. end
  128. def bot?
  129. %w(Application Service).include? actor_type
  130. end
  131. def instance_actor?
  132. id == -99
  133. end
  134. alias bot bot?
  135. def bot=(val)
  136. self.actor_type = ActiveModel::Type::Boolean.new.cast(val) ? 'Service' : 'Person'
  137. end
  138. def acct
  139. local? ? username : "#{username}@#{domain}"
  140. end
  141. def local_username_and_domain
  142. "#{username}@#{Rails.configuration.x.local_domain}"
  143. end
  144. def local_followers_count
  145. Follow.where(target_account_id: id).count
  146. end
  147. def to_webfinger_s
  148. "acct:#{local_username_and_domain}"
  149. end
  150. def subscribed?
  151. subscription_expires_at.present?
  152. end
  153. def searchable?
  154. !(suspended? || moved?)
  155. end
  156. def possibly_stale?
  157. last_webfingered_at.nil? || last_webfingered_at <= 1.day.ago
  158. end
  159. def trust_level
  160. self[:trust_level] || 0
  161. end
  162. def refresh!
  163. ResolveAccountService.new.call(acct) unless local?
  164. end
  165. def silenced?
  166. silenced_at.present?
  167. end
  168. def silence!(date = Time.now.utc)
  169. update!(silenced_at: date)
  170. end
  171. def unsilence!
  172. update!(silenced_at: nil, trust_level: trust_level == TRUST_LEVELS[:untrusted] ? TRUST_LEVELS[:trusted] : trust_level)
  173. end
  174. def suspended?
  175. suspended_at.present?
  176. end
  177. def suspend!(date = Time.now.utc)
  178. transaction do
  179. user&.disable! if local?
  180. update!(suspended_at: date)
  181. end
  182. end
  183. def unsuspend!
  184. transaction do
  185. user&.enable! if local?
  186. update!(suspended_at: nil)
  187. end
  188. end
  189. def memorialize!
  190. transaction do
  191. user&.disable! if local?
  192. update!(memorial: true)
  193. end
  194. end
  195. def sign?
  196. true
  197. end
  198. def keypair
  199. @keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key)
  200. end
  201. def tags_as_strings=(tag_names)
  202. hashtags_map = Tag.find_or_create_by_names(tag_names).each_with_object({}) { |tag, h| h[tag.name] = tag }
  203. # Remove hashtags that are to be deleted
  204. tags.each do |tag|
  205. if hashtags_map.key?(tag.name)
  206. hashtags_map.delete(tag.name)
  207. else
  208. transaction do
  209. tags.delete(tag)
  210. tag.decrement_count!(:accounts_count)
  211. end
  212. end
  213. end
  214. # Add hashtags that were so far missing
  215. hashtags_map.each_value do |tag|
  216. transaction do
  217. tags << tag
  218. tag.increment_count!(:accounts_count)
  219. end
  220. end
  221. end
  222. def also_known_as
  223. self[:also_known_as] || []
  224. end
  225. def fields
  226. (self[:fields] || []).map { |f| Field.new(self, f) }
  227. end
  228. def fields_attributes=(attributes)
  229. fields = []
  230. old_fields = self[:fields] || []
  231. old_fields = [] if old_fields.is_a?(Hash)
  232. if attributes.is_a?(Hash)
  233. attributes.each_value do |attr|
  234. next if attr[:name].blank?
  235. previous = old_fields.find { |item| item['value'] == attr[:value] }
  236. if previous && previous['verified_at'].present?
  237. attr[:verified_at] = previous['verified_at']
  238. end
  239. fields << attr
  240. end
  241. end
  242. self[:fields] = fields
  243. end
  244. DEFAULT_FIELDS_SIZE = 4
  245. def build_fields
  246. return if fields.size >= DEFAULT_FIELDS_SIZE
  247. tmp = self[:fields] || []
  248. tmp = [] if tmp.is_a?(Hash)
  249. (DEFAULT_FIELDS_SIZE - tmp.size).times do
  250. tmp << { name: '', value: '' }
  251. end
  252. self.fields = tmp
  253. end
  254. def subscription(webhook_url)
  255. @subscription ||= OStatus2::Subscription.new(remote_url, secret: secret, webhook: webhook_url, hub: hub_url)
  256. end
  257. def save_with_optional_media!
  258. save!
  259. rescue ActiveRecord::RecordInvalid
  260. self.avatar = nil
  261. self.header = nil
  262. self[:avatar_remote_url] = ''
  263. self[:header_remote_url] = ''
  264. save!
  265. end
  266. def object_type
  267. :person
  268. end
  269. def to_param
  270. username
  271. end
  272. def excluded_from_timeline_account_ids
  273. Rails.cache.fetch("exclude_account_ids_for:#{id}") { blocking.pluck(:target_account_id) + blocked_by.pluck(:account_id) + muting.pluck(:target_account_id) }
  274. end
  275. def excluded_from_timeline_domains
  276. Rails.cache.fetch("exclude_domains_for:#{id}") { domain_blocks.pluck(:domain) }
  277. end
  278. def preferred_inbox_url
  279. shared_inbox_url.presence || inbox_url
  280. end
  281. class Field < ActiveModelSerializers::Model
  282. attributes :name, :value, :verified_at, :account, :errors
  283. def initialize(account, attributes)
  284. @account = account
  285. @attributes = attributes
  286. @name = attributes['name'].strip[0, string_limit]
  287. @value = attributes['value'].strip[0, string_limit]
  288. @verified_at = attributes['verified_at']&.to_datetime
  289. @errors = {}
  290. end
  291. def verified?
  292. verified_at.present?
  293. end
  294. def value_for_verification
  295. @value_for_verification ||= begin
  296. if account.local?
  297. value
  298. else
  299. ActionController::Base.helpers.strip_tags(value)
  300. end
  301. end
  302. end
  303. def verifiable?
  304. value_for_verification.present? && value_for_verification.start_with?('http://', 'https://')
  305. end
  306. def mark_verified!
  307. @verified_at = Time.now.utc
  308. @attributes['verified_at'] = @verified_at
  309. end
  310. def to_h
  311. { name: @name, value: @value, verified_at: @verified_at }
  312. end
  313. private
  314. def string_limit
  315. if account.local?
  316. 255
  317. else
  318. 2047
  319. end
  320. end
  321. end
  322. class << self
  323. def readonly_attributes
  324. super - %w(statuses_count following_count followers_count)
  325. end
  326. def domains
  327. reorder(nil).pluck(Arel.sql('distinct accounts.domain'))
  328. end
  329. def inboxes
  330. urls = reorder(nil).where(protocol: :activitypub).pluck(Arel.sql("distinct coalesce(nullif(accounts.shared_inbox_url, ''), accounts.inbox_url)"))
  331. DeliveryFailureTracker.filter(urls)
  332. end
  333. def search_for(terms, limit = 10, offset = 0)
  334. textsearch, query = generate_query_for_search(terms)
  335. sql = <<-SQL.squish
  336. SELECT
  337. accounts.*,
  338. ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  339. FROM accounts
  340. WHERE #{query} @@ #{textsearch}
  341. AND accounts.suspended_at IS NULL
  342. AND accounts.moved_to_account_id IS NULL
  343. ORDER BY rank DESC
  344. LIMIT ? OFFSET ?
  345. SQL
  346. records = find_by_sql([sql, limit, offset])
  347. ActiveRecord::Associations::Preloader.new.preload(records, :account_stat)
  348. records
  349. end
  350. def advanced_search_for(terms, account, limit = 10, following = false, offset = 0)
  351. textsearch, query = generate_query_for_search(terms)
  352. if following
  353. sql = <<-SQL.squish
  354. WITH first_degree AS (
  355. SELECT target_account_id
  356. FROM follows
  357. WHERE account_id = ?
  358. )
  359. SELECT
  360. accounts.*,
  361. (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  362. FROM accounts
  363. LEFT OUTER JOIN follows AS f ON (accounts.id = f.account_id AND f.target_account_id = ?) OR (accounts.id = f.target_account_id AND f.account_id = ?)
  364. WHERE accounts.id IN (SELECT * FROM first_degree)
  365. AND #{query} @@ #{textsearch}
  366. AND accounts.suspended_at IS NULL
  367. AND accounts.moved_to_account_id IS NULL
  368. GROUP BY accounts.id
  369. ORDER BY rank DESC
  370. LIMIT ? OFFSET ?
  371. SQL
  372. records = find_by_sql([sql, account.id, account.id, account.id, limit, offset])
  373. else
  374. sql = <<-SQL.squish
  375. SELECT
  376. accounts.*,
  377. (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  378. FROM accounts
  379. LEFT OUTER JOIN follows AS f ON (accounts.id = f.account_id AND f.target_account_id = ?) OR (accounts.id = f.target_account_id AND f.account_id = ?)
  380. WHERE #{query} @@ #{textsearch}
  381. AND accounts.suspended_at IS NULL
  382. AND accounts.moved_to_account_id IS NULL
  383. GROUP BY accounts.id
  384. ORDER BY rank DESC
  385. LIMIT ? OFFSET ?
  386. SQL
  387. records = find_by_sql([sql, account.id, account.id, limit, offset])
  388. end
  389. ActiveRecord::Associations::Preloader.new.preload(records, :account_stat)
  390. records
  391. end
  392. private
  393. def generate_query_for_search(terms)
  394. terms = Arel.sql(connection.quote(terms.gsub(/['?\\:]/, ' ')))
  395. textsearch = "(setweight(to_tsvector('simple', accounts.display_name), 'A') || setweight(to_tsvector('simple', accounts.username), 'B') || setweight(to_tsvector('simple', coalesce(accounts.domain, '')), 'C'))"
  396. query = "to_tsquery('simple', ''' ' || #{terms} || ' ''' || ':*')"
  397. [textsearch, query]
  398. end
  399. end
  400. def emojis
  401. @emojis ||= CustomEmoji.from_text(emojifiable_text, domain)
  402. end
  403. before_create :generate_keys
  404. before_validation :prepare_contents, if: :local?
  405. before_validation :prepare_username, on: :create
  406. before_destroy :clean_feed_manager
  407. private
  408. def prepare_contents
  409. display_name&.strip!
  410. note&.strip!
  411. end
  412. def prepare_username
  413. username&.squish!
  414. end
  415. def generate_keys
  416. return unless local? && private_key.blank? && public_key.blank?
  417. keypair = OpenSSL::PKey::RSA.new(2048)
  418. self.private_key = keypair.to_pem
  419. self.public_key = keypair.public_key.to_pem
  420. end
  421. def normalize_domain
  422. return if local?
  423. super
  424. end
  425. def emojifiable_text
  426. [note, display_name, fields.map(&:name), fields.map(&:value)].join(' ')
  427. end
  428. def clean_feed_manager
  429. reblog_key = FeedManager.instance.key(:home, id, 'reblogs')
  430. reblogged_id_set = Redis.current.zrange(reblog_key, 0, -1)
  431. Redis.current.pipelined do
  432. Redis.current.del(FeedManager.instance.key(:home, id))
  433. Redis.current.del(reblog_key)
  434. reblogged_id_set.each do |reblogged_id|
  435. reblog_set_key = FeedManager.instance.key(:home, id, "reblogs:#{reblogged_id}")
  436. Redis.current.del(reblog_set_key)
  437. end
  438. end
  439. end
  440. end