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