]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/models/activitypub/actor.ts
Fix tsconfig with CLI tools
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor.ts
... / ...
CommitLineData
1import { values } from 'lodash'
2import { extname } from 'path'
3import * as Sequelize from 'sequelize'
4import {
5 AllowNull,
6 BelongsTo,
7 Column,
8 CreatedAt,
9 DataType,
10 DefaultScope,
11 ForeignKey,
12 HasMany,
13 HasOne,
14 Is,
15 Model,
16 Scopes,
17 Table,
18 UpdatedAt
19} from 'sequelize-typescript'
20import { ActivityPubActorType } from '../../../shared/models/activitypub'
21import { Avatar } from '../../../shared/models/avatars/avatar.model'
22import { activityPubContextify } from '../../helpers/activitypub'
23import {
24 isActorFollowersCountValid,
25 isActorFollowingCountValid,
26 isActorPreferredUsernameValid,
27 isActorPrivateKeyValid,
28 isActorPublicKeyValid
29} from '../../helpers/custom-validators/activitypub/actor'
30import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
31import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
32import { AccountModel } from '../account/account'
33import { AvatarModel } from '../avatar/avatar'
34import { ServerModel } from '../server/server'
35import { isOutdated, throwIfNotValid } from '../utils'
36import { VideoChannelModel } from '../video/video-channel'
37import { ActorFollowModel } from './actor-follow'
38import { VideoModel } from '../video/video'
39import {
40 MActor,
41 MActorAccountChannelId,
42 MActorAP,
43 MActorFormattable,
44 MActorFull,
45 MActorHost,
46 MActorServer,
47 MActorSummaryFormattable
48} from '../../typings/models'
49import * as Bluebird from 'bluebird'
50
51enum ScopeNames {
52 FULL = 'FULL'
53}
54
55export const unusedActorAttributesForAPI = [
56 'publicKey',
57 'privateKey',
58 'inboxUrl',
59 'outboxUrl',
60 'sharedInboxUrl',
61 'followersUrl',
62 'followingUrl',
63 'url',
64 'createdAt',
65 'updatedAt'
66]
67
68@DefaultScope(() => ({
69 include: [
70 {
71 model: ServerModel,
72 required: false
73 },
74 {
75 model: AvatarModel,
76 required: false
77 }
78 ]
79}))
80@Scopes(() => ({
81 [ScopeNames.FULL]: {
82 include: [
83 {
84 model: AccountModel.unscoped(),
85 required: false
86 },
87 {
88 model: VideoChannelModel.unscoped(),
89 required: false,
90 include: [
91 {
92 model: AccountModel,
93 required: true
94 }
95 ]
96 },
97 {
98 model: ServerModel,
99 required: false
100 },
101 {
102 model: AvatarModel,
103 required: false
104 }
105 ]
106 }
107}))
108@Table({
109 tableName: 'actor',
110 indexes: [
111 {
112 fields: [ 'url' ],
113 unique: true
114 },
115 {
116 fields: [ 'preferredUsername', 'serverId' ],
117 unique: true
118 },
119 {
120 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
121 },
122 {
123 fields: [ 'sharedInboxUrl' ]
124 },
125 {
126 fields: [ 'serverId' ]
127 },
128 {
129 fields: [ 'avatarId' ]
130 },
131 {
132 fields: [ 'followersUrl' ]
133 }
134 ]
135})
136export class ActorModel extends Model<ActorModel> {
137
138 @AllowNull(false)
139 @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES)))
140 type: ActivityPubActorType
141
142 @AllowNull(false)
143 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
144 @Column
145 preferredUsername: string
146
147 @AllowNull(false)
148 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
149 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
150 url: string
151
152 @AllowNull(true)
153 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true))
154 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
155 publicKey: string
156
157 @AllowNull(true)
158 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
159 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
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'))
174 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
175 inboxUrl: string
176
177 @AllowNull(true)
178 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true))
179 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
180 outboxUrl: string
181
182 @AllowNull(false)
183 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
184 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
185 sharedInboxUrl: string
186
187 @AllowNull(true)
188 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true))
189 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
190 followersUrl: string
191
192 @AllowNull(true)
193 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true))
194 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
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 },
211 onDelete: 'set null',
212 hooks: true
213 })
214 Avatar: AvatarModel
215
216 @HasMany(() => ActorFollowModel, {
217 foreignKey: {
218 name: 'actorId',
219 allowNull: false
220 },
221 as: 'ActorFollowings',
222 onDelete: 'cascade'
223 })
224 ActorFollowing: ActorFollowModel[]
225
226 @HasMany(() => ActorFollowModel, {
227 foreignKey: {
228 name: 'targetActorId',
229 allowNull: false
230 },
231 as: 'ActorFollowers',
232 onDelete: 'cascade'
233 })
234 ActorFollowers: ActorFollowModel[]
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
248 @HasOne(() => AccountModel, {
249 foreignKey: {
250 allowNull: true
251 },
252 onDelete: 'cascade',
253 hooks: true
254 })
255 Account: AccountModel
256
257 @HasOne(() => VideoChannelModel, {
258 foreignKey: {
259 allowNull: true
260 },
261 onDelete: 'cascade',
262 hooks: true
263 })
264 VideoChannel: VideoChannelModel
265
266 static load (id: number): Bluebird<MActor> {
267 return ActorModel.unscoped().findByPk(id)
268 }
269
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> {
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,
286 include: [
287 {
288 attributes: [ 'id' ],
289 model: VideoModel.unscoped(),
290 required: true,
291 where: {
292 id: videoId
293 }
294 }
295 ]
296 }
297 ]
298 }
299 ],
300 transaction
301 }
302
303 return ActorModel.unscoped().findOne(query)
304 }
305
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
318 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction): Bluebird<MActorFull[]> {
319 const query = {
320 where: {
321 followersUrl: {
322 [ Sequelize.Op.in ]: followersUrls
323 }
324 },
325 transaction
326 }
327
328 return ActorModel.scope(ScopeNames.FULL).findAll(query)
329 }
330
331 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction): Bluebird<MActorFull> {
332 const query = {
333 where: {
334 preferredUsername,
335 serverId: null
336 },
337 transaction
338 }
339
340 return ActorModel.scope(ScopeNames.FULL).findOne(query)
341 }
342
343 static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {
344 const query = {
345 where: {
346 preferredUsername
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
362 static loadByUrl (url: string, transaction?: Sequelize.Transaction): Bluebird<MActorAccountChannelId> {
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
385 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction): Bluebird<MActorFull> {
386 const query = {
387 where: {
388 url
389 },
390 transaction
391 }
392
393 return ActorModel.scope(ScopeNames.FULL).findOne(query)
394 }
395
396 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
397 return ActorModel.increment(column, {
398 by,
399 where: {
400 id
401 }
402 })
403 }
404
405 toFormattedSummaryJSON (this: MActorSummaryFormattable) {
406 let avatar: Avatar = null
407 if (this.Avatar) {
408 avatar = this.Avatar.toFormattedJSON()
409 }
410
411 return {
412 url: this.url,
413 name: this.preferredUsername,
414 host: this.getHost(),
415 avatar
416 }
417 }
418
419 toFormattedJSON (this: MActorFormattable) {
420 const base = this.toFormattedSummaryJSON()
421
422 return Object.assign(base, {
423 id: this.id,
424 hostRedundancyAllowed: this.getRedundancyAllowed(),
425 followingCount: this.followingCount,
426 followersCount: this.followersCount,
427 createdAt: this.createdAt,
428 updatedAt: this.updatedAt
429 })
430 }
431
432 toActivityPubObject (this: MActorAP, name: string) {
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
443 const json = {
444 type: this.type,
445 id: this.url,
446 following: this.getFollowingUrl(),
447 followers: this.getFollowersUrl(),
448 playlists: this.getPlaylistsUrl(),
449 inbox: this.inboxUrl,
450 outbox: this.outboxUrl,
451 preferredUsername: this.preferredUsername,
452 url: this.url,
453 name,
454 endpoints: {
455 sharedInbox: this.sharedInboxUrl
456 },
457 publicKey: {
458 id: this.getPublicKeyUrl(),
459 owner: this.url,
460 publicKeyPem: this.publicKey
461 },
462 icon
463 }
464
465 return activityPubContextify(json)
466 }
467
468 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
469 const query = {
470 attributes: [ 'sharedInboxUrl' ],
471 include: [
472 {
473 attribute: [],
474 model: ActorFollowModel.unscoped(),
475 required: true,
476 as: 'ActorFollowing',
477 where: {
478 state: 'accepted',
479 targetActorId: this.id
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
498 getPlaylistsUrl () {
499 return this.url + '/playlists'
500 }
501
502 getPublicKeyUrl () {
503 return this.url + '#main-key'
504 }
505
506 isOwned () {
507 return this.serverId === null
508 }
509
510 getWebfingerUrl (this: MActorServer) {
511 return 'acct:' + this.preferredUsername + '@' + this.getHost()
512 }
513
514 getIdentifier () {
515 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
516 }
517
518 getHost (this: MActorHost) {
519 return this.Server ? this.Server.host : WEBSERVER.HOST
520 }
521
522 getRedundancyAllowed () {
523 return this.Server ? this.Server.redundancyAllowed : false
524 }
525
526 getAvatarUrl () {
527 if (!this.avatarId) return undefined
528
529 return WEBSERVER.URL + this.Avatar.getStaticPath()
530 }
531
532 isOutdated () {
533 if (this.isOwned()) return false
534
535 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)
536 }
537}