]>
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: [ | |
65 | { | |
e12a0092 | 66 | fields: [ 'preferredUsername', 'serverId' ], |
50d6de9c C |
67 | unique: true |
68 | } | |
69 | ] | |
fadf619a C |
70 | }) |
71 | export class ActorModel extends Model<ActorModel> { | |
72 | ||
50d6de9c C |
73 | @AllowNull(false) |
74 | @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES))) | |
75 | type: ActivityPubActorType | |
76 | ||
fadf619a C |
77 | @AllowNull(false) |
78 | @Default(DataType.UUIDV4) | |
79 | @IsUUID(4) | |
80 | @Column(DataType.UUID) | |
81 | uuid: string | |
82 | ||
83 | @AllowNull(false) | |
e12a0092 | 84 | @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username')) |
fadf619a | 85 | @Column |
e12a0092 | 86 | preferredUsername: string |
fadf619a C |
87 | |
88 | @AllowNull(false) | |
89 | @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
01de67b9 | 90 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
91 | url: string |
92 | ||
93 | @AllowNull(true) | |
94 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key')) | |
01de67b9 | 95 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max)) |
fadf619a C |
96 | publicKey: string |
97 | ||
98 | @AllowNull(true) | |
99 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key')) | |
01de67b9 | 100 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max)) |
fadf619a C |
101 | privateKey: string |
102 | ||
103 | @AllowNull(false) | |
104 | @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count')) | |
105 | @Column | |
106 | followersCount: number | |
107 | ||
108 | @AllowNull(false) | |
109 | @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count')) | |
110 | @Column | |
111 | followingCount: number | |
112 | ||
113 | @AllowNull(false) | |
114 | @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url')) | |
01de67b9 | 115 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
116 | inboxUrl: string |
117 | ||
118 | @AllowNull(false) | |
119 | @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url')) | |
01de67b9 | 120 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
121 | outboxUrl: string |
122 | ||
123 | @AllowNull(false) | |
124 | @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url')) | |
01de67b9 | 125 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
126 | sharedInboxUrl: string |
127 | ||
128 | @AllowNull(false) | |
129 | @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url')) | |
01de67b9 | 130 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
131 | followersUrl: string |
132 | ||
133 | @AllowNull(false) | |
134 | @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url')) | |
01de67b9 | 135 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
136 | followingUrl: string |
137 | ||
138 | @CreatedAt | |
139 | createdAt: Date | |
140 | ||
141 | @UpdatedAt | |
142 | updatedAt: Date | |
143 | ||
144 | @ForeignKey(() => AvatarModel) | |
145 | @Column | |
146 | avatarId: number | |
147 | ||
148 | @BelongsTo(() => AvatarModel, { | |
149 | foreignKey: { | |
150 | allowNull: true | |
151 | }, | |
c5911fd3 | 152 | onDelete: 'set null' |
fadf619a C |
153 | }) |
154 | Avatar: AvatarModel | |
155 | ||
50d6de9c | 156 | @HasMany(() => ActorFollowModel, { |
fadf619a | 157 | foreignKey: { |
50d6de9c | 158 | name: 'actorId', |
fadf619a C |
159 | allowNull: false |
160 | }, | |
161 | onDelete: 'cascade' | |
162 | }) | |
50d6de9c | 163 | AccountFollowing: ActorFollowModel[] |
fadf619a | 164 | |
50d6de9c | 165 | @HasMany(() => ActorFollowModel, { |
fadf619a | 166 | foreignKey: { |
50d6de9c | 167 | name: 'targetActorId', |
fadf619a C |
168 | allowNull: false |
169 | }, | |
170 | as: 'followers', | |
171 | onDelete: 'cascade' | |
172 | }) | |
50d6de9c | 173 | AccountFollowers: ActorFollowModel[] |
fadf619a C |
174 | |
175 | @ForeignKey(() => ServerModel) | |
176 | @Column | |
177 | serverId: number | |
178 | ||
179 | @BelongsTo(() => ServerModel, { | |
180 | foreignKey: { | |
181 | allowNull: true | |
182 | }, | |
183 | onDelete: 'cascade' | |
184 | }) | |
185 | Server: ServerModel | |
186 | ||
50d6de9c C |
187 | @HasOne(() => AccountModel, { |
188 | foreignKey: { | |
189 | allowNull: true | |
190 | }, | |
191 | onDelete: 'cascade' | |
192 | }) | |
193 | Account: AccountModel | |
194 | ||
195 | @HasOne(() => VideoChannelModel, { | |
196 | foreignKey: { | |
197 | allowNull: true | |
198 | }, | |
199 | onDelete: 'cascade' | |
200 | }) | |
201 | VideoChannel: VideoChannelModel | |
202 | ||
203 | static load (id: number) { | |
204 | return ActorModel.scope(ScopeNames.FULL).findById(id) | |
205 | } | |
206 | ||
fadf619a C |
207 | static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) { |
208 | const query = { | |
209 | where: { | |
210 | followersUrl: { | |
211 | [ Sequelize.Op.in ]: followersUrls | |
212 | } | |
213 | }, | |
214 | transaction | |
215 | } | |
216 | ||
50d6de9c C |
217 | return ActorModel.scope(ScopeNames.FULL).findAll(query) |
218 | } | |
219 | ||
e12a0092 | 220 | static loadLocalByName (preferredUsername: string) { |
50d6de9c C |
221 | const query = { |
222 | where: { | |
e12a0092 | 223 | preferredUsername, |
50d6de9c C |
224 | serverId: null |
225 | } | |
226 | } | |
227 | ||
228 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
229 | } | |
230 | ||
e12a0092 | 231 | static loadByNameAndHost (preferredUsername: string, host: string) { |
50d6de9c C |
232 | const query = { |
233 | where: { | |
e12a0092 | 234 | preferredUsername |
50d6de9c C |
235 | }, |
236 | include: [ | |
237 | { | |
238 | model: ServerModel, | |
239 | required: true, | |
240 | where: { | |
241 | host | |
242 | } | |
243 | } | |
244 | ] | |
245 | } | |
246 | ||
247 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
248 | } | |
249 | ||
250 | static loadByUrl (url: string, transaction?: Sequelize.Transaction) { | |
251 | const query = { | |
252 | where: { | |
253 | url | |
254 | }, | |
255 | transaction | |
256 | } | |
257 | ||
258 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
fadf619a C |
259 | } |
260 | ||
261 | toFormattedJSON () { | |
262 | let avatar: Avatar = null | |
263 | if (this.Avatar) { | |
c5911fd3 | 264 | avatar = this.Avatar.toFormattedJSON() |
fadf619a C |
265 | } |
266 | ||
fadf619a C |
267 | let score: number |
268 | if (this.Server) { | |
fadf619a C |
269 | score = this.Server.score |
270 | } | |
271 | ||
272 | return { | |
273 | id: this.id, | |
4cb6d457 | 274 | url: this.url, |
50d6de9c | 275 | uuid: this.uuid, |
e12a0092 | 276 | host: this.getHost(), |
fadf619a C |
277 | score, |
278 | followingCount: this.followingCount, | |
279 | followersCount: this.followersCount, | |
280 | avatar | |
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 | } |