1 import * as Sequelize from 'sequelize'
4 isAccountFollowersCountValid,
5 isAccountFollowingCountValid,
6 isAccountPrivateKeyValid,
7 isAccountPublicKeyValid,
10 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
11 import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
12 import { sendDeleteAccount } from '../../lib/activitypub/send/send-delete'
14 import { addMethodsToModel } from '../utils'
15 import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface'
17 let Account: Sequelize.Model<AccountInstance, AccountAttributes>
18 let load: AccountMethods.Load
19 let loadApplication: AccountMethods.LoadApplication
20 let loadByUUID: AccountMethods.LoadByUUID
21 let loadByUrl: AccountMethods.LoadByUrl
22 let loadLocalByName: AccountMethods.LoadLocalByName
23 let loadByNameAndHost: AccountMethods.LoadByNameAndHost
24 let listByFollowersUrls: AccountMethods.ListByFollowersUrls
25 let isOwned: AccountMethods.IsOwned
26 let toActivityPubObject: AccountMethods.ToActivityPubObject
27 let toFormattedJSON: AccountMethods.ToFormattedJSON
28 let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
29 let getFollowingUrl: AccountMethods.GetFollowingUrl
30 let getFollowersUrl: AccountMethods.GetFollowersUrl
31 let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
33 export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
34 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
38 defaultValue: DataTypes.UUIDV4,
45 type: DataTypes.STRING,
49 const res = isUserUsernameValid(value)
50 if (res === false) throw new Error('Name is not valid.')
55 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
59 const res = isActivityPubUrlValid(value)
60 if (res === false) throw new Error('URL is not valid.')
65 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
68 publicKeyValid: value => {
69 const res = isAccountPublicKeyValid(value)
70 if (res === false) throw new Error('Public key is not valid.')
75 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
78 privateKeyValid: value => {
79 const res = isAccountPrivateKeyValid(value)
80 if (res === false) throw new Error('Private key is not valid.')
85 type: DataTypes.INTEGER,
88 followersCountValid: value => {
89 const res = isAccountFollowersCountValid(value)
90 if (res === false) throw new Error('Followers count is not valid.')
95 type: DataTypes.INTEGER,
98 followingCountValid: value => {
99 const res = isAccountFollowingCountValid(value)
100 if (res === false) throw new Error('Following count is not valid.')
105 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
108 inboxUrlValid: value => {
109 const res = isActivityPubUrlValid(value)
110 if (res === false) throw new Error('Inbox URL is not valid.')
115 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
118 outboxUrlValid: value => {
119 const res = isActivityPubUrlValid(value)
120 if (res === false) throw new Error('Outbox URL is not valid.')
125 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
128 sharedInboxUrlValid: value => {
129 const res = isActivityPubUrlValid(value)
130 if (res === false) throw new Error('Shared inbox URL is not valid.')
135 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
138 followersUrlValid: value => {
139 const res = isActivityPubUrlValid(value)
140 if (res === false) throw new Error('Followers URL is not valid.')
145 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
148 followingUrlValid: value => {
149 const res = isActivityPubUrlValid(value)
150 if (res === false) throw new Error('Following URL is not valid.')
161 fields: [ 'serverId' ]
164 fields: [ 'userId' ],
168 fields: [ 'applicationId' ],
172 fields: [ 'name', 'serverId', 'applicationId' ],
176 hooks: { afterDestroy }
180 const classMethods = [
190 const instanceMethods = [
194 getFollowerSharedInboxUrls,
199 addMethodsToModel(Account, classMethods, instanceMethods)
204 // ---------------------------------------------------------------------------
206 function associate (models) {
207 Account.belongsTo(models.Server, {
215 Account.belongsTo(models.User, {
223 Account.belongsTo(models.Application, {
225 name: 'applicationId',
231 Account.hasMany(models.VideoChannel, {
240 Account.hasMany(models.AccountFollow, {
248 Account.hasMany(models.AccountFollow, {
250 name: 'targetAccountId',
258 function afterDestroy (account: AccountInstance) {
259 if (account.isOwned()) {
260 return sendDeleteAccount(account, undefined)
266 toFormattedJSON = function (this: AccountInstance) {
267 let host = CONFIG.WEBSERVER.HOST
271 host = this.Server.host
272 score = this.Server.score as number
280 createdAt: this.createdAt,
281 updatedAt: this.updatedAt
287 toActivityPubObject = function (this: AccountInstance) {
288 const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
293 following: this.getFollowingUrl(),
294 followers: this.getFollowersUrl(),
295 inbox: this.inboxUrl,
296 outbox: this.outboxUrl,
297 preferredUsername: this.name,
301 sharedInbox: this.sharedInboxUrl
305 id: this.getPublicKeyUrl(),
307 publicKeyPem: this.publicKey
311 return activityPubContextify(json)
314 isOwned = function (this: AccountInstance) {
315 return this.serverId === null
318 getFollowerSharedInboxUrls = function (this: AccountInstance) {
319 const query: Sequelize.FindOptions<AccountAttributes> = {
320 attributes: [ 'sharedInboxUrl' ],
323 model: Account['sequelize'].models.AccountFollow,
327 targetAccountId: this.id
333 return Account.findAll(query)
334 .then(accounts => accounts.map(a => a.sharedInboxUrl))
337 getFollowingUrl = function (this: AccountInstance) {
338 return this.url + '/following'
341 getFollowersUrl = function (this: AccountInstance) {
342 return this.url + '/followers'
345 getPublicKeyUrl = function (this: AccountInstance) {
346 return this.url + '#main-key'
349 // ------------------------------ STATICS ------------------------------
351 loadApplication = function () {
352 return Account.findOne({
355 model: Account['sequelize'].models.Application,
362 load = function (id: number) {
363 return Account.findById(id)
366 loadByUUID = function (uuid: string) {
367 const query: Sequelize.FindOptions<AccountAttributes> = {
373 return Account.findOne(query)
376 loadLocalByName = function (name: string) {
377 const query: Sequelize.FindOptions<AccountAttributes> = {
383 [Sequelize.Op.ne]: null
388 [Sequelize.Op.ne]: null
395 return Account.findOne(query)
398 loadByNameAndHost = function (name: string, host: string) {
399 const query: Sequelize.FindOptions<AccountAttributes> = {
405 model: Account['sequelize'].models.Server,
414 return Account.findOne(query)
417 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
418 const query: Sequelize.FindOptions<AccountAttributes> = {
425 return Account.findOne(query)
428 listByFollowersUrls = function (followersUrls: string[], transaction?: Sequelize.Transaction) {
429 const query: Sequelize.FindOptions<AccountAttributes> = {
432 [Sequelize.Op.in]: followersUrls
438 return Account.findAll(query)