aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/account/account.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/account/account.ts')
-rw-r--r--server/models/account/account.ts673
1 files changed, 305 insertions, 368 deletions
diff --git a/server/models/account/account.ts b/server/models/account/account.ts
index 8b0819f39..d6758fa10 100644
--- a/server/models/account/account.ts
+++ b/server/models/account/account.ts
@@ -1,253 +1,200 @@
1import { join } from 'path' 1import { join } from 'path'
2import * as Sequelize from 'sequelize' 2import * as Sequelize from 'sequelize'
3import {
4 AfterDestroy,
5 AllowNull,
6 BelongsTo,
7 Column,
8 CreatedAt,
9 DataType,
10 Default,
11 ForeignKey,
12 HasMany,
13 Is,
14 IsUUID,
15 Model,
16 Table,
17 UpdatedAt
18} from 'sequelize-typescript'
3import { Avatar } from '../../../shared/models/avatars/avatar.model' 19import { Avatar } from '../../../shared/models/avatars/avatar.model'
20import { activityPubContextify } from '../../helpers'
4import { 21import {
5 activityPubContextify,
6 isAccountFollowersCountValid, 22 isAccountFollowersCountValid,
7 isAccountFollowingCountValid, 23 isAccountFollowingCountValid,
8 isAccountPrivateKeyValid, 24 isAccountPrivateKeyValid,
9 isAccountPublicKeyValid, 25 isAccountPublicKeyValid,
10 isUserUsernameValid 26 isActivityPubUrlValid
11} from '../../helpers' 27} from '../../helpers/custom-validators/activitypub'
12import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' 28import { isUserUsernameValid } from '../../helpers/custom-validators/users'
13import { AVATARS_DIR } from '../../initializers' 29import { AVATARS_DIR, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
14import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants' 30import { sendDeleteAccount } from '../../lib/activitypub/send'
15import { sendDeleteAccount } from '../../lib/activitypub/send/send-delete' 31import { ApplicationModel } from '../application/application'
16import { addMethodsToModel } from '../utils' 32import { AvatarModel } from '../avatar/avatar'
17import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface' 33import { ServerModel } from '../server/server'
18 34import { throwIfNotValid } from '../utils'
19let Account: Sequelize.Model<AccountInstance, AccountAttributes> 35import { VideoChannelModel } from '../video/video-channel'
20let load: AccountMethods.Load 36import { AccountFollowModel } from './account-follow'
21let loadApplication: AccountMethods.LoadApplication 37import { UserModel } from './user'
22let loadByUUID: AccountMethods.LoadByUUID 38
23let loadByUrl: AccountMethods.LoadByUrl 39@Table({
24let loadLocalByName: AccountMethods.LoadLocalByName 40 tableName: 'account',
25let loadByNameAndHost: AccountMethods.LoadByNameAndHost 41 indexes: [
26let listByFollowersUrls: AccountMethods.ListByFollowersUrls
27let isOwned: AccountMethods.IsOwned
28let toActivityPubObject: AccountMethods.ToActivityPubObject
29let toFormattedJSON: AccountMethods.ToFormattedJSON
30let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
31let getFollowingUrl: AccountMethods.GetFollowingUrl
32let getFollowersUrl: AccountMethods.GetFollowersUrl
33let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
34
35export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
36 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
37 { 42 {
38 uuid: { 43 fields: [ 'name' ]
39 type: DataTypes.UUID,
40 defaultValue: DataTypes.UUIDV4,
41 allowNull: false,
42 validate: {
43 isUUID: 4
44 }
45 },
46 name: {
47 type: DataTypes.STRING,
48 allowNull: false,
49 validate: {
50 nameValid: value => {
51 const res = isUserUsernameValid(value)
52 if (res === false) throw new Error('Name is not valid.')
53 }
54 }
55 },
56 url: {
57 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
58 allowNull: false,
59 validate: {
60 urlValid: value => {
61 const res = isActivityPubUrlValid(value)
62 if (res === false) throw new Error('URL is not valid.')
63 }
64 }
65 },
66 publicKey: {
67 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
68 allowNull: true,
69 validate: {
70 publicKeyValid: value => {
71 const res = isAccountPublicKeyValid(value)
72 if (res === false) throw new Error('Public key is not valid.')
73 }
74 }
75 },
76 privateKey: {
77 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
78 allowNull: true,
79 validate: {
80 privateKeyValid: value => {
81 const res = isAccountPrivateKeyValid(value)
82 if (res === false) throw new Error('Private key is not valid.')
83 }
84 }
85 },
86 followersCount: {
87 type: DataTypes.INTEGER,
88 allowNull: false,
89 validate: {
90 followersCountValid: value => {
91 const res = isAccountFollowersCountValid(value)
92 if (res === false) throw new Error('Followers count is not valid.')
93 }
94 }
95 },
96 followingCount: {
97 type: DataTypes.INTEGER,
98 allowNull: false,
99 validate: {
100 followingCountValid: value => {
101 const res = isAccountFollowingCountValid(value)
102 if (res === false) throw new Error('Following count is not valid.')
103 }
104 }
105 },
106 inboxUrl: {
107 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
108 allowNull: false,
109 validate: {
110 inboxUrlValid: value => {
111 const res = isActivityPubUrlValid(value)
112 if (res === false) throw new Error('Inbox URL is not valid.')
113 }
114 }
115 },
116 outboxUrl: {
117 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
118 allowNull: false,
119 validate: {
120 outboxUrlValid: value => {
121 const res = isActivityPubUrlValid(value)
122 if (res === false) throw new Error('Outbox URL is not valid.')
123 }
124 }
125 },
126 sharedInboxUrl: {
127 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
128 allowNull: false,
129 validate: {
130 sharedInboxUrlValid: value => {
131 const res = isActivityPubUrlValid(value)
132 if (res === false) throw new Error('Shared inbox URL is not valid.')
133 }
134 }
135 },
136 followersUrl: {
137 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
138 allowNull: false,
139 validate: {
140 followersUrlValid: value => {
141 const res = isActivityPubUrlValid(value)
142 if (res === false) throw new Error('Followers URL is not valid.')
143 }
144 }
145 },
146 followingUrl: {
147 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
148 allowNull: false,
149 validate: {
150 followingUrlValid: value => {
151 const res = isActivityPubUrlValid(value)
152 if (res === false) throw new Error('Following URL is not valid.')
153 }
154 }
155 }
156 }, 44 },
157 { 45 {
158 indexes: [ 46 fields: [ 'serverId' ]
159 { 47 },
160 fields: [ 'name' ] 48 {
161 }, 49 fields: [ 'userId' ],
162 { 50 unique: true
163 fields: [ 'serverId' ] 51 },
164 }, 52 {
165 { 53 fields: [ 'applicationId' ],
166 fields: [ 'userId' ], 54 unique: true
167 unique: true 55 },
168 }, 56 {
169 { 57 fields: [ 'name', 'serverId', 'applicationId' ],
170 fields: [ 'applicationId' ], 58 unique: true
171 unique: true
172 },
173 {
174 fields: [ 'name', 'serverId', 'applicationId' ],
175 unique: true
176 }
177 ],
178 hooks: { afterDestroy }
179 } 59 }
180 )
181
182 const classMethods = [
183 associate,
184 loadApplication,
185 load,
186 loadByUUID,
187 loadByUrl,
188 loadLocalByName,
189 loadByNameAndHost,
190 listByFollowersUrls
191 ]
192 const instanceMethods = [
193 isOwned,
194 toActivityPubObject,
195 toFormattedJSON,
196 getFollowerSharedInboxUrls,
197 getFollowingUrl,
198 getFollowersUrl,
199 getPublicKeyUrl
200 ] 60 ]
201 addMethodsToModel(Account, classMethods, instanceMethods) 61})
202 62export class AccountModel extends Model<Account> {
203 return Account 63
204} 64 @AllowNull(false)
65 @Default(DataType.UUIDV4)
66 @IsUUID(4)
67 @Column(DataType.UUID)
68 uuid: string
69
70 @AllowNull(false)
71 @Is('AccountName', value => throwIfNotValid(value, isUserUsernameValid, 'account name'))
72 @Column
73 name: string
74
75 @AllowNull(false)
76 @Is('AccountUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
77 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max))
78 url: string
79
80 @AllowNull(true)
81 @Is('AccountPublicKey', value => throwIfNotValid(value, isAccountPublicKeyValid, 'public key'))
82 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max))
83 publicKey: string
84
85 @AllowNull(true)
86 @Is('AccountPublicKey', value => throwIfNotValid(value, isAccountPrivateKeyValid, 'private key'))
87 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max))
88 privateKey: string
89
90 @AllowNull(false)
91 @Is('AccountFollowersCount', value => throwIfNotValid(value, isAccountFollowersCountValid, 'followers count'))
92 @Column
93 followersCount: number
94
95 @AllowNull(false)
96 @Is('AccountFollowersCount', value => throwIfNotValid(value, isAccountFollowingCountValid, 'following count'))
97 @Column
98 followingCount: number
99
100 @AllowNull(false)
101 @Is('AccountInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
102 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max))
103 inboxUrl: string
104
105 @AllowNull(false)
106 @Is('AccountOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
107 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max))
108 outboxUrl: string
109
110 @AllowNull(false)
111 @Is('AccountSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
112 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max))
113 sharedInboxUrl: string
114
115 @AllowNull(false)
116 @Is('AccountFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
117 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max))
118 followersUrl: string
119
120 @AllowNull(false)
121 @Is('AccountFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
122 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max))
123 followingUrl: string
124
125 @CreatedAt
126 createdAt: Date
127
128 @UpdatedAt
129 updatedAt: Date
130
131 @ForeignKey(() => AvatarModel)
132 @Column
133 avatarId: number
134
135 @BelongsTo(() => AvatarModel, {
136 foreignKey: {
137 allowNull: true
138 },
139 onDelete: 'cascade'
140 })
141 Avatar: AvatarModel
205 142
206// --------------------------------------------------------------------------- 143 @ForeignKey(() => ServerModel)
144 @Column
145 serverId: number
207 146
208function associate (models) { 147 @BelongsTo(() => ServerModel, {
209 Account.belongsTo(models.Server, {
210 foreignKey: { 148 foreignKey: {
211 name: 'serverId',
212 allowNull: true 149 allowNull: true
213 }, 150 },
214 onDelete: 'cascade' 151 onDelete: 'cascade'
215 }) 152 })
153 Server: ServerModel
216 154
217 Account.belongsTo(models.User, { 155 @ForeignKey(() => UserModel)
156 @Column
157 userId: number
158
159 @BelongsTo(() => UserModel, {
218 foreignKey: { 160 foreignKey: {
219 name: 'userId',
220 allowNull: true 161 allowNull: true
221 }, 162 },
222 onDelete: 'cascade' 163 onDelete: 'cascade'
223 }) 164 })
165 User: UserModel
166
167 @ForeignKey(() => ApplicationModel)
168 @Column
169 applicationId: number
224 170
225 Account.belongsTo(models.Application, { 171 @BelongsTo(() => ApplicationModel, {
226 foreignKey: { 172 foreignKey: {
227 name: 'applicationId',
228 allowNull: true 173 allowNull: true
229 }, 174 },
230 onDelete: 'cascade' 175 onDelete: 'cascade'
231 }) 176 })
177 Application: ApplicationModel
232 178
233 Account.hasMany(models.VideoChannel, { 179 @HasMany(() => VideoChannelModel, {
234 foreignKey: { 180 foreignKey: {
235 name: 'accountId',
236 allowNull: false 181 allowNull: false
237 }, 182 },
238 onDelete: 'cascade', 183 onDelete: 'cascade',
239 hooks: true 184 hooks: true
240 }) 185 })
186 VideoChannels: VideoChannelModel[]
241 187
242 Account.hasMany(models.AccountFollow, { 188 @HasMany(() => AccountFollowModel, {
243 foreignKey: { 189 foreignKey: {
244 name: 'accountId', 190 name: 'accountId',
245 allowNull: false 191 allowNull: false
246 }, 192 },
247 onDelete: 'cascade' 193 onDelete: 'cascade'
248 }) 194 })
195 AccountFollowing: AccountFollowModel[]
249 196
250 Account.hasMany(models.AccountFollow, { 197 @HasMany(() => AccountFollowModel, {
251 foreignKey: { 198 foreignKey: {
252 name: 'targetAccountId', 199 name: 'targetAccountId',
253 allowNull: false 200 allowNull: false
@@ -255,209 +202,199 @@ function associate (models) {
255 as: 'followers', 202 as: 'followers',
256 onDelete: 'cascade' 203 onDelete: 'cascade'
257 }) 204 })
205 AccountFollowers: AccountFollowModel[]
258 206
259 Account.hasOne(models.Avatar, { 207 @AfterDestroy
260 foreignKey: { 208 static sendDeleteIfOwned (instance: AccountModel) {
261 name: 'avatarId', 209 if (instance.isOwned()) {
262 allowNull: true 210 return sendDeleteAccount(instance, undefined)
263 }, 211 }
264 onDelete: 'cascade'
265 })
266}
267 212
268function afterDestroy (account: AccountInstance) { 213 return undefined
269 if (account.isOwned()) {
270 return sendDeleteAccount(account, undefined)
271 } 214 }
272 215
273 return undefined 216 static loadApplication () {
274} 217 return AccountModel.findOne({
218 include: [
219 {
220 model: ApplicationModel,
221 required: true
222 }
223 ]
224 })
225 }
275 226
276toFormattedJSON = function (this: AccountInstance) { 227 static load (id: number) {
277 let host = CONFIG.WEBSERVER.HOST 228 return AccountModel.findById(id)
278 let score: number 229 }
279 let avatar: Avatar = null
280 230
281 if (this.Avatar) { 231 static loadByUUID (uuid: string) {
282 avatar = { 232 const query = {
283 path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename), 233 where: {
284 createdAt: this.Avatar.createdAt, 234 uuid
285 updatedAt: this.Avatar.updatedAt 235 }
286 } 236 }
287 }
288 237
289 if (this.Server) { 238 return AccountModel.findOne(query)
290 host = this.Server.host
291 score = this.Server.score as number
292 } 239 }
293 240
294 const json = { 241 static loadLocalByName (name: string) {
295 id: this.id, 242 const query = {
296 uuid: this.uuid, 243 where: {
297 host, 244 name,
298 score, 245 [ Sequelize.Op.or ]: [
299 name: this.name, 246 {
300 followingCount: this.followingCount, 247 userId: {
301 followersCount: this.followersCount, 248 [ Sequelize.Op.ne ]: null
302 createdAt: this.createdAt, 249 }
303 updatedAt: this.updatedAt, 250 },
304 avatar 251 {
305 } 252 applicationId: {
253 [ Sequelize.Op.ne ]: null
254 }
255 }
256 ]
257 }
258 }
306 259
307 return json 260 return AccountModel.findOne(query)
308} 261 }
309 262
310toActivityPubObject = function (this: AccountInstance) { 263 static loadByNameAndHost (name: string, host: string) {
311 const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person' 264 const query = {
312 265 where: {
313 const json = { 266 name
314 type, 267 },
315 id: this.url, 268 include: [
316 following: this.getFollowingUrl(), 269 {
317 followers: this.getFollowersUrl(), 270 model: ServerModel,
318 inbox: this.inboxUrl, 271 required: true,
319 outbox: this.outboxUrl, 272 where: {
320 preferredUsername: this.name, 273 host
321 url: this.url, 274 }
322 name: this.name, 275 }
323 endpoints: { 276 ]
324 sharedInbox: this.sharedInboxUrl
325 },
326 uuid: this.uuid,
327 publicKey: {
328 id: this.getPublicKeyUrl(),
329 owner: this.url,
330 publicKeyPem: this.publicKey
331 } 277 }
278
279 return AccountModel.findOne(query)
332 } 280 }
333 281
334 return activityPubContextify(json) 282 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
335} 283 const query = {
284 where: {
285 url
286 },
287 transaction
288 }
336 289
337isOwned = function (this: AccountInstance) { 290 return AccountModel.findOne(query)
338 return this.serverId === null 291 }
339}
340 292
341getFollowerSharedInboxUrls = function (this: AccountInstance, t: Sequelize.Transaction) { 293 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
342 const query: Sequelize.FindOptions<AccountAttributes> = { 294 const query = {
343 attributes: [ 'sharedInboxUrl' ], 295 where: {
344 include: [ 296 followersUrl: {
345 { 297 [ Sequelize.Op.in ]: followersUrls
346 model: Account['sequelize'].models.AccountFollow,
347 required: true,
348 as: 'followers',
349 where: {
350 targetAccountId: this.id
351 } 298 }
352 } 299 },
353 ], 300 transaction
354 transaction: t 301 }
355 }
356 302
357 return Account.findAll(query) 303 return AccountModel.findAll(query)
358 .then(accounts => accounts.map(a => a.sharedInboxUrl)) 304 }
359}
360 305
361getFollowingUrl = function (this: AccountInstance) { 306 toFormattedJSON () {
362 return this.url + '/following' 307 let host = CONFIG.WEBSERVER.HOST
363} 308 let score: number
309 let avatar: Avatar = null
364 310
365getFollowersUrl = function (this: AccountInstance) { 311 if (this.Avatar) {
366 return this.url + '/followers' 312 avatar = {
367} 313 path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename),
314 createdAt: this.Avatar.createdAt,
315 updatedAt: this.Avatar.updatedAt
316 }
317 }
368 318
369getPublicKeyUrl = function (this: AccountInstance) { 319 if (this.Server) {
370 return this.url + '#main-key' 320 host = this.Server.host
371} 321 score = this.Server.score
322 }
372 323
373// ------------------------------ STATICS ------------------------------ 324 return {
325 id: this.id,
326 uuid: this.uuid,
327 host,
328 score,
329 name: this.name,
330 followingCount: this.followingCount,
331 followersCount: this.followersCount,
332 createdAt: this.createdAt,
333 updatedAt: this.updatedAt,
334 avatar
335 }
336 }
374 337
375loadApplication = function () { 338 toActivityPubObject () {
376 return Account.findOne({ 339 const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
377 include: [ 340
378 { 341 const json = {
379 model: Account['sequelize'].models.Application, 342 type,
380 required: true 343 id: this.url,
344 following: this.getFollowingUrl(),
345 followers: this.getFollowersUrl(),
346 inbox: this.inboxUrl,
347 outbox: this.outboxUrl,
348 preferredUsername: this.name,
349 url: this.url,
350 name: this.name,
351 endpoints: {
352 sharedInbox: this.sharedInboxUrl
353 },
354 uuid: this.uuid,
355 publicKey: {
356 id: this.getPublicKeyUrl(),
357 owner: this.url,
358 publicKeyPem: this.publicKey
381 } 359 }
382 ]
383 })
384}
385
386load = function (id: number) {
387 return Account.findById(id)
388}
389
390loadByUUID = function (uuid: string) {
391 const query: Sequelize.FindOptions<AccountAttributes> = {
392 where: {
393 uuid
394 } 360 }
361
362 return activityPubContextify(json)
395 } 363 }
396 364
397 return Account.findOne(query) 365 isOwned () {
398} 366 return this.serverId === null
367 }
399 368
400loadLocalByName = function (name: string) { 369 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
401 const query: Sequelize.FindOptions<AccountAttributes> = { 370 const query = {
402 where: { 371 attributes: [ 'sharedInboxUrl' ],
403 name, 372 include: [
404 [Sequelize.Op.or]: [
405 {
406 userId: {
407 [Sequelize.Op.ne]: null
408 }
409 },
410 { 373 {
411 applicationId: { 374 model: AccountFollowModel,
412 [Sequelize.Op.ne]: null 375 required: true,
376 as: 'followers',
377 where: {
378 targetAccountId: this.id
413 } 379 }
414 } 380 }
415 ] 381 ],
382 transaction: t
416 } 383 }
417 }
418 384
419 return Account.findOne(query) 385 return AccountModel.findAll(query)
420} 386 .then(accounts => accounts.map(a => a.sharedInboxUrl))
421
422loadByNameAndHost = function (name: string, host: string) {
423 const query: Sequelize.FindOptions<AccountAttributes> = {
424 where: {
425 name
426 },
427 include: [
428 {
429 model: Account['sequelize'].models.Server,
430 required: true,
431 where: {
432 host
433 }
434 }
435 ]
436 } 387 }
437 388
438 return Account.findOne(query) 389 getFollowingUrl () {
439} 390 return this.url + '/following'
440
441loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
442 const query: Sequelize.FindOptions<AccountAttributes> = {
443 where: {
444 url
445 },
446 transaction
447 } 391 }
448 392
449 return Account.findOne(query) 393 getFollowersUrl () {
450} 394 return this.url + '/followers'
451
452listByFollowersUrls = function (followersUrls: string[], transaction?: Sequelize.Transaction) {
453 const query: Sequelize.FindOptions<AccountAttributes> = {
454 where: {
455 followersUrl: {
456 [Sequelize.Op.in]: followersUrls
457 }
458 },
459 transaction
460 } 395 }
461 396
462 return Account.findAll(query) 397 getPublicKeyUrl () {
398 return this.url + '#main-key'
399 }
463} 400}