logo

pleroma-fe

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

lists.js (3570B)


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