]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/account/actor.model.ts
Implement avatar miniatures (#4639)
[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
16 // TODO: remove, deprecated in 4.2
17 avatar: never
18
19 avatars: ActorImage[]
20
21 isLocal: boolean
22
23 static GET_ACTOR_AVATAR_URL (actor: { avatars: { width: number, url?: string, path: string }[] }, size: number) {
24 const avatar = actor.avatars.sort((a, b) => a.width - b.width).find(a => a.width >= size)
25
26 if (!avatar) return ''
27 if (avatar.url) return avatar.url
28
29 const absoluteAPIUrl = getAbsoluteAPIUrl()
30
31 return absoluteAPIUrl + avatar.path
32 }
33
34 static CREATE_BY_STRING (accountName: string, host: string, forceHostname = false) {
35 const absoluteAPIUrl = getAbsoluteAPIUrl()
36 const thisHost = new URL(absoluteAPIUrl).host
37
38 if (host.trim() === thisHost && !forceHostname) return accountName
39
40 return accountName + '@' + host
41 }
42
43 static IS_LOCAL (host: string) {
44 const absoluteAPIUrl = getAbsoluteAPIUrl()
45 const thisHost = new URL(absoluteAPIUrl).host
46
47 return host.trim() === thisHost
48 }
49
50 protected constructor (hash: Partial<ServerActor>) {
51 this.id = hash.id
52 this.url = hash.url ?? ''
53 this.name = hash.name ?? ''
54 this.host = hash.host ?? ''
55 this.followingCount = hash.followingCount
56 this.followersCount = hash.followersCount
57
58 if (hash.createdAt) this.createdAt = new Date(hash.createdAt.toString())
59
60 this.avatars = hash.avatars
61 this.isLocal = Actor.IS_LOCAL(this.host)
62 }
63 }