funkwhale/front/src/store/moderation.ts

163 wiersze
3.7 KiB
TypeScript
Czysty Zwykły widok Historia

2022-07-04 22:52:53 +00:00
import type { Module } from 'vuex'
import type { RootState } from '~/store/index'
2019-02-14 09:49:06 +00:00
import axios from 'axios'
2022-04-17 22:53:53 +00:00
import { sortBy } from 'lodash-es'
import useLogger from '~/composables/useLogger'
export interface State {
2022-07-20 22:44:19 +00:00
filters: ContentFilter[]
showFilterModal: boolean
showReportModal: boolean
lastUpdate: Date,
filterModalTarget: {
2022-07-20 22:44:19 +00:00
type: null
target: null | { id: string, name: string }
}
reportModalTarget: {
2022-07-20 22:44:19 +00:00
type: null | 'channel'
target: null
2022-07-20 22:44:19 +00:00
typeLabel: string
label: string
_obj?: {
fid?: string
actor?: { fid: string }
}
}
}
2022-07-04 18:17:58 +00:00
export interface ContentFilter {
uuid: string
creation_date: Date
target: {
type: 'artist'
2022-07-04 18:17:58 +00:00
id: string
}
}
const logger = useLogger()
2019-02-14 09:49:06 +00:00
const store: Module<State, RootState> = {
2019-02-14 09:49:06 +00:00
namespaced: true,
state: {
filters: [],
showFilterModal: false,
2019-09-09 09:10:25 +00:00
showReportModal: false,
2019-02-14 09:49:06 +00:00
lastUpdate: new Date(),
filterModalTarget: {
type: null,
2021-12-06 10:35:20 +00:00
target: null
2019-09-09 09:10:25 +00:00
},
reportModalTarget: {
type: null,
2022-07-20 22:44:19 +00:00
target: null,
typeLabel: '',
label: ''
2019-02-14 09:49:06 +00:00
}
},
mutations: {
filterModalTarget (state, value) {
state.filterModalTarget = value
},
2019-09-09 09:10:25 +00:00
reportModalTarget (state, value) {
state.reportModalTarget = value
},
2019-02-14 09:49:06 +00:00
empty (state) {
state.filters = []
},
contentFilter (state, value) {
state.filters.push(value)
},
showFilterModal (state, value) {
state.showFilterModal = value
if (!value) {
state.filterModalTarget = {
type: null,
2021-12-06 10:35:20 +00:00
target: null
2019-02-14 09:49:06 +00:00
}
}
},
2019-09-09 09:10:25 +00:00
showReportModal (state, value) {
state.showReportModal = value
if (!value) {
state.reportModalTarget = {
type: null,
2022-07-20 22:44:19 +00:00
target: null,
typeLabel: '',
label: ''
2019-09-09 09:10:25 +00:00
}
}
},
2019-02-14 09:49:06 +00:00
reset (state) {
state.filters = []
state.filterModalTarget = {
type: null,
target: null
}
2019-02-14 09:49:06 +00:00
state.showFilterModal = false
2019-09-09 09:10:25 +00:00
state.showReportModal = false
state.reportModalTarget = {
type: null,
2022-07-20 22:44:19 +00:00
target: null,
typeLabel: '',
label: ''
}
2019-02-14 09:49:06 +00:00
},
deleteContentFilter (state, uuid) {
state.filters = state.filters.filter((e) => {
2021-12-06 10:35:20 +00:00
return e.uuid !== uuid
2019-02-14 09:49:06 +00:00
})
}
},
getters: {
artistFilters: (state) => () => {
2021-12-06 10:35:20 +00:00
const f = state.filters.filter((f) => {
2019-02-14 09:49:06 +00:00
return f.target.type === 'artist'
})
const p = sortBy(f, [(e) => { return e.creation_date }])
2019-02-14 09:49:06 +00:00
p.reverse()
return p
2021-12-06 10:35:20 +00:00
}
2019-02-14 09:49:06 +00:00
},
actions: {
2021-12-06 10:35:20 +00:00
hide ({ commit }, payload) {
2019-02-14 09:49:06 +00:00
commit('filterModalTarget', payload)
commit('showFilterModal', true)
},
2021-12-06 10:35:20 +00:00
report ({ commit }, payload) {
2019-09-09 09:10:25 +00:00
commit('reportModalTarget', payload)
commit('showReportModal', true)
},
fetchContentFilters ({ dispatch, commit }, url) {
2019-02-14 09:49:06 +00:00
let params = {}
let promise
if (url) {
promise = axios.get(url)
} else {
2021-12-06 10:35:20 +00:00
commit('empty')
params = {
page_size: 100,
ordering: '-creation_date'
}
promise = axios.get('moderation/content-filters/', { params: params })
2019-02-14 09:49:06 +00:00
}
return promise.then((response) => {
logger.info('Fetched a batch of ' + response.data.results.length + ' filters')
2019-02-14 09:49:06 +00:00
if (response.data.next) {
dispatch('fetchContentFilters', response.data.next)
}
response.data.results.forEach((result: ContentFilter) => {
2019-02-14 09:49:06 +00:00
commit('contentFilter', result)
})
})
},
2021-12-06 10:35:20 +00:00
deleteContentFilter ({ commit }, uuid) {
return axios.delete(`moderation/content-filters/${uuid}/`).then(() => {
2019-02-14 09:49:06 +00:00
commit('deleteContentFilter', uuid)
})
}
}
}
export default store