1
0

account_interactions_spec.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. require 'rails_helper'
  2. describe AccountInteractions do
  3. let(:account) { Fabricate(:account, username: 'account') }
  4. let(:account_id) { account.id }
  5. let(:account_ids) { [account_id] }
  6. let(:target_account) { Fabricate(:account, username: 'target') }
  7. let(:target_account_id) { target_account.id }
  8. let(:target_account_ids) { [target_account_id] }
  9. describe '.following_map' do
  10. subject { Account.following_map(target_account_ids, account_id) }
  11. context 'account with Follow' do
  12. it 'returns { target_account_id => true }' do
  13. Fabricate(:follow, account: account, target_account: target_account)
  14. is_expected.to eq(target_account_id => { reblogs: true, notify: false })
  15. end
  16. end
  17. context 'account without Follow' do
  18. it 'returns {}' do
  19. is_expected.to eq({})
  20. end
  21. end
  22. end
  23. describe '.followed_by_map' do
  24. subject { Account.followed_by_map(target_account_ids, account_id) }
  25. context 'account with Follow' do
  26. it 'returns { target_account_id => true }' do
  27. Fabricate(:follow, account: target_account, target_account: account)
  28. is_expected.to eq(target_account_id => true)
  29. end
  30. end
  31. context 'account without Follow' do
  32. it 'returns {}' do
  33. is_expected.to eq({})
  34. end
  35. end
  36. end
  37. describe '.blocking_map' do
  38. subject { Account.blocking_map(target_account_ids, account_id) }
  39. context 'account with Block' do
  40. it 'returns { target_account_id => true }' do
  41. Fabricate(:block, account: account, target_account: target_account)
  42. is_expected.to eq(target_account_id => true)
  43. end
  44. end
  45. context 'account without Block' do
  46. it 'returns {}' do
  47. is_expected.to eq({})
  48. end
  49. end
  50. end
  51. describe '.muting_map' do
  52. subject { Account.muting_map(target_account_ids, account_id) }
  53. context 'account with Mute' do
  54. before do
  55. Fabricate(:mute, target_account: target_account, account: account, hide_notifications: hide)
  56. end
  57. context 'if Mute#hide_notifications?' do
  58. let(:hide) { true }
  59. it 'returns { target_account_id => { notifications: true } }' do
  60. is_expected.to eq(target_account_id => { notifications: true })
  61. end
  62. end
  63. context 'unless Mute#hide_notifications?' do
  64. let(:hide) { false }
  65. it 'returns { target_account_id => { notifications: false } }' do
  66. is_expected.to eq(target_account_id => { notifications: false })
  67. end
  68. end
  69. end
  70. context 'account without Mute' do
  71. it 'returns {}' do
  72. is_expected.to eq({})
  73. end
  74. end
  75. end
  76. describe '#follow!' do
  77. it 'creates and returns Follow' do
  78. expect do
  79. expect(account.follow!(target_account)).to be_kind_of Follow
  80. end.to change { account.following.count }.by 1
  81. end
  82. end
  83. describe '#block' do
  84. it 'creates and returns Block' do
  85. expect do
  86. expect(account.block!(target_account)).to be_kind_of Block
  87. end.to change { account.block_relationships.count }.by 1
  88. end
  89. end
  90. describe '#mute!' do
  91. subject { account.mute!(target_account, notifications: arg_notifications) }
  92. context 'Mute does not exist yet' do
  93. context 'arg :notifications is nil' do
  94. let(:arg_notifications) { nil }
  95. it 'creates Mute, and returns Mute' do
  96. expect do
  97. expect(subject).to be_kind_of Mute
  98. end.to change { account.mute_relationships.count }.by 1
  99. end
  100. end
  101. context 'arg :notifications is false' do
  102. let(:arg_notifications) { false }
  103. it 'creates Mute, and returns Mute' do
  104. expect do
  105. expect(subject).to be_kind_of Mute
  106. end.to change { account.mute_relationships.count }.by 1
  107. end
  108. end
  109. context 'arg :notifications is true' do
  110. let(:arg_notifications) { true }
  111. it 'creates Mute, and returns Mute' do
  112. expect do
  113. expect(subject).to be_kind_of Mute
  114. end.to change { account.mute_relationships.count }.by 1
  115. end
  116. end
  117. end
  118. context 'Mute already exists' do
  119. before do
  120. account.mute_relationships << mute
  121. end
  122. let(:mute) do
  123. Fabricate(:mute,
  124. account: account,
  125. target_account: target_account,
  126. hide_notifications: hide_notifications)
  127. end
  128. context 'mute.hide_notifications is true' do
  129. let(:hide_notifications) { true }
  130. context 'arg :notifications is nil' do
  131. let(:arg_notifications) { nil }
  132. it 'returns Mute without updating mute.hide_notifications' do
  133. expect do
  134. expect(subject).to be_kind_of Mute
  135. end.not_to change { mute.reload.hide_notifications? }.from(true)
  136. end
  137. end
  138. context 'arg :notifications is false' do
  139. let(:arg_notifications) { false }
  140. it 'returns Mute, and updates mute.hide_notifications false' do
  141. expect do
  142. expect(subject).to be_kind_of Mute
  143. end.to change { mute.reload.hide_notifications? }.from(true).to(false)
  144. end
  145. end
  146. context 'arg :notifications is true' do
  147. let(:arg_notifications) { true }
  148. it 'returns Mute without updating mute.hide_notifications' do
  149. expect do
  150. expect(subject).to be_kind_of Mute
  151. end.not_to change { mute.reload.hide_notifications? }.from(true)
  152. end
  153. end
  154. end
  155. context 'mute.hide_notifications is false' do
  156. let(:hide_notifications) { false }
  157. context 'arg :notifications is nil' do
  158. let(:arg_notifications) { nil }
  159. it 'returns Mute, and updates mute.hide_notifications true' do
  160. expect do
  161. expect(subject).to be_kind_of Mute
  162. end.to change { mute.reload.hide_notifications? }.from(false).to(true)
  163. end
  164. end
  165. context 'arg :notifications is false' do
  166. let(:arg_notifications) { false }
  167. it 'returns Mute without updating mute.hide_notifications' do
  168. expect do
  169. expect(subject).to be_kind_of Mute
  170. end.not_to change { mute.reload.hide_notifications? }.from(false)
  171. end
  172. end
  173. context 'arg :notifications is true' do
  174. let(:arg_notifications) { true }
  175. it 'returns Mute, and updates mute.hide_notifications true' do
  176. expect do
  177. expect(subject).to be_kind_of Mute
  178. end.to change { mute.reload.hide_notifications? }.from(false).to(true)
  179. end
  180. end
  181. end
  182. end
  183. end
  184. describe '#mute_conversation!' do
  185. let(:conversation) { Fabricate(:conversation) }
  186. subject { account.mute_conversation!(conversation) }
  187. it 'creates and returns ConversationMute' do
  188. expect do
  189. is_expected.to be_kind_of ConversationMute
  190. end.to change { account.conversation_mutes.count }.by 1
  191. end
  192. end
  193. describe '#block_domain!' do
  194. let(:domain) { 'example.com' }
  195. subject { account.block_domain!(domain) }
  196. it 'creates and returns AccountDomainBlock' do
  197. expect do
  198. is_expected.to be_kind_of AccountDomainBlock
  199. end.to change { account.domain_blocks.count }.by 1
  200. end
  201. end
  202. describe '#unfollow!' do
  203. subject { account.unfollow!(target_account) }
  204. context 'following target_account' do
  205. it 'returns destroyed Follow' do
  206. account.active_relationships.create(target_account: target_account)
  207. is_expected.to be_kind_of Follow
  208. expect(subject).to be_destroyed
  209. end
  210. end
  211. context 'not following target_account' do
  212. it 'returns nil' do
  213. is_expected.to be_nil
  214. end
  215. end
  216. end
  217. describe '#unblock!' do
  218. subject { account.unblock!(target_account) }
  219. context 'blocking target_account' do
  220. it 'returns destroyed Block' do
  221. account.block_relationships.create(target_account: target_account)
  222. is_expected.to be_kind_of Block
  223. expect(subject).to be_destroyed
  224. end
  225. end
  226. context 'not blocking target_account' do
  227. it 'returns nil' do
  228. is_expected.to be_nil
  229. end
  230. end
  231. end
  232. describe '#unmute!' do
  233. subject { account.unmute!(target_account) }
  234. context 'muting target_account' do
  235. it 'returns destroyed Mute' do
  236. account.mute_relationships.create(target_account: target_account)
  237. is_expected.to be_kind_of Mute
  238. expect(subject).to be_destroyed
  239. end
  240. end
  241. context 'not muting target_account' do
  242. it 'returns nil' do
  243. is_expected.to be_nil
  244. end
  245. end
  246. end
  247. describe '#unmute_conversation!' do
  248. let(:conversation) { Fabricate(:conversation) }
  249. subject { account.unmute_conversation!(conversation) }
  250. context 'muting the conversation' do
  251. it 'returns destroyed ConversationMute' do
  252. account.conversation_mutes.create(conversation: conversation)
  253. is_expected.to be_kind_of ConversationMute
  254. expect(subject).to be_destroyed
  255. end
  256. end
  257. context 'not muting the conversation' do
  258. it 'returns nil' do
  259. is_expected.to be nil
  260. end
  261. end
  262. end
  263. describe '#unblock_domain!' do
  264. let(:domain) { 'example.com' }
  265. subject { account.unblock_domain!(domain) }
  266. context 'blocking the domain' do
  267. it 'returns destroyed AccountDomainBlock' do
  268. account_domain_block = Fabricate(:account_domain_block, domain: domain)
  269. account.domain_blocks << account_domain_block
  270. is_expected.to be_kind_of AccountDomainBlock
  271. expect(subject).to be_destroyed
  272. end
  273. end
  274. context 'unblocking the domain' do
  275. it 'returns nil' do
  276. is_expected.to be_nil
  277. end
  278. end
  279. end
  280. describe '#following?' do
  281. subject { account.following?(target_account) }
  282. context 'following target_account' do
  283. it 'returns true' do
  284. account.active_relationships.create(target_account: target_account)
  285. is_expected.to be true
  286. end
  287. end
  288. context 'not following target_account' do
  289. it 'returns false' do
  290. is_expected.to be false
  291. end
  292. end
  293. end
  294. describe '#followed_by?' do
  295. subject { account.followed_by?(target_account) }
  296. context 'followed by target_account' do
  297. it 'returns true' do
  298. account.passive_relationships.create(account: target_account)
  299. is_expected.to be true
  300. end
  301. end
  302. context 'not followed by target_account' do
  303. it 'returns false' do
  304. is_expected.to be false
  305. end
  306. end
  307. end
  308. describe '#blocking?' do
  309. subject { account.blocking?(target_account) }
  310. context 'blocking target_account' do
  311. it 'returns true' do
  312. account.block_relationships.create(target_account: target_account)
  313. is_expected.to be true
  314. end
  315. end
  316. context 'not blocking target_account' do
  317. it 'returns false' do
  318. is_expected.to be false
  319. end
  320. end
  321. end
  322. describe '#domain_blocking?' do
  323. let(:domain) { 'example.com' }
  324. subject { account.domain_blocking?(domain) }
  325. context 'blocking the domain' do
  326. it' returns true' do
  327. account_domain_block = Fabricate(:account_domain_block, domain: domain)
  328. account.domain_blocks << account_domain_block
  329. is_expected.to be true
  330. end
  331. end
  332. context 'not blocking the domain' do
  333. it 'returns false' do
  334. is_expected.to be false
  335. end
  336. end
  337. end
  338. describe '#muting?' do
  339. subject { account.muting?(target_account) }
  340. context 'muting target_account' do
  341. it 'returns true' do
  342. mute = Fabricate(:mute, account: account, target_account: target_account)
  343. account.mute_relationships << mute
  344. is_expected.to be true
  345. end
  346. end
  347. context 'not muting target_account' do
  348. it 'returns false' do
  349. is_expected.to be false
  350. end
  351. end
  352. end
  353. describe '#muting_conversation?' do
  354. let(:conversation) { Fabricate(:conversation) }
  355. subject { account.muting_conversation?(conversation) }
  356. context 'muting the conversation' do
  357. it 'returns true' do
  358. account.conversation_mutes.create(conversation: conversation)
  359. is_expected.to be true
  360. end
  361. end
  362. context 'not muting the conversation' do
  363. it 'returns false' do
  364. is_expected.to be false
  365. end
  366. end
  367. end
  368. describe '#muting_notifications?' do
  369. before do
  370. mute = Fabricate(:mute, target_account: target_account, account: account, hide_notifications: hide)
  371. account.mute_relationships << mute
  372. end
  373. subject { account.muting_notifications?(target_account) }
  374. context 'muting notifications of target_account' do
  375. let(:hide) { true }
  376. it 'returns true' do
  377. is_expected.to be true
  378. end
  379. end
  380. context 'not muting notifications of target_account' do
  381. let(:hide) { false }
  382. it 'returns false' do
  383. is_expected.to be false
  384. end
  385. end
  386. end
  387. describe '#requested?' do
  388. subject { account.requested?(target_account) }
  389. context 'requested by target_account' do
  390. it 'returns true' do
  391. Fabricate(:follow_request, account: account, target_account: target_account)
  392. is_expected.to be true
  393. end
  394. end
  395. context 'not requested by target_account' do
  396. it 'returns false' do
  397. is_expected.to be false
  398. end
  399. end
  400. end
  401. describe '#favourited?' do
  402. let(:status) { Fabricate(:status, account: account, favourites: favourites) }
  403. subject { account.favourited?(status) }
  404. context 'favorited' do
  405. let(:favourites) { [Fabricate(:favourite, account: account)] }
  406. it 'returns true' do
  407. is_expected.to be true
  408. end
  409. end
  410. context 'not favorited' do
  411. let(:favourites) { [] }
  412. it 'returns false' do
  413. is_expected.to be false
  414. end
  415. end
  416. end
  417. describe '#reblogged?' do
  418. let(:status) { Fabricate(:status, account: account, reblogs: reblogs) }
  419. subject { account.reblogged?(status) }
  420. context 'reblogged' do
  421. let(:reblogs) { [Fabricate(:status, account: account)] }
  422. it 'returns true' do
  423. is_expected.to be true
  424. end
  425. end
  426. context 'not reblogged' do
  427. let(:reblogs) { [] }
  428. it 'returns false' do
  429. is_expected.to be false
  430. end
  431. end
  432. end
  433. describe '#pinned?' do
  434. let(:status) { Fabricate(:status, account: account) }
  435. subject { account.pinned?(status) }
  436. context 'pinned' do
  437. it 'returns true' do
  438. Fabricate(:status_pin, account: account, status: status)
  439. is_expected.to be true
  440. end
  441. end
  442. context 'not pinned' do
  443. it 'returns false' do
  444. is_expected.to be false
  445. end
  446. end
  447. end
  448. describe '#remote_followers_hash' do
  449. let(:me) { Fabricate(:account, username: 'Me') }
  450. let(:remote_1) { Fabricate(:account, username: 'alice', domain: 'example.org', uri: 'https://example.org/users/alice') }
  451. let(:remote_2) { Fabricate(:account, username: 'bob', domain: 'example.org', uri: 'https://example.org/users/bob') }
  452. let(:remote_3) { Fabricate(:account, username: 'instance-actor', domain: 'example.org', uri: 'https://example.org') }
  453. let(:remote_4) { Fabricate(:account, username: 'eve', domain: 'foo.org', uri: 'https://foo.org/users/eve') }
  454. before do
  455. remote_1.follow!(me)
  456. remote_2.follow!(me)
  457. remote_3.follow!(me)
  458. remote_4.follow!(me)
  459. me.follow!(remote_1)
  460. end
  461. it 'returns correct hash for remote domains' do
  462. expect(me.remote_followers_hash('https://example.org/')).to eq '20aecbe774b3d61c25094370baf370012b9271c5b172ecedb05caff8d79ef0c7'
  463. expect(me.remote_followers_hash('https://foo.org/')).to eq 'ccb9c18a67134cfff9d62c7f7e7eb88e6b803446c244b84265565f4eba29df0e'
  464. expect(me.remote_followers_hash('https://foo.org.evil.com/')).to eq '0000000000000000000000000000000000000000000000000000000000000000'
  465. expect(me.remote_followers_hash('https://foo')).to eq '0000000000000000000000000000000000000000000000000000000000000000'
  466. end
  467. it 'invalidates cache as needed when removing or adding followers' do
  468. expect(me.remote_followers_hash('https://example.org/')).to eq '20aecbe774b3d61c25094370baf370012b9271c5b172ecedb05caff8d79ef0c7'
  469. remote_3.unfollow!(me)
  470. expect(me.remote_followers_hash('https://example.org/')).to eq '707962e297b7bd94468a21bc8e506a1bcea607a9142cd64e27c9b106b2a5f6ec'
  471. remote_1.unfollow!(me)
  472. expect(me.remote_followers_hash('https://example.org/')).to eq '241b00794ce9b46aa864f3220afadef128318da2659782985bac5ed5bd436bff'
  473. remote_1.follow!(me)
  474. expect(me.remote_followers_hash('https://example.org/')).to eq '707962e297b7bd94468a21bc8e506a1bcea607a9142cd64e27c9b106b2a5f6ec'
  475. end
  476. end
  477. describe '#local_followers_hash' do
  478. let(:me) { Fabricate(:account, username: 'Me') }
  479. let(:remote_1) { Fabricate(:account, username: 'alice', domain: 'example.org', uri: 'https://example.org/users/alice') }
  480. before do
  481. me.follow!(remote_1)
  482. end
  483. it 'returns correct hash for local users' do
  484. expect(remote_1.local_followers_hash).to eq Digest::SHA256.hexdigest(ActivityPub::TagManager.instance.uri_for(me))
  485. end
  486. it 'invalidates cache as needed when removing or adding followers' do
  487. expect(remote_1.local_followers_hash).to eq Digest::SHA256.hexdigest(ActivityPub::TagManager.instance.uri_for(me))
  488. me.unfollow!(remote_1)
  489. expect(remote_1.local_followers_hash).to eq '0000000000000000000000000000000000000000000000000000000000000000'
  490. me.follow!(remote_1)
  491. expect(remote_1.local_followers_hash).to eq Digest::SHA256.hexdigest(ActivityPub::TagManager.instance.uri_for(me))
  492. end
  493. end
  494. describe 'muting an account' do
  495. let(:me) { Fabricate(:account, username: 'Me') }
  496. let(:you) { Fabricate(:account, username: 'You') }
  497. context 'with the notifications option unspecified' do
  498. before do
  499. me.mute!(you)
  500. end
  501. it 'defaults to muting notifications' do
  502. expect(me.muting_notifications?(you)).to be true
  503. end
  504. end
  505. context 'with the notifications option set to false' do
  506. before do
  507. me.mute!(you, notifications: false)
  508. end
  509. it 'does not mute notifications' do
  510. expect(me.muting_notifications?(you)).to be false
  511. end
  512. end
  513. context 'with the notifications option set to true' do
  514. before do
  515. me.mute!(you, notifications: true)
  516. end
  517. it 'does mute notifications' do
  518. expect(me.muting_notifications?(you)).to be true
  519. end
  520. end
  521. end
  522. describe 'ignoring reblogs from an account' do
  523. before do
  524. @me = Fabricate(:account, username: 'Me')
  525. @you = Fabricate(:account, username: 'You')
  526. end
  527. context 'with the reblogs option unspecified' do
  528. before do
  529. @me.follow!(@you)
  530. end
  531. it 'defaults to showing reblogs' do
  532. expect(@me.muting_reblogs?(@you)).to be(false)
  533. end
  534. end
  535. context 'with the reblogs option set to false' do
  536. before do
  537. @me.follow!(@you, reblogs: false)
  538. end
  539. it 'does mute reblogs' do
  540. expect(@me.muting_reblogs?(@you)).to be(true)
  541. end
  542. end
  543. context 'with the reblogs option set to true' do
  544. before do
  545. @me.follow!(@you, reblogs: true)
  546. end
  547. it 'does not mute reblogs' do
  548. expect(@me.muting_reblogs?(@you)).to be(false)
  549. end
  550. end
  551. end
  552. end