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