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