logo

pleroma-fe

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

lists.js (4048B)


  1. import { remove, find } from 'lodash'
  2. export const defaultState = {
  3. allLists: [],
  4. allListsObject: {}
  5. }
  6. export const mutations = {
  7. setLists (state, value) {
  8. state.allLists = value
  9. },
  10. setList (state, { listId, title }) {
  11. if (!state.allListsObject[listId]) {
  12. state.allListsObject[listId] = { accountIds: [] }
  13. }
  14. state.allListsObject[listId].title = title
  15. const entry = find(state.allLists, { id: listId })
  16. if (!entry) {
  17. state.allLists.push({ id: listId, title })
  18. } else {
  19. entry.title = title
  20. }
  21. },
  22. setListAccounts (state, { listId, accountIds }) {
  23. if (!state.allListsObject[listId]) {
  24. state.allListsObject[listId] = { accountIds: [] }
  25. }
  26. state.allListsObject[listId].accountIds = accountIds
  27. },
  28. addListAccount (state, { listId, accountId }) {
  29. if (!state.allListsObject[listId]) {
  30. state.allListsObject[listId] = { accountIds: [] }
  31. }
  32. state.allListsObject[listId].accountIds.push(accountId)
  33. },
  34. removeListAccount (state, { listId, accountId }) {
  35. if (!state.allListsObject[listId]) {
  36. state.allListsObject[listId] = { accountIds: [] }
  37. }
  38. const { accountIds } = state.allListsObject[listId]
  39. const set = new Set(accountIds)
  40. set.delete(accountId)
  41. state.allListsObject[listId].accountIds = [...set]
  42. },
  43. deleteList (state, { listId }) {
  44. delete state.allListsObject[listId]
  45. remove(state.allLists, list => list.id === listId)
  46. }
  47. }
  48. const actions = {
  49. setLists ({ commit }, value) {
  50. commit('setLists', value)
  51. },
  52. createList ({ rootState, commit }, { title }) {
  53. return rootState.api.backendInteractor.createList({ title })
  54. .then((list) => {
  55. commit('setList', { listId: list.id, title })
  56. return list
  57. })
  58. },
  59. fetchList ({ rootState, commit }, { listId }) {
  60. return rootState.api.backendInteractor.getList({ listId })
  61. .then((list) => commit('setList', { listId: list.id, title: list.title }))
  62. },
  63. fetchListAccounts ({ rootState, commit }, { listId }) {
  64. return rootState.api.backendInteractor.getListAccounts({ listId })
  65. .then((accountIds) => commit('setListAccounts', { listId, accountIds }))
  66. },
  67. setList ({ rootState, commit }, { listId, title }) {
  68. rootState.api.backendInteractor.updateList({ listId, title })
  69. commit('setList', { listId, title })
  70. },
  71. setListAccounts ({ rootState, commit }, { listId, accountIds }) {
  72. const saved = rootState.lists.allListsObject[listId].accountIds || []
  73. const added = accountIds.filter(id => !saved.includes(id))
  74. const removed = saved.filter(id => !accountIds.includes(id))
  75. commit('setListAccounts', { listId, accountIds })
  76. if (added.length > 0) {
  77. rootState.api.backendInteractor.addAccountsToList({ listId, accountIds: added })
  78. }
  79. if (removed.length > 0) {
  80. rootState.api.backendInteractor.removeAccountsFromList({ listId, accountIds: removed })
  81. }
  82. },
  83. addListAccount ({ rootState, commit }, { listId, accountId }) {
  84. return rootState
  85. .api
  86. .backendInteractor
  87. .addAccountsToList({ listId, accountIds: [accountId] })
  88. .then((result) => {
  89. commit('addListAccount', { listId, accountId })
  90. return result
  91. })
  92. },
  93. removeListAccount ({ rootState, commit }, { listId, accountId }) {
  94. return rootState
  95. .api
  96. .backendInteractor
  97. .removeAccountsFromList({ listId, accountIds: [accountId] })
  98. .then((result) => {
  99. commit('removeListAccount', { listId, accountId })
  100. return result
  101. })
  102. },
  103. deleteList ({ rootState, commit }, { listId }) {
  104. rootState.api.backendInteractor.deleteList({ listId })
  105. commit('deleteList', { listId })
  106. }
  107. }
  108. export const getters = {
  109. findListTitle: state => id => {
  110. if (!state.allListsObject[id]) return
  111. return state.allListsObject[id].title
  112. },
  113. findListAccounts: state => id => {
  114. return [...state.allListsObject[id].accountIds]
  115. }
  116. }
  117. const lists = {
  118. state: defaultState,
  119. mutations,
  120. actions,
  121. getters
  122. }
  123. export default lists