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 loadAccountByPodAndUUID: AccountMethods.LoadAccountByPodAndUUID
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.')
176 fields: [ 'userId' ],
180 fields: [ 'applicationId' ],
184 fields: [ 'name', 'podId', 'applicationId' ],
188 hooks: { afterDestroy }
192 const classMethods = [
194 loadAccountByPodAndUUID,
203 const instanceMethods = [
207 getFollowerSharedInboxUrls,
212 addMethodsToModel(Account, classMethods, instanceMethods)
217 // ---------------------------------------------------------------------------
219 function associate (models) {
220 Account.belongsTo(models.Pod, {
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 = this.Pod ? this.Pod.host : CONFIG.WEBSERVER.HOST
292 toActivityPubObject = function (this: AccountInstance) {
293 const type = this.podId ? 'Application' as 'Application' : 'Person' as 'Person'
298 following: this.getFollowingUrl(),
299 followers: this.getFollowersUrl(),
300 inbox: this.inboxUrl,
301 outbox: this.outboxUrl,
302 preferredUsername: this.name,
306 sharedInbox: this.sharedInboxUrl
310 id: this.getPublicKeyUrl(),
312 publicKeyPem: this.publicKey
316 return activityPubContextify(json)
319 isOwned = function (this: AccountInstance) {
320 return this.podId === null
323 getFollowerSharedInboxUrls = function (this: AccountInstance) {
324 const query: Sequelize.FindOptions<AccountAttributes> = {
325 attributes: [ 'sharedInboxUrl' ],
328 model: Account['sequelize'].models.AccountFollow,
332 targetAccountId: this.id
338 return Account.findAll(query)
339 .then(accounts => accounts.map(a => a.sharedInboxUrl))
342 getFollowingUrl = function (this: AccountInstance) {
343 return this.url + '/following'
346 getFollowersUrl = function (this: AccountInstance) {
347 return this.url + '/followers'
350 getPublicKeyUrl = function (this: AccountInstance) {
351 return this.url + '#main-key'
354 // ------------------------------ STATICS ------------------------------
356 listOwned = function () {
357 const query: Sequelize.FindOptions<AccountAttributes> = {
363 return Account.findAll(query)
366 loadApplication = function () {
367 return Account.findOne({
370 model: Account['sequelize'].models.Application,
377 load = function (id: number) {
378 return Account.findById(id)
381 loadByUUID = function (uuid: string) {
382 const query: Sequelize.FindOptions<AccountAttributes> = {
388 return Account.findOne(query)
391 loadLocalByName = function (name: string) {
392 const query: Sequelize.FindOptions<AccountAttributes> = {
398 [Sequelize.Op.ne]: null
403 [Sequelize.Op.ne]: null
410 return Account.findOne(query)
413 loadByNameAndHost = function (name: string, host: string) {
414 const query: Sequelize.FindOptions<AccountAttributes> = {
420 model: Account['sequelize'].models.Pod,
429 return Account.findOne(query)
432 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
433 const query: Sequelize.FindOptions<AccountAttributes> = {
440 return Account.findOne(query)
443 loadAccountByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) {
444 const query: Sequelize.FindOptions<AccountAttributes> = {
452 return Account.find(query)