1 import * as Sequelize from 'sequelize'
5 isAccountPublicKeyValid,
7 isAccountPrivateKeyValid,
8 isAccountFollowersCountValid,
9 isAccountFollowingCountValid,
12 isAccountSharedInboxValid,
13 isAccountFollowersValid,
14 isAccountFollowingValid,
16 } from '../../helpers'
18 import { addMethodsToModel, getSort } from '../utils'
24 } from './account-interface'
25 import { sendDeleteAccount } from '../../lib/activitypub/send-request'
26 import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
28 let Account: Sequelize.Model<AccountInstance, AccountAttributes>
29 let loadAccountByServerAndUUID: AccountMethods.LoadAccountByServerAndUUID
30 let load: AccountMethods.Load
31 let loadApplication: AccountMethods.LoadApplication
32 let loadByUUID: AccountMethods.LoadByUUID
33 let loadByUrl: AccountMethods.LoadByUrl
34 let loadLocalByName: AccountMethods.LoadLocalByName
35 let loadByNameAndHost: AccountMethods.LoadByNameAndHost
36 let listOwned: AccountMethods.ListOwned
37 let isOwned: AccountMethods.IsOwned
38 let toActivityPubObject: AccountMethods.ToActivityPubObject
39 let toFormattedJSON: AccountMethods.ToFormattedJSON
40 let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
41 let getFollowingUrl: AccountMethods.GetFollowingUrl
42 let getFollowersUrl: AccountMethods.GetFollowersUrl
43 let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
45 export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
46 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
50 defaultValue: DataTypes.UUIDV4,
57 type: DataTypes.STRING,
61 const res = isUserUsernameValid(value)
62 if (res === false) throw new Error('Name is not valid.')
67 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
71 const res = isAccountUrlValid(value)
72 if (res === false) throw new Error('URL is not valid.')
77 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
80 publicKeyValid: value => {
81 const res = isAccountPublicKeyValid(value)
82 if (res === false) throw new Error('Public key is not valid.')
87 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
90 privateKeyValid: value => {
91 const res = isAccountPrivateKeyValid(value)
92 if (res === false) throw new Error('Private key is not valid.')
97 type: DataTypes.INTEGER,
100 followersCountValid: value => {
101 const res = isAccountFollowersCountValid(value)
102 if (res === false) throw new Error('Followers count is not valid.')
107 type: DataTypes.INTEGER,
110 followingCountValid: value => {
111 const res = isAccountFollowingCountValid(value)
112 if (res === false) throw new Error('Following count is not valid.')
117 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
120 inboxUrlValid: value => {
121 const res = isAccountInboxValid(value)
122 if (res === false) throw new Error('Inbox URL is not valid.')
127 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
130 outboxUrlValid: value => {
131 const res = isAccountOutboxValid(value)
132 if (res === false) throw new Error('Outbox URL is not valid.')
137 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
140 sharedInboxUrlValid: value => {
141 const res = isAccountSharedInboxValid(value)
142 if (res === false) throw new Error('Shared inbox URL is not valid.')
147 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
150 followersUrlValid: value => {
151 const res = isAccountFollowersValid(value)
152 if (res === false) throw new Error('Followers URL is not valid.')
157 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
160 followingUrlValid: value => {
161 const res = isAccountFollowingValid(value)
162 if (res === false) throw new Error('Following URL is not valid.')
173 fields: [ 'serverId' ]
176 fields: [ 'userId' ],
180 fields: [ 'applicationId' ],
184 fields: [ 'name', 'serverId', 'applicationId' ],
188 hooks: { afterDestroy }
192 const classMethods = [
194 loadAccountByServerAndUUID,
203 const instanceMethods = [
207 getFollowerSharedInboxUrls,
212 addMethodsToModel(Account, classMethods, instanceMethods)
217 // ---------------------------------------------------------------------------
219 function associate (models) {
220 Account.belongsTo(models.Server, {
228 Account.belongsTo(models.User, {
236 Account.belongsTo(models.Application, {
238 name: 'applicationId',
244 Account.hasMany(models.VideoChannel, {
253 Account.hasMany(models.AccountFollow, {
262 Account.hasMany(models.AccountFollow, {
264 name: 'targetAccountId',
272 function afterDestroy (account: AccountInstance) {
273 if (account.isOwned()) {
274 return sendDeleteAccount(account, undefined)
280 toFormattedJSON = function (this: AccountInstance) {
281 let host = CONFIG.WEBSERVER.HOST
285 host = this.Server.host
286 score = this.Server.score as number
294 createdAt: this.createdAt,
295 updatedAt: this.updatedAt
301 toActivityPubObject = function (this: AccountInstance) {
302 const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
307 following: this.getFollowingUrl(),
308 followers: this.getFollowersUrl(),
309 inbox: this.inboxUrl,
310 outbox: this.outboxUrl,
311 preferredUsername: this.name,
315 sharedInbox: this.sharedInboxUrl
319 id: this.getPublicKeyUrl(),
321 publicKeyPem: this.publicKey
325 return activityPubContextify(json)
328 isOwned = function (this: AccountInstance) {
329 return this.serverId === null
332 getFollowerSharedInboxUrls = function (this: AccountInstance) {
333 const query: Sequelize.FindOptions<AccountAttributes> = {
334 attributes: [ 'sharedInboxUrl' ],
337 model: Account['sequelize'].models.AccountFollow,
341 targetAccountId: this.id
347 return Account.findAll(query)
348 .then(accounts => accounts.map(a => a.sharedInboxUrl))
351 getFollowingUrl = function (this: AccountInstance) {
352 return this.url + '/following'
355 getFollowersUrl = function (this: AccountInstance) {
356 return this.url + '/followers'
359 getPublicKeyUrl = function (this: AccountInstance) {
360 return this.url + '#main-key'
363 // ------------------------------ STATICS ------------------------------
365 listOwned = function () {
366 const query: Sequelize.FindOptions<AccountAttributes> = {
372 return Account.findAll(query)
375 loadApplication = function () {
376 return Account.findOne({
379 model: Account['sequelize'].models.Application,
386 load = function (id: number) {
387 return Account.findById(id)
390 loadByUUID = function (uuid: string) {
391 const query: Sequelize.FindOptions<AccountAttributes> = {
397 return Account.findOne(query)
400 loadLocalByName = function (name: string) {
401 const query: Sequelize.FindOptions<AccountAttributes> = {
407 [Sequelize.Op.ne]: null
412 [Sequelize.Op.ne]: null
419 return Account.findOne(query)
422 loadByNameAndHost = function (name: string, host: string) {
423 const query: Sequelize.FindOptions<AccountAttributes> = {
429 model: Account['sequelize'].models.Server,
438 return Account.findOne(query)
441 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
442 const query: Sequelize.FindOptions<AccountAttributes> = {
449 return Account.findOne(query)
452 loadAccountByServerAndUUID = function (uuid: string, serverId: number, transaction: Sequelize.Transaction) {
453 const query: Sequelize.FindOptions<AccountAttributes> = {
461 return Account.find(query)