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