闭社主体 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.

516 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. 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. let(:account) { Fabricate(:account) }
  147. let(:followed) { Fabricate(:account) }
  148. let(:not_followed) { Fabricate(:account) }
  149. before do
  150. Fabricate(:follow, account: account, target_account: followed)
  151. @self_status = Fabricate(:status, account: account, visibility: :public)
  152. @self_direct_status = Fabricate(:status, account: account, visibility: :direct)
  153. @followed_status = Fabricate(:status, account: followed, visibility: :public)
  154. @followed_direct_status = Fabricate(:status, account: followed, visibility: :direct)
  155. @not_followed_status = Fabricate(:status, account: not_followed, visibility: :public)
  156. @results = Status.as_home_timeline(account)
  157. end
  158. it 'includes statuses from self' do
  159. expect(@results).to include(@self_status)
  160. end
  161. it 'does not include direct statuses from self' do
  162. expect(@results).to_not include(@self_direct_status)
  163. end
  164. it 'includes statuses from followed' do
  165. expect(@results).to include(@followed_status)
  166. end
  167. it 'does not include direct statuses mentioning recipient from followed' do
  168. Fabricate(:mention, account: account, status: @followed_direct_status)
  169. expect(@results).to_not include(@followed_direct_status)
  170. end
  171. it 'does not include direct statuses not mentioning recipient from followed' do
  172. expect(@results).not_to include(@followed_direct_status)
  173. end
  174. it 'does not include statuses from non-followed' do
  175. expect(@results).not_to include(@not_followed_status)
  176. end
  177. end
  178. describe '.as_public_timeline' do
  179. it 'only includes statuses with public visibility' do
  180. public_status = Fabricate(:status, visibility: :public)
  181. private_status = Fabricate(:status, visibility: :private)
  182. results = Status.as_public_timeline
  183. expect(results).to include(public_status)
  184. expect(results).not_to include(private_status)
  185. end
  186. it 'does not include replies' do
  187. status = Fabricate(:status)
  188. reply = Fabricate(:status, in_reply_to_id: status.id)
  189. results = Status.as_public_timeline
  190. expect(results).to include(status)
  191. expect(results).not_to include(reply)
  192. end
  193. it 'does not include boosts' do
  194. status = Fabricate(:status)
  195. boost = Fabricate(:status, reblog_of_id: status.id)
  196. results = Status.as_public_timeline
  197. expect(results).to include(status)
  198. expect(results).not_to include(boost)
  199. end
  200. it 'filters out silenced accounts' do
  201. account = Fabricate(:account)
  202. silenced_account = Fabricate(:account, silenced: true)
  203. status = Fabricate(:status, account: account)
  204. silenced_status = Fabricate(:status, account: silenced_account)
  205. results = Status.as_public_timeline
  206. expect(results).to include(status)
  207. expect(results).not_to include(silenced_status)
  208. end
  209. context 'without local_only option' do
  210. let(:viewer) { nil }
  211. let!(:local_account) { Fabricate(:account, domain: nil) }
  212. let!(:remote_account) { Fabricate(:account, domain: 'test.com') }
  213. let!(:local_status) { Fabricate(:status, account: local_account) }
  214. let!(:remote_status) { Fabricate(:status, account: remote_account) }
  215. subject { Status.as_public_timeline(viewer, false) }
  216. context 'without a viewer' do
  217. let(:viewer) { nil }
  218. it 'includes remote instances statuses' do
  219. expect(subject).to include(remote_status)
  220. end
  221. it 'includes local statuses' do
  222. expect(subject).to include(local_status)
  223. end
  224. end
  225. context 'with a viewer' do
  226. let(:viewer) { Fabricate(:account, username: 'viewer') }
  227. it 'includes remote instances statuses' do
  228. expect(subject).to include(remote_status)
  229. end
  230. it 'includes local statuses' do
  231. expect(subject).to include(local_status)
  232. end
  233. end
  234. end
  235. context 'with a local_only option set' do
  236. let!(:local_account) { Fabricate(:account, domain: nil) }
  237. let!(:remote_account) { Fabricate(:account, domain: 'test.com') }
  238. let!(:local_status) { Fabricate(:status, account: local_account) }
  239. let!(:remote_status) { Fabricate(:status, account: remote_account) }
  240. subject { Status.as_public_timeline(viewer, true) }
  241. context 'without a viewer' do
  242. let(:viewer) { nil }
  243. it 'does not include remote instances statuses' do
  244. expect(subject).to include(local_status)
  245. expect(subject).not_to include(remote_status)
  246. end
  247. end
  248. context 'with a viewer' do
  249. let(:viewer) { Fabricate(:account, username: 'viewer') }
  250. it 'does not include remote instances statuses' do
  251. expect(subject).to include(local_status)
  252. expect(subject).not_to include(remote_status)
  253. end
  254. it 'is not affected by personal domain blocks' do
  255. viewer.block_domain!('test.com')
  256. expect(subject).to include(local_status)
  257. expect(subject).not_to include(remote_status)
  258. end
  259. end
  260. end
  261. describe 'with an account passed in' do
  262. before do
  263. @account = Fabricate(:account)
  264. end
  265. it 'excludes statuses from accounts blocked by the account' do
  266. blocked = Fabricate(:account)
  267. Fabricate(:block, account: @account, target_account: blocked)
  268. blocked_status = Fabricate(:status, account: blocked)
  269. results = Status.as_public_timeline(@account)
  270. expect(results).not_to include(blocked_status)
  271. end
  272. it 'excludes statuses from accounts who have blocked the account' do
  273. blocked = Fabricate(:account)
  274. Fabricate(:block, account: blocked, target_account: @account)
  275. blocked_status = Fabricate(:status, account: blocked)
  276. results = Status.as_public_timeline(@account)
  277. expect(results).not_to include(blocked_status)
  278. end
  279. it 'excludes statuses from accounts muted by the account' do
  280. muted = Fabricate(:account)
  281. Fabricate(:mute, account: @account, target_account: muted)
  282. muted_status = Fabricate(:status, account: muted)
  283. results = Status.as_public_timeline(@account)
  284. expect(results).not_to include(muted_status)
  285. end
  286. it 'excludes statuses from accounts from personally blocked domains' do
  287. blocked = Fabricate(:account, domain: 'example.com')
  288. @account.block_domain!(blocked.domain)
  289. blocked_status = Fabricate(:status, account: blocked)
  290. results = Status.as_public_timeline(@account)
  291. expect(results).not_to include(blocked_status)
  292. end
  293. context 'with language preferences' do
  294. it 'excludes statuses in languages not allowed by the account user' do
  295. user = Fabricate(:user, filtered_languages: [:fr])
  296. @account.update(user: user)
  297. en_status = Fabricate(:status, language: 'en')
  298. es_status = Fabricate(:status, language: 'es')
  299. fr_status = Fabricate(:status, language: 'fr')
  300. results = Status.as_public_timeline(@account)
  301. expect(results).to include(en_status)
  302. expect(results).to include(es_status)
  303. expect(results).not_to include(fr_status)
  304. end
  305. it 'includes all languages when user does not have a setting' do
  306. user = Fabricate(:user, filtered_languages: [])
  307. @account.update(user: user)
  308. en_status = Fabricate(:status, language: 'en')
  309. es_status = Fabricate(:status, language: 'es')
  310. results = Status.as_public_timeline(@account)
  311. expect(results).to include(en_status)
  312. expect(results).to include(es_status)
  313. end
  314. it 'includes all languages when account does not have a user' do
  315. expect(@account.user).to be_nil
  316. en_status = Fabricate(:status, language: 'en')
  317. es_status = Fabricate(:status, language: 'es')
  318. results = Status.as_public_timeline(@account)
  319. expect(results).to include(en_status)
  320. expect(results).to include(es_status)
  321. end
  322. end
  323. context 'where that account is silenced' do
  324. it 'includes statuses from other accounts that are silenced' do
  325. @account.update(silenced: true)
  326. other_silenced_account = Fabricate(:account, silenced: true)
  327. other_status = Fabricate(:status, account: other_silenced_account)
  328. results = Status.as_public_timeline(@account)
  329. expect(results).to include(other_status)
  330. end
  331. end
  332. end
  333. end
  334. describe '.as_tag_timeline' do
  335. it 'includes statuses with a tag' do
  336. tag = Fabricate(:tag)
  337. status = Fabricate(:status, tags: [tag])
  338. other = Fabricate(:status)
  339. results = Status.as_tag_timeline(tag)
  340. expect(results).to include(status)
  341. expect(results).not_to include(other)
  342. end
  343. it 'allows replies to be included' do
  344. original = Fabricate(:status)
  345. tag = Fabricate(:tag)
  346. status = Fabricate(:status, tags: [tag], in_reply_to_id: original.id)
  347. results = Status.as_tag_timeline(tag)
  348. expect(results).to include(status)
  349. end
  350. end
  351. describe '.permitted_for' do
  352. subject { described_class.permitted_for(target_account, account).pluck(:visibility) }
  353. let(:target_account) { alice }
  354. let(:account) { bob }
  355. let!(:public_status) { Fabricate(:status, account: target_account, visibility: 'public') }
  356. let!(:unlisted_status) { Fabricate(:status, account: target_account, visibility: 'unlisted') }
  357. let!(:private_status) { Fabricate(:status, account: target_account, visibility: 'private') }
  358. let!(:direct_status) do
  359. Fabricate(:status, account: target_account, visibility: 'direct').tap do |status|
  360. Fabricate(:mention, status: status, account: account)
  361. end
  362. end
  363. let!(:other_direct_status) do
  364. Fabricate(:status, account: target_account, visibility: 'direct').tap do |status|
  365. Fabricate(:mention, status: status)
  366. end
  367. end
  368. context 'given nil' do
  369. let(:account) { nil }
  370. let(:direct_status) { nil }
  371. it { is_expected.to eq(%w(unlisted public)) }
  372. end
  373. context 'given blocked account' do
  374. before do
  375. target_account.block!(account)
  376. end
  377. it { is_expected.to be_empty }
  378. end
  379. context 'given same account' do
  380. let(:account) { target_account }
  381. it { is_expected.to eq(%w(direct direct private unlisted public)) }
  382. end
  383. context 'given followed account' do
  384. before do
  385. account.follow!(target_account)
  386. end
  387. it { is_expected.to eq(%w(direct private unlisted public)) }
  388. end
  389. context 'given unfollowed account' do
  390. it { is_expected.to eq(%w(direct unlisted public)) }
  391. end
  392. end
  393. describe 'before_create' do
  394. it 'sets account being replied to correctly over intermediary nodes' do
  395. first_status = Fabricate(:status, account: bob)
  396. intermediary = Fabricate(:status, thread: first_status, account: alice)
  397. final = Fabricate(:status, thread: intermediary, account: alice)
  398. expect(final.in_reply_to_account_id).to eq bob.id
  399. end
  400. it 'creates new conversation for stand-alone status' do
  401. expect(Status.create(account: alice, text: 'First').conversation_id).to_not be_nil
  402. end
  403. it 'keeps conversation of parent node' do
  404. parent = Fabricate(:status, text: 'First')
  405. expect(Status.create(account: alice, thread: parent, text: 'Response').conversation_id).to eq parent.conversation_id
  406. end
  407. end
  408. end