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