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.

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