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