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