instance.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import { getPreset, applyTheme } from '../services/style_setter/style_setter.js'
  2. import { CURRENT_VERSION } from '../services/theme_data/theme_data.service.js'
  3. import apiService from '../services/api/api.service.js'
  4. import { instanceDefaultProperties } from './config.js'
  5. import { langCodeToCldrName, ensureFinalFallback } from '../i18n/languages.js'
  6. const SORTED_EMOJI_GROUP_IDS = [
  7. 'smileys-and-emotion',
  8. 'people-and-body',
  9. 'animals-and-nature',
  10. 'food-and-drink',
  11. 'travel-and-places',
  12. 'activities',
  13. 'objects',
  14. 'symbols',
  15. 'flags'
  16. ]
  17. const REGIONAL_INDICATORS = (() => {
  18. const start = 0x1F1E6
  19. const end = 0x1F1FF
  20. const A = 'A'.codePointAt(0)
  21. const res = new Array(end - start + 1)
  22. for (let i = start; i <= end; ++i) {
  23. const letter = String.fromCodePoint(A + i - start)
  24. res[i - start] = {
  25. replacement: String.fromCodePoint(i),
  26. imageUrl: false,
  27. displayText: 'regional_indicator_' + letter,
  28. displayTextI18n: {
  29. key: 'emoji.regional_indicator',
  30. args: { letter }
  31. }
  32. }
  33. }
  34. return res
  35. })()
  36. const REMOTE_INTERACTION_URL = '/main/ostatus'
  37. const defaultState = {
  38. // Stuff from apiConfig
  39. name: 'Pleroma FE',
  40. registrationOpen: true,
  41. server: 'http://localhost:4040/',
  42. textlimit: 5000,
  43. themeData: undefined,
  44. vapidPublicKey: undefined,
  45. // Stuff from static/config.json
  46. alwaysShowSubjectInput: true,
  47. defaultAvatar: '/images/avi.png',
  48. defaultBanner: '/images/banner.png',
  49. background: '/static/aurora_borealis.jpg',
  50. collapseMessageWithSubject: false,
  51. greentext: false,
  52. useAtIcon: false,
  53. mentionLinkDisplay: 'short',
  54. mentionLinkShowTooltip: true,
  55. mentionLinkShowAvatar: false,
  56. mentionLinkFadeDomain: true,
  57. mentionLinkShowYous: false,
  58. mentionLinkBoldenYou: true,
  59. hideFilteredStatuses: false,
  60. // bad name: actually hides posts of muted USERS
  61. hideMutedPosts: false,
  62. hideMutedThreads: true,
  63. hideWordFilteredPosts: false,
  64. hidePostStats: false,
  65. hideBotIndication: false,
  66. hideSitename: false,
  67. hideUserStats: false,
  68. muteBotStatuses: false,
  69. loginMethod: 'password',
  70. logo: '/static/logo.svg',
  71. logoMargin: '.2em',
  72. logoMask: true,
  73. logoLeft: false,
  74. disableUpdateNotification: false,
  75. minimalScopesMode: false,
  76. nsfwCensorImage: undefined,
  77. postContentType: 'text/plain',
  78. redirectRootLogin: '/main/friends',
  79. redirectRootNoLogin: '/main/all',
  80. scopeCopy: true,
  81. showFeaturesPanel: true,
  82. showInstanceSpecificPanel: false,
  83. sidebarRight: false,
  84. subjectLineBehavior: 'email',
  85. theme: 'pleroma-dark',
  86. virtualScrolling: true,
  87. sensitiveByDefault: false,
  88. conversationDisplay: 'linear',
  89. conversationTreeAdvanced: false,
  90. conversationOtherRepliesButton: 'below',
  91. conversationTreeFadeAncestors: false,
  92. maxDepthInThread: 6,
  93. // Nasty stuff
  94. customEmoji: [],
  95. customEmojiFetched: false,
  96. emoji: {},
  97. emojiFetched: false,
  98. unicodeEmojiAnnotations: {},
  99. pleromaBackend: true,
  100. postFormats: [],
  101. restrictedNicknames: [],
  102. safeDM: true,
  103. knownDomains: [],
  104. // Feature-set, apparently, not everything here is reported...
  105. shoutAvailable: false,
  106. pleromaChatMessagesAvailable: false,
  107. gopherAvailable: false,
  108. mediaProxyAvailable: false,
  109. suggestionsEnabled: false,
  110. suggestionsWeb: '',
  111. // Html stuff
  112. instanceSpecificPanelContent: '',
  113. tos: '',
  114. // Version Information
  115. backendVersion: '',
  116. frontendVersion: '',
  117. pollsAvailable: false,
  118. pollLimits: {
  119. max_options: 4,
  120. max_option_chars: 255,
  121. min_expiration: 60,
  122. max_expiration: 60 * 60 * 24
  123. }
  124. }
  125. const loadAnnotations = (lang) => {
  126. return import(
  127. /* webpackChunkName: "emoji-annotations/[request]" */
  128. `@kazvmoe-infra/unicode-emoji-json/annotations/${langCodeToCldrName(lang)}.json`
  129. )
  130. .then(k => k.default)
  131. }
  132. const injectAnnotations = (emoji, annotations) => {
  133. const availableLangs = Object.keys(annotations)
  134. return {
  135. ...emoji,
  136. annotations: availableLangs.reduce((acc, cur) => {
  137. acc[cur] = annotations[cur][emoji.replacement]
  138. return acc
  139. }, {})
  140. }
  141. }
  142. const injectRegionalIndicators = groups => {
  143. groups.symbols.push(...REGIONAL_INDICATORS)
  144. return groups
  145. }
  146. const instance = {
  147. state: defaultState,
  148. mutations: {
  149. setInstanceOption (state, { name, value }) {
  150. if (typeof value !== 'undefined') {
  151. state[name] = value
  152. }
  153. },
  154. setKnownDomains (state, domains) {
  155. state.knownDomains = domains
  156. },
  157. setUnicodeEmojiAnnotations (state, { lang, annotations }) {
  158. state.unicodeEmojiAnnotations[lang] = annotations
  159. }
  160. },
  161. getters: {
  162. instanceDefaultConfig (state) {
  163. return instanceDefaultProperties
  164. .map(key => [key, state[key]])
  165. .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
  166. },
  167. groupedCustomEmojis (state) {
  168. const packsOf = emoji => {
  169. const packs = emoji.tags
  170. .filter(k => k.startsWith('pack:'))
  171. .map(k => {
  172. const packName = k.slice(5) // remove 'pack:' prefix
  173. return {
  174. id: `custom-${packName}`,
  175. text: packName
  176. }
  177. })
  178. if (!packs.length) {
  179. return [{
  180. id: 'unpacked'
  181. }]
  182. } else {
  183. return packs
  184. }
  185. }
  186. return state.customEmoji
  187. .reduce((res, emoji) => {
  188. packsOf(emoji).forEach(({ id: packId, text: packName }) => {
  189. if (!res[packId]) {
  190. res[packId] = ({
  191. id: packId,
  192. text: packName,
  193. image: emoji.imageUrl,
  194. emojis: []
  195. })
  196. }
  197. res[packId].emojis.push(emoji)
  198. })
  199. return res
  200. }, {})
  201. },
  202. standardEmojiList (state) {
  203. return SORTED_EMOJI_GROUP_IDS
  204. .map(groupId => (state.emoji[groupId] || []).map(k => injectAnnotations(k, state.unicodeEmojiAnnotations)))
  205. .reduce((a, b) => a.concat(b), [])
  206. },
  207. standardEmojiGroupList (state) {
  208. return SORTED_EMOJI_GROUP_IDS.map(groupId => ({
  209. id: groupId,
  210. emojis: (state.emoji[groupId] || []).map(k => injectAnnotations(k, state.unicodeEmojiAnnotations))
  211. }))
  212. },
  213. instanceDomain (state) {
  214. return new URL(state.server).hostname
  215. },
  216. remoteInteractionLink (state) {
  217. const server = state.server.endsWith('/') ? state.server.slice(0, -1) : state.server
  218. const link = server + REMOTE_INTERACTION_URL
  219. return ({ statusId, nickname }) => {
  220. if (statusId) {
  221. return `${link}?status_id=${statusId}`
  222. } else {
  223. return `${link}?nickname=${nickname}`
  224. }
  225. }
  226. }
  227. },
  228. actions: {
  229. setInstanceOption ({ commit, dispatch }, { name, value }) {
  230. commit('setInstanceOption', { name, value })
  231. switch (name) {
  232. case 'name':
  233. dispatch('setPageTitle')
  234. break
  235. case 'shoutAvailable':
  236. if (value) {
  237. dispatch('initializeSocket')
  238. }
  239. break
  240. case 'theme':
  241. dispatch('setTheme', value)
  242. break
  243. }
  244. },
  245. async getStaticEmoji ({ commit }) {
  246. try {
  247. const values = (await import(/* webpackChunkName: 'emoji' */ '../../static/emoji.json')).default
  248. const emoji = Object.keys(values).reduce((res, groupId) => {
  249. res[groupId] = values[groupId].map(e => ({
  250. displayText: e.slug,
  251. imageUrl: false,
  252. replacement: e.emoji
  253. }))
  254. return res
  255. }, {})
  256. commit('setInstanceOption', { name: 'emoji', value: injectRegionalIndicators(emoji) })
  257. } catch (e) {
  258. console.warn("Can't load static emoji")
  259. console.warn(e)
  260. }
  261. },
  262. loadUnicodeEmojiData ({ commit, state }, language) {
  263. const langList = ensureFinalFallback(language)
  264. return Promise.all(
  265. langList
  266. .map(async lang => {
  267. if (!state.unicodeEmojiAnnotations[lang]) {
  268. const annotations = await loadAnnotations(lang)
  269. commit('setUnicodeEmojiAnnotations', { lang, annotations })
  270. }
  271. }))
  272. },
  273. async getCustomEmoji ({ commit, state }) {
  274. try {
  275. const res = await window.fetch('/api/pleroma/emoji.json')
  276. if (res.ok) {
  277. const result = await res.json()
  278. const values = Array.isArray(result) ? Object.assign({}, ...result) : result
  279. const caseInsensitiveStrCmp = (a, b) => {
  280. const la = a.toLowerCase()
  281. const lb = b.toLowerCase()
  282. return la > lb ? 1 : (la < lb ? -1 : 0)
  283. }
  284. const noPackLast = (a, b) => {
  285. const aNull = a === ''
  286. const bNull = b === ''
  287. if (aNull === bNull) {
  288. return 0
  289. } else if (aNull && !bNull) {
  290. return 1
  291. } else {
  292. return -1
  293. }
  294. }
  295. const byPackThenByName = (a, b) => {
  296. const packOf = emoji => (emoji.tags.filter(k => k.startsWith('pack:'))[0] || '').slice(5)
  297. const packOfA = packOf(a)
  298. const packOfB = packOf(b)
  299. return noPackLast(packOfA, packOfB) || caseInsensitiveStrCmp(packOfA, packOfB) || caseInsensitiveStrCmp(a.displayText, b.displayText)
  300. }
  301. const emoji = Object.entries(values).map(([key, value]) => {
  302. const imageUrl = value.image_url
  303. return {
  304. displayText: key,
  305. imageUrl: imageUrl ? state.server + imageUrl : value,
  306. tags: imageUrl ? value.tags.sort((a, b) => a > b ? 1 : 0) : ['utf'],
  307. replacement: `:${key}: `
  308. }
  309. // Technically could use tags but those are kinda useless right now,
  310. // should have been "pack" field, that would be more useful
  311. }).sort(byPackThenByName)
  312. commit('setInstanceOption', { name: 'customEmoji', value: emoji })
  313. } else {
  314. throw (res)
  315. }
  316. } catch (e) {
  317. console.warn("Can't load custom emojis")
  318. console.warn(e)
  319. }
  320. },
  321. setTheme ({ commit, rootState }, themeName) {
  322. commit('setInstanceOption', { name: 'theme', value: themeName })
  323. getPreset(themeName)
  324. .then(themeData => {
  325. commit('setInstanceOption', { name: 'themeData', value: themeData })
  326. // No need to apply theme if there's user theme already
  327. const { customTheme } = rootState.config
  328. if (customTheme) return
  329. // New theme presets don't have 'theme' property, they use 'source'
  330. const themeSource = themeData.source
  331. if (!themeData.theme || (themeSource && themeSource.themeEngineVersion === CURRENT_VERSION)) {
  332. applyTheme(themeSource)
  333. } else {
  334. applyTheme(themeData.theme)
  335. }
  336. })
  337. },
  338. fetchEmoji ({ dispatch, state }) {
  339. if (!state.customEmojiFetched) {
  340. state.customEmojiFetched = true
  341. dispatch('getCustomEmoji')
  342. }
  343. if (!state.emojiFetched) {
  344. state.emojiFetched = true
  345. dispatch('getStaticEmoji')
  346. }
  347. },
  348. async getKnownDomains ({ commit, rootState }) {
  349. try {
  350. const result = await apiService.fetchKnownDomains({
  351. credentials: rootState.users.currentUser.credentials
  352. })
  353. commit('setKnownDomains', result)
  354. } catch (e) {
  355. console.warn("Can't load known domains")
  356. console.warn(e)
  357. }
  358. }
  359. }
  360. }
  361. export default instance