1 import * as Sequelize from 'sequelize'
5 isAccountFollowersCountValid,
6 isAccountFollowersValid,
7 isAccountFollowingCountValid,
8 isAccountFollowingValid,
11 isAccountPrivateKeyValid,
12 isAccountPublicKeyValid,
13 isAccountSharedInboxValid,
16 } from '../../helpers'
17 import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
18 import { sendDeleteAccount } from '../../lib/activitypub/send-request'
20 import { addMethodsToModel } from '../utils'
21 import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface'
23 let Account: Sequelize.Model<AccountInstance, AccountAttributes>
24 let loadAccountByServerAndUUID: AccountMethods.LoadAccountByServerAndUUID
25 let load: AccountMethods.Load
26 let loadApplication: AccountMethods.LoadApplication
27 let loadByUUID: AccountMethods.LoadByUUID
28 let loadByUrl: AccountMethods.LoadByUrl
29 let loadLocalByName: AccountMethods.LoadLocalByName
30 let loadByNameAndHost: AccountMethods.LoadByNameAndHost
31 let listOwned: AccountMethods.ListOwned
32 let isOwned: AccountMethods.IsOwned
33 let toActivityPubObject: AccountMethods.ToActivityPubObject
34 let toFormattedJSON: AccountMethods.ToFormattedJSON
35 let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
36 let getFollowingUrl: AccountMethods.GetFollowingUrl
37 let getFollowersUrl: AccountMethods.GetFollowersUrl
38 let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
40 export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
41 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
45 defaultValue: DataTypes.UUIDV4,
52 type: DataTypes.STRING,
56 const res = isUserUsernameValid(value)
57 if (res === false) throw new Error('Name is not valid.')
62 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
66 const res = isAccountUrlValid(value)
67 if (res === false) throw new Error('URL is not valid.')
72 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
75 publicKeyValid: value => {
76 const res = isAccountPublicKeyValid(value)
77 if (res === false) throw new Error('Public key is not valid.')
82 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
85 privateKeyValid: value => {
86 const res = isAccountPrivateKeyValid(value)
87 if (res === false) throw new Error('Private key is not valid.')
92 type: DataTypes.INTEGER,
95 followersCountValid: value => {
96 const res = isAccountFollowersCountValid(value)
97 if (res === false) throw new Error('Followers count is not valid.')
102 type: DataTypes.INTEGER,
105 followingCountValid: value => {
106 const res = isAccountFollowingCountValid(value)
107 if (res === false) throw new Error('Following count is not valid.')
112 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
115 inboxUrlValid: value => {
116 const res = isAccountInboxValid(value)
117 if (res === false) throw new Error('Inbox URL is not valid.')
122 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
125 outboxUrlValid: value => {
126 const res = isAccountOutboxValid(value)
127 if (res === false) throw new Error('Outbox URL is not valid.')
132 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
135 sharedInboxUrlValid: value => {
136 const res = isAccountSharedInboxValid(value)
137 if (res === false) throw new Error('Shared inbox URL is not valid.')
142 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
145 followersUrlValid: value => {
146 const res = isAccountFollowersValid(value)
147 if (res === false) throw new Error('Followers URL is not valid.')
152 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
155 followingUrlValid: value => {
156 const res = isAccountFollowingValid(value)
157 if (res === false) throw new Error('Following URL is not valid.')
168 fields: [ 'serverId' ]
171 fields: [ 'userId' ],
175 fields: [ 'applicationId' ],
179 fields: [ 'name', 'serverId', 'applicationId' ],
183 hooks: { afterDestroy }
187 const classMethods = [
189 loadAccountByServerAndUUID,
198 const instanceMethods = [
202 getFollowerSharedInboxUrls,
207 addMethodsToModel(Account, classMethods, instanceMethods)
212 // ---------------------------------------------------------------------------
214 function associate (models) {
215 Account.belongsTo(models.Server, {
223 Account.belongsTo(models.User, {
231 Account.belongsTo(models.Application, {
233 name: 'applicationId',
239 Account.hasMany(models.VideoChannel, {
248 Account.hasMany(models.AccountFollow, {
256 Account.hasMany(models.AccountFollow, {
258 name: 'targetAccountId',
266 function afterDestroy (account: AccountInstance) {
267 if (account.isOwned()) {
268 return sendDeleteAccount(account, undefined)
274 toFormattedJSON = function (this: AccountInstance) {
275 let host = CONFIG.WEBSERVER.HOST
279 host = this.Server.host
280 score = this.Server.score as number
288 createdAt: this.createdAt,
289 updatedAt: this.updatedAt
295 toActivityPubObject = function (this: AccountInstance) {
296 const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
301 following: this.getFollowingUrl(),
302 followers: this.getFollowersUrl(),
303 inbox: this.inboxUrl,
304 outbox: this.outboxUrl,
305 preferredUsername: this.name,
309 sharedInbox: this.sharedInboxUrl
313 id: this.getPublicKeyUrl(),
315 publicKeyPem: this.publicKey
319 return activityPubContextify(json)
322 isOwned = function (this: AccountInstance) {
323 return this.serverId === null
326 getFollowerSharedInboxUrls = function (this: AccountInstance) {
327 const query: Sequelize.FindOptions<AccountAttributes> = {
328 attributes: [ 'sharedInboxUrl' ],
331 model: Account['sequelize'].models.AccountFollow,
335 targetAccountId: this.id
341 return Account.findAll(query)
342 .then(accounts => accounts.map(a => a.sharedInboxUrl))
345 getFollowingUrl = function (this: AccountInstance) {
346 return this.url + '/following'
349 getFollowersUrl = function (this: AccountInstance) {
350 return this.url + '/followers'
353 getPublicKeyUrl = function (this: AccountInstance) {
354 return this.url + '#main-key'
357 // ------------------------------ STATICS ------------------------------
359 listOwned = function () {
360 const query: Sequelize.FindOptions<AccountAttributes> = {
366 return Account.findAll(query)
369 loadApplication = function () {
370 return Account.findOne({
373 model: Account['sequelize'].models.Application,
380 load = function (id: number) {
381 return Account.findById(id)
384 loadByUUID = function (uuid: string) {
385 const query: Sequelize.FindOptions<AccountAttributes> = {
391 return Account.findOne(query)
394 loadLocalByName = function (name: string) {
395 const query: Sequelize.FindOptions<AccountAttributes> = {
401 [Sequelize.Op.ne]: null
406 [Sequelize.Op.ne]: null
413 return Account.findOne(query)
416 loadByNameAndHost = function (name: string, host: string) {
417 const query: Sequelize.FindOptions<AccountAttributes> = {
423 model: Account['sequelize'].models.Server,
432 return Account.findOne(query)
435 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
436 const query: Sequelize.FindOptions<AccountAttributes> = {
443 return Account.findOne(query)
446 loadAccountByServerAndUUID = function (uuid: string, serverId: number, transaction: Sequelize.Transaction) {
447 const query: Sequelize.FindOptions<AccountAttributes> = {
455 return Account.find(query)