statuses.spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import { cloneDeep } from 'lodash'
  2. import { defaultState, mutations, findMaxId, prepareStatus, statusType } from '../../../../src/modules/statuses.js'
  3. // eslint-disable-next-line camelcase
  4. const makeMockStatus = ({id, text, is_post_verb = true}) => {
  5. return {
  6. id,
  7. user: {id: 0},
  8. name: 'status',
  9. text: text || `Text number ${id}`,
  10. fave_num: 0,
  11. uri: '',
  12. is_post_verb,
  13. attentions: []
  14. }
  15. }
  16. describe('Statuses.statusType', () => {
  17. it('identifies favorites', () => {
  18. const fav = {
  19. uri: 'tag:soykaf.com,2016-08-21:fave:2558:note:339495:2016-08-21T16:54:04+00:00'
  20. }
  21. const mastoFav = {
  22. uri: 'tag:mastodon.social,2016-11-27:objectId=73903:objectType=Favourite'
  23. }
  24. expect(statusType(fav)).to.eql('favorite')
  25. expect(statusType(mastoFav)).to.eql('favorite')
  26. })
  27. })
  28. describe('Statuses.prepareStatus', () => {
  29. it('sets nsfw for statuses with the #nsfw tag', () => {
  30. const safe = makeMockStatus({id: 1, text: 'Hello oniichan'})
  31. const nsfw = makeMockStatus({id: 1, text: 'Hello oniichan #nsfw'})
  32. expect(prepareStatus(safe).nsfw).to.eq(false)
  33. expect(prepareStatus(nsfw).nsfw).to.eq(true)
  34. })
  35. it('leaves existing nsfw settings alone', () => {
  36. const nsfw = makeMockStatus({id: 1, text: 'Hello oniichan #nsfw'})
  37. nsfw.nsfw = false
  38. expect(prepareStatus(nsfw).nsfw).to.eq(false)
  39. })
  40. it('sets deleted flag to false', () => {
  41. const aStatus = makeMockStatus({id: 1, text: 'Hello oniichan'})
  42. expect(prepareStatus(aStatus).deleted).to.eq(false)
  43. })
  44. })
  45. describe('Statuses.findMaxId', () => {
  46. it('returns the largest id in any of the given arrays', () => {
  47. const statusesOne = [{ id: 100 }, { id: 2 }]
  48. const statusesTwo = [{ id: 3 }]
  49. const maxId = findMaxId(statusesOne, statusesTwo)
  50. expect(maxId).to.eq(100)
  51. })
  52. it('returns undefined for empty arrays', () => {
  53. const maxId = findMaxId([], [])
  54. expect(maxId).to.eq(undefined)
  55. })
  56. })
  57. describe('The Statuses module', () => {
  58. it('adds the status to allStatuses and to the given timeline', () => {
  59. const state = cloneDeep(defaultState)
  60. const status = makeMockStatus({id: 1})
  61. mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' })
  62. expect(state.allStatuses).to.eql([status])
  63. expect(state.timelines.public.statuses).to.eql([status])
  64. expect(state.timelines.public.visibleStatuses).to.eql([])
  65. expect(state.timelines.public.newStatusCount).to.equal(1)
  66. })
  67. it('counts the status as new if it has not been seen on this timeline', () => {
  68. const state = cloneDeep(defaultState)
  69. const status = makeMockStatus({id: 1})
  70. mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' })
  71. mutations.addNewStatuses(state, { statuses: [status], timeline: 'friends' })
  72. expect(state.allStatuses).to.eql([status])
  73. expect(state.timelines.public.statuses).to.eql([status])
  74. expect(state.timelines.public.visibleStatuses).to.eql([])
  75. expect(state.timelines.public.newStatusCount).to.equal(1)
  76. expect(state.allStatuses).to.eql([status])
  77. expect(state.timelines.friends.statuses).to.eql([status])
  78. expect(state.timelines.friends.visibleStatuses).to.eql([])
  79. expect(state.timelines.friends.newStatusCount).to.equal(1)
  80. })
  81. it('add the statuses to allStatuses if no timeline is given', () => {
  82. const state = cloneDeep(defaultState)
  83. const status = makeMockStatus({id: 1})
  84. mutations.addNewStatuses(state, { statuses: [status] })
  85. expect(state.allStatuses).to.eql([status])
  86. expect(state.timelines.public.statuses).to.eql([])
  87. expect(state.timelines.public.visibleStatuses).to.eql([])
  88. expect(state.timelines.public.newStatusCount).to.equal(0)
  89. })
  90. it('adds the status to allStatuses and to the given timeline, directly visible', () => {
  91. const state = cloneDeep(defaultState)
  92. const status = makeMockStatus({id: 1})
  93. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  94. expect(state.allStatuses).to.eql([status])
  95. expect(state.timelines.public.statuses).to.eql([status])
  96. expect(state.timelines.public.visibleStatuses).to.eql([status])
  97. expect(state.timelines.public.newStatusCount).to.equal(0)
  98. })
  99. it('removes statuses by tag on deletion', () => {
  100. const state = cloneDeep(defaultState)
  101. const status = makeMockStatus({id: 1})
  102. const otherStatus = makeMockStatus({id: 3})
  103. status.uri = 'xxx'
  104. const deletion = makeMockStatus({id: 2, is_post_verb: false})
  105. deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.'
  106. deletion.uri = 'xxx'
  107. mutations.addNewStatuses(state, { statuses: [status, otherStatus], showImmediately: true, timeline: 'public' })
  108. mutations.addNewStatuses(state, { statuses: [deletion], showImmediately: true, timeline: 'public' })
  109. expect(state.allStatuses).to.eql([otherStatus])
  110. expect(state.timelines.public.statuses).to.eql([otherStatus])
  111. expect(state.timelines.public.visibleStatuses).to.eql([otherStatus])
  112. expect(state.timelines.public.maxId).to.eql(3)
  113. })
  114. it('does not update the maxId when the noIdUpdate flag is set', () => {
  115. const state = cloneDeep(defaultState)
  116. const status = makeMockStatus({id: 1})
  117. const secondStatus = makeMockStatus({id: 2})
  118. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  119. expect(state.timelines.public.maxId).to.eql(1)
  120. mutations.addNewStatuses(state, { statuses: [secondStatus], showImmediately: true, timeline: 'public', noIdUpdate: true })
  121. expect(state.timelines.public.statuses).to.eql([secondStatus, status])
  122. expect(state.timelines.public.visibleStatuses).to.eql([secondStatus, status])
  123. expect(state.timelines.public.maxId).to.eql(1)
  124. })
  125. it('keeps a descending by id order in timeline.visibleStatuses and timeline.statuses', () => {
  126. const state = cloneDeep(defaultState)
  127. const nonVisibleStatus = makeMockStatus({id: 1})
  128. const status = makeMockStatus({id: 3})
  129. const statusTwo = makeMockStatus({id: 2})
  130. const statusThree = makeMockStatus({id: 4})
  131. mutations.addNewStatuses(state, { statuses: [nonVisibleStatus], showImmediately: false, timeline: 'public' })
  132. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  133. mutations.addNewStatuses(state, { statuses: [statusTwo], showImmediately: true, timeline: 'public' })
  134. expect(state.timelines.public.minVisibleId).to.equal(2)
  135. mutations.addNewStatuses(state, { statuses: [statusThree], showImmediately: true, timeline: 'public' })
  136. expect(state.timelines.public.statuses).to.eql([statusThree, status, statusTwo, nonVisibleStatus])
  137. expect(state.timelines.public.visibleStatuses).to.eql([statusThree, status, statusTwo])
  138. })
  139. it('splits retweets from their status and links them', () => {
  140. const state = cloneDeep(defaultState)
  141. const status = makeMockStatus({id: 1})
  142. const retweet = makeMockStatus({id: 2, is_post_verb: false})
  143. const modStatus = makeMockStatus({id: 1, text: 'something else'})
  144. retweet.retweeted_status = status
  145. // It adds both statuses, but only the retweet to visible.
  146. mutations.addNewStatuses(state, { statuses: [retweet], timeline: 'public', showImmediately: true })
  147. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  148. expect(state.timelines.public.statuses).to.have.length(1)
  149. expect(state.allStatuses).to.have.length(2)
  150. expect(state.allStatuses[0].id).to.equal(1)
  151. expect(state.allStatuses[1].id).to.equal(2)
  152. // It refers to the modified status.
  153. mutations.addNewStatuses(state, { statuses: [modStatus], timeline: 'public' })
  154. expect(state.allStatuses).to.have.length(2)
  155. expect(state.allStatuses[0].id).to.equal(1)
  156. expect(state.allStatuses[0].text).to.equal(modStatus.text)
  157. expect(state.allStatuses[1].id).to.equal(2)
  158. expect(retweet.retweeted_status.text).to.eql(modStatus.text)
  159. })
  160. it('replaces existing statuses with the same id', () => {
  161. const state = cloneDeep(defaultState)
  162. const status = makeMockStatus({id: 1})
  163. const modStatus = makeMockStatus({id: 1, text: 'something else'})
  164. // Add original status
  165. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  166. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  167. expect(state.allStatuses).to.have.length(1)
  168. // Add new version of status
  169. mutations.addNewStatuses(state, { statuses: [modStatus], showImmediately: true, timeline: 'public' })
  170. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  171. expect(state.allStatuses).to.have.length(1)
  172. expect(state.allStatuses[0].text).to.eql(modStatus.text)
  173. })
  174. it('replaces existing statuses with the same id, coming from a retweet', () => {
  175. const state = cloneDeep(defaultState)
  176. const status = makeMockStatus({id: 1})
  177. const modStatus = makeMockStatus({id: 1, text: 'something else'})
  178. const retweet = makeMockStatus({id: 2, is_post_verb: false})
  179. retweet.retweeted_status = modStatus
  180. // Add original status
  181. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  182. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  183. expect(state.allStatuses).to.have.length(1)
  184. // Add new version of status
  185. mutations.addNewStatuses(state, { statuses: [retweet], showImmediately: false, timeline: 'public' })
  186. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  187. // Don't add the retweet itself if the tweet is visible
  188. expect(state.timelines.public.statuses).to.have.length(1)
  189. expect(state.allStatuses).to.have.length(2)
  190. expect(state.allStatuses[0].text).to.eql(modStatus.text)
  191. })
  192. it('handles favorite actions', () => {
  193. const state = cloneDeep(defaultState)
  194. const status = makeMockStatus({id: 1})
  195. const favorite = {
  196. id: 2,
  197. is_post_verb: false,
  198. in_reply_to_status_id: '1', // The API uses strings here...
  199. uri: 'tag:shitposter.club,2016-08-21:fave:3895:note:773501:2016-08-21T16:52:15+00:00',
  200. text: 'a favorited something by b',
  201. user: { id: 99 }
  202. }
  203. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  204. mutations.addNewStatuses(state, { statuses: [favorite], showImmediately: true, timeline: 'public' })
  205. expect(state.timelines.public.visibleStatuses.length).to.eql(1)
  206. expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1)
  207. expect(state.timelines.public.maxId).to.eq(favorite.id)
  208. // Adding it again does nothing
  209. mutations.addNewStatuses(state, { statuses: [favorite], showImmediately: true, timeline: 'public' })
  210. expect(state.timelines.public.visibleStatuses.length).to.eql(1)
  211. expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1)
  212. expect(state.timelines.public.maxId).to.eq(favorite.id)
  213. // If something is favorited by the current user, it also sets the 'favorited' property but does not increment counter to avoid over-counting. Counter is incremented (updated, really) via response to the favorite request.
  214. const user = {
  215. id: 1
  216. }
  217. const ownFavorite = {
  218. id: 3,
  219. is_post_verb: false,
  220. in_reply_to_status_id: '1', // The API uses strings here...
  221. uri: 'tag:shitposter.club,2016-08-21:fave:3895:note:773501:2016-08-21T16:52:15+00:00',
  222. text: 'a favorited something by b',
  223. user
  224. }
  225. mutations.addNewStatuses(state, { statuses: [ownFavorite], showImmediately: true, timeline: 'public', user })
  226. expect(state.timelines.public.visibleStatuses.length).to.eql(1)
  227. expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1)
  228. expect(state.timelines.public.visibleStatuses[0].favorited).to.eql(true)
  229. })
  230. describe('notifications', () => {
  231. it('removes a notification when the notice gets removed', () => {
  232. const user = { id: 1 }
  233. const state = cloneDeep(defaultState)
  234. const status = makeMockStatus({id: 1})
  235. const otherStatus = makeMockStatus({id: 3})
  236. const mentionedStatus = makeMockStatus({id: 2})
  237. mentionedStatus.attentions = [user]
  238. mentionedStatus.uri = 'xxx'
  239. otherStatus.attentions = [user]
  240. const deletion = makeMockStatus({id: 4, is_post_verb: false})
  241. deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.'
  242. deletion.uri = 'xxx'
  243. mutations.addNewStatuses(state, { statuses: [status, otherStatus], user })
  244. mutations.addNewNotifications(
  245. state,
  246. {
  247. notifications: [{
  248. ntype: 'mention',
  249. status: otherStatus,
  250. notice: otherStatus,
  251. is_seen: false
  252. }]
  253. })
  254. expect(state.notifications.data.length).to.eql(1)
  255. mutations.addNewNotifications(
  256. state,
  257. {
  258. notifications: [{
  259. ntype: 'mention',
  260. status: mentionedStatus,
  261. notice: mentionedStatus,
  262. is_seen: false
  263. }]
  264. })
  265. mutations.addNewStatuses(state, { statuses: [mentionedStatus], user })
  266. expect(state.allStatuses.length).to.eql(3)
  267. expect(state.notifications.data.length).to.eql(2)
  268. expect(state.notifications.data[1].status).to.eql(mentionedStatus)
  269. expect(state.notifications.data[1].action).to.eql(mentionedStatus)
  270. expect(state.notifications.data[1].type).to.eql('mention')
  271. mutations.addNewStatuses(state, { statuses: [deletion], user })
  272. expect(state.allStatuses.length).to.eql(2)
  273. expect(state.notifications.data.length).to.eql(1)
  274. })
  275. })
  276. })