]>
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 | ||
f37dc0dd C |
45 | export const unusedActorAttributesForAPI = [ |
46 | 'publicKey', | |
47 | 'privateKey', | |
48 | 'inboxUrl', | |
49 | 'outboxUrl', | |
50 | 'sharedInboxUrl', | |
51 | 'followersUrl', | |
aa55a4da | 52 | 'followingUrl', |
f5b0af50 C |
53 | 'url', |
54 | 'createdAt', | |
55 | 'updatedAt' | |
f37dc0dd C |
56 | ] |
57 | ||
ce33ee01 C |
58 | @DefaultScope({ |
59 | include: [ | |
60 | { | |
61 | model: () => ServerModel, | |
62 | required: false | |
c5911fd3 C |
63 | }, |
64 | { | |
65 | model: () => AvatarModel, | |
66 | required: false | |
ce33ee01 C |
67 | } |
68 | ] | |
69 | }) | |
50d6de9c C |
70 | @Scopes({ |
71 | [ScopeNames.FULL]: { | |
72 | include: [ | |
73 | { | |
7bc29171 | 74 | model: () => AccountModel.unscoped(), |
50d6de9c C |
75 | required: false |
76 | }, | |
77 | { | |
7bc29171 | 78 | model: () => VideoChannelModel.unscoped(), |
50d6de9c | 79 | required: false |
ce33ee01 C |
80 | }, |
81 | { | |
82 | model: () => ServerModel, | |
83 | required: false | |
c5911fd3 C |
84 | }, |
85 | { | |
86 | model: () => AvatarModel, | |
87 | required: false | |
50d6de9c C |
88 | } |
89 | ] | |
90 | } | |
91 | }) | |
fadf619a | 92 | @Table({ |
50d6de9c C |
93 | tableName: 'actor', |
94 | indexes: [ | |
2ccaeeb3 | 95 | { |
8cd72bd3 C |
96 | fields: [ 'url' ], |
97 | unique: true | |
2ccaeeb3 | 98 | }, |
50d6de9c | 99 | { |
e12a0092 | 100 | fields: [ 'preferredUsername', 'serverId' ], |
50d6de9c | 101 | unique: true |
6502c3d4 C |
102 | }, |
103 | { | |
104 | fields: [ 'inboxUrl', 'sharedInboxUrl' ] | |
57c36b27 | 105 | }, |
a3d1026b C |
106 | { |
107 | fields: [ 'sharedInboxUrl' ] | |
108 | }, | |
57c36b27 C |
109 | { |
110 | fields: [ 'serverId' ] | |
111 | }, | |
112 | { | |
113 | fields: [ 'avatarId' ] | |
8cd72bd3 C |
114 | }, |
115 | { | |
116 | fields: [ 'uuid' ], | |
117 | unique: true | |
118 | }, | |
119 | { | |
120 | fields: [ 'followersUrl' ] | |
50d6de9c C |
121 | } |
122 | ] | |
fadf619a C |
123 | }) |
124 | export class ActorModel extends Model<ActorModel> { | |
125 | ||
50d6de9c C |
126 | @AllowNull(false) |
127 | @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES))) | |
128 | type: ActivityPubActorType | |
129 | ||
fadf619a C |
130 | @AllowNull(false) |
131 | @Default(DataType.UUIDV4) | |
132 | @IsUUID(4) | |
133 | @Column(DataType.UUID) | |
134 | uuid: string | |
135 | ||
136 | @AllowNull(false) | |
e12a0092 | 137 | @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username')) |
fadf619a | 138 | @Column |
e12a0092 | 139 | preferredUsername: string |
fadf619a C |
140 | |
141 | @AllowNull(false) | |
142 | @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
01de67b9 | 143 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
144 | url: string |
145 | ||
146 | @AllowNull(true) | |
147 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key')) | |
01de67b9 | 148 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max)) |
fadf619a C |
149 | publicKey: string |
150 | ||
151 | @AllowNull(true) | |
152 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key')) | |
01de67b9 | 153 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max)) |
fadf619a C |
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')) | |
01de67b9 | 168 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
169 | inboxUrl: string |
170 | ||
171 | @AllowNull(false) | |
172 | @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url')) | |
01de67b9 | 173 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
174 | outboxUrl: string |
175 | ||
176 | @AllowNull(false) | |
177 | @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url')) | |
01de67b9 | 178 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
179 | sharedInboxUrl: string |
180 | ||
181 | @AllowNull(false) | |
182 | @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url')) | |
01de67b9 | 183 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
184 | followersUrl: string |
185 | ||
186 | @AllowNull(false) | |
187 | @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url')) | |
01de67b9 | 188 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
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 | }, | |
f05a1c30 C |
205 | onDelete: 'set null', |
206 | hooks: true | |
fadf619a C |
207 | }) |
208 | Avatar: AvatarModel | |
209 | ||
50d6de9c | 210 | @HasMany(() => ActorFollowModel, { |
fadf619a | 211 | foreignKey: { |
50d6de9c | 212 | name: 'actorId', |
fadf619a C |
213 | allowNull: false |
214 | }, | |
215 | onDelete: 'cascade' | |
216 | }) | |
54e74059 | 217 | ActorFollowing: ActorFollowModel[] |
fadf619a | 218 | |
50d6de9c | 219 | @HasMany(() => ActorFollowModel, { |
fadf619a | 220 | foreignKey: { |
50d6de9c | 221 | name: 'targetActorId', |
fadf619a C |
222 | allowNull: false |
223 | }, | |
54e74059 | 224 | as: 'ActorFollowers', |
fadf619a C |
225 | onDelete: 'cascade' |
226 | }) | |
54e74059 | 227 | ActorFollowers: ActorFollowModel[] |
fadf619a C |
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 | ||
50d6de9c C |
241 | @HasOne(() => AccountModel, { |
242 | foreignKey: { | |
c5a893d5 C |
243 | allowNull: true |
244 | }, | |
245 | onDelete: 'cascade', | |
246 | hooks: true | |
50d6de9c C |
247 | }) |
248 | Account: AccountModel | |
249 | ||
250 | @HasOne(() => VideoChannelModel, { | |
251 | foreignKey: { | |
c5a893d5 C |
252 | allowNull: true |
253 | }, | |
254 | onDelete: 'cascade', | |
255 | hooks: true | |
50d6de9c C |
256 | }) |
257 | VideoChannel: VideoChannelModel | |
258 | ||
259 | static load (id: number) { | |
60650c77 | 260 | return ActorModel.unscoped().findById(id) |
50d6de9c C |
261 | } |
262 | ||
fadf619a C |
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 | ||
50d6de9c C |
273 | return ActorModel.scope(ScopeNames.FULL).findAll(query) |
274 | } | |
275 | ||
8a19bee1 | 276 | static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) { |
50d6de9c C |
277 | const query = { |
278 | where: { | |
e12a0092 | 279 | preferredUsername, |
50d6de9c | 280 | serverId: null |
8a19bee1 C |
281 | }, |
282 | transaction | |
50d6de9c C |
283 | } |
284 | ||
285 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
286 | } | |
287 | ||
e12a0092 | 288 | static loadByNameAndHost (preferredUsername: string, host: string) { |
50d6de9c C |
289 | const query = { |
290 | where: { | |
e12a0092 | 291 | preferredUsername |
50d6de9c C |
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) | |
fadf619a C |
316 | } |
317 | ||
32b2b43c C |
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 | ||
fadf619a C |
328 | toFormattedJSON () { |
329 | let avatar: Avatar = null | |
330 | if (this.Avatar) { | |
c5911fd3 | 331 | avatar = this.Avatar.toFormattedJSON() |
fadf619a C |
332 | } |
333 | ||
fadf619a C |
334 | return { |
335 | id: this.id, | |
4cb6d457 | 336 | url: this.url, |
50d6de9c | 337 | uuid: this.uuid, |
60650c77 | 338 | name: this.preferredUsername, |
e12a0092 | 339 | host: this.getHost(), |
fadf619a C |
340 | followingCount: this.followingCount, |
341 | followersCount: this.followersCount, | |
60650c77 C |
342 | avatar, |
343 | createdAt: this.createdAt, | |
344 | updatedAt: this.updatedAt | |
fadf619a C |
345 | } |
346 | } | |
347 | ||
e12a0092 | 348 | toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') { |
fadf619a C |
349 | let activityPubType |
350 | if (type === 'Account') { | |
50d6de9c C |
351 | activityPubType = 'Person' as 'Person' |
352 | } else if (type === 'Application') { | |
353 | activityPubType = 'Application' as 'Application' | |
fadf619a | 354 | } else { // VideoChannel |
50d6de9c | 355 | activityPubType = 'Group' as 'Group' |
fadf619a C |
356 | } |
357 | ||
c5911fd3 C |
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 | ||
fadf619a | 368 | const json = { |
50d6de9c | 369 | type: activityPubType, |
fadf619a C |
370 | id: this.url, |
371 | following: this.getFollowingUrl(), | |
372 | followers: this.getFollowersUrl(), | |
373 | inbox: this.inboxUrl, | |
374 | outbox: this.outboxUrl, | |
e12a0092 | 375 | preferredUsername: this.preferredUsername, |
fadf619a | 376 | url: this.url, |
e12a0092 | 377 | name, |
fadf619a C |
378 | endpoints: { |
379 | sharedInbox: this.sharedInboxUrl | |
380 | }, | |
50d6de9c | 381 | uuid: this.uuid, |
fadf619a C |
382 | publicKey: { |
383 | id: this.getPublicKeyUrl(), | |
384 | owner: this.url, | |
385 | publicKeyPem: this.publicKey | |
c5911fd3 C |
386 | }, |
387 | icon | |
fadf619a C |
388 | } |
389 | ||
390 | return activityPubContextify(json) | |
391 | } | |
392 | ||
393 | getFollowerSharedInboxUrls (t: Sequelize.Transaction) { | |
394 | const query = { | |
395 | attributes: [ 'sharedInboxUrl' ], | |
396 | include: [ | |
397 | { | |
54e74059 C |
398 | attribute: [], |
399 | model: ActorFollowModel.unscoped(), | |
fadf619a | 400 | required: true, |
d6e99e53 | 401 | as: 'ActorFollowing', |
fadf619a | 402 | where: { |
54e74059 | 403 | state: 'accepted', |
50d6de9c | 404 | targetActorId: this.id |
fadf619a C |
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 | } | |
e12a0092 C |
430 | |
431 | getWebfingerUrl () { | |
432 | return 'acct:' + this.preferredUsername + '@' + this.getHost() | |
433 | } | |
434 | ||
80e36cd9 AB |
435 | getIdentifier () { |
436 | return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername | |
437 | } | |
438 | ||
e12a0092 C |
439 | getHost () { |
440 | return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST | |
441 | } | |
c5911fd3 C |
442 | |
443 | getAvatarUrl () { | |
444 | if (!this.avatarId) return undefined | |
445 | ||
265ba139 | 446 | return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath() |
c5911fd3 | 447 | } |
a5625b41 C |
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 | } | |
fadf619a | 459 | } |