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, {
261 Account.hasMany(models.AccountFollow, {
263 name: 'targetAccountId',
270 function afterDestroy (account: AccountInstance) {
271 if (account.isOwned()) {
272 return sendDeleteAccount(account, undefined)
278 toFormattedJSON = function (this: AccountInstance) {
279 let host = CONFIG.WEBSERVER.HOST
283 host = this.Server.host
284 score = this.Server.score as number
292 createdAt: this.createdAt,
293 updatedAt: this.updatedAt
299 toActivityPubObject = function (this: AccountInstance) {
300 const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
305 following: this.getFollowingUrl(),
306 followers: this.getFollowersUrl(),
307 inbox: this.inboxUrl,
308 outbox: this.outboxUrl,
309 preferredUsername: this.name,
313 sharedInbox: this.sharedInboxUrl
317 id: this.getPublicKeyUrl(),
319 publicKeyPem: this.publicKey
323 return activityPubContextify(json)
326 isOwned = function (this: AccountInstance) {
327 return this.serverId === null
330 getFollowerSharedInboxUrls = function (this: AccountInstance) {
331 const query: Sequelize.FindOptions<AccountAttributes> = {
332 attributes: [ 'sharedInboxUrl' ],
335 model: Account['sequelize'].models.AccountFollow,
339 targetAccountId: this.id
345 return Account.findAll(query)
346 .then(accounts => accounts.map(a => a.sharedInboxUrl))
349 getFollowingUrl = function (this: AccountInstance) {
350 return this.url + '/following'
353 getFollowersUrl = function (this: AccountInstance) {
354 return this.url + '/followers'
357 getPublicKeyUrl = function (this: AccountInstance) {
358 return this.url + '#main-key'
361 // ------------------------------ STATICS ------------------------------
363 listOwned = function () {
364 const query: Sequelize.FindOptions<AccountAttributes> = {
370 return Account.findAll(query)
373 loadApplication = function () {
374 return Account.findOne({
377 model: Account['sequelize'].models.Application,
384 load = function (id: number) {
385 return Account.findById(id)
388 loadByUUID = function (uuid: string) {
389 const query: Sequelize.FindOptions<AccountAttributes> = {
395 return Account.findOne(query)
398 loadLocalByName = function (name: string) {
399 const query: Sequelize.FindOptions<AccountAttributes> = {
405 [Sequelize.Op.ne]: null
410 [Sequelize.Op.ne]: null
417 return Account.findOne(query)
420 loadByNameAndHost = function (name: string, host: string) {
421 const query: Sequelize.FindOptions<AccountAttributes> = {
427 model: Account['sequelize'].models.Server,
436 return Account.findOne(query)
439 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
440 const query: Sequelize.FindOptions<AccountAttributes> = {
447 return Account.findOne(query)
450 loadAccountByServerAndUUID = function (uuid: string, serverId: number, transaction: Sequelize.Transaction) {
451 const query: Sequelize.FindOptions<AccountAttributes> = {
459 return Account.find(query)