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 isOwned: AccountMethods.IsOwned
30 let toActivityPubObject: AccountMethods.ToActivityPubObject
31 let toFormattedJSON: AccountMethods.ToFormattedJSON
32 let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
33 let getFollowingUrl: AccountMethods.GetFollowingUrl
34 let getFollowersUrl: AccountMethods.GetFollowersUrl
35 let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
37 export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
38 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
42 defaultValue: DataTypes.UUIDV4,
49 type: DataTypes.STRING,
53 const res = isUserUsernameValid(value)
54 if (res === false) throw new Error('Name is not valid.')
59 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
63 const res = isAccountUrlValid(value)
64 if (res === false) throw new Error('URL is not valid.')
69 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
72 publicKeyValid: value => {
73 const res = isAccountPublicKeyValid(value)
74 if (res === false) throw new Error('Public key is not valid.')
79 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
82 privateKeyValid: value => {
83 const res = isAccountPrivateKeyValid(value)
84 if (res === false) throw new Error('Private key is not valid.')
89 type: DataTypes.INTEGER,
92 followersCountValid: value => {
93 const res = isAccountFollowersCountValid(value)
94 if (res === false) throw new Error('Followers count is not valid.')
99 type: DataTypes.INTEGER,
102 followingCountValid: value => {
103 const res = isAccountFollowingCountValid(value)
104 if (res === false) throw new Error('Following count is not valid.')
109 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
112 inboxUrlValid: value => {
113 const res = isAccountInboxValid(value)
114 if (res === false) throw new Error('Inbox URL is not valid.')
119 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
122 outboxUrlValid: value => {
123 const res = isAccountOutboxValid(value)
124 if (res === false) throw new Error('Outbox URL is not valid.')
129 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
132 sharedInboxUrlValid: value => {
133 const res = isAccountSharedInboxValid(value)
134 if (res === false) throw new Error('Shared inbox URL is not valid.')
139 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
142 followersUrlValid: value => {
143 const res = isAccountFollowersValid(value)
144 if (res === false) throw new Error('Followers URL is not valid.')
149 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
152 followingUrlValid: value => {
153 const res = isAccountFollowingValid(value)
154 if (res === false) throw new Error('Following URL is not valid.')
165 fields: [ 'serverId' ]
168 fields: [ 'userId' ],
172 fields: [ 'applicationId' ],
176 fields: [ 'name', 'serverId', 'applicationId' ],
180 hooks: { afterDestroy }
184 const classMethods = [
193 const instanceMethods = [
197 getFollowerSharedInboxUrls,
202 addMethodsToModel(Account, classMethods, instanceMethods)
207 // ---------------------------------------------------------------------------
209 function associate (models) {
210 Account.belongsTo(models.Server, {
218 Account.belongsTo(models.User, {
226 Account.belongsTo(models.Application, {
228 name: 'applicationId',
234 Account.hasMany(models.VideoChannel, {
243 Account.hasMany(models.AccountFollow, {
251 Account.hasMany(models.AccountFollow, {
253 name: 'targetAccountId',
261 function afterDestroy (account: AccountInstance) {
262 if (account.isOwned()) {
263 return sendDeleteAccount(account, undefined)
269 toFormattedJSON = function (this: AccountInstance) {
270 let host = CONFIG.WEBSERVER.HOST
274 host = this.Server.host
275 score = this.Server.score as number
283 createdAt: this.createdAt,
284 updatedAt: this.updatedAt
290 toActivityPubObject = function (this: AccountInstance) {
291 const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
296 following: this.getFollowingUrl(),
297 followers: this.getFollowersUrl(),
298 inbox: this.inboxUrl,
299 outbox: this.outboxUrl,
300 preferredUsername: this.name,
304 sharedInbox: this.sharedInboxUrl
308 id: this.getPublicKeyUrl(),
310 publicKeyPem: this.publicKey
314 return activityPubContextify(json)
317 isOwned = function (this: AccountInstance) {
318 return this.serverId === null
321 getFollowerSharedInboxUrls = function (this: AccountInstance) {
322 const query: Sequelize.FindOptions<AccountAttributes> = {
323 attributes: [ 'sharedInboxUrl' ],
326 model: Account['sequelize'].models.AccountFollow,
330 targetAccountId: this.id
336 return Account.findAll(query)
337 .then(accounts => accounts.map(a => a.sharedInboxUrl))
340 getFollowingUrl = function (this: AccountInstance) {
341 return this.url + '/following'
344 getFollowersUrl = function (this: AccountInstance) {
345 return this.url + '/followers'
348 getPublicKeyUrl = function (this: AccountInstance) {
349 return this.url + '#main-key'
352 // ------------------------------ STATICS ------------------------------
354 loadApplication = function () {
355 return Account.findOne({
358 model: Account['sequelize'].models.Application,
365 load = function (id: number) {
366 return Account.findById(id)
369 loadByUUID = function (uuid: string) {
370 const query: Sequelize.FindOptions<AccountAttributes> = {
376 return Account.findOne(query)
379 loadLocalByName = function (name: string) {
380 const query: Sequelize.FindOptions<AccountAttributes> = {
386 [Sequelize.Op.ne]: null
391 [Sequelize.Op.ne]: null
398 return Account.findOne(query)
401 loadByNameAndHost = function (name: string, host: string) {
402 const query: Sequelize.FindOptions<AccountAttributes> = {
408 model: Account['sequelize'].models.Server,
417 return Account.findOne(query)
420 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
421 const query: Sequelize.FindOptions<AccountAttributes> = {
428 return Account.findOne(query)