pleroma-fe/src/modules/instance.js

405 lines
12 KiB
JavaScript
Raw Normal View History

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'
2022-09-21 02:15:32 +02:00
import { langCodeToCldrName, ensureFinalFallback } from '../i18n/languages.js'
2018-09-09 20:21:23 +02:00
const SORTED_EMOJI_GROUP_IDS = [
'smileys-and-emotion',
'people-and-body',
'animals-and-nature',
'food-and-drink',
'travel-and-places',
'activities',
'objects',
'symbols',
'flags'
]
2022-09-21 03:50:40 +02:00
const REGIONAL_INDICATORS = (() => {
const start = 0x1F1E6
const end = 0x1F1FF
const A = 'A'.codePointAt(0)
const res = new Array(end - start + 1)
for (let i = start; i <= end; ++i) {
const letter = String.fromCodePoint(A + i - start)
res[i - start] = {
replacement: String.fromCodePoint(i),
imageUrl: false,
displayText: 'regional_indicator_' + letter,
displayTextI18n: {
key: 'emoji.regional_indicator',
args: { letter }
}
}
}
return res
})()
2018-09-09 20:21:23 +02:00
const REMOTE_INTERACTION_URL = '/main/ostatus'
2018-09-09 20:21:23 +02:00
const defaultState = {
// Stuff from apiConfig
name: 'Pleroma FE',
2020-05-13 23:46:58 +02:00
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 21:40:46 +02:00
background: '/static/aurora_borealis.jpg',
2018-09-09 20:21:23 +02:00
collapseMessageWithSubject: false,
2020-05-14 14:06:28 +02:00
greentext: true,
useAtIcon: false,
mentionLinkDisplay: 'short',
mentionLinkShowTooltip: true,
2022-01-20 18:07:09 +01:00
mentionLinkShowAvatar: false,
mentionLinkFadeDomain: true,
mentionLinkShowYous: false,
mentionLinkBoldenYou: true,
2020-05-13 21:40:46 +02:00
hideFilteredStatuses: false,
// bad name: actually hides posts of muted USERS
2020-05-13 21:40:46 +02:00
hideMutedPosts: false,
hideMutedThreads: true,
hideWordFilteredPosts: false,
hidePostStats: false,
hideBotIndication: false,
2020-05-13 21:40:46 +02:00
hideSitename: false,
hideUserStats: false,
2022-02-28 21:07:20 +01:00
muteBotStatuses: false,
2022-02-09 21:04:53 +01:00
modalOnRepeat: false,
modalOnUnfollow: false,
modalOnBlock: true,
modalOnMute: false,
modalOnDelete: true,
modalOnLogout: true,
modalOnApproveFollow: false,
modalOnDenyFollow: false,
2022-09-28 00:47:50 +02:00
modalOnRemoveUserFromFollowers: false,
2020-05-13 21:46:31 +02:00
loginMethod: 'password',
2020-11-02 18:37:01 +01:00
logo: '/static/logo.svg',
2020-05-13 21:40:46 +02:00
logoMargin: '.2em',
logoMask: true,
logoLeft: false,
disableUpdateNotification: false,
2020-05-13 21:40:46 +02:00
minimalScopesMode: false,
nsfwCensorImage: undefined,
postContentType: 'text/plain',
redirectRootLogin: '/main/friends',
redirectRootNoLogin: '/main/all',
2018-09-25 14:16:26 +02:00
scopeCopy: true,
2020-05-13 21:40:46 +02:00
showFeaturesPanel: true,
showInstanceSpecificPanel: false,
sidebarRight: false,
2018-09-25 13:47:02 +02:00
subjectLineBehavior: 'email',
2020-05-13 21:40:46 +02:00
theme: 'pleroma-dark',
2020-09-29 12:18:37 +02:00
virtualScrolling: true,
2021-02-23 09:00:23 +01:00
sensitiveByDefault: false,
conversationDisplay: 'linear',
conversationTreeAdvanced: false,
conversationOtherRepliesButton: 'below',
2022-03-06 20:13:35 +01:00
conversationTreeFadeAncestors: false,
2021-08-08 02:14:14 +02:00
maxDepthInThread: 6,
2018-09-09 20:21:23 +02:00
// Nasty stuff
customEmoji: [],
customEmojiFetched: false,
emoji: {},
emojiFetched: false,
2022-09-21 02:15:32 +02:00
unicodeEmojiAnnotations: {},
pleromaBackend: true,
postFormats: [],
restrictedNicknames: [],
safeDM: true,
knownDomains: [],
2023-01-22 17:15:52 +01:00
birthdayRequired: false,
birthdayMinAge: 0,
// Feature-set, apparently, not everything here is reported...
shoutAvailable: false,
2020-05-07 15:10:53 +02:00
pleromaChatMessagesAvailable: false,
gopherAvailable: false,
2020-05-13 22:48:21 +02:00
mediaProxyAvailable: false,
suggestionsEnabled: false,
suggestionsWeb: '',
2018-09-09 20:21:23 +02:00
// Html stuff
instanceSpecificPanelContent: '',
tos: '',
// Version Information
backendVersion: '',
2019-06-18 22:28:31 +02:00
frontendVersion: '',
pollsAvailable: false,
pollLimits: {
max_options: 4,
max_option_chars: 255,
min_expiration: 60,
max_expiration: 60 * 60 * 24
}
2018-09-09 20:21:23 +02:00
}
2022-09-21 02:15:32 +02:00
const loadAnnotations = (lang) => {
return import(
2022-09-21 05:13:07 +02:00
/* webpackChunkName: "emoji-annotations/[request]" */
2022-09-21 02:15:32 +02:00
`@kazvmoe-infra/unicode-emoji-json/annotations/${langCodeToCldrName(lang)}.json`
)
2022-09-21 04:03:31 +02:00
.then(k => k.default)
2022-09-21 02:15:32 +02:00
}
2022-09-21 02:44:52 +02:00
const injectAnnotations = (emoji, annotations) => {
const availableLangs = Object.keys(annotations)
return {
...emoji,
annotations: availableLangs.reduce((acc, cur) => {
acc[cur] = annotations[cur][emoji.replacement]
return acc
}, {})
}
}
2022-09-21 03:50:40 +02:00
const injectRegionalIndicators = groups => {
groups.symbols.push(...REGIONAL_INDICATORS)
return groups
}
2018-09-09 20:21:23 +02:00
const instance = {
state: defaultState,
mutations: {
setInstanceOption (state, { name, value }) {
if (typeof value !== 'undefined') {
2021-04-25 12:24:08 +02:00
state[name] = value
}
},
setKnownDomains (state, domains) {
state.knownDomains = domains
2022-09-21 02:15:32 +02:00
},
setUnicodeEmojiAnnotations (state, { lang, annotations }) {
state.unicodeEmojiAnnotations[lang] = annotations
2018-09-09 20:21:23 +02:00
}
},
getters: {
instanceDefaultConfig (state) {
return instanceDefaultProperties
.map(key => [key, state[key]])
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
},
2021-08-15 06:43:35 +02:00
groupedCustomEmojis (state) {
const packsOf = emoji => {
const packs = emoji.tags
.filter(k => k.startsWith('pack:'))
.map(k => {
const packName = k.slice(5) // remove 'pack:' prefix
return {
id: `custom-${packName}`,
text: packName
}
})
if (!packs.length) {
return [{
id: 'unpacked'
}]
} else {
return packs
}
}
2021-08-15 06:43:35 +02:00
return state.customEmoji
.reduce((res, emoji) => {
packsOf(emoji).forEach(({ id: packId, text: packName }) => {
2021-08-15 06:43:35 +02:00
if (!res[packId]) {
res[packId] = ({
id: packId,
text: packName,
image: emoji.imageUrl,
emojis: []
})
}
res[packId].emojis.push(emoji)
})
return res
}, {})
},
standardEmojiList (state) {
return SORTED_EMOJI_GROUP_IDS
2022-09-21 02:44:52 +02:00
.map(groupId => (state.emoji[groupId] || []).map(k => injectAnnotations(k, state.unicodeEmojiAnnotations)))
.reduce((a, b) => a.concat(b), [])
},
standardEmojiGroupList (state) {
return SORTED_EMOJI_GROUP_IDS.map(groupId => ({
id: groupId,
2022-09-21 02:44:52 +02:00
emojis: (state.emoji[groupId] || []).map(k => injectAnnotations(k, state.unicodeEmojiAnnotations))
}))
},
instanceDomain (state) {
return new URL(state.server).hostname
},
remoteInteractionLink (state) {
const server = state.server.endsWith('/') ? state.server.slice(0, -1) : state.server
const link = server + REMOTE_INTERACTION_URL
return ({ statusId, nickname }) => {
if (statusId) {
return `${link}?status_id=${statusId}`
} else {
return `${link}?nickname=${nickname}`
}
}
}
},
2018-09-09 20:21:23 +02:00
actions: {
setInstanceOption ({ commit, dispatch }, { name, value }) {
2019-07-05 09:02:14 +02:00
commit('setInstanceOption', { name, value })
2018-09-09 20:21:23 +02:00
switch (name) {
case 'name':
dispatch('setPageTitle')
break
case 'shoutAvailable':
if (value) {
dispatch('initializeSocket')
}
break
case 'theme':
dispatch('setTheme', value)
break
2018-09-09 20:21:23 +02:00
}
},
async getStaticEmoji ({ commit }) {
try {
2022-09-21 05:13:07 +02:00
const values = (await import(/* webpackChunkName: 'emoji' */ '../../static/emoji.json')).default
2022-09-21 04:03:31 +02:00
const emoji = Object.keys(values).reduce((res, groupId) => {
res[groupId] = values[groupId].map(e => ({
displayText: e.slug,
imageUrl: false,
replacement: e.emoji
}))
return res
}, {})
commit('setInstanceOption', { name: 'emoji', value: injectRegionalIndicators(emoji) })
} catch (e) {
console.warn("Can't load static emoji")
console.warn(e)
}
},
2022-09-21 02:15:32 +02:00
loadUnicodeEmojiData ({ commit, state }, language) {
2022-09-21 02:44:52 +02:00
const langList = ensureFinalFallback(language)
2022-09-21 02:15:32 +02:00
return Promise.all(
langList
2022-09-21 03:50:40 +02:00
.map(async lang => {
2022-09-21 02:15:32 +02:00
if (!state.unicodeEmojiAnnotations[lang]) {
2022-12-24 05:02:21 +01:00
try {
const annotations = await loadAnnotations(lang)
commit('setUnicodeEmojiAnnotations', { lang, annotations })
} catch (e) {
console.warn(`Error loading unicode emoji annotations for ${lang}: `, e)
// ignore
}
2022-09-21 02:15:32 +02:00
}
}))
},
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
2021-08-15 03:23:45 +02:00
const caseInsensitiveStrCmp = (a, b) => {
const la = a.toLowerCase()
const lb = b.toLowerCase()
return la > lb ? 1 : (la < lb ? -1 : 0)
}
const noPackLast = (a, b) => {
const aNull = a === ''
const bNull = b === ''
if (aNull === bNull) {
return 0
} else if (aNull && !bNull) {
return 1
} else {
return -1
}
}
2021-08-15 03:23:45 +02:00
const byPackThenByName = (a, b) => {
const packOf = emoji => (emoji.tags.filter(k => k.startsWith('pack:'))[0] || '').slice(5)
const packOfA = packOf(a)
const packOfB = packOf(b)
return noPackLast(packOfA, packOfB) || caseInsensitiveStrCmp(packOfA, packOfB) || caseInsensitiveStrCmp(a.displayText, b.displayText)
2021-08-15 03:23:45 +02:00
}
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
2021-08-15 03:23:45 +02:00
}).sort(byPackThenByName)
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 20:21:23 +02:00
}
}
}
export default instance