yandere_fe/src/modules/instance.js

216 lines
6.2 KiB
JavaScript
Raw Normal View History

2018-09-09 11:21:23 -07:00
import { set } from 'vue'
import { getPreset, applyTheme } from '../services/style_setter/style_setter.js'
import { CURRENT_VERSION } from '../services/theme_data/theme_data.service.js'
import apiService from '../services/api/api.service.js'
import { instanceDefaultProperties } from './config.js'
2018-09-09 11:21:23 -07:00
const defaultState = {
// Stuff from apiConfig
2018-09-09 11:21:23 -07:00
name: 'Pleroma FE',
registrationOpen: true,
server: 'http://localhost:4040/',
textlimit: 5000,
themeData: undefined,
vapidPublicKey: undefined,
// Stuff from static/config.json
alwaysShowSubjectInput: true,
defaultAvatar: '/images/avi.png',
defaultBanner: '/images/banner.png',
2020-05-13 12:40:46 -07:00
background: '/static/aurora_borealis.jpg',
2018-09-09 11:21:23 -07:00
collapseMessageWithSubject: false,
2020-05-13 12:40:46 -07:00
greentext: false,
useAtIcon: false,
2020-05-13 12:40:46 -07:00
hideFilteredStatuses: false,
// bad name: actually hides posts of muted USERS
2020-05-13 12:40:46 -07:00
hideMutedPosts: false,
hideMutedThreads: true,
hideWordFilteredPosts: false,
hidePostStats: false,
hideSitename: false,
hideUserStats: false,
2020-05-13 12:46:31 -07:00
loginMethod: 'password',
2020-11-02 09:37:01 -08:00
logo: '/static/logo.svg',
2020-05-13 12:40:46 -07:00
logoMargin: '.2em',
logoMask: true,
logoLeft: false,
2020-05-13 12:40:46 -07:00
minimalScopesMode: false,
2018-12-13 09:41:01 -08:00
nsfwCensorImage: undefined,
2020-05-13 12:40:46 -07:00
postContentType: 'text/plain',
redirectRootLogin: '/main/friends',
redirectRootNoLogin: '/main/all',
2018-09-25 05:16:26 -07:00
scopeCopy: true,
2019-02-08 13:20:47 -08:00
showFeaturesPanel: true,
2020-05-13 12:40:46 -07:00
showInstanceSpecificPanel: false,
sidebarRight: false,
2018-09-25 04:47:02 -07:00
subjectLineBehavior: 'email',
2020-05-13 12:40:46 -07:00
theme: 'pleroma-dark',
2020-09-29 03:18:37 -07:00
virtualScrolling: true,
2021-02-23 00:00:23 -08:00
sensitiveByDefault: false,
2018-09-09 11:21:23 -07:00
// Nasty stuff
customEmoji: [],
customEmojiFetched: false,
emoji: [],
emojiFetched: false,
pleromaBackend: true,
postFormats: [],
restrictedNicknames: [],
safeDM: true,
knownDomains: [],
// Feature-set, apparently, not everything here is reported...
shoutAvailable: false,
2020-05-07 06:10:53 -07:00
pleromaChatMessagesAvailable: false,
gopherAvailable: false,
2020-05-13 13:48:21 -07:00
mediaProxyAvailable: false,
suggestionsEnabled: false,
suggestionsWeb: '',
2018-09-09 11:21:23 -07:00
// Html stuff
instanceSpecificPanelContent: '',
tos: '',
// Version Information
backendVersion: '',
2019-06-18 13:28:31 -07:00
frontendVersion: '',
pollsAvailable: false,
pollLimits: {
max_options: 4,
max_option_chars: 255,
min_expiration: 60,
max_expiration: 60 * 60 * 24
}
2018-09-09 11:21:23 -07:00
}
const instance = {
state: defaultState,
mutations: {
setInstanceOption (state, { name, value }) {
if (typeof value !== 'undefined') {
set(state, name, value)
}
},
setKnownDomains (state, domains) {
state.knownDomains = domains
2018-09-09 11:21:23 -07:00
}
},
getters: {
instanceDefaultConfig (state) {
return instanceDefaultProperties
.map(key => [key, state[key]])
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
}
},
2018-09-09 11:21:23 -07:00
actions: {
setInstanceOption ({ commit, dispatch }, { name, value }) {
2019-07-05 00:02:14 -07:00
commit('setInstanceOption', { name, value })
2018-09-09 11:21:23 -07:00
switch (name) {
case 'name':
dispatch('setPageTitle')
break
case 'shoutAvailable':
if (value) {
dispatch('initializeSocket')
}
break
case 'theme':
dispatch('setTheme', value)
break
2018-09-09 11:21:23 -07: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.name > b.name ? 1 : -1)
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 : -1)
commit('setInstanceOption', { name: 'customEmoji', value: emoji })
} else {
throw (res)
}
} catch (e) {
console.warn("Can't load custom emojis")
console.warn(e)
}
},
setTheme ({ commit, rootState }, themeName) {
commit('setInstanceOption', { name: 'theme', value: themeName })
getPreset(themeName)
.then(themeData => {
commit('setInstanceOption', { name: 'themeData', value: themeData })
// No need to apply theme if there's user theme already
const { customTheme } = rootState.config
if (customTheme) return
// New theme presets don't have 'theme' property, they use 'source'
const themeSource = themeData.source
if (!themeData.theme || (themeSource && themeSource.themeEngineVersion === CURRENT_VERSION)) {
applyTheme(themeSource)
} else {
applyTheme(themeData.theme)
}
})
},
fetchEmoji ({ dispatch, state }) {
if (!state.customEmojiFetched) {
state.customEmojiFetched = true
dispatch('getCustomEmoji')
}
if (!state.emojiFetched) {
state.emojiFetched = true
dispatch('getStaticEmoji')
}
},
async getKnownDomains ({ commit, rootState }) {
try {
const result = await apiService.fetchKnownDomains({
credentials: rootState.users.currentUser.credentials
})
commit('setKnownDomains', result)
} catch (e) {
console.warn("Can't load known domains")
console.warn(e)
}
2018-09-09 11:21:23 -07:00
}
}
}
export default instance