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