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