]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/activitypub/actor.ts
Translated using Weblate (French (France))
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor.ts
CommitLineData
50d6de9c 1import { values } from 'lodash'
47564bbe 2import { extname } from 'path'
fadf619a 3import {
2422c46b
C
4 AllowNull,
5 BelongsTo,
6 Column,
7 CreatedAt,
8 DataType,
2422c46b
C
9 DefaultScope,
10 ForeignKey,
11 HasMany,
12 HasOne,
13 Is,
2422c46b
C
14 Model,
15 Scopes,
16 Table,
17 UpdatedAt
fadf619a 18} from 'sequelize-typescript'
50d6de9c 19import { ActivityPubActorType } from '../../../shared/models/activitypub'
fadf619a 20import { Avatar } from '../../../shared/models/avatars/avatar.model'
da854ddd 21import { activityPubContextify } from '../../helpers/activitypub'
fadf619a 22import {
2422c46b
C
23 isActorFollowersCountValid,
24 isActorFollowingCountValid,
25 isActorPreferredUsernameValid,
26 isActorPrivateKeyValid,
da854ddd
C
27 isActorPublicKeyValid
28} from '../../helpers/custom-validators/activitypub/actor'
29import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
e4a686b4 30import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
50d6de9c 31import { AccountModel } from '../account/account'
fadf619a
C
32import { AvatarModel } from '../avatar/avatar'
33import { ServerModel } from '../server/server'
9f79ade6 34import { isOutdated, throwIfNotValid } from '../utils'
50d6de9c
C
35import { VideoChannelModel } from '../video/video-channel'
36import { ActorFollowModel } from './actor-follow'
e5565833 37import { VideoModel } from '../video/video'
1ca9f7c3
C
38import {
39 MActor,
40 MActorAccountChannelId,
b5fecbf4 41 MActorAP,
1ca9f7c3 42 MActorFormattable,
b5fecbf4
C
43 MActorFull,
44 MActorHost,
1ca9f7c3 45 MActorServer,
47581df0
C
46 MActorSummaryFormattable,
47 MActorWithInboxes
1ca9f7c3 48} from '../../typings/models'
453e83ea 49import * as Bluebird from 'bluebird'
e6122097 50import { Op, Transaction, literal } from 'sequelize'
fadf619a 51
50d6de9c
C
52enum ScopeNames {
53 FULL = 'FULL'
54}
55
f37dc0dd
C
56export const unusedActorAttributesForAPI = [
57 'publicKey',
58 'privateKey',
59 'inboxUrl',
60 'outboxUrl',
61 'sharedInboxUrl',
62 'followersUrl',
aa55a4da 63 'followingUrl',
f5b0af50
C
64 'url',
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 },
458218d2
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
C
148})
149export class ActorModel extends Model<ActorModel> {
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
e4a686b4
C
279 private static cache: { [ id: string ]: any } = {}
280
453e83ea 281 static load (id: number): Bluebird<MActor> {
9b39106d 282 return ActorModel.unscoped().findByPk(id)
50d6de9c
C
283 }
284
453e83ea
C
285 static loadFull (id: number): Bluebird<MActorFull> {
286 return ActorModel.scope(ScopeNames.FULL).findByPk(id)
287 }
288
941c5eac 289 static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Bluebird<MActor> {
e5565833
C
290 const query = {
291 include: [
292 {
293 attributes: [ 'id' ],
294 model: AccountModel.unscoped(),
295 required: true,
296 include: [
297 {
298 attributes: [ 'id' ],
299 model: VideoChannelModel.unscoped(),
300 required: true,
3acc5084
C
301 include: [
302 {
303 attributes: [ 'id' ],
304 model: VideoModel.unscoped(),
305 required: true,
306 where: {
307 id: videoId
308 }
e5565833 309 }
3acc5084 310 ]
e5565833
C
311 }
312 ]
313 }
314 ],
315 transaction
316 }
317
3acc5084 318 return ActorModel.unscoped().findOne(query)
e5565833
C
319 }
320
d4defe07
C
321 static isActorUrlExist (url: string) {
322 const query = {
323 raw: true,
324 where: {
325 url
326 }
327 }
328
329 return ActorModel.unscoped().findOne(query)
330 .then(a => !!a)
331 }
332
941c5eac 333 static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Bluebird<MActorFull[]> {
fadf619a
C
334 const query = {
335 where: {
336 followersUrl: {
941c5eac 337 [ Op.in ]: followersUrls
fadf619a
C
338 }
339 },
340 transaction
341 }
342
50d6de9c
C
343 return ActorModel.scope(ScopeNames.FULL).findAll(query)
344 }
345
941c5eac 346 static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> {
e4a686b4
C
347 // The server actor never change, so we can easily cache it
348 if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.cache[preferredUsername]) {
349 return Bluebird.resolve(ActorModel.cache[preferredUsername])
350 }
351
50d6de9c
C
352 const query = {
353 where: {
e12a0092 354 preferredUsername,
50d6de9c 355 serverId: null
8a19bee1
C
356 },
357 transaction
50d6de9c
C
358 }
359
e4a686b4
C
360 return ActorModel.scope(ScopeNames.FULL)
361 .findOne(query)
362 .then(actor => {
363 if (preferredUsername === SERVER_ACTOR_NAME) {
364 ActorModel.cache[ preferredUsername ] = actor
365 }
366
367 return actor
368 })
50d6de9c
C
369 }
370
453e83ea 371 static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {
50d6de9c
C
372 const query = {
373 where: {
e12a0092 374 preferredUsername
50d6de9c
C
375 },
376 include: [
377 {
378 model: ServerModel,
379 required: true,
380 where: {
381 host
382 }
383 }
384 ]
385 }
386
387 return ActorModel.scope(ScopeNames.FULL).findOne(query)
388 }
389
941c5eac 390 static loadByUrl (url: string, transaction?: Transaction): Bluebird<MActorAccountChannelId> {
e587e0ec
C
391 const query = {
392 where: {
393 url
394 },
395 transaction,
396 include: [
397 {
398 attributes: [ 'id' ],
399 model: AccountModel.unscoped(),
400 required: false
401 },
402 {
403 attributes: [ 'id' ],
404 model: VideoChannelModel.unscoped(),
405 required: false
406 }
407 ]
408 }
409
410 return ActorModel.unscoped().findOne(query)
411 }
412
941c5eac 413 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Bluebird<MActorFull> {
50d6de9c
C
414 const query = {
415 where: {
416 url
417 },
418 transaction
419 }
420
421 return ActorModel.scope(ScopeNames.FULL).findOne(query)
fadf619a
C
422 }
423
e6122097
C
424 static rebuildFollowsCount (ofId: number, type: 'followers' | 'following', transaction?: Transaction) {
425 const sanitizedOfId = parseInt(ofId + '', 10)
426 const where = { id: sanitizedOfId }
427
428 let columnToUpdate: string
429 let columnOfCount: string
430
431 if (type === 'followers') {
432 columnToUpdate = 'followersCount'
433 columnOfCount = 'targetActorId'
434 } else {
435 columnToUpdate = 'followingCount'
436 columnOfCount = 'actorId'
437 }
438
439 return ActorModel.update({
440 [columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId})`)
441 }, { where, transaction })
32b2b43c
C
442 }
443
47581df0
C
444 getSharedInbox (this: MActorWithInboxes) {
445 return this.sharedInboxUrl || this.inboxUrl
446 }
447
1ca9f7c3 448 toFormattedSummaryJSON (this: MActorSummaryFormattable) {
fadf619a
C
449 let avatar: Avatar = null
450 if (this.Avatar) {
c5911fd3 451 avatar = this.Avatar.toFormattedJSON()
fadf619a
C
452 }
453
fadf619a 454 return {
4cb6d457 455 url: this.url,
60650c77 456 name: this.preferredUsername,
e12a0092 457 host: this.getHost(),
1ca9f7c3
C
458 avatar
459 }
460 }
461
462 toFormattedJSON (this: MActorFormattable) {
463 const base = this.toFormattedSummaryJSON()
464
465 return Object.assign(base, {
466 id: this.id,
c48e82b5 467 hostRedundancyAllowed: this.getRedundancyAllowed(),
fadf619a
C
468 followingCount: this.followingCount,
469 followersCount: this.followersCount,
60650c77
C
470 createdAt: this.createdAt,
471 updatedAt: this.updatedAt
1ca9f7c3 472 })
fadf619a
C
473 }
474
8424c402 475 toActivityPubObject (this: MActorAP, name: string) {
c5911fd3
C
476 let icon = undefined
477 if (this.avatarId) {
478 const extension = extname(this.Avatar.filename)
479 icon = {
480 type: 'Image',
481 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
482 url: this.getAvatarUrl()
483 }
484 }
485
fadf619a 486 const json = {
8424c402 487 type: this.type,
fadf619a
C
488 id: this.url,
489 following: this.getFollowingUrl(),
490 followers: this.getFollowersUrl(),
418d092a 491 playlists: this.getPlaylistsUrl(),
fadf619a
C
492 inbox: this.inboxUrl,
493 outbox: this.outboxUrl,
e12a0092 494 preferredUsername: this.preferredUsername,
fadf619a 495 url: this.url,
e12a0092 496 name,
fadf619a
C
497 endpoints: {
498 sharedInbox: this.sharedInboxUrl
499 },
fadf619a
C
500 publicKey: {
501 id: this.getPublicKeyUrl(),
502 owner: this.url,
503 publicKeyPem: this.publicKey
c5911fd3
C
504 },
505 icon
fadf619a
C
506 }
507
508 return activityPubContextify(json)
509 }
510
941c5eac 511 getFollowerSharedInboxUrls (t: Transaction) {
fadf619a
C
512 const query = {
513 attributes: [ 'sharedInboxUrl' ],
514 include: [
515 {
54e74059
C
516 attribute: [],
517 model: ActorFollowModel.unscoped(),
fadf619a 518 required: true,
d6e99e53 519 as: 'ActorFollowing',
fadf619a 520 where: {
54e74059 521 state: 'accepted',
50d6de9c 522 targetActorId: this.id
fadf619a
C
523 }
524 }
525 ],
526 transaction: t
527 }
528
529 return ActorModel.findAll(query)
530 .then(accounts => accounts.map(a => a.sharedInboxUrl))
531 }
532
533 getFollowingUrl () {
534 return this.url + '/following'
535 }
536
537 getFollowersUrl () {
538 return this.url + '/followers'
539 }
540
418d092a
C
541 getPlaylistsUrl () {
542 return this.url + '/playlists'
543 }
544
fadf619a
C
545 getPublicKeyUrl () {
546 return this.url + '#main-key'
547 }
548
549 isOwned () {
550 return this.serverId === null
551 }
e12a0092 552
1ca9f7c3 553 getWebfingerUrl (this: MActorServer) {
e12a0092
C
554 return 'acct:' + this.preferredUsername + '@' + this.getHost()
555 }
556
80e36cd9
AB
557 getIdentifier () {
558 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
559 }
560
1ca9f7c3 561 getHost (this: MActorHost) {
6dd9de95 562 return this.Server ? this.Server.host : WEBSERVER.HOST
e12a0092 563 }
c5911fd3 564
b5fecbf4 565 getRedundancyAllowed () {
c48e82b5
C
566 return this.Server ? this.Server.redundancyAllowed : false
567 }
568
c5911fd3
C
569 getAvatarUrl () {
570 if (!this.avatarId) return undefined
571
557b13ae 572 return WEBSERVER.URL + this.Avatar.getStaticPath()
c5911fd3 573 }
a5625b41
C
574
575 isOutdated () {
576 if (this.isOwned()) return false
577
9f79ade6 578 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)
a5625b41 579 }
fadf619a 580}