logo

pleroma-fe

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

profile_tab.js (9611B)


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