]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/activitypub/actor.ts
Fix notification scrollbar color
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor.ts
CommitLineData
50d6de9c 1import { values } from 'lodash'
47564bbe 2import { extname } from 'path'
b49f22d8 3import { literal, Op, Transaction } from 'sequelize'
fadf619a 4import {
2422c46b
C
5 AllowNull,
6 BelongsTo,
7 Column,
8 CreatedAt,
9 DataType,
2422c46b
C
10 DefaultScope,
11 ForeignKey,
12 HasMany,
13 HasOne,
14 Is,
2422c46b
C
15 Model,
16 Scopes,
17 Table,
18 UpdatedAt
fadf619a 19} from 'sequelize-typescript'
b49f22d8 20import { ModelCache } from '@server/models/model-cache'
a1587156 21import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub'
fadf619a 22import { Avatar } from '../../../shared/models/avatars/avatar.model'
da854ddd 23import { activityPubContextify } from '../../helpers/activitypub'
fadf619a 24import {
2422c46b
C
25 isActorFollowersCountValid,
26 isActorFollowingCountValid,
27 isActorPreferredUsernameValid,
28 isActorPrivateKeyValid,
da854ddd
C
29 isActorPublicKeyValid
30} from '../../helpers/custom-validators/activitypub/actor'
31import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
e4a686b4 32import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
1ca9f7c3
C
33import {
34 MActor,
35 MActorAccountChannelId,
b5fecbf4 36 MActorAP,
1ca9f7c3 37 MActorFormattable,
b5fecbf4
C
38 MActorFull,
39 MActorHost,
1ca9f7c3 40 MActorServer,
b49f22d8
C
41 MActorSummaryFormattable,
42 MActorUrl,
47581df0 43 MActorWithInboxes
26d6bf65 44} from '../../types/models'
b49f22d8
C
45import { AccountModel } from '../account/account'
46import { AvatarModel } from '../avatar/avatar'
47import { ServerModel } from '../server/server'
48import { isOutdated, throwIfNotValid } from '../utils'
49import { VideoModel } from '../video/video'
50import { VideoChannelModel } from '../video/video-channel'
51import { ActorFollowModel } from './actor-follow'
fadf619a 52
50d6de9c
C
53enum ScopeNames {
54 FULL = 'FULL'
55}
56
f37dc0dd
C
57export const unusedActorAttributesForAPI = [
58 'publicKey',
59 'privateKey',
60 'inboxUrl',
61 'outboxUrl',
62 'sharedInboxUrl',
63 'followersUrl',
aa55a4da 64 'followingUrl',
f5b0af50
C
65 'createdAt',
66 'updatedAt'
f37dc0dd
C
67]
68
3acc5084 69@DefaultScope(() => ({
ce33ee01
C
70 include: [
71 {
3acc5084 72 model: ServerModel,
ce33ee01 73 required: false
c5911fd3
C
74 },
75 {
3acc5084 76 model: AvatarModel,
c5911fd3 77 required: false
ce33ee01
C
78 }
79 ]
3acc5084
C
80}))
81@Scopes(() => ({
50d6de9c
C
82 [ScopeNames.FULL]: {
83 include: [
84 {
3acc5084 85 model: AccountModel.unscoped(),
50d6de9c
C
86 required: false
87 },
88 {
3acc5084 89 model: VideoChannelModel.unscoped(),
c48e82b5
C
90 required: false,
91 include: [
92 {
3acc5084 93 model: AccountModel,
c48e82b5
C
94 required: true
95 }
96 ]
ce33ee01
C
97 },
98 {
3acc5084 99 model: ServerModel,
ce33ee01 100 required: false
c5911fd3
C
101 },
102 {
3acc5084 103 model: AvatarModel,
c5911fd3 104 required: false
50d6de9c 105 }
3acc5084 106 ]
50d6de9c 107 }
3acc5084 108}))
fadf619a 109@Table({
50d6de9c
C
110 tableName: 'actor',
111 indexes: [
2ccaeeb3 112 {
8cd72bd3
C
113 fields: [ 'url' ],
114 unique: true
2ccaeeb3 115 },
50d6de9c 116 {
e12a0092 117 fields: [ 'preferredUsername', 'serverId' ],
77e08517
C
118 unique: true,
119 where: {
120 serverId: {
121 [Op.ne]: null
122 }
123 }
124 },
0f06c4de
C
125 {
126 fields: [ 'preferredUsername' ],
127 unique: true,
128 where: {
129 serverId: null
130 }
131 },
6502c3d4
C
132 {
133 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
57c36b27 134 },
a3d1026b
C
135 {
136 fields: [ 'sharedInboxUrl' ]
137 },
57c36b27
C
138 {
139 fields: [ 'serverId' ]
140 },
141 {
142 fields: [ 'avatarId' ]
8cd72bd3 143 },
8cd72bd3
C
144 {
145 fields: [ 'followersUrl' ]
50d6de9c
C
146 }
147 ]
fadf619a 148})
b49f22d8 149export class ActorModel extends Model {
fadf619a 150
50d6de9c 151 @AllowNull(false)
3acc5084 152 @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES)))
50d6de9c
C
153 type: ActivityPubActorType
154
fadf619a 155 @AllowNull(false)
e12a0092 156 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
fadf619a 157 @Column
e12a0092 158 preferredUsername: string
fadf619a
C
159
160 @AllowNull(false)
161 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
01de67b9 162 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
fadf619a
C
163 url: string
164
165 @AllowNull(true)
1735c825 166 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true))
01de67b9 167 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
fadf619a
C
168 publicKey: string
169
170 @AllowNull(true)
1735c825 171 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
01de67b9 172 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
fadf619a
C
173 privateKey: string
174
175 @AllowNull(false)
176 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
177 @Column
178 followersCount: number
179
180 @AllowNull(false)
181 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
182 @Column
183 followingCount: number
184
185 @AllowNull(false)
186 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
01de67b9 187 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
fadf619a
C
188 inboxUrl: string
189
0b5c385b
C
190 @AllowNull(true)
191 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true))
01de67b9 192 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
fadf619a
C
193 outboxUrl: string
194
47581df0
C
195 @AllowNull(true)
196 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url', true))
01de67b9 197 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
fadf619a
C
198 sharedInboxUrl: string
199
0b5c385b
C
200 @AllowNull(true)
201 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true))
01de67b9 202 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
fadf619a
C
203 followersUrl: string
204
0b5c385b
C
205 @AllowNull(true)
206 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true))
01de67b9 207 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
fadf619a
C
208 followingUrl: string
209
210 @CreatedAt
211 createdAt: Date
212
213 @UpdatedAt
214 updatedAt: Date
215
216 @ForeignKey(() => AvatarModel)
217 @Column
218 avatarId: number
219
220 @BelongsTo(() => AvatarModel, {
221 foreignKey: {
222 allowNull: true
223 },
f05a1c30
C
224 onDelete: 'set null',
225 hooks: true
fadf619a
C
226 })
227 Avatar: AvatarModel
228
50d6de9c 229 @HasMany(() => ActorFollowModel, {
fadf619a 230 foreignKey: {
50d6de9c 231 name: 'actorId',
fadf619a
C
232 allowNull: false
233 },
cef534ed 234 as: 'ActorFollowings',
fadf619a
C
235 onDelete: 'cascade'
236 })
54e74059 237 ActorFollowing: ActorFollowModel[]
fadf619a 238
50d6de9c 239 @HasMany(() => ActorFollowModel, {
fadf619a 240 foreignKey: {
50d6de9c 241 name: 'targetActorId',
fadf619a
C
242 allowNull: false
243 },
54e74059 244 as: 'ActorFollowers',
fadf619a
C
245 onDelete: 'cascade'
246 })
54e74059 247 ActorFollowers: ActorFollowModel[]
fadf619a
C
248
249 @ForeignKey(() => ServerModel)
250 @Column
251 serverId: number
252
253 @BelongsTo(() => ServerModel, {
254 foreignKey: {
255 allowNull: true
256 },
257 onDelete: 'cascade'
258 })
259 Server: ServerModel
260
50d6de9c
C
261 @HasOne(() => AccountModel, {
262 foreignKey: {
c5a893d5
C
263 allowNull: true
264 },
265 onDelete: 'cascade',
266 hooks: true
50d6de9c
C
267 })
268 Account: AccountModel
269
270 @HasOne(() => VideoChannelModel, {
271 foreignKey: {
c5a893d5
C
272 allowNull: true
273 },
274 onDelete: 'cascade',
275 hooks: true
50d6de9c
C
276 })
277 VideoChannel: VideoChannelModel
278
b49f22d8 279 static load (id: number): Promise<MActor> {
9b39106d 280 return ActorModel.unscoped().findByPk(id)
50d6de9c
C
281 }
282
b49f22d8 283 static loadFull (id: number): Promise<MActorFull> {
453e83ea
C
284 return ActorModel.scope(ScopeNames.FULL).findByPk(id)
285 }
286
b49f22d8 287 static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Promise<MActor> {
e5565833
C
288 const query = {
289 include: [
290 {
291 attributes: [ 'id' ],
292 model: AccountModel.unscoped(),
293 required: true,
294 include: [
295 {
296 attributes: [ 'id' ],
297 model: VideoChannelModel.unscoped(),
298 required: true,
3acc5084
C
299 include: [
300 {
301 attributes: [ 'id' ],
302 model: VideoModel.unscoped(),
303 required: true,
304 where: {
305 id: videoId
306 }
e5565833 307 }
3acc5084 308 ]
e5565833
C
309 }
310 ]
311 }
312 ],
313 transaction
314 }
315
3acc5084 316 return ActorModel.unscoped().findOne(query)
e5565833
C
317 }
318
d4defe07
C
319 static isActorUrlExist (url: string) {
320 const query = {
321 raw: true,
322 where: {
323 url
324 }
325 }
326
327 return ActorModel.unscoped().findOne(query)
328 .then(a => !!a)
329 }
330
b49f22d8 331 static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Promise<MActorFull[]> {
fadf619a
C
332 const query = {
333 where: {
334 followersUrl: {
a1587156 335 [Op.in]: followersUrls
fadf619a
C
336 }
337 },
338 transaction
339 }
340
50d6de9c
C
341 return ActorModel.scope(ScopeNames.FULL).findAll(query)
342 }
343
b49f22d8 344 static loadLocalByName (preferredUsername: string, transaction?: Transaction): Promise<MActorFull> {
0ffd6d32
C
345 const fun = () => {
346 const query = {
347 where: {
348 preferredUsername,
349 serverId: null
350 },
351 transaction
352 }
e4a686b4 353
0ffd6d32
C
354 return ActorModel.scope(ScopeNames.FULL)
355 .findOne(query)
50d6de9c
C
356 }
357
0ffd6d32
C
358 return ModelCache.Instance.doCache({
359 cacheType: 'local-actor-name',
360 key: preferredUsername,
361 // The server actor never change, so we can easily cache it
362 whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
363 fun
364 })
0374b6b5
C
365 }
366
b49f22d8 367 static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Promise<MActorUrl> {
0ffd6d32
C
368 const fun = () => {
369 const query = {
370 attributes: [ 'url' ],
371 where: {
372 preferredUsername,
373 serverId: null
374 },
375 transaction
376 }
0374b6b5 377
0ffd6d32
C
378 return ActorModel.unscoped()
379 .findOne(query)
0374b6b5
C
380 }
381
0ffd6d32
C
382 return ModelCache.Instance.doCache({
383 cacheType: 'local-actor-name',
384 key: preferredUsername,
385 // The server actor never change, so we can easily cache it
386 whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
387 fun
388 })
50d6de9c
C
389 }
390
b49f22d8 391 static loadByNameAndHost (preferredUsername: string, host: string): Promise<MActorFull> {
50d6de9c
C
392 const query = {
393 where: {
e12a0092 394 preferredUsername
50d6de9c
C
395 },
396 include: [
397 {
398 model: ServerModel,
399 required: true,
400 where: {
401 host
402 }
403 }
404 ]
405 }
406
407 return ActorModel.scope(ScopeNames.FULL).findOne(query)
408 }
409
b49f22d8 410 static loadByUrl (url: string, transaction?: Transaction): Promise<MActorAccountChannelId> {
e587e0ec
C
411 const query = {
412 where: {
413 url
414 },
415 transaction,
416 include: [
417 {
418 attributes: [ 'id' ],
419 model: AccountModel.unscoped(),
420 required: false
421 },
422 {
423 attributes: [ 'id' ],
424 model: VideoChannelModel.unscoped(),
425 required: false
426 }
427 ]
428 }
429
430 return ActorModel.unscoped().findOne(query)
431 }
432
b49f22d8 433 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Promise<MActorFull> {
50d6de9c
C
434 const query = {
435 where: {
436 url
437 },
438 transaction
439 }
440
441 return ActorModel.scope(ScopeNames.FULL).findOne(query)
fadf619a
C
442 }
443
e6122097
C
444 static rebuildFollowsCount (ofId: number, type: 'followers' | 'following', transaction?: Transaction) {
445 const sanitizedOfId = parseInt(ofId + '', 10)
446 const where = { id: sanitizedOfId }
447
448 let columnToUpdate: string
449 let columnOfCount: string
450
451 if (type === 'followers') {
452 columnToUpdate = 'followersCount'
453 columnOfCount = 'targetActorId'
454 } else {
455 columnToUpdate = 'followingCount'
456 columnOfCount = 'actorId'
457 }
458
459 return ActorModel.update({
460 [columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId})`)
461 }, { where, transaction })
32b2b43c
C
462 }
463
b49f22d8 464 static loadAccountActorByVideoId (videoId: number): Promise<MActor> {
2c8776fc
C
465 const query = {
466 include: [
467 {
468 attributes: [ 'id' ],
469 model: AccountModel.unscoped(),
470 required: true,
471 include: [
472 {
473 attributes: [ 'id', 'accountId' ],
474 model: VideoChannelModel.unscoped(),
475 required: true,
476 include: [
477 {
478 attributes: [ 'id', 'channelId' ],
479 model: VideoModel.unscoped(),
480 where: {
481 id: videoId
482 }
483 }
484 ]
485 }
486 ]
487 }
488 ]
489 }
490
491 return ActorModel.unscoped().findOne(query)
492 }
493
47581df0
C
494 getSharedInbox (this: MActorWithInboxes) {
495 return this.sharedInboxUrl || this.inboxUrl
496 }
497
1ca9f7c3 498 toFormattedSummaryJSON (this: MActorSummaryFormattable) {
fadf619a
C
499 let avatar: Avatar = null
500 if (this.Avatar) {
c5911fd3 501 avatar = this.Avatar.toFormattedJSON()
fadf619a
C
502 }
503
fadf619a 504 return {
4cb6d457 505 url: this.url,
60650c77 506 name: this.preferredUsername,
e12a0092 507 host: this.getHost(),
1ca9f7c3
C
508 avatar
509 }
510 }
511
512 toFormattedJSON (this: MActorFormattable) {
513 const base = this.toFormattedSummaryJSON()
514
515 return Object.assign(base, {
516 id: this.id,
c48e82b5 517 hostRedundancyAllowed: this.getRedundancyAllowed(),
fadf619a
C
518 followingCount: this.followingCount,
519 followersCount: this.followersCount,
60650c77
C
520 createdAt: this.createdAt,
521 updatedAt: this.updatedAt
1ca9f7c3 522 })
fadf619a
C
523 }
524
8424c402 525 toActivityPubObject (this: MActorAP, name: string) {
a1587156
C
526 let icon: ActivityIconObject
527
c5911fd3
C
528 if (this.avatarId) {
529 const extension = extname(this.Avatar.filename)
a1587156 530
c5911fd3
C
531 icon = {
532 type: 'Image',
533 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
534 url: this.getAvatarUrl()
535 }
536 }
537
fadf619a 538 const json = {
8424c402 539 type: this.type,
fadf619a
C
540 id: this.url,
541 following: this.getFollowingUrl(),
542 followers: this.getFollowersUrl(),
418d092a 543 playlists: this.getPlaylistsUrl(),
fadf619a
C
544 inbox: this.inboxUrl,
545 outbox: this.outboxUrl,
e12a0092 546 preferredUsername: this.preferredUsername,
fadf619a 547 url: this.url,
e12a0092 548 name,
fadf619a
C
549 endpoints: {
550 sharedInbox: this.sharedInboxUrl
551 },
fadf619a
C
552 publicKey: {
553 id: this.getPublicKeyUrl(),
554 owner: this.url,
555 publicKeyPem: this.publicKey
c5911fd3
C
556 },
557 icon
fadf619a
C
558 }
559
560 return activityPubContextify(json)
561 }
562
941c5eac 563 getFollowerSharedInboxUrls (t: Transaction) {
fadf619a
C
564 const query = {
565 attributes: [ 'sharedInboxUrl' ],
566 include: [
567 {
54e74059
C
568 attribute: [],
569 model: ActorFollowModel.unscoped(),
fadf619a 570 required: true,
d6e99e53 571 as: 'ActorFollowing',
fadf619a 572 where: {
54e74059 573 state: 'accepted',
50d6de9c 574 targetActorId: this.id
fadf619a
C
575 }
576 }
577 ],
578 transaction: t
579 }
580
581 return ActorModel.findAll(query)
582 .then(accounts => accounts.map(a => a.sharedInboxUrl))
583 }
584
585 getFollowingUrl () {
586 return this.url + '/following'
587 }
588
589 getFollowersUrl () {
590 return this.url + '/followers'
591 }
592
418d092a
C
593 getPlaylistsUrl () {
594 return this.url + '/playlists'
595 }
596
fadf619a
C
597 getPublicKeyUrl () {
598 return this.url + '#main-key'
599 }
600
601 isOwned () {
602 return this.serverId === null
603 }
e12a0092 604
1ca9f7c3 605 getWebfingerUrl (this: MActorServer) {
e12a0092
C
606 return 'acct:' + this.preferredUsername + '@' + this.getHost()
607 }
608
80e36cd9
AB
609 getIdentifier () {
610 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
611 }
612
1ca9f7c3 613 getHost (this: MActorHost) {
6dd9de95 614 return this.Server ? this.Server.host : WEBSERVER.HOST
e12a0092 615 }
c5911fd3 616
b5fecbf4 617 getRedundancyAllowed () {
c48e82b5
C
618 return this.Server ? this.Server.redundancyAllowed : false
619 }
620
c5911fd3
C
621 getAvatarUrl () {
622 if (!this.avatarId) return undefined
623
557b13ae 624 return WEBSERVER.URL + this.Avatar.getStaticPath()
c5911fd3 625 }
a5625b41
C
626
627 isOutdated () {
628 if (this.isOwned()) return false
629
9f79ade6 630 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)
a5625b41 631 }
fadf619a 632}