logo

pleroma-fe

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

profile_tab.js (9535B)


  1. import unescape from 'lodash/unescape'
  2. import merge from 'lodash/merge'
  3. import ImageCropper from 'src/components/image_cropper/image_cropper.vue'
  4. import ScopeSelector from 'src/components/scope_selector/scope_selector.vue'
  5. import fileSizeFormatService from 'src/components/../services/file_size_format/file_size_format.js'
  6. import ProgressButton from 'src/components/progress_button/progress_button.vue'
  7. import EmojiInput from 'src/components/emoji_input/emoji_input.vue'
  8. import suggestor from 'src/components/emoji_input/suggestor.js'
  9. import Autosuggest from 'src/components/autosuggest/autosuggest.vue'
  10. import Checkbox from 'src/components/checkbox/checkbox.vue'
  11. import InterfaceLanguageSwitcher from 'src/components/interface_language_switcher/interface_language_switcher.vue'
  12. import Select from 'src/components/select/select.vue'
  13. import BooleanSetting from '../helpers/boolean_setting.vue'
  14. import SharedComputedObject from '../helpers/shared_computed_object.js'
  15. import localeService from 'src/services/locale/locale.service.js'
  16. import { propsToNative } from 'src/services/attributes_helper/attributes_helper.service.js'
  17. import { library } from '@fortawesome/fontawesome-svg-core'
  18. import {
  19. faTimes,
  20. faPlus,
  21. faCircleNotch
  22. } from '@fortawesome/free-solid-svg-icons'
  23. import { useInterfaceStore } from 'src/stores/interface'
  24. library.add(
  25. faTimes,
  26. faPlus,
  27. faCircleNotch
  28. )
  29. const ProfileTab = {
  30. data () {
  31. return {
  32. newName: this.$store.state.users.currentUser.name_unescaped,
  33. newBio: unescape(this.$store.state.users.currentUser.description),
  34. newLocked: this.$store.state.users.currentUser.locked,
  35. newBirthday: this.$store.state.users.currentUser.birthday,
  36. showBirthday: this.$store.state.users.currentUser.show_birthday,
  37. newFields: this.$store.state.users.currentUser.fields.map(field => ({ name: field.name, value: field.value })),
  38. showRole: this.$store.state.users.currentUser.show_role,
  39. role: this.$store.state.users.currentUser.role,
  40. bot: this.$store.state.users.currentUser.bot,
  41. actorType: this.$store.state.users.currentUser.actor_type,
  42. pickAvatarBtnVisible: true,
  43. bannerUploading: false,
  44. backgroundUploading: false,
  45. banner: null,
  46. bannerPreview: null,
  47. background: null,
  48. backgroundPreview: null,
  49. emailLanguage: this.$store.state.users.currentUser.language || ['']
  50. }
  51. },
  52. components: {
  53. ScopeSelector,
  54. ImageCropper,
  55. EmojiInput,
  56. Autosuggest,
  57. ProgressButton,
  58. Checkbox,
  59. BooleanSetting,
  60. InterfaceLanguageSwitcher,
  61. Select
  62. },
  63. computed: {
  64. user () {
  65. return this.$store.state.users.currentUser
  66. },
  67. ...SharedComputedObject(),
  68. emojiUserSuggestor () {
  69. return suggestor({
  70. emoji: [
  71. ...this.$store.getters.standardEmojiList,
  72. ...this.$store.state.instance.customEmoji
  73. ],
  74. store: this.$store
  75. })
  76. },
  77. emojiSuggestor () {
  78. return suggestor({
  79. emoji: [
  80. ...this.$store.getters.standardEmojiList,
  81. ...this.$store.state.instance.customEmoji
  82. ]
  83. })
  84. },
  85. userSuggestor () {
  86. return suggestor({ store: this.$store })
  87. },
  88. fieldsLimits () {
  89. return this.$store.state.instance.fieldsLimits
  90. },
  91. maxFields () {
  92. return this.fieldsLimits ? this.fieldsLimits.maxFields : 0
  93. },
  94. defaultAvatar () {
  95. return this.$store.state.instance.server + this.$store.state.instance.defaultAvatar
  96. },
  97. defaultBanner () {
  98. return this.$store.state.instance.server + this.$store.state.instance.defaultBanner
  99. },
  100. isDefaultAvatar () {
  101. const baseAvatar = this.$store.state.instance.defaultAvatar
  102. return !(this.$store.state.users.currentUser.profile_image_url) ||
  103. this.$store.state.users.currentUser.profile_image_url.includes(baseAvatar)
  104. },
  105. isDefaultBanner () {
  106. const baseBanner = this.$store.state.instance.defaultBanner
  107. return !(this.$store.state.users.currentUser.cover_photo) ||
  108. this.$store.state.users.currentUser.cover_photo.includes(baseBanner)
  109. },
  110. isDefaultBackground () {
  111. return !(this.$store.state.users.currentUser.background_image)
  112. },
  113. avatarImgSrc () {
  114. const src = this.$store.state.users.currentUser.profile_image_url_original
  115. return (!src) ? this.defaultAvatar : src
  116. },
  117. bannerImgSrc () {
  118. const src = this.$store.state.users.currentUser.cover_photo
  119. return (!src) ? this.defaultBanner : src
  120. },
  121. groupActorAvailable () {
  122. return this.$store.state.instance.groupActorAvailable
  123. },
  124. availableActorTypes () {
  125. return this.groupActorAvailable ? ['Person', 'Service', 'Group'] : ['Person', 'Service']
  126. }
  127. },
  128. methods: {
  129. updateProfile () {
  130. const params = {
  131. note: this.newBio,
  132. locked: this.newLocked,
  133. // Backend notation.
  134. display_name: this.newName,
  135. fields_attributes: this.newFields.filter(el => el != null),
  136. actor_type: this.actorType,
  137. show_role: this.showRole,
  138. birthday: this.newBirthday || '',
  139. show_birthday: this.showBirthday
  140. }
  141. if (this.emailLanguage) {
  142. params.language = localeService.internalToBackendLocaleMulti(this.emailLanguage)
  143. }
  144. this.$store.state.api.backendInteractor
  145. .updateProfile({ params })
  146. .then((user) => {
  147. this.newFields.splice(user.fields.length)
  148. merge(this.newFields, user.fields)
  149. this.$store.commit('addNewUsers', [user])
  150. this.$store.commit('setCurrentUser', user)
  151. })
  152. },
  153. changeVis (visibility) {
  154. this.newDefaultScope = visibility
  155. },
  156. addField () {
  157. if (this.newFields.length < this.maxFields) {
  158. this.newFields.push({ name: '', value: '' })
  159. return true
  160. }
  161. return false
  162. },
  163. deleteField (index) {
  164. this.newFields.splice(index, 1)
  165. },
  166. uploadFile (slot, e) {
  167. const file = e.target.files[0]
  168. if (!file) { return }
  169. if (file.size > this.$store.state.instance[slot + 'limit']) {
  170. const filesize = fileSizeFormatService.fileSizeFormat(file.size)
  171. const allowedsize = fileSizeFormatService.fileSizeFormat(this.$store.state.instance[slot + 'limit'])
  172. useInterfaceStore().pushGlobalNotice({
  173. messageKey: 'upload.error.message',
  174. messageArgs: [
  175. this.$t('upload.error.file_too_big', {
  176. filesize: filesize.num,
  177. filesizeunit: filesize.unit,
  178. allowedsize: allowedsize.num,
  179. allowedsizeunit: allowedsize.unit
  180. })
  181. ],
  182. level: 'error'
  183. })
  184. return
  185. }
  186. const reader = new FileReader()
  187. reader.onload = ({ target }) => {
  188. const img = target.result
  189. this[slot + 'Preview'] = img
  190. this[slot] = file
  191. }
  192. reader.readAsDataURL(file)
  193. },
  194. resetAvatar () {
  195. const confirmed = window.confirm(this.$t('settings.reset_avatar_confirm'))
  196. if (confirmed) {
  197. this.submitAvatar(undefined, '')
  198. }
  199. },
  200. resetBanner () {
  201. const confirmed = window.confirm(this.$t('settings.reset_banner_confirm'))
  202. if (confirmed) {
  203. this.submitBanner('')
  204. }
  205. },
  206. resetBackground () {
  207. const confirmed = window.confirm(this.$t('settings.reset_background_confirm'))
  208. if (confirmed) {
  209. this.submitBackground('')
  210. }
  211. },
  212. submitAvatar (cropper, file) {
  213. const that = this
  214. return new Promise((resolve, reject) => {
  215. function updateAvatar (avatar, avatarName) {
  216. that.$store.state.api.backendInteractor.updateProfileImages({ avatar, avatarName })
  217. .then((user) => {
  218. that.$store.commit('addNewUsers', [user])
  219. that.$store.commit('setCurrentUser', user)
  220. resolve()
  221. })
  222. .catch((error) => {
  223. that.displayUploadError(error)
  224. reject(error)
  225. })
  226. }
  227. if (cropper) {
  228. cropper.getCroppedCanvas().toBlob((data) => updateAvatar(data, file.name), file.type)
  229. } else {
  230. updateAvatar(file, file.name)
  231. }
  232. })
  233. },
  234. submitBanner (banner) {
  235. if (!this.bannerPreview && banner !== '') { return }
  236. this.bannerUploading = true
  237. this.$store.state.api.backendInteractor.updateProfileImages({ banner })
  238. .then((user) => {
  239. this.$store.commit('addNewUsers', [user])
  240. this.$store.commit('setCurrentUser', user)
  241. this.bannerPreview = null
  242. })
  243. .catch(this.displayUploadError)
  244. .finally(() => { this.bannerUploading = false })
  245. },
  246. submitBackground (background) {
  247. if (!this.backgroundPreview && background !== '') { return }
  248. this.backgroundUploading = true
  249. this.$store.state.api.backendInteractor.updateProfileImages({ background })
  250. .then((data) => {
  251. this.$store.commit('addNewUsers', [data])
  252. this.$store.commit('setCurrentUser', data)
  253. this.backgroundPreview = null
  254. })
  255. .catch(this.displayUploadError)
  256. .finally(() => { this.backgroundUploading = false })
  257. },
  258. displayUploadError (error) {
  259. useInterfaceStore().pushGlobalNotice({
  260. messageKey: 'upload.error.message',
  261. messageArgs: [error.message],
  262. level: 'error'
  263. })
  264. },
  265. propsToNative (props) {
  266. return propsToNative(props)
  267. }
  268. }
  269. }
  270. export default ProfileTab