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