logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

announcements.js (4252B)


  1. const FETCH_ANNOUNCEMENT_INTERVAL_MS = 1000 * 60 * 5
  2. export const defaultState = {
  3. announcements: [],
  4. supportsAnnouncements: true,
  5. fetchAnnouncementsTimer: undefined
  6. }
  7. export const mutations = {
  8. setAnnouncements (state, announcements) {
  9. state.announcements = announcements
  10. },
  11. setAnnouncementRead (state, { id, read }) {
  12. const index = state.announcements.findIndex(a => a.id === id)
  13. if (index < 0) {
  14. return
  15. }
  16. state.announcements[index].read = read
  17. },
  18. setFetchAnnouncementsTimer (state, timer) {
  19. state.fetchAnnouncementsTimer = timer
  20. },
  21. setSupportsAnnouncements (state, supportsAnnouncements) {
  22. state.supportsAnnouncements = supportsAnnouncements
  23. }
  24. }
  25. export const getters = {
  26. unreadAnnouncementCount (state, _getters, rootState) {
  27. if (!rootState.users.currentUser) {
  28. return 0
  29. }
  30. const unread = state.announcements.filter(announcement => !(announcement.inactive || announcement.read))
  31. return unread.length
  32. }
  33. }
  34. const announcements = {
  35. state: defaultState,
  36. mutations,
  37. getters,
  38. actions: {
  39. fetchAnnouncements (store) {
  40. if (!store.state.supportsAnnouncements) {
  41. return Promise.resolve()
  42. }
  43. const currentUser = store.rootState.users.currentUser
  44. const isAdmin = currentUser && currentUser.privileges.includes('announcements_manage_announcements')
  45. const getAnnouncements = async () => {
  46. if (!isAdmin) {
  47. return store.rootState.api.backendInteractor.fetchAnnouncements()
  48. }
  49. const all = await store.rootState.api.backendInteractor.adminFetchAnnouncements()
  50. const visible = await store.rootState.api.backendInteractor.fetchAnnouncements()
  51. const visibleObject = visible.reduce((a, c) => {
  52. a[c.id] = c
  53. return a
  54. }, {})
  55. const getWithinVisible = announcement => visibleObject[announcement.id]
  56. all.forEach(announcement => {
  57. const visibleAnnouncement = getWithinVisible(announcement)
  58. if (!visibleAnnouncement) {
  59. announcement.inactive = true
  60. } else {
  61. announcement.read = visibleAnnouncement.read
  62. }
  63. })
  64. return all
  65. }
  66. return getAnnouncements()
  67. .then(announcements => {
  68. store.commit('setAnnouncements', announcements)
  69. })
  70. .catch(error => {
  71. // If and only if backend does not support announcements, it would return 404.
  72. // In this case, silently ignores it.
  73. if (error && error.statusCode === 404) {
  74. store.commit('setSupportsAnnouncements', false)
  75. } else {
  76. throw error
  77. }
  78. })
  79. },
  80. markAnnouncementAsRead (store, id) {
  81. return store.rootState.api.backendInteractor.dismissAnnouncement({ id })
  82. .then(() => {
  83. store.commit('setAnnouncementRead', { id, read: true })
  84. })
  85. },
  86. startFetchingAnnouncements (store) {
  87. if (store.state.fetchAnnouncementsTimer) {
  88. return
  89. }
  90. const interval = setInterval(() => store.dispatch('fetchAnnouncements'), FETCH_ANNOUNCEMENT_INTERVAL_MS)
  91. store.commit('setFetchAnnouncementsTimer', interval)
  92. return store.dispatch('fetchAnnouncements')
  93. },
  94. stopFetchingAnnouncements (store) {
  95. const interval = store.state.fetchAnnouncementsTimer
  96. store.commit('setFetchAnnouncementsTimer', undefined)
  97. clearInterval(interval)
  98. },
  99. postAnnouncement (store, { content, startsAt, endsAt, allDay }) {
  100. return store.rootState.api.backendInteractor.postAnnouncement({ content, startsAt, endsAt, allDay })
  101. .then(() => {
  102. return store.dispatch('fetchAnnouncements')
  103. })
  104. },
  105. editAnnouncement (store, { id, content, startsAt, endsAt, allDay }) {
  106. return store.rootState.api.backendInteractor.editAnnouncement({ id, content, startsAt, endsAt, allDay })
  107. .then(() => {
  108. return store.dispatch('fetchAnnouncements')
  109. })
  110. },
  111. deleteAnnouncement (store, id) {
  112. return store.rootState.api.backendInteractor.deleteAnnouncement({ id })
  113. .then(() => {
  114. return store.dispatch('fetchAnnouncements')
  115. })
  116. }
  117. }
  118. }
  119. export default announcements