1 import * as Sequelize from 'sequelize'
4 isAccountFollowersCountValid,
5 isAccountFollowersValid,
6 isAccountFollowingCountValid,
7 isAccountFollowingValid,
10 isAccountPrivateKeyValid,
11 isAccountPublicKeyValid,
12 isAccountSharedInboxValid,
15 } from '../../helpers'
16 import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
17 import { sendDeleteAccount } from '../../lib/activitypub/send/send-delete'
19 import { addMethodsToModel } from '../utils'
20 import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface'
22 let Account: Sequelize.Model<AccountInstance, AccountAttributes>
23 let load: AccountMethods.Load
24 let loadApplication: AccountMethods.LoadApplication
25 let loadByUUID: AccountMethods.LoadByUUID
26 let loadByUrl: AccountMethods.LoadByUrl
27 let loadLocalByName: AccountMethods.LoadLocalByName
28 let loadByNameAndHost: AccountMethods.LoadByNameAndHost
29 let listByFollowersUrls: AccountMethods.ListByFollowersUrls
30 let isOwned: AccountMethods.IsOwned
31 let toActivityPubObject: AccountMethods.ToActivityPubObject
32 let toFormattedJSON: AccountMethods.ToFormattedJSON
33 let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
34 let getFollowingUrl: AccountMethods.GetFollowingUrl
35 let getFollowersUrl: AccountMethods.GetFollowersUrl
36 let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
38 export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
39 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
43 defaultValue: DataTypes.UUIDV4,
50 type: DataTypes.STRING,
54 const res = isUserUsernameValid(value)
55 if (res === false) throw new Error('Name is not valid.')
60 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
64 const res = isAccountUrlValid(value)
65 if (res === false) throw new Error('URL is not valid.')
70 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
73 publicKeyValid: value => {
74 const res = isAccountPublicKeyValid(value)
75 if (res === false) throw new Error('Public key is not valid.')
80 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
83 privateKeyValid: value => {
84 const res = isAccountPrivateKeyValid(value)
85 if (res === false) throw new Error('Private key is not valid.')
90 type: DataTypes.INTEGER,
93 followersCountValid: value => {
94 const res = isAccountFollowersCountValid(value)
95 if (res === false) throw new Error('Followers count is not valid.')
100 type: DataTypes.INTEGER,
103 followingCountValid: value => {
104 const res = isAccountFollowingCountValid(value)
105 if (res === false) throw new Error('Following count is not valid.')
110 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
113 inboxUrlValid: value => {
114 const res = isAccountInboxValid(value)
115 if (res === false) throw new Error('Inbox URL is not valid.')
120 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
123 outboxUrlValid: value => {
124 const res = isAccountOutboxValid(value)
125 if (res === false) throw new Error('Outbox URL is not valid.')
130 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
133 sharedInboxUrlValid: value => {
134 const res = isAccountSharedInboxValid(value)
135 if (res === false) throw new Error('Shared inbox URL is not valid.')
140 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
143 followersUrlValid: value => {
144 const res = isAccountFollowersValid(value)
145 if (res === false) throw new Error('Followers URL is not valid.')
150 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
153 followingUrlValid: value => {
154 const res = isAccountFollowingValid(value)
155 if (res === false) throw new Error('Following URL is not valid.')
166 fields: [ 'serverId' ]
169 fields: [ 'userId' ],
173 fields: [ 'applicationId' ],
177 fields: [ 'name', 'serverId', 'applicationId' ],
181 hooks: { afterDestroy }
185 const classMethods = [
195 const instanceMethods = [
199 getFollowerSharedInboxUrls,
204 addMethodsToModel(Account, classMethods, instanceMethods)
209 // ---------------------------------------------------------------------------
211 function associate (models) {
212 Account.belongsTo(models.Server, {
220 Account.belongsTo(models.User, {
228 Account.belongsTo(models.Application, {
230 name: 'applicationId',
236 Account.hasMany(models.VideoChannel, {
245 Account.hasMany(models.AccountFollow, {
253 Account.hasMany(models.AccountFollow, {
255 name: 'targetAccountId',
263 function afterDestroy (account: AccountInstance) {
264 if (account.isOwned()) {
265 return sendDeleteAccount(account, undefined)
271 toFormattedJSON = function (this: AccountInstance) {
272 let host = CONFIG.WEBSERVER.HOST
276 host = this.Server.host
277 score = this.Server.score as number
285 createdAt: this.createdAt,
286 updatedAt: this.updatedAt
292 toActivityPubObject = function (this: AccountInstance) {
293 const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
298 following: this.getFollowingUrl(),
299 followers: this.getFollowersUrl(),
300 inbox: this.inboxUrl,
301 outbox: this.outboxUrl,
302 preferredUsername: this.name,
306 sharedInbox: this.sharedInboxUrl
310 id: this.getPublicKeyUrl(),
312 publicKeyPem: this.publicKey
316 return activityPubContextify(json)
319 isOwned = function (this: AccountInstance) {
320 return this.serverId === null
323 getFollowerSharedInboxUrls = function (this: AccountInstance) {
324 const query: Sequelize.FindOptions<AccountAttributes> = {
325 attributes: [ 'sharedInboxUrl' ],
328 model: Account['sequelize'].models.AccountFollow,
332 targetAccountId: this.id
338 return Account.findAll(query)
339 .then(accounts => accounts.map(a => a.sharedInboxUrl))
342 getFollowingUrl = function (this: AccountInstance) {
343 return this.url + '/following'
346 getFollowersUrl = function (this: AccountInstance) {
347 return this.url + '/followers'
350 getPublicKeyUrl = function (this: AccountInstance) {
351 return this.url + '#main-key'
354 // ------------------------------ STATICS ------------------------------
356 loadApplication = function () {
357 return Account.findOne({
360 model: Account['sequelize'].models.Application,
367 load = function (id: number) {
368 return Account.findById(id)
371 loadByUUID = function (uuid: string) {
372 const query: Sequelize.FindOptions<AccountAttributes> = {
378 return Account.findOne(query)
381 loadLocalByName = function (name: string) {
382 const query: Sequelize.FindOptions<AccountAttributes> = {
388 [Sequelize.Op.ne]: null
393 [Sequelize.Op.ne]: null
400 return Account.findOne(query)
403 loadByNameAndHost = function (name: string, host: string) {
404 const query: Sequelize.FindOptions<AccountAttributes> = {
410 model: Account['sequelize'].models.Server,
419 return Account.findOne(query)
422 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
423 const query: Sequelize.FindOptions<AccountAttributes> = {
430 return Account.findOne(query)
433 listByFollowersUrls = function (followersUrls: string[], transaction?: Sequelize.Transaction) {
434 const query: Sequelize.FindOptions<AccountAttributes> = {
437 [Sequelize.Op.in]: followersUrls
443 return Account.findAll(query)