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