]>
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 | AccountFollowing: ActorFollowModel[] | |
171 | ||
172 | @HasMany(() => ActorFollowModel, { | |
173 | foreignKey: { | |
174 | name: 'targetActorId', | |
175 | allowNull: false | |
176 | }, | |
177 | as: 'followers', | |
178 | onDelete: 'cascade' | |
179 | }) | |
180 | AccountFollowers: 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 | toFormattedJSON () { | |
281 | let avatar: Avatar = null | |
282 | if (this.Avatar) { | |
283 | avatar = this.Avatar.toFormattedJSON() | |
284 | } | |
285 | ||
286 | return { | |
287 | id: this.id, | |
288 | url: this.url, | |
289 | uuid: this.uuid, | |
290 | name: this.preferredUsername, | |
291 | host: this.getHost(), | |
292 | followingCount: this.followingCount, | |
293 | followersCount: this.followersCount, | |
294 | avatar, | |
295 | createdAt: this.createdAt, | |
296 | updatedAt: this.updatedAt | |
297 | } | |
298 | } | |
299 | ||
300 | toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') { | |
301 | let activityPubType | |
302 | if (type === 'Account') { | |
303 | activityPubType = 'Person' as 'Person' | |
304 | } else if (type === 'Application') { | |
305 | activityPubType = 'Application' as 'Application' | |
306 | } else { // VideoChannel | |
307 | activityPubType = 'Group' as 'Group' | |
308 | } | |
309 | ||
310 | let icon = undefined | |
311 | if (this.avatarId) { | |
312 | const extension = extname(this.Avatar.filename) | |
313 | icon = { | |
314 | type: 'Image', | |
315 | mediaType: extension === '.png' ? 'image/png' : 'image/jpeg', | |
316 | url: this.getAvatarUrl() | |
317 | } | |
318 | } | |
319 | ||
320 | const json = { | |
321 | type: activityPubType, | |
322 | id: this.url, | |
323 | following: this.getFollowingUrl(), | |
324 | followers: this.getFollowersUrl(), | |
325 | inbox: this.inboxUrl, | |
326 | outbox: this.outboxUrl, | |
327 | preferredUsername: this.preferredUsername, | |
328 | url: this.url, | |
329 | name, | |
330 | endpoints: { | |
331 | sharedInbox: this.sharedInboxUrl | |
332 | }, | |
333 | uuid: this.uuid, | |
334 | publicKey: { | |
335 | id: this.getPublicKeyUrl(), | |
336 | owner: this.url, | |
337 | publicKeyPem: this.publicKey | |
338 | }, | |
339 | icon | |
340 | } | |
341 | ||
342 | return activityPubContextify(json) | |
343 | } | |
344 | ||
345 | getFollowerSharedInboxUrls (t: Sequelize.Transaction) { | |
346 | const query = { | |
347 | attributes: [ 'sharedInboxUrl' ], | |
348 | include: [ | |
349 | { | |
350 | model: ActorFollowModel, | |
351 | required: true, | |
352 | as: 'followers', | |
353 | where: { | |
354 | targetActorId: this.id | |
355 | } | |
356 | } | |
357 | ], | |
358 | transaction: t | |
359 | } | |
360 | ||
361 | return ActorModel.findAll(query) | |
362 | .then(accounts => accounts.map(a => a.sharedInboxUrl)) | |
363 | } | |
364 | ||
365 | getFollowingUrl () { | |
366 | return this.url + '/following' | |
367 | } | |
368 | ||
369 | getFollowersUrl () { | |
370 | return this.url + '/followers' | |
371 | } | |
372 | ||
373 | getPublicKeyUrl () { | |
374 | return this.url + '#main-key' | |
375 | } | |
376 | ||
377 | isOwned () { | |
378 | return this.serverId === null | |
379 | } | |
380 | ||
381 | getWebfingerUrl () { | |
382 | return 'acct:' + this.preferredUsername + '@' + this.getHost() | |
383 | } | |
384 | ||
385 | getHost () { | |
386 | return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST | |
387 | } | |
388 | ||
389 | getAvatarUrl () { | |
390 | if (!this.avatarId) return undefined | |
391 | ||
392 | return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath() | |
393 | } | |
394 | ||
395 | isOutdated () { | |
396 | if (this.isOwned()) return false | |
397 | ||
398 | const now = Date.now() | |
399 | const createdAtTime = this.createdAt.getTime() | |
400 | const updatedAtTime = this.updatedAt.getTime() | |
401 | ||
402 | return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL && | |
403 | (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL | |
404 | } | |
405 | } |