Client: Add ability to update video channel avatar
[oweals/peertube.git] / client / src / app / shared / actor / actor.model.ts
1 import { Actor as ActorServer } from '../../../../../shared/models/actors/actor.model'
2 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
3 import { getAbsoluteAPIUrl } from '@app/shared/misc/utils'
4
5 export abstract class Actor implements ActorServer {
6   id: number
7   uuid: string
8   url: string
9   name: string
10   host: string
11   followingCount: number
12   followersCount: number
13   createdAt: Date | string
14   updatedAt: Date | string
15   avatar: Avatar
16
17   avatarUrl: string
18
19   static GET_ACTOR_AVATAR_URL (actor: { avatar: Avatar }) {
20     const absoluteAPIUrl = getAbsoluteAPIUrl()
21
22     if (actor && actor.avatar) return absoluteAPIUrl + actor.avatar.path
23
24     return window.location.origin + '/client/assets/images/default-avatar.png'
25   }
26
27   static CREATE_BY_STRING (accountName: string, host: string) {
28     const absoluteAPIUrl = getAbsoluteAPIUrl()
29     const thisHost = new URL(absoluteAPIUrl).host
30
31     if (host.trim() === thisHost) return accountName
32
33     return accountName + '@' + host
34   }
35
36   protected constructor (hash: ActorServer) {
37     this.id = hash.id
38     this.uuid = hash.uuid
39     this.url = hash.url
40     this.name = hash.name
41     this.host = hash.host
42     this.followingCount = hash.followingCount
43     this.followersCount = hash.followersCount
44     this.createdAt = new Date(hash.createdAt.toString())
45     this.updatedAt = new Date(hash.updatedAt.toString())
46     this.avatar = hash.avatar
47
48     this.updateComputedAttributes()
49   }
50
51   updateAvatar (newAvatar: Avatar) {
52     this.avatar = newAvatar
53
54     this.updateComputedAttributes()
55   }
56
57   private updateComputedAttributes () {
58     this.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(this)
59   }
60 }