]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-actor-image/actor-avatar.component.ts
Fix displaying remote actor avatars
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-actor-image / actor-avatar.component.ts
CommitLineData
e2d8587b 1import { Component, Input, OnChanges, OnInit } from '@angular/core'
746018f6
C
2import { VideoChannel } from '../shared-main'
3import { Account } from '../shared-main/account/account.model'
4
5type ActorInput = {
6 name: string
d0800f76 7 avatars: { width: number, url?: string, path: string }[]
746018f6
C
8 url: string
9}
10
d0800f76 11export type ActorAvatarSize = '18' | '25' | '28' | '32' | '34' | '35' | '36' | '40' | '48' | '75' | '80' | '100' | '120'
06ec4bdd 12
746018f6
C
13@Component({
14 selector: 'my-actor-avatar',
15 styleUrls: [ './actor-avatar.component.scss' ],
16 templateUrl: './actor-avatar.component.html'
17})
e2d8587b 18export class ActorAvatarComponent implements OnInit, OnChanges {
9df52d66
C
19 private _title: string
20
87fdea2f
C
21 @Input() actor: ActorInput
22 @Input() actorType: 'channel' | 'account' | 'unlogged'
746018f6 23
0ea2f79d 24 @Input() previewImage: string
746018f6 25
4428ad54 26 @Input() size: ActorAvatarSize
746018f6
C
27
28 // Use an external link
29 @Input() href: string
30 // Use routerLink
31 @Input() internalHref: string | any[]
32
33 @Input() set title (value) {
34 this._title = value
35 }
36
746018f6
C
37 get title () {
38 if (this._title) return this._title
87fdea2f
C
39 if (this.isAccount()) return $localize`${this.actor.name} (account page)`
40 if (this.isChannel()) return $localize`${this.actor.name} (channel page)`
746018f6
C
41
42 return ''
43 }
44
5b0ec7cd 45 classes: string[] = []
e2d8587b
C
46 alt: string
47 defaultAvatarUrl: string
48 avatarUrl: string
5b0ec7cd 49
e2d8587b
C
50 ngOnInit () {
51 this.buildDefaultAvatarUrl()
746018f6 52
e2d8587b
C
53 this.buildClasses()
54 this.buildAlt()
55 this.buildAvatarUrl()
746018f6
C
56 }
57
e2d8587b
C
58 ngOnChanges () {
59 this.buildClasses()
60 this.buildAlt()
61 this.buildAvatarUrl()
746018f6
C
62 }
63
e2d8587b 64 private buildClasses () {
5b0ec7cd 65 this.classes = [ 'avatar' ]
9df52d66 66
87fdea2f
C
67 if (this.size) {
68 this.classes.push(`avatar-${this.size}`)
69 }
9df52d66 70
87fdea2f
C
71 if (this.isChannel()) {
72 this.classes.push('channel')
73 } else {
74 this.classes.push('account')
75 }
9df52d66 76
87fdea2f
C
77 // No avatar, use actor name initial
78 if (this.displayActorInitial()) {
5b0ec7cd
C
79 this.classes.push('initial')
80 this.classes.push(this.getColorTheme())
9df52d66 81 }
9df52d66
C
82 }
83
e2d8587b
C
84 private buildAlt () {
85 if (this.isAccount()) this.alt = $localize`Account avatar`
86 else if (this.isChannel()) this.alt = $localize`Channel avatar`
87 else this.alt = ''
88 }
89
90 private buildDefaultAvatarUrl () {
91 this.defaultAvatarUrl = this.isChannel()
92 ? VideoChannel.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
93 : Account.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
94 }
95
96 private buildAvatarUrl () {
97 if (!this.actor) {
98 this.avatarUrl = ''
99 return
100 }
101
102 if (this.isAccount()) {
103 this.avatarUrl = Account.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber())
104 return
105 }
106
107 if (this.isChannel()) {
108 this.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber())
109 return
110 }
111
112 this.avatarUrl = ''
113 }
114
87fdea2f
C
115 displayImage () {
116 if (this.actorType === 'unlogged') return true
117
118 return !!(this.actor && this.avatarUrl)
119 }
120
121 displayActorInitial () {
122 return this.actor && !this.avatarUrl
123 }
124
125 displayPlaceholder () {
126 return this.actorType !== 'unlogged' && !this.actor
127 }
128
129 getActorInitial () {
130 const name = this.actor?.name
131 if (!name) return ''
132
133 return name.slice(0, 1)
134 }
135
136 private isAccount () {
137 return this.actorType === 'account'
138 }
139
140 private isChannel () {
141 return this.actorType === 'channel'
746018f6
C
142 }
143
4428ad54
C
144 private getSizeNumber () {
145 if (this.size) return +this.size
146
147 return undefined
148 }
149
746018f6 150 private getColorTheme () {
87fdea2f 151 const initialLowercase = this.getActorInitial().toLowerCase()
f41efa52 152
746018f6
C
153 // Keep consistency with CSS
154 const themes = {
f41efa52 155 '0123456789abc': 'blue',
746018f6
C
156 def: 'green',
157 ghi: 'purple',
158 jkl: 'gray',
159 mno: 'yellow',
160 pqr: 'orange',
1fd61899 161 stvu: 'red',
746018f6
C
162 wxyz: 'dark-blue'
163 }
164
165 const theme = Object.keys(themes)
f41efa52 166 .find(chars => chars.includes(initialLowercase))
746018f6 167
dbf49527 168 return themes[theme] || 'blue'
746018f6
C
169 }
170}