]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/account/actor.model.ts
Migrate to $localize
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / account / actor.model.ts
1 import { Actor as ActorServer, Avatar } from '@shared/models'
2 import { getAbsoluteAPIUrl } from '@app/helpers'
3
4 export abstract class Actor implements ActorServer {
5 id: number
6 url: string
7 name: string
8 host: string
9 followingCount: number
10 followersCount: number
11 createdAt: Date | string
12 updatedAt: Date | string
13 avatar: Avatar
14
15 avatarUrl: string
16
17 isLocal: boolean
18
19 static GET_ACTOR_AVATAR_URL (actor: { avatar?: { url?: string, path: string } }) {
20 if (actor?.avatar?.url) return actor.avatar.url
21
22 if (actor && actor.avatar) {
23 const absoluteAPIUrl = getAbsoluteAPIUrl()
24
25 return absoluteAPIUrl + actor.avatar.path
26 }
27
28 return this.GET_DEFAULT_AVATAR_URL()
29 }
30
31 static GET_DEFAULT_AVATAR_URL () {
32 return window.location.origin + '/client/assets/images/default-avatar.png'
33 }
34
35 static CREATE_BY_STRING (accountName: string, host: string, forceHostname = false) {
36 const absoluteAPIUrl = getAbsoluteAPIUrl()
37 const thisHost = new URL(absoluteAPIUrl).host
38
39 if (host.trim() === thisHost && !forceHostname) return accountName
40
41 return accountName + '@' + host
42 }
43
44 static IS_LOCAL (host: string) {
45 const absoluteAPIUrl = getAbsoluteAPIUrl()
46 const thisHost = new URL(absoluteAPIUrl).host
47
48 return host.trim() === thisHost
49 }
50
51 protected constructor (hash: ActorServer) {
52 this.id = hash.id
53 this.url = hash.url
54 this.name = hash.name
55 this.host = hash.host
56 this.followingCount = hash.followingCount
57 this.followersCount = hash.followersCount
58
59 if (hash.createdAt) this.createdAt = new Date(hash.createdAt.toString())
60 if (hash.updatedAt) this.updatedAt = new Date(hash.updatedAt.toString())
61
62 this.avatar = hash.avatar
63 this.isLocal = Actor.IS_LOCAL(this.host)
64
65 this.updateComputedAttributes()
66 }
67
68 updateAvatar (newAvatar: Avatar) {
69 this.avatar = newAvatar
70
71 this.updateComputedAttributes()
72 }
73
74 private updateComputedAttributes () {
75 this.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(this)
76 }
77 }