pleroma-fe/src/modules/instance.js

168 lines
4.6 KiB
JavaScript
Raw Normal View History

2018-09-09 18:21:23 +00:00
import { set } from 'vue'
2018-11-19 17:22:46 +00:00
import { setPreset } from '../services/style_setter/style_setter.js'
import { instanceDefaultProperties } from './config.js'
2018-09-09 18:21:23 +00:00
const defaultState = {
// Stuff from static/config.json and apiConfig
2018-09-09 18:21:23 +00:00
name: 'Pleroma FE',
registrationOpen: true,
safeDM: true,
2018-09-09 18:21:23 +00:00
textlimit: 5000,
server: 'http://localhost:4040/',
theme: 'pleroma-dark',
background: '/static/aurora_borealis.jpg',
2018-09-09 18:21:23 +00:00
logo: '/static/logo.png',
logoMask: true,
logoMargin: '.2em',
redirectRootNoLogin: '/main/all',
redirectRootLogin: '/main/friends',
2018-09-09 18:21:23 +00:00
showInstanceSpecificPanel: false,
alwaysShowSubjectInput: true,
hideMutedPosts: false,
2018-09-09 18:21:23 +00:00
collapseMessageWithSubject: false,
hidePostStats: false,
hideUserStats: false,
2019-02-21 17:52:58 +00:00
hideFilteredStatuses: false,
2018-09-09 18:21:23 +00:00
disableChat: false,
2018-09-25 12:16:26 +00:00
scopeCopy: true,
2018-09-25 11:47:02 +00:00
subjectLineBehavior: 'email',
postContentType: 'text/plain',
hideSitename: false,
2018-12-13 17:41:01 +00:00
nsfwCensorImage: undefined,
vapidPublicKey: undefined,
noAttachmentLinks: false,
2019-02-08 21:20:47 +00:00
showFeaturesPanel: true,
minimalScopesMode: false,
2019-11-13 22:47:20 +00:00
greentext: false,
2018-09-09 18:21:23 +00:00
// Nasty stuff
pleromaBackend: true,
emoji: [],
emojiFetched: false,
2018-09-09 18:21:23 +00:00
customEmoji: [],
customEmojiFetched: false,
restrictedNicknames: [],
postFormats: [],
// Feature-set, apparently, not everything here is reported...
mediaProxyAvailable: false,
chatAvailable: false,
gopherAvailable: false,
suggestionsEnabled: false,
suggestionsWeb: '',
2018-09-09 18:21:23 +00:00
// Html stuff
instanceSpecificPanelContent: '',
tos: '',
// Version Information
backendVersion: '',
2019-06-18 20:28:31 +00:00
frontendVersion: '',
pollsAvailable: false,
pollLimits: {
max_options: 4,
max_option_chars: 255,
min_expiration: 60,
max_expiration: 60 * 60 * 24
}
2018-09-09 18:21:23 +00:00
}
const instance = {
state: defaultState,
mutations: {
setInstanceOption (state, { name, value }) {
if (typeof value !== 'undefined') {
set(state, name, value)
}
2018-09-09 18:21:23 +00:00
}
},
getters: {
instanceDefaultConfig (state) {
return instanceDefaultProperties
.map(key => [key, state[key]])
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
}
},
2018-09-09 18:21:23 +00:00
actions: {
setInstanceOption ({ commit, dispatch }, { name, value }) {
2019-07-05 07:02:14 +00:00
commit('setInstanceOption', { name, value })
2018-09-09 18:21:23 +00:00
switch (name) {
case 'name':
dispatch('setPageTitle')
break
case 'chatAvailable':
if (value) {
dispatch('initializeSocket')
}
break
2018-09-09 18:21:23 +00:00
}
},
async getStaticEmoji ({ commit }) {
try {
const res = await window.fetch('/static/emoji.json')
if (res.ok) {
const values = await res.json()
const emoji = Object.keys(values).map((key) => {
return {
displayText: key,
imageUrl: false,
replacement: values[key]
}
}).sort((a, b) => a.displayText - b.displayText)
commit('setInstanceOption', { name: 'emoji', value: emoji })
} else {
throw (res)
}
} catch (e) {
console.warn("Can't load static emoji")
console.warn(e)
}
},
async getCustomEmoji ({ commit, state }) {
try {
const res = await window.fetch('/api/pleroma/emoji.json')
if (res.ok) {
const result = await res.json()
const values = Array.isArray(result) ? Object.assign({}, ...result) : result
const emoji = Object.entries(values).map(([key, value]) => {
const imageUrl = value.image_url
return {
displayText: key,
imageUrl: imageUrl ? state.server + imageUrl : value,
tags: imageUrl ? value.tags.sort((a, b) => a > b ? 1 : 0) : ['utf'],
replacement: `:${key}: `
}
// Technically could use tags but those are kinda useless right now,
// should have been "pack" field, that would be more useful
}).sort((a, b) => a.displayText.toLowerCase() > b.displayText.toLowerCase() ? 1 : 0)
commit('setInstanceOption', { name: 'customEmoji', value: emoji })
} else {
throw (res)
}
} catch (e) {
console.warn("Can't load custom emojis")
console.warn(e)
}
},
setTheme ({ commit }, themeName) {
commit('setInstanceOption', { name: 'theme', value: themeName })
return setPreset(themeName, commit)
},
fetchEmoji ({ dispatch, state }) {
if (!state.customEmojiFetched) {
state.customEmojiFetched = true
dispatch('getCustomEmoji')
}
if (!state.emojiFetched) {
state.emojiFetched = true
dispatch('getStaticEmoji')
}
2018-09-09 18:21:23 +00:00
}
}
}
export default instance