]>
Commit | Line | Data |
---|---|---|
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 | @DefaultScope({ | |
46 | include: [ | |
47 | { | |
48 | model: () => ServerModel, | |
49 | required: false | |
50 | }, | |
51 | { | |
52 | model: () => AvatarModel, | |
53 | required: false | |
54 | } | |
55 | ] | |
56 | }) | |
57 | @Scopes({ | |
58 | [ScopeNames.FULL]: { | |
59 | include: [ | |
60 | { | |
61 | model: () => AccountModel.unscoped(), | |
62 | required: false | |
63 | }, | |
64 | { | |
65 | model: () => VideoChannelModel.unscoped(), | |
66 | required: false | |
67 | }, | |
68 | { | |
69 | model: () => ServerModel, | |
70 | required: false | |
71 | }, | |
72 | { | |
73 | model: () => AvatarModel, | |
74 | required: false | |
75 | } | |
76 | ] | |
77 | } | |
78 | }) | |
79 | @Table({ | |
80 | tableName: 'actor', | |
81 | indexes: [ | |
82 | { | |
83 | fields: [ 'url' ], | |
84 | unique: true | |
85 | }, | |
86 | { | |
87 | fields: [ 'preferredUsername', 'serverId' ], | |
88 | unique: true | |
89 | }, | |
90 | { | |
91 | fields: [ 'inboxUrl', 'sharedInboxUrl' ] | |
92 | }, | |
93 | { | |
94 | fields: [ 'serverId' ] | |
95 | }, | |
96 | { | |
97 | fields: [ 'avatarId' ] | |
98 | }, | |
99 | { | |
100 | fields: [ 'uuid' ], | |
101 | unique: true | |
102 | }, | |
103 | { | |
104 | fields: [ 'followersUrl' ] | |
105 | } | |
106 | ] | |
107 | }) | |
108 | export class ActorModel extends Model<ActorModel> { | |
109 | ||
110 | @AllowNull(false) | |
111 | @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES))) | |
112 | type: ActivityPubActorType | |
113 | ||
114 | @AllowNull(false) | |
115 | @Default(DataType.UUIDV4) | |
116 | @IsUUID(4) | |
117 | @Column(DataType.UUID) | |
118 | uuid: string | |
119 | ||
120 | @AllowNull(false) | |
121 | @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username')) | |
122 | @Column | |
123 | preferredUsername: string | |
124 | ||
125 | @AllowNull(false) | |
126 | @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
127 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | |
128 | url: string | |
129 | ||
130 | @AllowNull(true) | |
131 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key')) | |
132 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max)) | |
133 | publicKey: string | |
134 | ||
135 | @AllowNull(true) | |
136 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key')) | |
137 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max)) | |
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')) | |
152 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | |
153 | inboxUrl: string | |
154 | ||
155 | @AllowNull(false) | |
156 | @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url')) | |
157 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | |
158 | outboxUrl: string | |
159 | ||
160 | @AllowNull(false) | |
161 | @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url')) | |
162 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | |
163 | sharedInboxUrl: string | |
164 | ||
165 | @AllowNull(false) | |
166 | @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url')) | |
167 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | |
168 | followersUrl: string | |
169 | ||
170 | @AllowNull(false) | |
171 | @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url')) | |
172 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | |
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 | }, | |
189 | onDelete: 'set null', | |
190 | hooks: true | |
191 | }) | |
192 | Avatar: AvatarModel | |
193 | ||
194 | @HasMany(() => ActorFollowModel, { | |
195 | foreignKey: { | |
196 | name: 'actorId', | |
197 | allowNull: false | |
198 | }, | |
199 | onDelete: 'cascade' | |
200 | }) | |
201 | ActorFollowing: ActorFollowModel[] | |
202 | ||
203 | @HasMany(() => ActorFollowModel, { | |
204 | foreignKey: { | |
205 | name: 'targetActorId', | |
206 | allowNull: false | |
207 | }, | |
208 | as: 'ActorFollowers', | |
209 | onDelete: 'cascade' | |
210 | }) | |
211 | ActorFollowers: ActorFollowModel[] | |
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 | ||
225 | @HasOne(() => AccountModel, { | |
226 | foreignKey: { | |
227 | allowNull: true | |
228 | }, | |
229 | onDelete: 'cascade', | |
230 | hooks: true | |
231 | }) | |
232 | Account: AccountModel | |
233 | ||
234 | @HasOne(() => VideoChannelModel, { | |
235 | foreignKey: { | |
236 | allowNull: true | |
237 | }, | |
238 | onDelete: 'cascade', | |
239 | hooks: true | |
240 | }) | |
241 | VideoChannel: VideoChannelModel | |
242 | ||
243 | static load (id: number) { | |
244 | return ActorModel.unscoped().findById(id) | |
245 | } | |
246 | ||
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 | ||
257 | return ActorModel.scope(ScopeNames.FULL).findAll(query) | |
258 | } | |
259 | ||
260 | static loadLocalByName (preferredUsername: string) { | |
261 | const query = { | |
262 | where: { | |
263 | preferredUsername, | |
264 | serverId: null | |
265 | } | |
266 | } | |
267 | ||
268 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
269 | } | |
270 | ||
271 | static loadByNameAndHost (preferredUsername: string, host: string) { | |
272 | const query = { | |
273 | where: { | |
274 | preferredUsername | |
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) | |
299 | } | |
300 | ||
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 | ||
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 | ||
350 | toFormattedJSON () { | |
351 | let avatar: Avatar = null | |
352 | if (this.Avatar) { | |
353 | avatar = this.Avatar.toFormattedJSON() | |
354 | } | |
355 | ||
356 | return { | |
357 | id: this.id, | |
358 | url: this.url, | |
359 | uuid: this.uuid, | |
360 | name: this.preferredUsername, | |
361 | host: this.getHost(), | |
362 | followingCount: this.followingCount, | |
363 | followersCount: this.followersCount, | |
364 | avatar, | |
365 | createdAt: this.createdAt, | |
366 | updatedAt: this.updatedAt | |
367 | } | |
368 | } | |
369 | ||
370 | toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') { | |
371 | let activityPubType | |
372 | if (type === 'Account') { | |
373 | activityPubType = 'Person' as 'Person' | |
374 | } else if (type === 'Application') { | |
375 | activityPubType = 'Application' as 'Application' | |
376 | } else { // VideoChannel | |
377 | activityPubType = 'Group' as 'Group' | |
378 | } | |
379 | ||
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 | ||
390 | const json = { | |
391 | type: activityPubType, | |
392 | id: this.url, | |
393 | following: this.getFollowingUrl(), | |
394 | followers: this.getFollowersUrl(), | |
395 | inbox: this.inboxUrl, | |
396 | outbox: this.outboxUrl, | |
397 | preferredUsername: this.preferredUsername, | |
398 | url: this.url, | |
399 | name, | |
400 | endpoints: { | |
401 | sharedInbox: this.sharedInboxUrl | |
402 | }, | |
403 | uuid: this.uuid, | |
404 | publicKey: { | |
405 | id: this.getPublicKeyUrl(), | |
406 | owner: this.url, | |
407 | publicKeyPem: this.publicKey | |
408 | }, | |
409 | icon | |
410 | } | |
411 | ||
412 | return activityPubContextify(json) | |
413 | } | |
414 | ||
415 | getFollowerSharedInboxUrls (t: Sequelize.Transaction) { | |
416 | const query = { | |
417 | attributes: [ 'sharedInboxUrl' ], | |
418 | include: [ | |
419 | { | |
420 | attribute: [], | |
421 | model: ActorFollowModel.unscoped(), | |
422 | required: true, | |
423 | as: 'ActorFollowing', | |
424 | where: { | |
425 | state: 'accepted', | |
426 | targetActorId: this.id | |
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 | } | |
452 | ||
453 | getWebfingerUrl () { | |
454 | return 'acct:' + this.preferredUsername + '@' + this.getHost() | |
455 | } | |
456 | ||
457 | getIdentifier () { | |
458 | return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername | |
459 | } | |
460 | ||
461 | getHost () { | |
462 | return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST | |
463 | } | |
464 | ||
465 | getAvatarUrl () { | |
466 | if (!this.avatarId) return undefined | |
467 | ||
468 | return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath() | |
469 | } | |
470 | ||
471 | isOutdated () { | |
472 | if (this.isOwned()) return false | |
473 | ||
474 | const now = Date.now() | |
475 | const createdAtTime = this.createdAt.getTime() | |
476 | const updatedAtTime = this.updatedAt.getTime() | |
477 | ||
478 | return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL && | |
479 | (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL | |
480 | } | |
481 | } |