]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/actor/actor.model.ts
Feature/completeUsernameCopy (#1913)
[github/Chocobozzz/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 url: string
8 name: string
9 host: string
10 followingCount: number
11 followersCount: number
12 createdAt: Date | string
13 updatedAt: Date | string
14 avatar: Avatar
15
16 avatarUrl: string
17
18 static GET_ACTOR_AVATAR_URL (actor: { avatar?: { path: string } }) {
19 const absoluteAPIUrl = getAbsoluteAPIUrl()
20
21 if (actor && actor.avatar) return absoluteAPIUrl + actor.avatar.path
22
23 return window.location.origin + '/client/assets/images/default-avatar.png'
24 }
25
26 static CREATE_BY_STRING (accountName: string, host: string, forceHostname = false) {
27 const absoluteAPIUrl = getAbsoluteAPIUrl()
28 const thisHost = new URL(absoluteAPIUrl).host
29
30 if (host.trim() === thisHost && !forceHostname) return accountName
31
32 return accountName + '@' + host
33 }
34
35 protected constructor (hash: ActorServer) {
36 this.id = hash.id
37 this.url = hash.url
38 this.name = hash.name
39 this.host = hash.host
40 this.followingCount = hash.followingCount
41 this.followersCount = hash.followersCount
42 this.createdAt = new Date(hash.createdAt.toString())
43 this.updatedAt = new Date(hash.updatedAt.toString())
44 this.avatar = hash.avatar
45
46 this.updateComputedAttributes()
47 }
48
49 updateAvatar (newAvatar: Avatar) {
50 this.avatar = newAvatar
51
52 this.updateComputedAttributes()
53 }
54
55 private updateComputedAttributes () {
56 this.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(this)
57 }
58 }