status_spec.rb 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. it 'is decremented when reblog is removed' do
  149. reblog = Fabricate(:status, account: bob, reblog: subject)
  150. expect(subject.reblogs_count).to eq 1
  151. reblog.destroy
  152. expect(subject.reblogs_count).to eq 0
  153. end
  154. it 'does not fail when original is deleted before reblog' do
  155. reblog = Fabricate(:status, account: bob, reblog: subject)
  156. expect(subject.reblogs_count).to eq 1
  157. expect { subject.destroy }.to_not raise_error
  158. expect(Status.find_by(id: reblog.id)).to be_nil
  159. end
  160. end
  161. describe '#replies_count' do
  162. it 'is the number of replies' do
  163. reply = Fabricate(:status, account: bob, thread: subject)
  164. expect(subject.replies_count).to eq 1
  165. end
  166. it 'is decremented when reply is removed' do
  167. reply = Fabricate(:status, account: bob, thread: subject)
  168. expect(subject.replies_count).to eq 1
  169. reply.destroy
  170. expect(subject.replies_count).to eq 0
  171. end
  172. end
  173. describe '#favourites_count' do
  174. it 'is the number of favorites' do
  175. Fabricate(:favourite, account: bob, status: subject)
  176. Fabricate(:favourite, account: alice, status: subject)
  177. expect(subject.favourites_count).to eq 2
  178. end
  179. it 'is decremented when favourite is removed' do
  180. favourite = Fabricate(:favourite, account: bob, status: subject)
  181. expect(subject.favourites_count).to eq 1
  182. favourite.destroy
  183. expect(subject.favourites_count).to eq 0
  184. end
  185. end
  186. describe '#proper' do
  187. it 'is itself for original statuses' do
  188. expect(subject.proper).to eq subject
  189. end
  190. it 'is the source status for reblogs' do
  191. subject.reblog = other
  192. expect(subject.proper).to eq other
  193. end
  194. end
  195. describe '.mutes_map' do
  196. let(:status) { Fabricate(:status) }
  197. let(:account) { Fabricate(:account) }
  198. subject { Status.mutes_map([status.conversation.id], account) }
  199. it 'returns a hash' do
  200. expect(subject).to be_a Hash
  201. end
  202. it 'contains true value' do
  203. account.mute_conversation!(status.conversation)
  204. expect(subject[status.conversation.id]).to be true
  205. end
  206. end
  207. describe '.favourites_map' do
  208. let(:status) { Fabricate(:status) }
  209. let(:account) { Fabricate(:account) }
  210. subject { Status.favourites_map([status], account) }
  211. it 'returns a hash' do
  212. expect(subject).to be_a Hash
  213. end
  214. it 'contains true value' do
  215. Fabricate(:favourite, status: status, account: account)
  216. expect(subject[status.id]).to be true
  217. end
  218. end
  219. describe '.reblogs_map' do
  220. let(:status) { Fabricate(:status) }
  221. let(:account) { Fabricate(:account) }
  222. subject { Status.reblogs_map([status], account) }
  223. it 'returns a hash' do
  224. expect(subject).to be_a Hash
  225. end
  226. it 'contains true value' do
  227. Fabricate(:status, account: account, reblog: status)
  228. expect(subject[status.id]).to be true
  229. end
  230. end
  231. describe '.in_chosen_languages' do
  232. context 'for accounts with language filters' do
  233. let(:user) { Fabricate(:user, chosen_languages: ['en']) }
  234. it 'does not include statuses in not in chosen languages' do
  235. status = Fabricate(:status, language: 'de')
  236. expect(Status.in_chosen_languages(user.account)).not_to include status
  237. end
  238. it 'includes status with unknown language' do
  239. status = Fabricate(:status, language: nil)
  240. expect(Status.in_chosen_languages(user.account)).to include status
  241. end
  242. end
  243. end
  244. describe '.as_home_timeline' do
  245. let(:account) { Fabricate(:account) }
  246. let(:followed) { Fabricate(:account) }
  247. let(:not_followed) { Fabricate(:account) }
  248. before do
  249. Fabricate(:follow, account: account, target_account: followed)
  250. @self_status = Fabricate(:status, account: account, visibility: :public)
  251. @self_direct_status = Fabricate(:status, account: account, visibility: :direct)
  252. @followed_status = Fabricate(:status, account: followed, visibility: :public)
  253. @followed_direct_status = Fabricate(:status, account: followed, visibility: :direct)
  254. @not_followed_status = Fabricate(:status, account: not_followed, visibility: :public)
  255. @results = Status.as_home_timeline(account)
  256. end
  257. it 'includes statuses from self' do
  258. expect(@results).to include(@self_status)
  259. end
  260. it 'does not include direct statuses from self' do
  261. expect(@results).to_not include(@self_direct_status)
  262. end
  263. it 'includes statuses from followed' do
  264. expect(@results).to include(@followed_status)
  265. end
  266. it 'does not include direct statuses mentioning recipient from followed' do
  267. Fabricate(:mention, account: account, status: @followed_direct_status)
  268. expect(@results).to_not include(@followed_direct_status)
  269. end
  270. it 'does not include direct statuses not mentioning recipient from followed' do
  271. expect(@results).not_to include(@followed_direct_status)
  272. end
  273. it 'does not include statuses from non-followed' do
  274. expect(@results).not_to include(@not_followed_status)
  275. end
  276. end
  277. describe '.as_direct_timeline' do
  278. let(:account) { Fabricate(:account) }
  279. let(:followed) { Fabricate(:account) }
  280. let(:not_followed) { Fabricate(:account) }
  281. before do
  282. Fabricate(:follow, account: account, target_account: followed)
  283. @self_public_status = Fabricate(:status, account: account, visibility: :public)
  284. @self_direct_status = Fabricate(:status, account: account, visibility: :direct)
  285. @followed_public_status = Fabricate(:status, account: followed, visibility: :public)
  286. @followed_direct_status = Fabricate(:status, account: followed, visibility: :direct)
  287. @not_followed_direct_status = Fabricate(:status, account: not_followed, visibility: :direct)
  288. @results = Status.as_direct_timeline(account)
  289. end
  290. it 'does not include public statuses from self' do
  291. expect(@results).to_not include(@self_public_status)
  292. end
  293. it 'includes direct statuses from self' do
  294. expect(@results).to include(@self_direct_status)
  295. end
  296. it 'does not include public statuses from followed' do
  297. expect(@results).to_not include(@followed_public_status)
  298. end
  299. it 'does not include direct statuses not mentioning recipient from followed' do
  300. expect(@results).to_not include(@followed_direct_status)
  301. end
  302. it 'does not include direct statuses not mentioning recipient from non-followed' do
  303. expect(@results).to_not include(@not_followed_direct_status)
  304. end
  305. it 'includes direct statuses mentioning recipient from followed' do
  306. Fabricate(:mention, account: account, status: @followed_direct_status)
  307. results2 = Status.as_direct_timeline(account)
  308. expect(results2).to include(@followed_direct_status)
  309. end
  310. it 'includes direct statuses mentioning recipient from non-followed' do
  311. Fabricate(:mention, account: account, status: @not_followed_direct_status)
  312. results2 = Status.as_direct_timeline(account)
  313. expect(results2).to include(@not_followed_direct_status)
  314. end
  315. end
  316. describe '.as_public_timeline' do
  317. it 'only includes statuses with public visibility' do
  318. public_status = Fabricate(:status, visibility: :public)
  319. private_status = Fabricate(:status, visibility: :private)
  320. results = Status.as_public_timeline
  321. expect(results).to include(public_status)
  322. expect(results).not_to include(private_status)
  323. end
  324. it 'does not include replies' do
  325. status = Fabricate(:status)
  326. reply = Fabricate(:status, in_reply_to_id: status.id)
  327. results = Status.as_public_timeline
  328. expect(results).to include(status)
  329. expect(results).not_to include(reply)
  330. end
  331. it 'does not include boosts' do
  332. status = Fabricate(:status)
  333. boost = Fabricate(:status, reblog_of_id: status.id)
  334. results = Status.as_public_timeline
  335. expect(results).to include(status)
  336. expect(results).not_to include(boost)
  337. end
  338. it 'filters out silenced accounts' do
  339. account = Fabricate(:account)
  340. silenced_account = Fabricate(:account, silenced: true)
  341. status = Fabricate(:status, account: account)
  342. silenced_status = Fabricate(:status, account: silenced_account)
  343. results = Status.as_public_timeline
  344. expect(results).to include(status)
  345. expect(results).not_to include(silenced_status)
  346. end
  347. context 'without local_only option' do
  348. let(:viewer) { nil }
  349. let!(:local_account) { Fabricate(:account, domain: nil) }
  350. let!(:remote_account) { Fabricate(:account, domain: 'test.com') }
  351. let!(:local_status) { Fabricate(:status, account: local_account) }
  352. let!(:remote_status) { Fabricate(:status, account: remote_account) }
  353. subject { Status.as_public_timeline(viewer, false) }
  354. context 'without a viewer' do
  355. let(:viewer) { nil }
  356. it 'includes remote instances statuses' do
  357. expect(subject).to include(remote_status)
  358. end
  359. it 'includes local statuses' do
  360. expect(subject).to include(local_status)
  361. end
  362. end
  363. context 'with a viewer' do
  364. let(:viewer) { Fabricate(:account, username: 'viewer') }
  365. it 'includes remote instances statuses' do
  366. expect(subject).to include(remote_status)
  367. end
  368. it 'includes local statuses' do
  369. expect(subject).to include(local_status)
  370. end
  371. end
  372. end
  373. context 'with a local_only option set' do
  374. let!(:local_account) { Fabricate(:account, domain: nil) }
  375. let!(:remote_account) { Fabricate(:account, domain: 'test.com') }
  376. let!(:local_status) { Fabricate(:status, account: local_account) }
  377. let!(:remote_status) { Fabricate(:status, account: remote_account) }
  378. subject { Status.as_public_timeline(viewer, true) }
  379. context 'without a viewer' do
  380. let(:viewer) { nil }
  381. it 'does not include remote instances statuses' do
  382. expect(subject).to include(local_status)
  383. expect(subject).not_to include(remote_status)
  384. end
  385. end
  386. context 'with a viewer' do
  387. let(:viewer) { Fabricate(:account, username: 'viewer') }
  388. it 'does not include remote instances statuses' do
  389. expect(subject).to include(local_status)
  390. expect(subject).not_to include(remote_status)
  391. end
  392. it 'is not affected by personal domain blocks' do
  393. viewer.block_domain!('test.com')
  394. expect(subject).to include(local_status)
  395. expect(subject).not_to include(remote_status)
  396. end
  397. end
  398. end
  399. describe 'with an account passed in' do
  400. before do
  401. @account = Fabricate(:account)
  402. end
  403. it 'excludes statuses from accounts blocked by the account' do
  404. blocked = Fabricate(:account)
  405. Fabricate(:block, account: @account, target_account: blocked)
  406. blocked_status = Fabricate(:status, account: blocked)
  407. results = Status.as_public_timeline(@account)
  408. expect(results).not_to include(blocked_status)
  409. end
  410. it 'excludes statuses from accounts who have blocked the account' do
  411. blocked = Fabricate(:account)
  412. Fabricate(:block, account: blocked, target_account: @account)
  413. blocked_status = Fabricate(:status, account: blocked)
  414. results = Status.as_public_timeline(@account)
  415. expect(results).not_to include(blocked_status)
  416. end
  417. it 'excludes statuses from accounts muted by the account' do
  418. muted = Fabricate(:account)
  419. Fabricate(:mute, account: @account, target_account: muted)
  420. muted_status = Fabricate(:status, account: muted)
  421. results = Status.as_public_timeline(@account)
  422. expect(results).not_to include(muted_status)
  423. end
  424. it 'excludes statuses from accounts from personally blocked domains' do
  425. blocked = Fabricate(:account, domain: 'example.com')
  426. @account.block_domain!(blocked.domain)
  427. blocked_status = Fabricate(:status, account: blocked)
  428. results = Status.as_public_timeline(@account)
  429. expect(results).not_to include(blocked_status)
  430. end
  431. context 'with language preferences' do
  432. it 'excludes statuses in languages not allowed by the account user' do
  433. user = Fabricate(:user, chosen_languages: [:en, :es])
  434. @account.update(user: user)
  435. en_status = Fabricate(:status, language: 'en')
  436. es_status = Fabricate(:status, language: 'es')
  437. fr_status = Fabricate(:status, language: 'fr')
  438. results = Status.as_public_timeline(@account)
  439. expect(results).to include(en_status)
  440. expect(results).to include(es_status)
  441. expect(results).not_to include(fr_status)
  442. end
  443. it 'includes all languages when user does not have a setting' do
  444. user = Fabricate(:user, chosen_languages: nil)
  445. @account.update(user: user)
  446. en_status = Fabricate(:status, language: 'en')
  447. es_status = Fabricate(:status, language: 'es')
  448. results = Status.as_public_timeline(@account)
  449. expect(results).to include(en_status)
  450. expect(results).to include(es_status)
  451. end
  452. it 'includes all languages when account does not have a user' do
  453. expect(@account.user).to be_nil
  454. en_status = Fabricate(:status, language: 'en')
  455. es_status = Fabricate(:status, language: 'es')
  456. results = Status.as_public_timeline(@account)
  457. expect(results).to include(en_status)
  458. expect(results).to include(es_status)
  459. end
  460. end
  461. end
  462. end
  463. describe '.as_tag_timeline' do
  464. it 'includes statuses with a tag' do
  465. tag = Fabricate(:tag)
  466. status = Fabricate(:status, tags: [tag])
  467. other = Fabricate(:status)
  468. results = Status.as_tag_timeline(tag)
  469. expect(results).to include(status)
  470. expect(results).not_to include(other)
  471. end
  472. it 'allows replies to be included' do
  473. original = Fabricate(:status)
  474. tag = Fabricate(:tag)
  475. status = Fabricate(:status, tags: [tag], in_reply_to_id: original.id)
  476. results = Status.as_tag_timeline(tag)
  477. expect(results).to include(status)
  478. end
  479. end
  480. describe '.permitted_for' do
  481. subject { described_class.permitted_for(target_account, account).pluck(:visibility) }
  482. let(:target_account) { alice }
  483. let(:account) { bob }
  484. let!(:public_status) { Fabricate(:status, account: target_account, visibility: 'public') }
  485. let!(:unlisted_status) { Fabricate(:status, account: target_account, visibility: 'unlisted') }
  486. let!(:private_status) { Fabricate(:status, account: target_account, visibility: 'private') }
  487. let!(:direct_status) do
  488. Fabricate(:status, account: target_account, visibility: 'direct').tap do |status|
  489. Fabricate(:mention, status: status, account: account)
  490. end
  491. end
  492. let!(:other_direct_status) do
  493. Fabricate(:status, account: target_account, visibility: 'direct').tap do |status|
  494. Fabricate(:mention, status: status)
  495. end
  496. end
  497. context 'given nil' do
  498. let(:account) { nil }
  499. let(:direct_status) { nil }
  500. it { is_expected.to eq(%w(unlisted public)) }
  501. end
  502. context 'given blocked account' do
  503. before do
  504. target_account.block!(account)
  505. end
  506. it { is_expected.to be_empty }
  507. end
  508. context 'given same account' do
  509. let(:account) { target_account }
  510. it { is_expected.to eq(%w(direct direct private unlisted public)) }
  511. end
  512. context 'given followed account' do
  513. before do
  514. account.follow!(target_account)
  515. end
  516. it { is_expected.to eq(%w(direct private unlisted public)) }
  517. end
  518. context 'given unfollowed account' do
  519. it { is_expected.to eq(%w(direct unlisted public)) }
  520. end
  521. end
  522. describe 'before_validation' do
  523. it 'sets account being replied to correctly over intermediary nodes' do
  524. first_status = Fabricate(:status, account: bob)
  525. intermediary = Fabricate(:status, thread: first_status, account: alice)
  526. final = Fabricate(:status, thread: intermediary, account: alice)
  527. expect(final.in_reply_to_account_id).to eq bob.id
  528. end
  529. it 'creates new conversation for stand-alone status' do
  530. expect(Status.create(account: alice, text: 'First').conversation_id).to_not be_nil
  531. end
  532. it 'keeps conversation of parent node' do
  533. parent = Fabricate(:status, text: 'First')
  534. expect(Status.create(account: alice, thread: parent, text: 'Response').conversation_id).to eq parent.conversation_id
  535. end
  536. it 'sets `local` to true for status by local account' do
  537. expect(Status.create(account: alice, text: 'foo').local).to be true
  538. end
  539. it 'sets `local` to false for status by remote account' do
  540. alice.update(domain: 'example.com')
  541. expect(Status.create(account: alice, text: 'foo').local).to be false
  542. end
  543. end
  544. describe 'validation' do
  545. it 'disallow empty uri for remote status' do
  546. alice.update(domain: 'example.com')
  547. status = Fabricate.build(:status, uri: '', account: alice)
  548. expect(status).to model_have_error_on_field(:uri)
  549. end
  550. end
  551. describe 'after_create' do
  552. it 'saves ActivityPub uri as uri for local status' do
  553. status = Status.create(account: alice, text: 'foo')
  554. status.reload
  555. expect(status.uri).to start_with('https://')
  556. end
  557. end
  558. end