闭社主体 forked from https://github.com/tootsuite/mastodon
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.

500 lines
15 KiB

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. require 'rails_helper'
  2. RSpec.describe Status, type: :model do
  3. let(:alice) { Fabricate(:account, username: 'alice') }
  4. let(:bob) { Fabricate(:account, username: 'bob') }
  5. let(:other) { Fabricate(:status, account: bob, text: 'Skulls for the skull god! The enemy\'s gates are sideways!')}
  6. subject { Fabricate(:status, account: alice) }
  7. describe '#local?' do
  8. it 'returns true when no remote URI is set' do
  9. expect(subject.local?).to be true
  10. end
  11. it 'returns false if a remote URI is set' do
  12. subject.uri = 'a'
  13. expect(subject.local?).to be false
  14. end
  15. end
  16. describe '#reblog?' do
  17. it 'returns true when the status reblogs another status' do
  18. subject.reblog = other
  19. expect(subject.reblog?).to be true
  20. end
  21. it 'returns false if the status is self-contained' do
  22. expect(subject.reblog?).to be false
  23. end
  24. end
  25. describe '#reply?' do
  26. it 'returns true if the status references another' do
  27. subject.thread = other
  28. expect(subject.reply?).to be true
  29. end
  30. it 'returns false if the status is self-contained' do
  31. expect(subject.reply?).to be false
  32. end
  33. end
  34. describe '#verb' do
  35. it 'is always post' do
  36. expect(subject.verb).to be :post
  37. end
  38. end
  39. describe '#object_type' do
  40. it 'is note when the status is self-contained' do
  41. expect(subject.object_type).to be :note
  42. end
  43. it 'is comment when the status replies to another' do
  44. subject.thread = other
  45. expect(subject.object_type).to be :comment
  46. end
  47. end
  48. describe '#title' do
  49. it 'is a shorter version of the content' do
  50. expect(subject.title).to be_a String
  51. end
  52. end
  53. describe '#content' do
  54. it 'returns the text of the status if it is not a reblog' do
  55. expect(subject.content).to eql subject.text
  56. end
  57. it 'returns the text of the reblogged status' do
  58. subject.reblog = other
  59. expect(subject.content).to eql other.text
  60. end
  61. end
  62. describe '#target' do
  63. it 'returns nil if the status is self-contained' do
  64. expect(subject.target).to be_nil
  65. end
  66. it 'returns nil if the status is a reply' do
  67. subject.thread = other
  68. expect(subject.target).to be_nil
  69. end
  70. it 'returns the reblogged status' do
  71. subject.reblog = other
  72. expect(subject.target).to eq other
  73. end
  74. end
  75. describe '#reblogs_count' do
  76. it 'is the number of reblogs' do
  77. Fabricate(:status, account: bob, reblog: subject)
  78. Fabricate(:status, account: alice, reblog: subject)
  79. expect(subject.reblogs_count).to eq 2
  80. end
  81. end
  82. describe '#favourites_count' do
  83. it 'is the number of favorites' do
  84. Fabricate(:favourite, account: bob, status: subject)
  85. Fabricate(:favourite, account: alice, status: subject)
  86. expect(subject.favourites_count).to eq 2
  87. end
  88. end
  89. describe '#proper' do
  90. it 'is itself for original statuses' do
  91. expect(subject.proper).to eq subject
  92. end
  93. it 'is the source status for reblogs' do
  94. subject.reblog = other
  95. expect(subject.proper).to eq other
  96. end
  97. end
  98. describe '.mutes_map' do
  99. let(:status) { Fabricate(:status) }
  100. let(:account) { Fabricate(:account) }
  101. subject { Status.mutes_map([status.conversation.id], account) }
  102. it 'returns a hash' do
  103. expect(subject).to be_a Hash
  104. end
  105. it 'contains true value' do
  106. account.mute_conversation!(status.conversation)
  107. expect(subject[status.conversation.id]).to be true
  108. end
  109. end
  110. describe '.favourites_map' do
  111. let(:status) { Fabricate(:status) }
  112. let(:account) { Fabricate(:account) }
  113. subject { Status.favourites_map([status], account) }
  114. it 'returns a hash' do
  115. expect(subject).to be_a Hash
  116. end
  117. it 'contains true value' do
  118. Fabricate(:favourite, status: status, account: account)
  119. expect(subject[status.id]).to be true
  120. end
  121. end
  122. describe '.reblogs_map' do
  123. let(:status) { Fabricate(:status) }
  124. let(:account) { Fabricate(:account) }
  125. subject { Status.reblogs_map([status], account) }
  126. it 'returns a hash' do
  127. expect(subject).to be_a Hash
  128. end
  129. it 'contains true value' do
  130. Fabricate(:status, account: account, reblog: status)
  131. expect(subject[status.id]).to be true
  132. end
  133. end
  134. describe '.local_only' do
  135. it 'returns only statuses from local accounts' do
  136. local_account = Fabricate(:account, domain: nil)
  137. remote_account = Fabricate(:account, domain: 'test.com')
  138. local_status = Fabricate(:status, account: local_account)
  139. remote_status = Fabricate(:status, account: remote_account)
  140. results = described_class.local_only
  141. expect(results).to include(local_status)
  142. expect(results).not_to include(remote_status)
  143. end
  144. end
  145. describe '.as_home_timeline' do
  146. before do
  147. account = Fabricate(:account)
  148. followed = Fabricate(:account)
  149. not_followed = Fabricate(:account)
  150. Fabricate(:follow, account: account, target_account: followed)
  151. @self_status = Fabricate(:status, account: account)
  152. @followed_status = Fabricate(:status, account: followed)
  153. @not_followed_status = Fabricate(:status, account: not_followed)
  154. @results = Status.as_home_timeline(account)
  155. end
  156. it 'includes statuses from self' do
  157. expect(@results).to include(@self_status)
  158. end
  159. it 'includes statuses from followed' do
  160. expect(@results).to include(@followed_status)
  161. end
  162. it 'does not include statuses from non-followed' do
  163. expect(@results).not_to include(@not_followed_status)
  164. end
  165. end
  166. describe '.as_public_timeline' do
  167. it 'only includes statuses with public visibility' do
  168. public_status = Fabricate(:status, visibility: :public)
  169. private_status = Fabricate(:status, visibility: :private)
  170. results = Status.as_public_timeline
  171. expect(results).to include(public_status)
  172. expect(results).not_to include(private_status)
  173. end
  174. it 'does not include replies' do
  175. status = Fabricate(:status)
  176. reply = Fabricate(:status, in_reply_to_id: status.id)
  177. results = Status.as_public_timeline
  178. expect(results).to include(status)
  179. expect(results).not_to include(reply)
  180. end
  181. it 'does not include boosts' do
  182. status = Fabricate(:status)
  183. boost = Fabricate(:status, reblog_of_id: status.id)
  184. results = Status.as_public_timeline
  185. expect(results).to include(status)
  186. expect(results).not_to include(boost)
  187. end
  188. it 'filters out silenced accounts' do
  189. account = Fabricate(:account)
  190. silenced_account = Fabricate(:account, silenced: true)
  191. status = Fabricate(:status, account: account)
  192. silenced_status = Fabricate(:status, account: silenced_account)
  193. results = Status.as_public_timeline
  194. expect(results).to include(status)
  195. expect(results).not_to include(silenced_status)
  196. end
  197. context 'without local_only option' do
  198. let(:viewer) { nil }
  199. let!(:local_account) { Fabricate(:account, domain: nil) }
  200. let!(:remote_account) { Fabricate(:account, domain: 'test.com') }
  201. let!(:local_status) { Fabricate(:status, account: local_account) }
  202. let!(:remote_status) { Fabricate(:status, account: remote_account) }
  203. subject { Status.as_public_timeline(viewer, false) }
  204. context 'without a viewer' do
  205. let(:viewer) { nil }
  206. it 'includes remote instances statuses' do
  207. expect(subject).to include(remote_status)
  208. end
  209. it 'includes local statuses' do
  210. expect(subject).to include(local_status)
  211. end
  212. end
  213. context 'with a viewer' do
  214. let(:viewer) { Fabricate(:account, username: 'viewer') }
  215. it 'includes remote instances statuses' do
  216. expect(subject).to include(remote_status)
  217. end
  218. it 'includes local statuses' do
  219. expect(subject).to include(local_status)
  220. end
  221. end
  222. end
  223. context 'with a local_only option set' do
  224. let!(:local_account) { Fabricate(:account, domain: nil) }
  225. let!(:remote_account) { Fabricate(:account, domain: 'test.com') }
  226. let!(:local_status) { Fabricate(:status, account: local_account) }
  227. let!(:remote_status) { Fabricate(:status, account: remote_account) }
  228. subject { Status.as_public_timeline(viewer, true) }
  229. context 'without a viewer' do
  230. let(:viewer) { nil }
  231. it 'does not include remote instances statuses' do
  232. expect(subject).to include(local_status)
  233. expect(subject).not_to include(remote_status)
  234. end
  235. end
  236. context 'with a viewer' do
  237. let(:viewer) { Fabricate(:account, username: 'viewer') }
  238. it 'does not include remote instances statuses' do
  239. expect(subject).to include(local_status)
  240. expect(subject).not_to include(remote_status)
  241. end
  242. it 'is not affected by personal domain blocks' do
  243. viewer.block_domain!('test.com')
  244. expect(subject).to include(local_status)
  245. expect(subject).not_to include(remote_status)
  246. end
  247. end
  248. end
  249. describe 'with an account passed in' do
  250. before do
  251. @account = Fabricate(:account)
  252. end
  253. it 'excludes statuses from accounts blocked by the account' do
  254. blocked = Fabricate(:account)
  255. Fabricate(:block, account: @account, target_account: blocked)
  256. blocked_status = Fabricate(:status, account: blocked)
  257. results = Status.as_public_timeline(@account)
  258. expect(results).not_to include(blocked_status)
  259. end
  260. it 'excludes statuses from accounts who have blocked the account' do
  261. blocked = Fabricate(:account)
  262. Fabricate(:block, account: blocked, target_account: @account)
  263. blocked_status = Fabricate(:status, account: blocked)
  264. results = Status.as_public_timeline(@account)
  265. expect(results).not_to include(blocked_status)
  266. end
  267. it 'excludes statuses from accounts muted by the account' do
  268. muted = Fabricate(:account)
  269. Fabricate(:mute, account: @account, target_account: muted)
  270. muted_status = Fabricate(:status, account: muted)
  271. results = Status.as_public_timeline(@account)
  272. expect(results).not_to include(muted_status)
  273. end
  274. it 'excludes statuses from accounts from personally blocked domains' do
  275. blocked = Fabricate(:account, domain: 'example.com')
  276. @account.block_domain!(blocked.domain)
  277. blocked_status = Fabricate(:status, account: blocked)
  278. results = Status.as_public_timeline(@account)
  279. expect(results).not_to include(blocked_status)
  280. end
  281. context 'with language preferences' do
  282. it 'excludes statuses in languages not allowed by the account user' do
  283. user = Fabricate(:user, filtered_languages: [:fr])
  284. @account.update(user: user)
  285. en_status = Fabricate(:status, language: 'en')
  286. es_status = Fabricate(:status, language: 'es')
  287. fr_status = Fabricate(:status, language: 'fr')
  288. results = Status.as_public_timeline(@account)
  289. expect(results).to include(en_status)
  290. expect(results).to include(es_status)
  291. expect(results).not_to include(fr_status)
  292. end
  293. it 'includes all languages when user does not have a setting' do
  294. user = Fabricate(:user, filtered_languages: [])
  295. @account.update(user: user)
  296. en_status = Fabricate(:status, language: 'en')
  297. es_status = Fabricate(:status, language: 'es')
  298. results = Status.as_public_timeline(@account)
  299. expect(results).to include(en_status)
  300. expect(results).to include(es_status)
  301. end
  302. it 'includes all languages when account does not have a user' do
  303. expect(@account.user).to be_nil
  304. en_status = Fabricate(:status, language: 'en')
  305. es_status = Fabricate(:status, language: 'es')
  306. results = Status.as_public_timeline(@account)
  307. expect(results).to include(en_status)
  308. expect(results).to include(es_status)
  309. end
  310. end
  311. context 'where that account is silenced' do
  312. it 'includes statuses from other accounts that are silenced' do
  313. @account.update(silenced: true)
  314. other_silenced_account = Fabricate(:account, silenced: true)
  315. other_status = Fabricate(:status, account: other_silenced_account)
  316. results = Status.as_public_timeline(@account)
  317. expect(results).to include(other_status)
  318. end
  319. end
  320. end
  321. end
  322. describe '.as_tag_timeline' do
  323. it 'includes statuses with a tag' do
  324. tag = Fabricate(:tag)
  325. status = Fabricate(:status, tags: [tag])
  326. other = Fabricate(:status)
  327. results = Status.as_tag_timeline(tag)
  328. expect(results).to include(status)
  329. expect(results).not_to include(other)
  330. end
  331. it 'allows replies to be included' do
  332. original = Fabricate(:status)
  333. tag = Fabricate(:tag)
  334. status = Fabricate(:status, tags: [tag], in_reply_to_id: original.id)
  335. results = Status.as_tag_timeline(tag)
  336. expect(results).to include(status)
  337. end
  338. end
  339. describe '.permitted_for' do
  340. subject { described_class.permitted_for(target_account, account).pluck(:visibility) }
  341. let(:target_account) { alice }
  342. let(:account) { bob }
  343. let!(:public_status) { Fabricate(:status, account: target_account, visibility: 'public') }
  344. let!(:unlisted_status) { Fabricate(:status, account: target_account, visibility: 'unlisted') }
  345. let!(:private_status) { Fabricate(:status, account: target_account, visibility: 'private') }
  346. let!(:direct_status) do
  347. Fabricate(:status, account: target_account, visibility: 'direct').tap do |status|
  348. Fabricate(:mention, status: status, account: account)
  349. end
  350. end
  351. let!(:other_direct_status) do
  352. Fabricate(:status, account: target_account, visibility: 'direct').tap do |status|
  353. Fabricate(:mention, status: status)
  354. end
  355. end
  356. context 'given nil' do
  357. let(:account) { nil }
  358. let(:direct_status) { nil }
  359. it { is_expected.to eq(%w(unlisted public)) }
  360. end
  361. context 'given blocked account' do
  362. before do
  363. target_account.block!(account)
  364. end
  365. it { is_expected.to be_empty }
  366. end
  367. context 'given same account' do
  368. let(:account) { target_account }
  369. it { is_expected.to eq(%w(direct direct private unlisted public)) }
  370. end
  371. context 'given followed account' do
  372. before do
  373. account.follow!(target_account)
  374. end
  375. it { is_expected.to eq(%w(direct private unlisted public)) }
  376. end
  377. context 'given unfollowed account' do
  378. it { is_expected.to eq(%w(direct unlisted public)) }
  379. end
  380. end
  381. describe 'before_create' do
  382. it 'sets account being replied to correctly over intermediary nodes' do
  383. first_status = Fabricate(:status, account: bob)
  384. intermediary = Fabricate(:status, thread: first_status, account: alice)
  385. final = Fabricate(:status, thread: intermediary, account: alice)
  386. expect(final.in_reply_to_account_id).to eq bob.id
  387. end
  388. it 'creates new conversation for stand-alone status' do
  389. expect(Status.create(account: alice, text: 'First').conversation_id).to_not be_nil
  390. end
  391. it 'keeps conversation of parent node' do
  392. parent = Fabricate(:status, text: 'First')
  393. expect(Status.create(account: alice, thread: parent, text: 'Response').conversation_id).to eq parent.conversation_id
  394. end
  395. end
  396. end