aboutsummaryrefslogtreecommitdiff
path: root/src/components/settings_modal/admin_tabs/emoji_tab.js
blob: 58e1468f89450b102a4ed07efb561d532105ef1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { clone, assign } from 'lodash'
import TabSwitcher from 'src/components/tab_switcher/tab_switcher.jsx'
import StringSetting from '../helpers/string_setting.vue'
import Checkbox from 'components/checkbox/checkbox.vue'
import StillImage from 'components/still-image/still-image.vue'
import Select from 'components/select/select.vue'
import Popover from 'components/popover/popover.vue'
import ConfirmModal from 'components/confirm_modal/confirm_modal.vue'
import ModifiedIndicator from '../helpers/modified_indicator.vue'
import EmojiEditingPopover from '../helpers/emoji_editing_popover.vue'

const EmojiTab = {
  components: {
    TabSwitcher,
    StringSetting,
    Checkbox,
    StillImage,
    Select,
    Popover,
    ConfirmModal,
    ModifiedIndicator,
    EmojiEditingPopover
  },

  data () {
    return {
      knownLocalPacks: { },
      knownRemotePacks: { },
      editedMetadata: { },
      packName: '',
      newPackName: '',
      deleteModalVisible: false,
      remotePackInstance: '',
      remotePackDownloadAs: ''
    }
  },

  provide () {
    return { emojiAddr: this.emojiAddr }
  },

  computed: {
    pack () {
      return this.packName !== '' ? this.knownPacks[this.packName] : undefined
    },
    packMeta () {
      if (this.editedMetadata[this.packName] === undefined) {
        this.editedMetadata[this.packName] = clone(this.pack.pack)
      }

      return this.editedMetadata[this.packName]
    },
    knownPacks () {
      // Copy the object itself but not the children, so they are still passed by reference and modified
      const result = clone(this.knownLocalPacks)
      for (const instName in this.knownRemotePacks) {
        for (const instPack in this.knownRemotePacks[instName]) {
          result[`${instPack}@${instName}`] = this.knownRemotePacks[instName][instPack]
        }
      }

      return result
    },
    downloadWillReplaceLocal () {
      return (this.remotePackDownloadAs.trim() === '' && this.pack.remote && this.pack.remote.baseName in this.knownLocalPacks) ||
             (this.remotePackDownloadAs in this.knownLocalPacks)
    }
  },

  methods: {
    reloadEmoji () {
      this.$store.state.api.backendInteractor.reloadEmoji()
    },
    importFromFS () {
      this.$store.state.api.backendInteractor.importEmojiFromFS()
    },
    emojiAddr (name) {
      if (this.pack.remote !== undefined) {
        // Remote pack
        return `${this.pack.remote.instance}/emoji/${encodeURIComponent(this.pack.remote.baseName)}/${name}`
      } else {
        return `${this.$store.state.instance.server}/emoji/${encodeURIComponent(this.packName)}/${name}`
      }
    },

    createEmojiPack () {
      this.$store.state.api.backendInteractor.createEmojiPack(
        { name: this.newPackName }
      ).then(resp => resp.json()).then(resp => {
        if (resp === 'ok') {
          return this.refreshPackList()
        } else {
          this.displayError(resp.error)
          return Promise.reject(resp)
        }
      }).then(done => {
        this.$refs.createPackPopover.hidePopover()

        this.packName = this.newPackName
        this.newPackName = ''
      })
    },
    deleteEmojiPack () {
      this.$store.state.api.backendInteractor.deleteEmojiPack(
        { name: this.packName }
      ).then(resp => resp.json()).then(resp => {
        if (resp === 'ok') {
          return this.refreshPackList()
        } else {
          this.displayError(resp.error)
          return Promise.reject(resp)
        }
      }).then(done => {
        delete this.editedMetadata[this.packName]

        this.deleteModalVisible = false
        this.packName = ''
      })
    },

    metaEdited (prop) {
      if (!this.pack) return

      const def = this.pack.pack[prop] || ''
      const edited = this.packMeta[prop] || ''
      return edited !== def
    },
    savePackMetadata () {
      this.$store.state.api.backendInteractor.saveEmojiPackMetadata({ name: this.packName, newData: this.packMeta }).then(
        resp => resp.json()
      ).then(resp => {
        if (resp.error !== undefined) {
          this.displayError(resp.error)
          return
        }

        // Update actual pack data
        this.pack.pack = resp
        // Delete edited pack data, should auto-update itself
        delete this.editedMetadata[this.packName]
      })
    },

    updatePackFiles (newFiles) {
      this.pack.files = newFiles
      this.sortPackFiles(this.packName)
    },

    loadPacksPaginated (listFunction) {
      const pageSize = 25
      const allPacks = {}

      return listFunction({ instance: this.remotePackInstance, page: 1, pageSize: 0 })
        .then(data => data.json())
        .then(data => {
          if (data.error !== undefined) { return Promise.reject(data.error) }

          let resultingPromise = Promise.resolve({})
          for (let i = 0; i < Math.ceil(data.count / pageSize); i++) {
            resultingPromise = resultingPromise.then(() => listFunction({ instance: this.remotePackInstance, page: i, pageSize })
            ).then(data => data.json()).then(pageData => {
              if (pageData.error !== undefined) { return Promise.reject(pageData.error) }

              assign(allPacks, pageData.packs)
            })
          }

          return resultingPromise
        })
        .then(finished => allPacks)
        .catch(data => {
          this.displayError(data)
        })
    },

    refreshPackList () {
      this.loadPacksPaginated(this.$store.state.api.backendInteractor.listEmojiPacks)
        .then(allPacks => {
          this.knownLocalPacks = allPacks
          for (const name of Object.keys(this.knownLocalPacks)) {
            this.sortPackFiles(name)
          }
        })
    },
    listRemotePacks () {
      this.loadPacksPaginated(this.$store.state.api.backendInteractor.listRemoteEmojiPacks)
        .then(allPacks => {
          let inst = this.remotePackInstance
          if (!inst.startsWith('http')) { inst = 'https://' + inst }
          const instUrl = new URL(inst)
          inst = instUrl.host

          for (const packName in allPacks) {
            allPacks[packName].remote = {
              baseName: packName,
              instance: instUrl.origin
            }
          }

          this.knownRemotePacks[inst] = allPacks
          for (const pack in this.knownRemotePacks[inst]) {
            this.sortPackFiles(`${pack}@${inst}`)
          }

          this.$refs.remotePackPopover.hidePopover()
        })
        .catch(data => {
          this.displayError(data)
        })
    },
    downloadRemotePack () {
      if (this.remotePackDownloadAs.trim() === '') {
        this.remotePackDownloadAs = this.pack.remote.baseName
      }

      this.$store.state.api.backendInteractor.downloadRemoteEmojiPack({
        instance: this.pack.remote.instance, packName: this.pack.remote.baseName, as: this.remotePackDownloadAs
      })
        .then(data => data.json())
        .then(resp => {
          if (resp === 'ok') {
            this.$refs.dlPackPopover.hidePopover()

            return this.refreshPackList()
          } else {
            this.displayError(resp.error)
            return Promise.reject(resp)
          }
        }).then(done => {
          this.packName = this.remotePackDownloadAs
          this.remotePackDownloadAs = ''
        })
    },
    displayError (msg) {
      this.$store.dispatch('pushGlobalNotice', {
        messageKey: 'admin_dash.emoji.error',
        messageArgs: [msg],
        level: 'error'
      })
    },
    sortPackFiles (nameOfPack) {
      // Sort by key
      const sorted = Object.keys(this.knownPacks[nameOfPack].files).sort().reduce((acc, key) => {
        if (key.length === 0) return acc
        acc[key] = this.knownPacks[nameOfPack].files[key]
        return acc
      }, {})
      this.knownPacks[nameOfPack].files = sorted
    }
  },

  mounted () {
    this.refreshPackList()
  }
}

export default EmojiTab