]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/activitypub/actor.ts
Move config in its own file
[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'
6dd9de95 33import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers'
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
ce33ee01
C
59@DefaultScope({
60 include: [
61 {
62 model: () => ServerModel,
63 required: false
c5911fd3
C
64 },
65 {
66 model: () => AvatarModel,
67 required: false
ce33ee01
C
68 }
69 ]
70})
50d6de9c
C
71@Scopes({
72 [ScopeNames.FULL]: {
73 include: [
74 {
7bc29171 75 model: () => AccountModel.unscoped(),
50d6de9c
C
76 required: false
77 },
78 {
7bc29171 79 model: () => VideoChannelModel.unscoped(),
c48e82b5
C
80 required: false,
81 include: [
82 {
83 model: () => AccountModel,
84 required: true
85 }
86 ]
ce33ee01
C
87 },
88 {
89 model: () => ServerModel,
90 required: false
c5911fd3
C
91 },
92 {
93 model: () => AvatarModel,
94 required: false
50d6de9c
C
95 }
96 ]
97 }
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
C
133 @AllowNull(false)
134 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
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)
154 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
01de67b9 155 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
fadf619a
C
156 publicKey: string
157
158 @AllowNull(true)
159 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
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,
283 include: {
284 attributes: [ 'id' ],
285 model: VideoModel.unscoped(),
286 required: true,
287 where: {
288 id: videoId
289 }
290 }
291 }
292 ]
293 }
294 ],
295 transaction
296 }
297
298 return ActorModel.unscoped().findOne(query as any) // FIXME: typings
299 }
300
d4defe07
C
301 static isActorUrlExist (url: string) {
302 const query = {
303 raw: true,
304 where: {
305 url
306 }
307 }
308
309 return ActorModel.unscoped().findOne(query)
310 .then(a => !!a)
311 }
312
fadf619a
C
313 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
314 const query = {
315 where: {
316 followersUrl: {
317 [ Sequelize.Op.in ]: followersUrls
318 }
319 },
320 transaction
321 }
322
50d6de9c
C
323 return ActorModel.scope(ScopeNames.FULL).findAll(query)
324 }
325
8a19bee1 326 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) {
50d6de9c
C
327 const query = {
328 where: {
e12a0092 329 preferredUsername,
50d6de9c 330 serverId: null
8a19bee1
C
331 },
332 transaction
50d6de9c
C
333 }
334
335 return ActorModel.scope(ScopeNames.FULL).findOne(query)
336 }
337
e12a0092 338 static loadByNameAndHost (preferredUsername: string, host: string) {
50d6de9c
C
339 const query = {
340 where: {
e12a0092 341 preferredUsername
50d6de9c
C
342 },
343 include: [
344 {
345 model: ServerModel,
346 required: true,
347 where: {
348 host
349 }
350 }
351 ]
352 }
353
354 return ActorModel.scope(ScopeNames.FULL).findOne(query)
355 }
356
357 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
e587e0ec
C
358 const query = {
359 where: {
360 url
361 },
362 transaction,
363 include: [
364 {
365 attributes: [ 'id' ],
366 model: AccountModel.unscoped(),
367 required: false
368 },
369 {
370 attributes: [ 'id' ],
371 model: VideoChannelModel.unscoped(),
372 required: false
373 }
374 ]
375 }
376
377 return ActorModel.unscoped().findOne(query)
378 }
379
380 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction) {
50d6de9c
C
381 const query = {
382 where: {
383 url
384 },
385 transaction
386 }
387
388 return ActorModel.scope(ScopeNames.FULL).findOne(query)
fadf619a
C
389 }
390
32b2b43c
C
391 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
392 // FIXME: typings
393 return (ActorModel as any).increment(column, {
394 by,
395 where: {
396 id
397 }
398 })
399 }
400
fadf619a
C
401 toFormattedJSON () {
402 let avatar: Avatar = null
403 if (this.Avatar) {
c5911fd3 404 avatar = this.Avatar.toFormattedJSON()
fadf619a
C
405 }
406
fadf619a
C
407 return {
408 id: this.id,
4cb6d457 409 url: this.url,
50d6de9c 410 uuid: this.uuid,
60650c77 411 name: this.preferredUsername,
e12a0092 412 host: this.getHost(),
c48e82b5 413 hostRedundancyAllowed: this.getRedundancyAllowed(),
fadf619a
C
414 followingCount: this.followingCount,
415 followersCount: this.followersCount,
60650c77
C
416 avatar,
417 createdAt: this.createdAt,
418 updatedAt: this.updatedAt
fadf619a
C
419 }
420 }
421
e12a0092 422 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
fadf619a
C
423 let activityPubType
424 if (type === 'Account') {
50d6de9c
C
425 activityPubType = 'Person' as 'Person'
426 } else if (type === 'Application') {
427 activityPubType = 'Application' as 'Application'
fadf619a 428 } else { // VideoChannel
50d6de9c 429 activityPubType = 'Group' as 'Group'
fadf619a
C
430 }
431
c5911fd3
C
432 let icon = undefined
433 if (this.avatarId) {
434 const extension = extname(this.Avatar.filename)
435 icon = {
436 type: 'Image',
437 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
438 url: this.getAvatarUrl()
439 }
440 }
441
fadf619a 442 const json = {
50d6de9c 443 type: activityPubType,
fadf619a
C
444 id: this.url,
445 following: this.getFollowingUrl(),
446 followers: this.getFollowersUrl(),
418d092a 447 playlists: this.getPlaylistsUrl(),
fadf619a
C
448 inbox: this.inboxUrl,
449 outbox: this.outboxUrl,
e12a0092 450 preferredUsername: this.preferredUsername,
fadf619a 451 url: this.url,
e12a0092 452 name,
fadf619a
C
453 endpoints: {
454 sharedInbox: this.sharedInboxUrl
455 },
50d6de9c 456 uuid: this.uuid,
fadf619a
C
457 publicKey: {
458 id: this.getPublicKeyUrl(),
459 owner: this.url,
460 publicKeyPem: this.publicKey
c5911fd3
C
461 },
462 icon
fadf619a
C
463 }
464
465 return activityPubContextify(json)
466 }
467
468 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
469 const query = {
470 attributes: [ 'sharedInboxUrl' ],
471 include: [
472 {
54e74059
C
473 attribute: [],
474 model: ActorFollowModel.unscoped(),
fadf619a 475 required: true,
d6e99e53 476 as: 'ActorFollowing',
fadf619a 477 where: {
54e74059 478 state: 'accepted',
50d6de9c 479 targetActorId: this.id
fadf619a
C
480 }
481 }
482 ],
483 transaction: t
484 }
485
486 return ActorModel.findAll(query)
487 .then(accounts => accounts.map(a => a.sharedInboxUrl))
488 }
489
490 getFollowingUrl () {
491 return this.url + '/following'
492 }
493
494 getFollowersUrl () {
495 return this.url + '/followers'
496 }
497
418d092a
C
498 getPlaylistsUrl () {
499 return this.url + '/playlists'
500 }
501
fadf619a
C
502 getPublicKeyUrl () {
503 return this.url + '#main-key'
504 }
505
506 isOwned () {
507 return this.serverId === null
508 }
e12a0092
C
509
510 getWebfingerUrl () {
511 return 'acct:' + this.preferredUsername + '@' + this.getHost()
512 }
513
80e36cd9
AB
514 getIdentifier () {
515 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
516 }
517
e12a0092 518 getHost () {
6dd9de95 519 return this.Server ? this.Server.host : WEBSERVER.HOST
e12a0092 520 }
c5911fd3 521
c48e82b5
C
522 getRedundancyAllowed () {
523 return this.Server ? this.Server.redundancyAllowed : false
524 }
525
c5911fd3
C
526 getAvatarUrl () {
527 if (!this.avatarId) return undefined
528
6dd9de95 529 return WEBSERVER.URL + this.Avatar.getWebserverPath()
c5911fd3 530 }
a5625b41
C
531
532 isOutdated () {
533 if (this.isOwned()) return false
534
9f79ade6 535 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)
a5625b41 536 }
fadf619a 537}