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 loadAccountByServerAndUUID: AccountMethods.LoadAccountByServerAndUUID
24 let load: AccountMethods.Load
25 let loadApplication: AccountMethods.LoadApplication
26 let loadByUUID: AccountMethods.LoadByUUID
27 let loadByUrl: AccountMethods.LoadByUrl
28 let loadLocalByName: AccountMethods.LoadLocalByName
29 let loadByNameAndHost: AccountMethods.LoadByNameAndHost
30 let listOwned: AccountMethods.ListOwned
31 let isOwned: AccountMethods.IsOwned
32 let toActivityPubObject: AccountMethods.ToActivityPubObject
33 let toFormattedJSON: AccountMethods.ToFormattedJSON
34 let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
35 let getFollowingUrl: AccountMethods.GetFollowingUrl
36 let getFollowersUrl: AccountMethods.GetFollowersUrl
37 let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
39 export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
40 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
44 defaultValue: DataTypes.UUIDV4,
51 type: DataTypes.STRING,
55 const res = isUserUsernameValid(value)
56 if (res === false) throw new Error('Name is not valid.')
61 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
65 const res = isAccountUrlValid(value)
66 if (res === false) throw new Error('URL is not valid.')
71 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
74 publicKeyValid: value => {
75 const res = isAccountPublicKeyValid(value)
76 if (res === false) throw new Error('Public key is not valid.')
81 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
84 privateKeyValid: value => {
85 const res = isAccountPrivateKeyValid(value)
86 if (res === false) throw new Error('Private key is not valid.')
91 type: DataTypes.INTEGER,
94 followersCountValid: value => {
95 const res = isAccountFollowersCountValid(value)
96 if (res === false) throw new Error('Followers count is not valid.')
101 type: DataTypes.INTEGER,
104 followingCountValid: value => {
105 const res = isAccountFollowingCountValid(value)
106 if (res === false) throw new Error('Following count is not valid.')
111 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
114 inboxUrlValid: value => {
115 const res = isAccountInboxValid(value)
116 if (res === false) throw new Error('Inbox URL is not valid.')
121 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
124 outboxUrlValid: value => {
125 const res = isAccountOutboxValid(value)
126 if (res === false) throw new Error('Outbox URL is not valid.')
131 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
134 sharedInboxUrlValid: value => {
135 const res = isAccountSharedInboxValid(value)
136 if (res === false) throw new Error('Shared inbox URL is not valid.')
141 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
144 followersUrlValid: value => {
145 const res = isAccountFollowersValid(value)
146 if (res === false) throw new Error('Followers URL is not valid.')
151 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
154 followingUrlValid: value => {
155 const res = isAccountFollowingValid(value)
156 if (res === false) throw new Error('Following URL is not valid.')
167 fields: [ 'serverId' ]
170 fields: [ 'userId' ],
174 fields: [ 'applicationId' ],
178 fields: [ 'name', 'serverId', 'applicationId' ],
182 hooks: { afterDestroy }
186 const classMethods = [
188 loadAccountByServerAndUUID,
197 const instanceMethods = [
201 getFollowerSharedInboxUrls,
206 addMethodsToModel(Account, classMethods, instanceMethods)
211 // ---------------------------------------------------------------------------
213 function associate (models) {
214 Account.belongsTo(models.Server, {
222 Account.belongsTo(models.User, {
230 Account.belongsTo(models.Application, {
232 name: 'applicationId',
238 Account.hasMany(models.VideoChannel, {
247 Account.hasMany(models.AccountFollow, {
255 Account.hasMany(models.AccountFollow, {
257 name: 'targetAccountId',
265 function afterDestroy (account: AccountInstance) {
266 if (account.isOwned()) {
267 return sendDeleteAccount(account, undefined)
273 toFormattedJSON = function (this: AccountInstance) {
274 let host = CONFIG.WEBSERVER.HOST
278 host = this.Server.host
279 score = this.Server.score as number
287 createdAt: this.createdAt,
288 updatedAt: this.updatedAt
294 toActivityPubObject = function (this: AccountInstance) {
295 const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
300 following: this.getFollowingUrl(),
301 followers: this.getFollowersUrl(),
302 inbox: this.inboxUrl,
303 outbox: this.outboxUrl,
304 preferredUsername: this.name,
308 sharedInbox: this.sharedInboxUrl
312 id: this.getPublicKeyUrl(),
314 publicKeyPem: this.publicKey
318 return activityPubContextify(json)
321 isOwned = function (this: AccountInstance) {
322 return this.serverId === null
325 getFollowerSharedInboxUrls = function (this: AccountInstance) {
326 const query: Sequelize.FindOptions<AccountAttributes> = {
327 attributes: [ 'sharedInboxUrl' ],
330 model: Account['sequelize'].models.AccountFollow,
334 targetAccountId: this.id
340 return Account.findAll(query)
341 .then(accounts => accounts.map(a => a.sharedInboxUrl))
344 getFollowingUrl = function (this: AccountInstance) {
345 return this.url + '/following'
348 getFollowersUrl = function (this: AccountInstance) {
349 return this.url + '/followers'
352 getPublicKeyUrl = function (this: AccountInstance) {
353 return this.url + '#main-key'
356 // ------------------------------ STATICS ------------------------------
358 listOwned = function () {
359 const query: Sequelize.FindOptions<AccountAttributes> = {
365 return Account.findAll(query)
368 loadApplication = function () {
369 return Account.findOne({
372 model: Account['sequelize'].models.Application,
379 load = function (id: number) {
380 return Account.findById(id)
383 loadByUUID = function (uuid: string) {
384 const query: Sequelize.FindOptions<AccountAttributes> = {
390 return Account.findOne(query)
393 loadLocalByName = function (name: string) {
394 const query: Sequelize.FindOptions<AccountAttributes> = {
400 [Sequelize.Op.ne]: null
405 [Sequelize.Op.ne]: null
412 return Account.findOne(query)
415 loadByNameAndHost = function (name: string, host: string) {
416 const query: Sequelize.FindOptions<AccountAttributes> = {
422 model: Account['sequelize'].models.Server,
431 return Account.findOne(query)
434 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
435 const query: Sequelize.FindOptions<AccountAttributes> = {
442 return Account.findOne(query)
445 loadAccountByServerAndUUID = function (uuid: string, serverId: number, transaction: Sequelize.Transaction) {
446 const query: Sequelize.FindOptions<AccountAttributes> = {
454 return Account.find(query)