diff options
author | Chocobozzz <me@florianbigard.com> | 2020-06-23 14:10:17 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-06-23 16:00:49 +0200 |
commit | 67ed6552b831df66713bac9e672738796128d33f (patch) | |
tree | 59c97d41e0b49d75a90aa3de987968ab9b1ff447 /client/src/app/shared/shared-main/account/actor.model.ts | |
parent | 0c4bacbff53bc732f5a2677d62a6ead7752e2405 (diff) | |
download | PeerTube-67ed6552b831df66713bac9e672738796128d33f.tar.gz PeerTube-67ed6552b831df66713bac9e672738796128d33f.tar.zst PeerTube-67ed6552b831df66713bac9e672738796128d33f.zip |
Reorganize client shared modules
Diffstat (limited to 'client/src/app/shared/shared-main/account/actor.model.ts')
-rw-r--r-- | client/src/app/shared/shared-main/account/actor.model.ts | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/client/src/app/shared/shared-main/account/actor.model.ts b/client/src/app/shared/shared-main/account/actor.model.ts new file mode 100644 index 000000000..5fc7989dd --- /dev/null +++ b/client/src/app/shared/shared-main/account/actor.model.ts | |||
@@ -0,0 +1,65 @@ | |||
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 | static GET_ACTOR_AVATAR_URL (actor: { avatar?: Avatar }) { | ||
18 | if (actor?.avatar?.url) return actor.avatar.url | ||
19 | |||
20 | if (actor && actor.avatar) { | ||
21 | const absoluteAPIUrl = getAbsoluteAPIUrl() | ||
22 | |||
23 | return absoluteAPIUrl + actor.avatar.path | ||
24 | } | ||
25 | |||
26 | return this.GET_DEFAULT_AVATAR_URL() | ||
27 | } | ||
28 | |||
29 | static GET_DEFAULT_AVATAR_URL () { | ||
30 | return window.location.origin + '/client/assets/images/default-avatar.png' | ||
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 | protected constructor (hash: ActorServer) { | ||
43 | this.id = hash.id | ||
44 | this.url = hash.url | ||
45 | this.name = hash.name | ||
46 | this.host = hash.host | ||
47 | this.followingCount = hash.followingCount | ||
48 | this.followersCount = hash.followersCount | ||
49 | this.createdAt = new Date(hash.createdAt.toString()) | ||
50 | this.updatedAt = new Date(hash.updatedAt.toString()) | ||
51 | this.avatar = hash.avatar | ||
52 | |||
53 | this.updateComputedAttributes() | ||
54 | } | ||
55 | |||
56 | updateAvatar (newAvatar: Avatar) { | ||
57 | this.avatar = newAvatar | ||
58 | |||
59 | this.updateComputedAttributes() | ||
60 | } | ||
61 | |||
62 | private updateComputedAttributes () { | ||
63 | this.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(this) | ||
64 | } | ||
65 | } | ||