]> cat aescling's git repositories - mastodon.git/blob - app/models/status.rb
Do not filter statuses with unknown languages (#5045)
[mastodon.git] / app / models / status.rb
1 # frozen_string_literal: true
2 # == Schema Information
3 #
4 # Table name: statuses
5 #
6 # id :integer not null, primary key
7 # uri :string
8 # account_id :integer not null
9 # text :text default(""), not null
10 # created_at :datetime not null
11 # updated_at :datetime not null
12 # in_reply_to_id :integer
13 # reblog_of_id :integer
14 # url :string
15 # sensitive :boolean default(FALSE), not null
16 # visibility :integer default("public"), not null
17 # in_reply_to_account_id :integer
18 # application_id :integer
19 # spoiler_text :text default(""), not null
20 # reply :boolean default(FALSE), not null
21 # favourites_count :integer default(0), not null
22 # reblogs_count :integer default(0), not null
23 # language :string
24 # conversation_id :integer
25 # local :boolean
26 #
27
28 class Status < ApplicationRecord
29 include Paginable
30 include Streamable
31 include Cacheable
32 include StatusThreadingConcern
33 include EmojiHelper
34
35 enum visibility: [:public, :unlisted, :private, :direct], _suffix: :visibility
36
37 belongs_to :application, class_name: 'Doorkeeper::Application'
38
39 belongs_to :account, inverse_of: :statuses, counter_cache: true, required: true
40 belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account'
41 belongs_to :conversation
42
43 belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies
44 belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, counter_cache: :reblogs_count
45
46 has_many :favourites, inverse_of: :status, dependent: :destroy
47 has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
48 has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
49 has_many :mentions, dependent: :destroy
50 has_many :media_attachments, dependent: :destroy
51
52 has_and_belongs_to_many :tags
53 has_and_belongs_to_many :preview_cards
54
55 has_one :notification, as: :activity, dependent: :destroy
56 has_one :stream_entry, as: :activity, inverse_of: :status
57
58 validates :uri, uniqueness: true, presence: true, unless: :local?
59 validates :text, presence: true, unless: :reblog?
60 validates_with StatusLengthValidator
61 validates :reblog, uniqueness: { scope: :account }, if: :reblog?
62
63 default_scope { recent }
64
65 scope :recent, -> { reorder(id: :desc) }
66 scope :remote, -> { where(local: false).or(where.not(uri: nil)) }
67 scope :local, -> { where(local: true).or(where(uri: nil)) }
68
69 scope :without_replies, -> { where('statuses.reply = FALSE OR statuses.in_reply_to_account_id = statuses.account_id') }
70 scope :without_reblogs, -> { where('statuses.reblog_of_id IS NULL') }
71 scope :with_public_visibility, -> { where(visibility: :public) }
72 scope :tagged_with, ->(tag) { joins(:statuses_tags).where(statuses_tags: { tag_id: tag }) }
73 scope :excluding_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced: false }) }
74 scope :including_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced: true }) }
75 scope :not_excluded_by_account, ->(account) { where.not(account_id: account.excluded_from_timeline_account_ids) }
76 scope :not_domain_blocked_by_account, ->(account) { account.excluded_from_timeline_domains.blank? ? left_outer_joins(:account) : left_outer_joins(:account).where('accounts.domain IS NULL OR accounts.domain NOT IN (?)', account.excluded_from_timeline_domains) }
77
78 cache_associated :account, :application, :media_attachments, :tags, :stream_entry, mentions: :account, reblog: [:account, :application, :stream_entry, :tags, :media_attachments, mentions: :account], thread: :account
79
80 delegate :domain, to: :account, prefix: true
81
82 def reply?
83 !in_reply_to_id.nil? || attributes['reply']
84 end
85
86 def local?
87 attributes['local'] || uri.nil?
88 end
89
90 def reblog?
91 !reblog_of_id.nil?
92 end
93
94 def verb
95 if destroyed?
96 :delete
97 else
98 reblog? ? :share : :post
99 end
100 end
101
102 def object_type
103 reply? ? :comment : :note
104 end
105
106 def proper
107 reblog? ? reblog : self
108 end
109
110 def content
111 proper.text
112 end
113
114 def target
115 reblog
116 end
117
118 def title
119 if destroyed?
120 "#{account.acct} deleted status"
121 else
122 reblog? ? "#{account.acct} shared a status by #{reblog.account.acct}" : "New status by #{account.acct}"
123 end
124 end
125
126 def hidden?
127 private_visibility? || direct_visibility?
128 end
129
130 def non_sensitive_with_media?
131 !sensitive? && media_attachments.any?
132 end
133
134 def emojis
135 CustomEmoji.from_text(text, account.domain)
136 end
137
138 after_create :store_uri, if: :local?
139
140 before_validation :prepare_contents, if: :local?
141 before_validation :set_reblog
142 before_validation :set_visibility
143 before_validation :set_conversation
144 before_validation :set_sensitivity
145 before_validation :set_local
146
147 class << self
148 def not_in_filtered_languages(account)
149 where(language: nil).or where.not(language: account.filtered_languages)
150 end
151
152 def as_home_timeline(account)
153 where(account: [account] + account.following).where(visibility: [:public, :unlisted, :private])
154 end
155
156 def as_public_timeline(account = nil, local_only = false)
157 query = timeline_scope(local_only).without_replies
158
159 apply_timeline_filters(query, account, local_only)
160 end
161
162 def as_tag_timeline(tag, account = nil, local_only = false)
163 query = timeline_scope(local_only).tagged_with(tag)
164
165 apply_timeline_filters(query, account, local_only)
166 end
167
168 def as_outbox_timeline(account)
169 where(account: account, visibility: :public)
170 end
171
172 def favourites_map(status_ids, account_id)
173 Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
174 end
175
176 def reblogs_map(status_ids, account_id)
177 select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h
178 end
179
180 def mutes_map(conversation_ids, account_id)
181 ConversationMute.select('conversation_id').where(conversation_id: conversation_ids).where(account_id: account_id).map { |m| [m.conversation_id, true] }.to_h
182 end
183
184 def pins_map(status_ids, account_id)
185 StatusPin.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |p| [p.status_id, true] }.to_h
186 end
187
188 def reload_stale_associations!(cached_items)
189 account_ids = []
190
191 cached_items.each do |item|
192 account_ids << item.account_id
193 account_ids << item.reblog.account_id if item.reblog?
194 end
195
196 accounts = Account.where(id: account_ids.uniq).map { |a| [a.id, a] }.to_h
197
198 cached_items.each do |item|
199 item.account = accounts[item.account_id]
200 item.reblog.account = accounts[item.reblog.account_id] if item.reblog?
201 end
202 end
203
204 def permitted_for(target_account, account)
205 visibility = [:public, :unlisted]
206
207 if account.nil?
208 where(visibility: visibility)
209 elsif target_account.blocking?(account) # get rid of blocked peeps
210 none
211 elsif account.id == target_account.id # author can see own stuff
212 all
213 else
214 # followers can see followers-only stuff, but also things they are mentioned in.
215 # non-followers can see everything that isn't private/direct, but can see stuff they are mentioned in.
216 visibility.push(:private) if account.following?(target_account)
217
218 joins("LEFT OUTER JOIN mentions ON statuses.id = mentions.status_id AND mentions.account_id = #{account.id}")
219 .where(arel_table[:visibility].in(visibility).or(Mention.arel_table[:id].not_eq(nil)))
220 .order(visibility: :desc)
221 end
222 end
223
224 private
225
226 def timeline_scope(local_only = false)
227 starting_scope = local_only ? Status.local : Status
228 starting_scope
229 .with_public_visibility
230 .without_reblogs
231 end
232
233 def apply_timeline_filters(query, account, local_only)
234 if account.nil?
235 filter_timeline_default(query)
236 else
237 filter_timeline_for_account(query, account, local_only)
238 end
239 end
240
241 def filter_timeline_for_account(query, account, local_only)
242 query = query.not_excluded_by_account(account)
243 query = query.not_domain_blocked_by_account(account) unless local_only
244 query = query.not_in_filtered_languages(account) if account.filtered_languages.present?
245 query.merge(account_silencing_filter(account))
246 end
247
248 def filter_timeline_default(query)
249 query.excluding_silenced_accounts
250 end
251
252 def account_silencing_filter(account)
253 if account.silenced?
254 including_silenced_accounts
255 else
256 excluding_silenced_accounts
257 end
258 end
259 end
260
261 private
262
263 def store_uri
264 update_attribute(:uri, ActivityPub::TagManager.instance.uri_for(self)) if uri.nil?
265 end
266
267 def prepare_contents
268 text&.strip!
269 spoiler_text&.strip!
270
271 self.text = emojify(text)
272 self.spoiler_text = emojify(spoiler_text)
273 end
274
275 def set_reblog
276 self.reblog = reblog.reblog if reblog? && reblog.reblog?
277 end
278
279 def set_visibility
280 self.visibility = (account.locked? ? :private : :public) if visibility.nil?
281 self.sensitive = false if sensitive.nil?
282 end
283
284 def set_sensitivity
285 self.sensitive = sensitive || spoiler_text.present?
286 end
287
288 def set_conversation
289 self.reply = !(in_reply_to_id.nil? && thread.nil?) unless reply
290
291 if reply? && !thread.nil?
292 self.in_reply_to_account_id = carried_over_reply_to_account_id
293 self.conversation_id = thread.conversation_id if conversation_id.nil?
294 elsif conversation_id.nil?
295 create_conversation
296 end
297 end
298
299 def carried_over_reply_to_account_id
300 if thread.account_id == account_id && thread.reply?
301 thread.in_reply_to_account_id
302 else
303 thread.account_id
304 end
305 end
306
307 def set_local
308 self.local = account.local?
309 end
310 end
This page took 0.128438 seconds and 4 git commands to generate.