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