logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

instance.js (12454B)


  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. modalOnRepeat: false,
  70. modalOnUnfollow: false,
  71. modalOnBlock: true,
  72. modalOnMute: false,
  73. modalOnDelete: true,
  74. modalOnLogout: true,
  75. modalOnApproveFollow: false,
  76. modalOnDenyFollow: false,
  77. modalOnRemoveUserFromFollowers: false,
  78. loginMethod: 'password',
  79. logo: '/static/logo.svg',
  80. logoMargin: '.2em',
  81. logoMask: true,
  82. logoLeft: false,
  83. disableUpdateNotification: false,
  84. minimalScopesMode: false,
  85. nsfwCensorImage: undefined,
  86. postContentType: 'text/plain',
  87. redirectRootLogin: '/main/friends',
  88. redirectRootNoLogin: '/main/all',
  89. scopeCopy: true,
  90. showFeaturesPanel: true,
  91. showInstanceSpecificPanel: false,
  92. sidebarRight: false,
  93. subjectLineBehavior: 'email',
  94. theme: 'pleroma-dark',
  95. virtualScrolling: true,
  96. sensitiveByDefault: false,
  97. conversationDisplay: 'linear',
  98. conversationTreeAdvanced: false,
  99. conversationOtherRepliesButton: 'below',
  100. conversationTreeFadeAncestors: false,
  101. showExtraNotifications: true,
  102. showExtraNotificationsTip: true,
  103. showChatsInExtraNotifications: true,
  104. showAnnouncementsInExtraNotifications: true,
  105. showFollowRequestsInExtraNotifications: true,
  106. maxDepthInThread: 6,
  107. autocompleteSelect: false,
  108. closingDrawerMarksAsSeen: true,
  109. unseenAtTop: false,
  110. ignoreInactionableSeen: false,
  111. // Nasty stuff
  112. customEmoji: [],
  113. customEmojiFetched: false,
  114. emoji: {},
  115. emojiFetched: false,
  116. unicodeEmojiAnnotations: {},
  117. pleromaBackend: true,
  118. postFormats: [],
  119. restrictedNicknames: [],
  120. safeDM: true,
  121. knownDomains: [],
  122. birthdayRequired: false,
  123. birthdayMinAge: 0,
  124. // Feature-set, apparently, not everything here is reported...
  125. shoutAvailable: false,
  126. pleromaChatMessagesAvailable: false,
  127. pleromaCustomEmojiReactionsAvailable: false,
  128. gopherAvailable: false,
  129. mediaProxyAvailable: false,
  130. suggestionsEnabled: false,
  131. suggestionsWeb: '',
  132. quotingAvailable: false,
  133. groupActorAvailable: false,
  134. // Html stuff
  135. instanceSpecificPanelContent: '',
  136. tos: '',
  137. // Version Information
  138. backendVersion: '',
  139. backendRepository: '',
  140. frontendVersion: '',
  141. pollsAvailable: false,
  142. pollLimits: {
  143. max_options: 4,
  144. max_option_chars: 255,
  145. min_expiration: 60,
  146. max_expiration: 60 * 60 * 24
  147. }
  148. }
  149. const loadAnnotations = (lang) => {
  150. return import(
  151. /* webpackChunkName: "emoji-annotations/[request]" */
  152. `@kazvmoe-infra/unicode-emoji-json/annotations/${langCodeToCldrName(lang)}.json`
  153. )
  154. .then(k => k.default)
  155. }
  156. const injectAnnotations = (emoji, annotations) => {
  157. const availableLangs = Object.keys(annotations)
  158. return {
  159. ...emoji,
  160. annotations: availableLangs.reduce((acc, cur) => {
  161. acc[cur] = annotations[cur][emoji.replacement]
  162. return acc
  163. }, {})
  164. }
  165. }
  166. const injectRegionalIndicators = groups => {
  167. groups.symbols.push(...REGIONAL_INDICATORS)
  168. return groups
  169. }
  170. const instance = {
  171. state: defaultState,
  172. mutations: {
  173. setInstanceOption (state, { name, value }) {
  174. if (typeof value !== 'undefined') {
  175. state[name] = value
  176. }
  177. },
  178. setKnownDomains (state, domains) {
  179. state.knownDomains = domains
  180. },
  181. setUnicodeEmojiAnnotations (state, { lang, annotations }) {
  182. state.unicodeEmojiAnnotations[lang] = annotations
  183. }
  184. },
  185. getters: {
  186. instanceDefaultConfig (state) {
  187. return instanceDefaultProperties
  188. .map(key => [key, state[key]])
  189. .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
  190. },
  191. groupedCustomEmojis (state) {
  192. const packsOf = emoji => {
  193. const packs = emoji.tags
  194. .filter(k => k.startsWith('pack:'))
  195. .map(k => {
  196. const packName = k.slice(5) // remove 'pack:' prefix
  197. return {
  198. id: `custom-${packName}`,
  199. text: packName
  200. }
  201. })
  202. if (!packs.length) {
  203. return [{
  204. id: 'unpacked'
  205. }]
  206. } else {
  207. return packs
  208. }
  209. }
  210. return state.customEmoji
  211. .reduce((res, emoji) => {
  212. packsOf(emoji).forEach(({ id: packId, text: packName }) => {
  213. if (!res[packId]) {
  214. res[packId] = ({
  215. id: packId,
  216. text: packName,
  217. image: emoji.imageUrl,
  218. emojis: []
  219. })
  220. }
  221. res[packId].emojis.push(emoji)
  222. })
  223. return res
  224. }, {})
  225. },
  226. standardEmojiList (state) {
  227. return SORTED_EMOJI_GROUP_IDS
  228. .map(groupId => (state.emoji[groupId] || []).map(k => injectAnnotations(k, state.unicodeEmojiAnnotations)))
  229. .reduce((a, b) => a.concat(b), [])
  230. },
  231. standardEmojiGroupList (state) {
  232. return SORTED_EMOJI_GROUP_IDS.map(groupId => ({
  233. id: groupId,
  234. emojis: (state.emoji[groupId] || []).map(k => injectAnnotations(k, state.unicodeEmojiAnnotations))
  235. }))
  236. },
  237. instanceDomain (state) {
  238. return new URL(state.server).hostname
  239. },
  240. remoteInteractionLink (state) {
  241. const server = state.server.endsWith('/') ? state.server.slice(0, -1) : state.server
  242. const link = server + REMOTE_INTERACTION_URL
  243. return ({ statusId, nickname }) => {
  244. if (statusId) {
  245. return `${link}?status_id=${statusId}`
  246. } else {
  247. return `${link}?nickname=${nickname}`
  248. }
  249. }
  250. }
  251. },
  252. actions: {
  253. setInstanceOption ({ commit, dispatch }, { name, value }) {
  254. commit('setInstanceOption', { name, value })
  255. switch (name) {
  256. case 'name':
  257. dispatch('setPageTitle')
  258. break
  259. case 'shoutAvailable':
  260. if (value) {
  261. dispatch('initializeSocket')
  262. }
  263. break
  264. case 'theme':
  265. dispatch('setTheme', value)
  266. break
  267. }
  268. },
  269. async getStaticEmoji ({ commit }) {
  270. try {
  271. const values = (await import(/* webpackChunkName: 'emoji' */ '../../static/emoji.json')).default
  272. const emoji = Object.keys(values).reduce((res, groupId) => {
  273. res[groupId] = values[groupId].map(e => ({
  274. displayText: e.slug,
  275. imageUrl: false,
  276. replacement: e.emoji
  277. }))
  278. return res
  279. }, {})
  280. commit('setInstanceOption', { name: 'emoji', value: injectRegionalIndicators(emoji) })
  281. } catch (e) {
  282. console.warn("Can't load static emoji")
  283. console.warn(e)
  284. }
  285. },
  286. loadUnicodeEmojiData ({ commit, state }, language) {
  287. const langList = ensureFinalFallback(language)
  288. return Promise.all(
  289. langList
  290. .map(async lang => {
  291. if (!state.unicodeEmojiAnnotations[lang]) {
  292. try {
  293. const annotations = await loadAnnotations(lang)
  294. commit('setUnicodeEmojiAnnotations', { lang, annotations })
  295. } catch (e) {
  296. console.warn(`Error loading unicode emoji annotations for ${lang}: `, e)
  297. // ignore
  298. }
  299. }
  300. }))
  301. },
  302. async getCustomEmoji ({ commit, state }) {
  303. try {
  304. const res = await window.fetch('/api/pleroma/emoji.json')
  305. if (res.ok) {
  306. const result = await res.json()
  307. const values = Array.isArray(result) ? Object.assign({}, ...result) : result
  308. const caseInsensitiveStrCmp = (a, b) => {
  309. const la = a.toLowerCase()
  310. const lb = b.toLowerCase()
  311. return la > lb ? 1 : (la < lb ? -1 : 0)
  312. }
  313. const noPackLast = (a, b) => {
  314. const aNull = a === ''
  315. const bNull = b === ''
  316. if (aNull === bNull) {
  317. return 0
  318. } else if (aNull && !bNull) {
  319. return 1
  320. } else {
  321. return -1
  322. }
  323. }
  324. const byPackThenByName = (a, b) => {
  325. const packOf = emoji => (emoji.tags.filter(k => k.startsWith('pack:'))[0] || '').slice(5)
  326. const packOfA = packOf(a)
  327. const packOfB = packOf(b)
  328. return noPackLast(packOfA, packOfB) || caseInsensitiveStrCmp(packOfA, packOfB) || caseInsensitiveStrCmp(a.displayText, b.displayText)
  329. }
  330. const emoji = Object.entries(values).map(([key, value]) => {
  331. const imageUrl = value.image_url
  332. return {
  333. displayText: key,
  334. imageUrl: imageUrl ? state.server + imageUrl : value,
  335. tags: imageUrl ? value.tags.sort((a, b) => a > b ? 1 : 0) : ['utf'],
  336. replacement: `:${key}: `
  337. }
  338. // Technically could use tags but those are kinda useless right now,
  339. // should have been "pack" field, that would be more useful
  340. }).sort(byPackThenByName)
  341. commit('setInstanceOption', { name: 'customEmoji', value: emoji })
  342. } else {
  343. throw (res)
  344. }
  345. } catch (e) {
  346. console.warn("Can't load custom emojis")
  347. console.warn(e)
  348. }
  349. },
  350. setTheme ({ commit, rootState }, themeName) {
  351. commit('setInstanceOption', { name: 'theme', value: themeName })
  352. getPreset(themeName)
  353. .then(themeData => {
  354. commit('setInstanceOption', { name: 'themeData', value: themeData })
  355. // No need to apply theme if there's user theme already
  356. const { customTheme } = rootState.config
  357. if (customTheme) return
  358. // New theme presets don't have 'theme' property, they use 'source'
  359. const themeSource = themeData.source
  360. if (!themeData.theme || (themeSource && themeSource.themeEngineVersion === CURRENT_VERSION)) {
  361. applyTheme(themeSource)
  362. } else {
  363. applyTheme(themeData.theme)
  364. }
  365. commit('setThemeApplied')
  366. })
  367. },
  368. fetchEmoji ({ dispatch, state }) {
  369. if (!state.customEmojiFetched) {
  370. state.customEmojiFetched = true
  371. dispatch('getCustomEmoji')
  372. }
  373. if (!state.emojiFetched) {
  374. state.emojiFetched = true
  375. dispatch('getStaticEmoji')
  376. }
  377. },
  378. async getKnownDomains ({ commit, rootState }) {
  379. try {
  380. const result = await apiService.fetchKnownDomains({
  381. credentials: rootState.users.currentUser.credentials
  382. })
  383. commit('setKnownDomains', result)
  384. } catch (e) {
  385. console.warn("Can't load known domains")
  386. console.warn(e)
  387. }
  388. }
  389. }
  390. }
  391. export default instance