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