logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe

direct_conversations.js (8346B)


      1 import { find, omitBy, debounce } from 'lodash'
      2 import directConversationService from '../services/direct_conversation_service/direct_conversation_service.js'
      3 
      4 const emptyDirectConversations = () => ({
      5   data: [],
      6   pagination: { maxId: undefined, minId: undefined },
      7   idStore: {},
      8   currentDirectConversationId: null,
      9   directConversationFocused: false,
     10   currentDirectConversationStatuses: []
     11 })
     12 
     13 const defaultState = {
     14   directConversations: emptyDirectConversations(),
     15   currentDirectConversation: directConversationService.empty(),
     16   fetcher: undefined
     17 }
     18 
     19 const directConversations = {
     20   state: { ...defaultState },
     21   actions: {
     22     startFetchingDirectConversations ({ dispatch }) {
     23       setInterval(() => {
     24         dispatch('fetchDirectConversations', { reset: true })
     25         dispatch('refreshCurrentUser')
     26       }, 5000)
     27     },
     28     startFetchingCurrentDirectConversation ({ commit, dispatch }, { fetcher, conversationId }) {
     29       commit('setCurrentDirectConversationId', conversationId)
     30       dispatch('setCurrentDirectConversationFetcher', { fetcher })
     31     },
     32     setCurrentDirectConversationFetcher ({ rootState, commit }, { fetcher }) {
     33       commit('setCurrentDirectConversationFetcher', { fetcher })
     34     },
     35     fetchDirectConversations ({ dispatch, rootState, commit }, params = {}) {
     36       const pagination = rootState.directConversations.directConversations.pagination
     37       const opts = { maxId: params.reset ? undefined : pagination.maxId }
     38 
     39       return rootState.api.backendInteractor.directConversations(opts)
     40         .then(({ directConversations, pagination }) => {
     41           dispatch('addNewDirectConversations', { directConversations, pagination })
     42           return directConversations
     43         })
     44     },
     45     addNewDirectConversations ({ rootState, commit, dispatch, rootGetters }, { directConversations, pagination }) {
     46       commit('addNewDirectConversations', { dispatch, directConversations, pagination, rootGetters })
     47     },
     48     refreshCurrentUser: debounce(({ rootState, commit, dispatch }) => {
     49       const currentUser = rootState.users.currentUser
     50       dispatch('fetchUser', currentUser.screen_name).then(updatedCurrentUser => {
     51         commit('setCurrentUser', updatedCurrentUser)
     52       })
     53     }, 500, { leading: false }),
     54     updateDirectConversation ({ rootState, commit, dispatch, rootGetters }, { directConversation }) {
     55       commit('updateDirectConversation', { dispatch, rootGetters, directConversation })
     56       if (rootState.directConversations.directConversations.currentDirectConversationId === directConversation.id) {
     57         dispatch('addToCurrentDirectConversationStatuses', { statuses: [directConversation.last_status] })
     58       }
     59       dispatch('refreshCurrentUser')
     60     },
     61     deleteDirectConversation ({ rootState, commit, dispatch, rootGetters }, { id }) {
     62       commit('deleteDirectConversation', { dispatch, rootGetters, id })
     63       dispatch('refreshCurrentUser')
     64     },
     65     resetDirectConversations ({ rootState, commit, dispatch, rootGetters }) {
     66       commit('resetDirectConversations', { dispatch })
     67     },
     68     setDirectConversationFocused ({ commit }, value) {
     69       commit('setDirectConversationFocused', value)
     70     },
     71     addToCurrentDirectConversationStatuses ({ commit }, value) {
     72       commit('addToCurrentDirectConversationStatuses', value)
     73     },
     74     resetDirectConversationNewMessageCount ({ commit }, value) {
     75       commit('resetDirectConversationNewMessageCount', value)
     76     },
     77     markAllDirectConversationsAsRead ({ commit }, value) {
     78       commit('markAllDirectConversationsAsRead', value)
     79     },
     80     removeFromCurrentDirectConversationStatuses ({ commit }, { id }) {
     81       commit('removeFromCurrentDirectConversationStatuses', id)
     82     },
     83     clearCurrentDirectConversationStatuses ({ commit }, value) {
     84       commit('clearCurrentDirectConversationStatuses', value)
     85     },
     86     clearCurrentDirectConversation ({ commit, dispatch }, value) {
     87       commit('setCurrentDirectConversationId', undefined)
     88       commit('setCurrentDirectConversationFetcher', { fetcher: undefined })
     89       dispatch('clearCurrentDirectConversationStatuses')
     90     },
     91     readDirectConversation ({ rootState, dispatch }, { id }) {
     92       dispatch('resetDirectConversationNewMessageCount')
     93       rootState.api.backendInteractor.readDirectConversation({ id }).then(() => {
     94         dispatch('refreshCurrentUser')
     95       })
     96     }
     97   },
     98   mutations: {
     99     setCurrentDirectConversationFetcher (state, { fetcher }) {
    100       let prevFetcher = state.fetcher
    101       if (prevFetcher) {
    102         clearInterval(prevFetcher)
    103       }
    104       state.fetcher = fetcher && fetcher()
    105     },
    106     addNewDirectConversations (state, { _dispatch, directConversations, pagination, _rootGetters }) {
    107       state.directConversations.pagination = pagination
    108       directConversations.forEach((conversation) => {
    109         // This is to prevent duplicate conversations being added
    110         // (right now, backend can return the same conversation on different pages)
    111         if (!state.directConversations.idStore[conversation.id]) {
    112           state.directConversations.data.push(conversation)
    113           state.directConversations.idStore[conversation.id] = conversation
    114         } else {
    115           const directConversation = find(state.directConversations.data, { id: conversation.id })
    116           directConversation.last_status = conversation.last_status
    117           directConversation.unread = conversation.unread
    118           state.directConversations.idStore[conversation.id] = conversation
    119         }
    120       })
    121     },
    122     updateDirectConversation (state, { _dispatch, directConversation: updatedDirectConversation, _rootGetters }) {
    123       let directConversation = find(state.directConversations.data, { id: updatedDirectConversation.id })
    124       if (directConversation) {
    125         if (directConversation.last_status.id < updatedDirectConversation.last_status.id) {
    126           state.directConversations.data = state.directConversations.data.filter(d => {
    127             return d.id !== updatedDirectConversation.id
    128           })
    129           state.directConversations.data.unshift(updatedDirectConversation)
    130           state.directConversations.idStore[updatedDirectConversation.id] = updatedDirectConversation
    131         } else {
    132           directConversation.last_status = updatedDirectConversation.last_status
    133           directConversation.unread = updatedDirectConversation.unread
    134         }
    135       } else {
    136         state.directConversations.data.unshift(updatedDirectConversation)
    137         state.directConversations.idStore[updatedDirectConversation.id] = updatedDirectConversation
    138       }
    139     },
    140     deleteDirectConversation (state, { _dispatch, id, _rootGetters }) {
    141       state.directConversations.data = state.directConversations.data.filter(conversation =>
    142         conversation.last_status.id !== id
    143       )
    144       state.directConversations.idStore = omitBy(state.directConversations.idStore, conversation => conversation.last_status.id === id)
    145     },
    146     resetDirectConversations (state, { _dispatch }) {
    147       state.directConversations.data = []
    148       state.directConversations.idStore = {}
    149     },
    150     setDirectConversationsLoading (state, { value }) {
    151       state.directConversations.loading = value
    152     },
    153     setCurrentDirectConversationId (state, value) {
    154       state.directConversations.currentDirectConversationId = value
    155       directConversationService.setConversationId(state.currentDirectConversation, value)
    156     },
    157     setDirectConversationFocused (state, value) {
    158       state.directConversations.directConversationFocused = value
    159     },
    160     markAllDirectConversationsAsRead (state, _value) {
    161       state.directConversations.data.forEach(directConversation => {
    162         directConversation.unread = false
    163       })
    164     },
    165     addToCurrentDirectConversationStatuses (state, value) {
    166       directConversationService.add(state.currentDirectConversation, value)
    167     },
    168     resetDirectConversationNewMessageCount (state, _value) {
    169       directConversationService.resetNewMessageCount(state.currentDirectConversation)
    170     },
    171     removeFromCurrentDirectConversationStatuses (state, value) {
    172       directConversationService.remove(state.currentDirectConversation, value)
    173     },
    174     clearCurrentDirectConversationStatuses (state, _value) {
    175       directConversationService.clear(state.currentDirectConversation)
    176     }
    177   }
    178 }
    179 
    180 export default directConversations