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.

671 lines
21 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. alice.update(domain: 'example.com')
  13. subject.save
  14. expect(subject.local?).to be false
  15. end
  16. it 'returns true if a URI is set and `local` is true' do
  17. subject.update(uri: 'example.com', local: true)
  18. expect(subject.local?).to be true
  19. end
  20. end
  21. describe '#reblog?' do
  22. it 'returns true when the status reblogs another status' do
  23. subject.reblog = other
  24. expect(subject.reblog?).to be true
  25. end
  26. it 'returns false if the status is self-contained' do
  27. expect(subject.reblog?).to be false
  28. end
  29. end
  30. describe '#reply?' do
  31. it 'returns true if the status references another' do
  32. subject.thread = other
  33. expect(subject.reply?).to be true
  34. end
  35. it 'returns false if the status is self-contained' do
  36. expect(subject.reply?).to be false
  37. end
  38. end
  39. describe '#verb' do
  40. context 'if destroyed?' do
  41. it 'returns :delete' do
  42. subject.destroy!
  43. expect(subject.verb).to be :delete
  44. end
  45. end
  46. context 'unless destroyed?' do
  47. context 'if reblog?' do
  48. it 'returns :share' do
  49. subject.reblog = other
  50. expect(subject.verb).to be :share
  51. end
  52. end
  53. context 'unless reblog?' do
  54. it 'returns :post' do
  55. subject.reblog = nil
  56. expect(subject.verb).to be :post
  57. end
  58. end
  59. end
  60. end
  61. describe '#object_type' do
  62. it 'is note when the status is self-contained' do
  63. expect(subject.object_type).to be :note
  64. end
  65. it 'is comment when the status replies to another' do
  66. subject.thread = other
  67. expect(subject.object_type).to be :comment
  68. end
  69. end
  70. describe '#title' do
  71. # rubocop:disable Style/InterpolationCheck
  72. let(:account) { subject.account }
  73. context 'if destroyed?' do
  74. it 'returns "#{account.acct} deleted status"' do
  75. subject.destroy!
  76. expect(subject.title).to eq "#{account.acct} deleted status"
  77. end
  78. end
  79. context 'unless destroyed?' do
  80. context 'if reblog?' do
  81. it 'returns "#{account.acct} shared a status by #{reblog.account.acct}"' do
  82. reblog = subject.reblog = other
  83. expect(subject.title).to eq "#{account.acct} shared a status by #{reblog.account.acct}"
  84. end
  85. end
  86. context 'unless reblog?' do
  87. it 'returns "New status by #{account.acct}"' do
  88. subject.reblog = nil
  89. expect(subject.title).to eq "New status by #{account.acct}"
  90. end
  91. end
  92. end
  93. end
  94. describe '#hidden?' do
  95. context 'if private_visibility?' do
  96. it 'returns true' do
  97. subject.visibility = :private
  98. expect(subject.hidden?).to be true
  99. end
  100. end
  101. context 'if direct_visibility?' do
  102. it 'returns true' do
  103. subject.visibility = :direct
  104. expect(subject.hidden?).to be true
  105. end
  106. end
  107. context 'if public_visibility?' do
  108. it 'returns false' do
  109. subject.visibility = :public
  110. expect(subject.hidden?).to be false
  111. end
  112. end
  113. context 'if unlisted_visibility?' do
  114. it 'returns false' do
  115. subject.visibility = :unlisted
  116. expect(subject.hidden?).to be false
  117. end
  118. end
  119. end
  120. describe '#content' do
  121. it 'returns the text of the status if it is not a reblog' do
  122. expect(subject.content).to eql subject.text
  123. end
  124. it 'returns the text of the reblogged status' do
  125. subject.reblog = other
  126. expect(subject.content).to eql other.text
  127. end
  128. end
  129. describe '#target' do
  130. it 'returns nil if the status is self-contained' do
  131. expect(subject.target).to be_nil
  132. end
  133. it 'returns nil if the status is a reply' do
  134. subject.thread = other
  135. expect(subject.target).to be_nil
  136. end
  137. it 'returns the reblogged status' do
  138. subject.reblog = other
  139. expect(subject.target).to eq other
  140. end
  141. end
  142. describe '#reblogs_count' do
  143. it 'is the number of reblogs' do
  144. Fabricate(:status, account: bob, reblog: subject)
  145. Fabricate(:status, account: alice, reblog: subject)
  146. expect(subject.reblogs_count).to eq 2
  147. end
  148. end
  149. describe '#favourites_count' do
  150. it 'is the number of favorites' do
  151. Fabricate(:favourite, account: bob, status: subject)
  152. Fabricate(:favourite, account: alice, status: subject)
  153. expect(subject.favourites_count).to eq 2
  154. end
  155. end
  156. describe '#proper' do
  157. it 'is itself for original statuses' do
  158. expect(subject.proper).to eq subject
  159. end
  160. it 'is the source status for reblogs' do
  161. subject.reblog = other
  162. expect(subject.proper).to eq other
  163. end
  164. end
  165. describe '.mutes_map' do
  166. let(:status) { Fabricate(:status) }
  167. let(:account) { Fabricate(:account) }
  168. subject { Status.mutes_map([status.conversation.id], account) }
  169. it 'returns a hash' do
  170. expect(subject).to be_a Hash
  171. end
  172. it 'contains true value' do
  173. account.mute_conversation!(status.conversation)
  174. expect(subject[status.conversation.id]).to be true
  175. end
  176. end
  177. describe '.favourites_map' do
  178. let(:status) { Fabricate(:status) }
  179. let(:account) { Fabricate(:account) }
  180. subject { Status.favourites_map([status], account) }
  181. it 'returns a hash' do
  182. expect(subject).to be_a Hash
  183. end
  184. it 'contains true value' do
  185. Fabricate(:favourite, status: status, account: account)
  186. expect(subject[status.id]).to be true
  187. end
  188. end
  189. describe '.reblogs_map' do
  190. let(:status) { Fabricate(:status) }
  191. let(:account) { Fabricate(:account) }
  192. subject { Status.reblogs_map([status], account) }
  193. it 'returns a hash' do
  194. expect(subject).to be_a Hash
  195. end
  196. it 'contains true value' do
  197. Fabricate(:status, account: account, reblog: status)
  198. expect(subject[status.id]).to be true
  199. end
  200. end
  201. describe '.not_in_filtered_languages' do
  202. context 'for accounts with language filters' do
  203. let(:user) { Fabricate(:user, filtered_languages: ['en']) }
  204. it 'does not include statuses in filtered languages' do
  205. status = Fabricate(:status, language: 'en')
  206. expect(Status.not_in_filtered_languages(user.account)).not_to include status
  207. end
  208. it 'includes status with unknown language' do
  209. status = Fabricate(:status, language: nil)
  210. expect(Status.not_in_filtered_languages(user.account)).to include status
  211. end
  212. end
  213. end
  214. describe '.as_home_timeline' do
  215. let(:account) { Fabricate(:account) }
  216. let(:followed) { Fabricate(:account) }
  217. let(:not_followed) { Fabricate(:account) }
  218. before do
  219. Fabricate(:follow, account: account, target_account: followed)
  220. @self_status = Fabricate(:status, account: account, visibility: :public)
  221. @self_direct_status = Fabricate(:status, account: account, visibility: :direct)
  222. @followed_status = Fabricate(:status, account: followed, visibility: :public)
  223. @followed_direct_status = Fabricate(:status, account: followed, visibility: :direct)
  224. @not_followed_status = Fabricate(:status, account: not_followed, visibility: :public)
  225. @results = Status.as_home_timeline(account)
  226. end
  227. it 'includes statuses from self' do
  228. expect(@results).to include(@self_status)
  229. end
  230. it 'does not include direct statuses from self' do
  231. expect(@results).to_not include(@self_direct_status)
  232. end
  233. it 'includes statuses from followed' do
  234. expect(@results).to include(@followed_status)
  235. end
  236. it 'does not include direct statuses mentioning recipient from followed' do
  237. Fabricate(:mention, account: account, status: @followed_direct_status)
  238. expect(@results).to_not include(@followed_direct_status)
  239. end
  240. it 'does not include direct statuses not mentioning recipient from followed' do
  241. expect(@results).not_to include(@followed_direct_status)
  242. end
  243. it 'does not include statuses from non-followed' do
  244. expect(@results).not_to include(@not_followed_status)
  245. end
  246. end
  247. describe '.as_direct_timeline' do
  248. let(:account) { Fabricate(:account) }
  249. let(:followed) { Fabricate(:account) }
  250. let(:not_followed) { Fabricate(:account) }
  251. before do
  252. Fabricate(:follow, account: account, target_account: followed)
  253. @self_public_status = Fabricate(:status, account: account, visibility: :public)
  254. @self_direct_status = Fabricate(:status, account: account, visibility: :direct)
  255. @followed_public_status = Fabricate(:status, account: followed, visibility: :public)
  256. @followed_direct_status = Fabricate(:status, account: followed, visibility: :direct)
  257. @not_followed_direct_status = Fabricate(:status, account: not_followed, visibility: :direct)
  258. @results = Status.as_direct_timeline(account)
  259. end
  260. it 'does not include public statuses from self' do
  261. expect(@results).to_not include(@self_public_status)
  262. end
  263. it 'includes direct statuses from self' do
  264. expect(@results).to include(@self_direct_status)
  265. end
  266. it 'does not include public statuses from followed' do
  267. expect(@results).to_not include(@followed_public_status)
  268. end
  269. it 'includes direct statuses mentioning recipient from followed' do
  270. Fabricate(:mention, account: account, status: @followed_direct_status)
  271. expect(@results).to include(@followed_direct_status)
  272. end
  273. it 'does not include direct statuses not mentioning recipient from followed' do
  274. expect(@results).to_not include(@followed_direct_status)
  275. end
  276. it 'includes direct statuses mentioning recipient from non-followed' do
  277. Fabricate(:mention, account: account, status: @not_followed_direct_status)
  278. expect(@results).to include(@not_followed_direct_status)
  279. end
  280. it 'does not include direct statuses not mentioning recipient from non-followed' do
  281. expect(@results).to_not include(@not_followed_direct_status)
  282. end
  283. end
  284. describe '.as_public_timeline' do
  285. it 'only includes statuses with public visibility' do
  286. public_status = Fabricate(:status, visibility: :public)
  287. private_status = Fabricate(:status, visibility: :private)
  288. results = Status.as_public_timeline
  289. expect(results).to include(public_status)
  290. expect(results).not_to include(private_status)
  291. end
  292. it 'does not include replies' do
  293. status = Fabricate(:status)
  294. reply = Fabricate(:status, in_reply_to_id: status.id)
  295. results = Status.as_public_timeline
  296. expect(results).to include(status)
  297. expect(results).not_to include(reply)
  298. end
  299. it 'does not include boosts' do
  300. status = Fabricate(:status)
  301. boost = Fabricate(:status, reblog_of_id: status.id)
  302. results = Status.as_public_timeline
  303. expect(results).to include(status)
  304. expect(results).not_to include(boost)
  305. end
  306. it 'filters out silenced accounts' do
  307. account = Fabricate(:account)
  308. silenced_account = Fabricate(:account, silenced: true)
  309. status = Fabricate(:status, account: account)
  310. silenced_status = Fabricate(:status, account: silenced_account)
  311. results = Status.as_public_timeline
  312. expect(results).to include(status)
  313. expect(results).not_to include(silenced_status)
  314. end
  315. context 'without local_only option' do
  316. let(:viewer) { nil }
  317. let!(:local_account) { Fabricate(:account, domain: nil) }
  318. let!(:remote_account) { Fabricate(:account, domain: 'test.com') }
  319. let!(:local_status) { Fabricate(:status, account: local_account) }
  320. let!(:remote_status) { Fabricate(:status, account: remote_account) }
  321. subject { Status.as_public_timeline(viewer, false) }
  322. context 'without a viewer' do
  323. let(:viewer) { nil }
  324. it 'includes remote instances statuses' do
  325. expect(subject).to include(remote_status)
  326. end
  327. it 'includes local statuses' do
  328. expect(subject).to include(local_status)
  329. end
  330. end
  331. context 'with a viewer' do
  332. let(:viewer) { Fabricate(:account, username: 'viewer') }
  333. it 'includes remote instances statuses' do
  334. expect(subject).to include(remote_status)
  335. end
  336. it 'includes local statuses' do
  337. expect(subject).to include(local_status)
  338. end
  339. end
  340. end
  341. context 'with a local_only option set' do
  342. let!(:local_account) { Fabricate(:account, domain: nil) }
  343. let!(:remote_account) { Fabricate(:account, domain: 'test.com') }
  344. let!(:local_status) { Fabricate(:status, account: local_account) }
  345. let!(:remote_status) { Fabricate(:status, account: remote_account) }
  346. subject { Status.as_public_timeline(viewer, true) }
  347. context 'without a viewer' do
  348. let(:viewer) { nil }
  349. it 'does not include remote instances statuses' do
  350. expect(subject).to include(local_status)
  351. expect(subject).not_to include(remote_status)
  352. end
  353. end
  354. context 'with a viewer' do
  355. let(:viewer) { Fabricate(:account, username: 'viewer') }
  356. it 'does not include remote instances statuses' do
  357. expect(subject).to include(local_status)
  358. expect(subject).not_to include(remote_status)
  359. end
  360. it 'is not affected by personal domain blocks' do
  361. viewer.block_domain!('test.com')
  362. expect(subject).to include(local_status)
  363. expect(subject).not_to include(remote_status)
  364. end
  365. end
  366. end
  367. describe 'with an account passed in' do
  368. before do
  369. @account = Fabricate(:account)
  370. end
  371. it 'excludes statuses from accounts blocked by the account' do
  372. blocked = Fabricate(:account)
  373. Fabricate(:block, account: @account, target_account: blocked)
  374. blocked_status = Fabricate(:status, account: blocked)
  375. results = Status.as_public_timeline(@account)
  376. expect(results).not_to include(blocked_status)
  377. end
  378. it 'excludes statuses from accounts who have blocked the account' do
  379. blocked = Fabricate(:account)
  380. Fabricate(:block, account: blocked, target_account: @account)
  381. blocked_status = Fabricate(:status, account: blocked)
  382. results = Status.as_public_timeline(@account)
  383. expect(results).not_to include(blocked_status)
  384. end
  385. it 'excludes statuses from accounts muted by the account' do
  386. muted = Fabricate(:account)
  387. Fabricate(:mute, account: @account, target_account: muted)
  388. muted_status = Fabricate(:status, account: muted)
  389. results = Status.as_public_timeline(@account)
  390. expect(results).not_to include(muted_status)
  391. end
  392. it 'excludes statuses from accounts from personally blocked domains' do
  393. blocked = Fabricate(:account, domain: 'example.com')
  394. @account.block_domain!(blocked.domain)
  395. blocked_status = Fabricate(:status, account: blocked)
  396. results = Status.as_public_timeline(@account)
  397. expect(results).not_to include(blocked_status)
  398. end
  399. context 'with language preferences' do
  400. it 'excludes statuses in languages not allowed by the account user' do
  401. user = Fabricate(:user, filtered_languages: [:fr])
  402. @account.update(user: user)
  403. en_status = Fabricate(:status, language: 'en')
  404. es_status = Fabricate(:status, language: 'es')
  405. fr_status = Fabricate(:status, language: 'fr')
  406. results = Status.as_public_timeline(@account)
  407. expect(results).to include(en_status)
  408. expect(results).to include(es_status)
  409. expect(results).not_to include(fr_status)
  410. end
  411. it 'includes all languages when user does not have a setting' do
  412. user = Fabricate(:user, filtered_languages: [])
  413. @account.update(user: user)
  414. en_status = Fabricate(:status, language: 'en')
  415. es_status = Fabricate(:status, language: 'es')
  416. results = Status.as_public_timeline(@account)
  417. expect(results).to include(en_status)
  418. expect(results).to include(es_status)
  419. end
  420. it 'includes all languages when account does not have a user' do
  421. expect(@account.user).to be_nil
  422. en_status = Fabricate(:status, language: 'en')
  423. es_status = Fabricate(:status, language: 'es')
  424. results = Status.as_public_timeline(@account)
  425. expect(results).to include(en_status)
  426. expect(results).to include(es_status)
  427. end
  428. end
  429. context 'where that account is silenced' do
  430. it 'includes statuses from other accounts that are silenced' do
  431. @account.update(silenced: true)
  432. other_silenced_account = Fabricate(:account, silenced: true)
  433. other_status = Fabricate(:status, account: other_silenced_account)
  434. results = Status.as_public_timeline(@account)
  435. expect(results).to include(other_status)
  436. end
  437. end
  438. end
  439. end
  440. describe '.as_tag_timeline' do
  441. it 'includes statuses with a tag' do
  442. tag = Fabricate(:tag)
  443. status = Fabricate(:status, tags: [tag])
  444. other = Fabricate(:status)
  445. results = Status.as_tag_timeline(tag)
  446. expect(results).to include(status)
  447. expect(results).not_to include(other)
  448. end
  449. it 'allows replies to be included' do
  450. original = Fabricate(:status)
  451. tag = Fabricate(:tag)
  452. status = Fabricate(:status, tags: [tag], in_reply_to_id: original.id)
  453. results = Status.as_tag_timeline(tag)
  454. expect(results).to include(status)
  455. end
  456. end
  457. describe '.permitted_for' do
  458. subject { described_class.permitted_for(target_account, account).pluck(:visibility) }
  459. let(:target_account) { alice }
  460. let(:account) { bob }
  461. let!(:public_status) { Fabricate(:status, account: target_account, visibility: 'public') }
  462. let!(:unlisted_status) { Fabricate(:status, account: target_account, visibility: 'unlisted') }
  463. let!(:private_status) { Fabricate(:status, account: target_account, visibility: 'private') }
  464. let!(:direct_status) do
  465. Fabricate(:status, account: target_account, visibility: 'direct').tap do |status|
  466. Fabricate(:mention, status: status, account: account)
  467. end
  468. end
  469. let!(:other_direct_status) do
  470. Fabricate(:status, account: target_account, visibility: 'direct').tap do |status|
  471. Fabricate(:mention, status: status)
  472. end
  473. end
  474. context 'given nil' do
  475. let(:account) { nil }
  476. let(:direct_status) { nil }
  477. it { is_expected.to eq(%w(unlisted public)) }
  478. end
  479. context 'given blocked account' do
  480. before do
  481. target_account.block!(account)
  482. end
  483. it { is_expected.to be_empty }
  484. end
  485. context 'given same account' do
  486. let(:account) { target_account }
  487. it { is_expected.to eq(%w(direct direct private unlisted public)) }
  488. end
  489. context 'given followed account' do
  490. before do
  491. account.follow!(target_account)
  492. end
  493. it { is_expected.to eq(%w(direct private unlisted public)) }
  494. end
  495. context 'given unfollowed account' do
  496. it { is_expected.to eq(%w(direct unlisted public)) }
  497. end
  498. end
  499. describe 'before_validation' do
  500. it 'sets account being replied to correctly over intermediary nodes' do
  501. first_status = Fabricate(:status, account: bob)
  502. intermediary = Fabricate(:status, thread: first_status, account: alice)
  503. final = Fabricate(:status, thread: intermediary, account: alice)
  504. expect(final.in_reply_to_account_id).to eq bob.id
  505. end
  506. it 'creates new conversation for stand-alone status' do
  507. expect(Status.create(account: alice, text: 'First').conversation_id).to_not be_nil
  508. end
  509. it 'keeps conversation of parent node' do
  510. parent = Fabricate(:status, text: 'First')
  511. expect(Status.create(account: alice, thread: parent, text: 'Response').conversation_id).to eq parent.conversation_id
  512. end
  513. it 'sets `local` to true for status by local account' do
  514. expect(Status.create(account: alice, text: 'foo').local).to be true
  515. end
  516. it 'sets `local` to false for status by remote account' do
  517. alice.update(domain: 'example.com')
  518. expect(Status.create(account: alice, text: 'foo').local).to be false
  519. end
  520. end
  521. describe 'validation' do
  522. it 'disallow empty uri for remote status' do
  523. alice.update(domain: 'example.com')
  524. status = Fabricate.build(:status, uri: '', account: alice)
  525. expect(status).to model_have_error_on_field(:uri)
  526. end
  527. end
  528. describe 'after_create' do
  529. it 'saves ActivityPub uri as uri for local status' do
  530. status = Status.create(account: alice, text: 'foo')
  531. status.reload
  532. expect(status.uri).to start_with('https://')
  533. end
  534. end
  535. end