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