]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/account/actor.model.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / account / actor.model.ts
1 import { getAbsoluteAPIUrl } from '@app/helpers'
2 import { Actor as ServerActor, ActorImage } from '@shared/models'
3
4 export abstract class Actor implements ServerActor {
5 id: number
6 name: string
7
8 host: string
9 url: string
10
11 followingCount: number
12 followersCount: number
13
14 createdAt: Date | string
15 updatedAt: Date | string
16
17 avatar: ActorImage
18
19 isLocal: boolean
20
21 static GET_ACTOR_AVATAR_URL (actor: { avatar?: { url?: string, path: string } }) {
22 if (actor?.avatar?.url) return actor.avatar.url
23
24 if (actor && actor.avatar) {
25 const absoluteAPIUrl = getAbsoluteAPIUrl()
26
27 return absoluteAPIUrl + actor.avatar.path
28 }
29
30 return ''
31 }
32
33 static CREATE_BY_STRING (accountName: string, host: string, forceHostname = false) {
34 const absoluteAPIUrl = getAbsoluteAPIUrl()
35 const thisHost = new URL(absoluteAPIUrl).host
36
37 if (host.trim() === thisHost && !forceHostname) return accountName
38
39 return accountName + '@' + host
40 }
41
42 static IS_LOCAL (host: string) {
43 const absoluteAPIUrl = getAbsoluteAPIUrl()
44 const thisHost = new URL(absoluteAPIUrl).host
45
46 return host.trim() === thisHost
47 }
48
49 protected constructor (hash: Partial<ServerActor>) {
50 this.id = hash.id
51 this.url = hash.url ?? ''
52 this.name = hash.name ?? ''
53 this.host = hash.host ?? ''
54 this.followingCount = hash.followingCount
55 this.followersCount = hash.followersCount
56
57 if (hash.createdAt) this.createdAt = new Date(hash.createdAt.toString())
58 if (hash.updatedAt) this.updatedAt = new Date(hash.updatedAt.toString())
59
60 this.avatar = hash.avatar
61 this.isLocal = Actor.IS_LOCAL(this.host)
62 }
63 }