pleroma-fe/src/modules/instance.js

374 lines
10 KiB
JavaScript
Raw Normal View History

import apiService from '../services/api/api.service.js'
import { instanceDefaultProperties } from './config.js'
import { ensureFinalFallback } from '../i18n/languages.js'
2025-02-03 13:02:14 +02:00
import { useInterfaceStore } from 'src/stores/interface.js'
// See build/emojis_plugin for more details
import { annotationsLoader } from 'virtual:pleroma-fe/emoji-annotations'
2026-01-06 16:22:52 +02:00
import {
staticOrApiConfigDefault,
instanceDefaultConfig,
} from './default_config_state.js'
2018-09-09 21:21:23 +03:00
const SORTED_EMOJI_GROUP_IDS = [
'smileys-and-emotion',
'people-and-body',
'animals-and-nature',
'food-and-drink',
'travel-and-places',
'activities',
'objects',
'symbols',
2026-01-06 16:22:52 +02:00
'flags',
]
2022-09-20 21:50:40 -04:00
const REGIONAL_INDICATORS = (() => {
2026-01-06 16:22:52 +02:00
const start = 0x1f1e6
const end = 0x1f1ff
2022-09-20 21:50:40 -04:00
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',
2026-01-06 16:22:52 +02:00
args: { letter },
},
2022-09-20 21:50:40 -04:00
}
}
return res
})()
const REMOTE_INTERACTION_URL = '/main/ostatus'
2018-09-09 21:21:23 +03:00
const defaultState = {
// Stuff from apiConfig
2018-09-09 21:21:23 +03:00
name: 'Pleroma FE',
registrationOpen: true,
server: 'http://localhost:4040/',
textlimit: 5000,
themesIndex: undefined,
stylesIndex: undefined,
palettesIndex: undefined,
themeData: undefined, // used for theme editor v2
vapidPublicKey: undefined,
// Stuff from static/config.json
2020-05-13 14:46:31 -05:00
loginMethod: 'password',
disableUpdateNotification: false,
2024-06-27 00:59:24 +03:00
fontsOverride: {},
// Instance-wide configurations that should not be changed by individual users
...staticOrApiConfigDefault,
// Instance admins can override default settings for the whole instance
...instanceDefaultConfig,
2018-09-09 21:21:23 +03:00
// Nasty stuff
customEmoji: [],
customEmojiFetched: false,
emoji: {},
emojiFetched: false,
2022-09-20 20:15:32 -04:00
unicodeEmojiAnnotations: {},
pleromaExtensionsAvailable: true,
postFormats: [],
restrictedNicknames: [],
safeDM: true,
knownDomains: [],
2023-01-22 11:15:52 -05:00
birthdayRequired: false,
birthdayMinAge: 0,
// Feature-set, apparently, not everything here is reported...
shoutAvailable: false,
2020-05-07 16:10:53 +03:00
pleromaChatMessagesAvailable: false,
pleromaCustomEmojiReactionsAvailable: false,
pleromaBookmarkFoldersAvailable: false,
pleromaPublicFavouritesAvailable: true,
statusNotificationTypeAvailable: true,
gopherAvailable: false,
2020-05-13 15:48:21 -05:00
mediaProxyAvailable: false,
suggestionsEnabled: false,
suggestionsWeb: '',
quotingAvailable: false,
2023-12-27 22:30:19 -05:00
groupActorAvailable: false,
blockExpiration: false,
2025-06-24 15:56:08 +03:00
localBubbleInstances: [], // Akkoma
2018-09-09 21:21:23 +03:00
// Html stuff
instanceSpecificPanelContent: '',
tos: '',
// Version Information
backendVersion: '',
backendRepository: '',
2019-06-18 20:28:31 +00:00
frontendVersion: '',
pollsAvailable: false,
pollLimits: {
max_options: 4,
max_option_chars: 255,
min_expiration: 60,
2026-01-06 16:22:52 +02:00
max_expiration: 60 * 60 * 24,
},
2018-09-09 21:21:23 +03:00
}
2022-09-20 20:15:32 -04:00
const loadAnnotations = (lang) => {
2026-01-06 16:22:52 +02:00
return annotationsLoader[lang]().then((k) => k.default)
2022-09-20 20:15:32 -04:00
}
2022-09-20 20:44:52 -04: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
2026-01-06 16:22:52 +02:00
}, {}),
2022-09-20 20:44:52 -04:00
}
}
2026-01-06 16:22:52 +02:00
const injectRegionalIndicators = (groups) => {
2022-09-20 21:50:40 -04:00
groups.symbols.push(...REGIONAL_INDICATORS)
return groups
}
2018-09-09 21:21:23 +03:00
const instance = {
state: defaultState,
mutations: {
2026-01-06 16:22:52 +02:00
setInstanceOption(state, { name, value }) {
if (typeof value !== 'undefined') {
2021-04-25 13:24:08 +03:00
state[name] = value
}
},
2026-01-06 16:22:52 +02:00
setKnownDomains(state, domains) {
state.knownDomains = domains
2022-09-20 20:15:32 -04:00
},
2026-01-06 16:22:52 +02:00
setUnicodeEmojiAnnotations(state, { lang, annotations }) {
2022-09-20 20:15:32 -04:00
state.unicodeEmojiAnnotations[lang] = annotations
2026-01-06 16:22:52 +02:00
},
2018-09-09 21:21:23 +03:00
},
getters: {
2026-01-06 16:22:52 +02:00
instanceDefaultConfig(state) {
return instanceDefaultProperties
2026-01-06 16:22:52 +02:00
.map((key) => [key, state[key]])
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
},
2026-01-06 16:22:52 +02:00
groupedCustomEmojis(state) {
const packsOf = (emoji) => {
const packs = emoji.tags
2026-01-06 16:22:52 +02:00
.filter((k) => k.startsWith('pack:'))
.map((k) => {
const packName = k.slice(5) // remove 'pack:' prefix
return {
id: `custom-${packName}`,
2026-01-06 16:22:52 +02:00
text: packName,
}
})
if (!packs.length) {
2026-01-06 16:22:52 +02:00
return [
{
id: 'unpacked',
},
]
} else {
return packs
}
}
2026-01-06 16:22:52 +02:00
return state.customEmoji.reduce((res, emoji) => {
packsOf(emoji).forEach(({ id: packId, text: packName }) => {
if (!res[packId]) {
res[packId] = {
id: packId,
text: packName,
image: emoji.imageUrl,
emojis: [],
2021-08-15 00:43:35 -04:00
}
2026-01-06 16:22:52 +02:00
}
res[packId].emojis.push(emoji)
})
return res
}, {})
2021-08-15 00:43:35 -04:00
},
2026-01-06 16:22:52 +02:00
standardEmojiList(state) {
return SORTED_EMOJI_GROUP_IDS.map((groupId) =>
(state.emoji[groupId] || []).map((k) =>
injectAnnotations(k, state.unicodeEmojiAnnotations),
),
).reduce((a, b) => a.concat(b), [])
},
2026-01-06 16:22:52 +02:00
standardEmojiGroupList(state) {
return SORTED_EMOJI_GROUP_IDS.map((groupId) => ({
id: groupId,
2026-01-06 16:22:52 +02:00
emojis: (state.emoji[groupId] || []).map((k) =>
injectAnnotations(k, state.unicodeEmojiAnnotations),
),
}))
},
2026-01-06 16:22:52 +02:00
instanceDomain(state) {
return new URL(state.server).hostname
},
2026-01-06 16:22:52 +02:00
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}`
}
}
2026-01-06 16:22:52 +02:00
},
},
2018-09-09 21:21:23 +03:00
actions: {
2026-01-06 16:22:52 +02:00
setInstanceOption({ commit, dispatch }, { name, value }) {
2019-07-05 10:02:14 +03:00
commit('setInstanceOption', { name, value })
2018-09-09 21:21:23 +03:00
switch (name) {
case 'name':
2023-04-05 21:06:37 -06:00
useInterfaceStore().setPageTitle()
2018-09-09 21:21:23 +03:00
break
case 'shoutAvailable':
if (value) {
dispatch('initializeSocket')
}
break
2018-09-09 21:21:23 +03:00
}
},
2026-01-06 16:22:52 +02:00
async getStaticEmoji({ commit }) {
try {
2025-02-28 10:52:04 -05:00
const values = (await import('/src/assets/emoji.json')).default
2022-09-20 22:03:31 -04:00
const emoji = Object.keys(values).reduce((res, groupId) => {
2026-01-06 16:22:52 +02:00
res[groupId] = values[groupId].map((e) => ({
2022-09-20 22:03:31 -04:00
displayText: e.slug,
imageUrl: false,
2026-01-06 16:22:52 +02:00
replacement: e.emoji,
2022-09-20 22:03:31 -04:00
}))
return res
}, {})
2026-01-06 16:22:52 +02:00
commit('setInstanceOption', {
name: 'emoji',
value: injectRegionalIndicators(emoji),
})
} catch (e) {
console.warn("Can't load static emoji\n", e)
}
},
2026-01-06 16:22:52 +02:00
loadUnicodeEmojiData({ commit, state }, language) {
2022-09-20 20:44:52 -04:00
const langList = ensureFinalFallback(language)
2022-09-20 20:15:32 -04:00
return Promise.all(
2026-01-06 16:22:52 +02:00
langList.map(async (lang) => {
if (!state.unicodeEmojiAnnotations[lang]) {
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-20 20:15:32 -04:00
}
2026-01-06 16:22:52 +02:00
}
}),
)
2022-09-20 20:15:32 -04:00
},
2026-01-06 16:22:52 +02:00
async getCustomEmoji({ commit, state }) {
try {
2025-06-18 16:58:11 +03:00
let res = await window.fetch('/api/v1/pleroma/emoji')
if (!res.ok) {
res = await window.fetch('/api/pleroma/emoji.json')
}
if (res.ok) {
const result = await res.json()
2026-01-06 16:22:52 +02:00
const values = Array.isArray(result)
? Object.assign({}, ...result)
: result
2021-08-14 21:23:45 -04:00
const caseInsensitiveStrCmp = (a, b) => {
const la = a.toLowerCase()
const lb = b.toLowerCase()
2026-01-06 16:22:52 +02:00
return la > lb ? 1 : la < lb ? -1 : 0
2021-08-14 21:23:45 -04:00
}
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-14 21:23:45 -04:00
const byPackThenByName = (a, b) => {
2026-01-06 16:22:52 +02:00
const packOf = (emoji) =>
(emoji.tags.filter((k) => k.startsWith('pack:'))[0] || '').slice(
5,
)
const packOfA = packOf(a)
const packOfB = packOf(b)
2026-01-06 16:22:52 +02:00
return (
noPackLast(packOfA, packOfB) ||
caseInsensitiveStrCmp(packOfA, packOfB) ||
caseInsensitiveStrCmp(a.displayText, b.displayText)
)
2021-08-14 21:23:45 -04:00
}
2026-01-06 16:22:52 +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
})
.sort(byPackThenByName)
commit('setInstanceOption', { name: 'customEmoji', value: emoji })
} else {
2026-01-06 16:22:52 +02:00
throw res
}
} catch (e) {
console.warn("Can't load custom emojis\n", e)
}
},
2026-01-06 16:22:52 +02:00
fetchEmoji({ dispatch, state }) {
if (!state.customEmojiFetched) {
state.customEmojiFetched = true
dispatch('getCustomEmoji')
}
if (!state.emojiFetched) {
state.emojiFetched = true
dispatch('getStaticEmoji')
}
},
2026-01-06 16:22:52 +02:00
async getKnownDomains({ commit, rootState }) {
try {
const result = await apiService.fetchKnownDomains({
2026-01-06 16:22:52 +02:00
credentials: rootState.users.currentUser.credentials,
})
commit('setKnownDomains', result)
} catch (e) {
console.warn("Can't load known domains\n", e)
}
2026-01-06 16:22:52 +02:00
},
},
2018-09-09 21:21:23 +03:00
}
export default instance