1 import { Actor as ActorServer, Avatar } from '@shared/models'
2 import { getAbsoluteAPIUrl } from '@app/helpers'
4 export abstract class Actor implements ActorServer {
10 followersCount: number
11 createdAt: Date | string
12 updatedAt: Date | string
19 static GET_ACTOR_AVATAR_URL (actor: { avatar?: { url?: string, path: string } }) {
20 if (actor?.avatar?.url) return actor.avatar.url
22 if (actor && actor.avatar) {
23 const absoluteAPIUrl = getAbsoluteAPIUrl()
25 return absoluteAPIUrl + actor.avatar.path
28 return this.GET_DEFAULT_AVATAR_URL()
31 static GET_DEFAULT_AVATAR_URL () {
32 return window.location.origin + '/client/assets/images/default-avatar.png'
35 static CREATE_BY_STRING (accountName: string, host: string, forceHostname = false) {
36 const absoluteAPIUrl = getAbsoluteAPIUrl()
37 const thisHost = new URL(absoluteAPIUrl).host
39 if (host.trim() === thisHost && !forceHostname) return accountName
41 return accountName + '@' + host
44 protected constructor (hash: ActorServer) {
49 this.followingCount = hash.followingCount
50 this.followersCount = hash.followersCount
52 if (hash.createdAt) this.createdAt = new Date(hash.createdAt.toString())
53 if (hash.updatedAt) this.updatedAt = new Date(hash.updatedAt.toString())
55 this.avatar = hash.avatar
57 const absoluteAPIUrl = getAbsoluteAPIUrl()
58 const thisHost = new URL(absoluteAPIUrl).host
59 this.isLocal = this.host.trim() === thisHost
61 this.updateComputedAttributes()
64 updateAvatar (newAvatar: Avatar) {
65 this.avatar = newAvatar
67 this.updateComputedAttributes()
70 private updateComputedAttributes () {
71 this.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(this)