1 import * as Sequelize from 'sequelize'
4 isAccountFollowersCountValid,
5 isAccountFollowingCountValid,
6 isAccountPrivateKeyValid,
7 isAccountPublicKeyValid,
10 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
11 import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
12 import { sendDeleteAccount } from '../../lib/activitypub/send/send-delete'
13 import { addMethodsToModel } from '../utils'
14 import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface'
16 let Account: Sequelize.Model<AccountInstance, AccountAttributes>
17 let load: AccountMethods.Load
18 let loadApplication: AccountMethods.LoadApplication
19 let loadByUUID: AccountMethods.LoadByUUID
20 let loadByUrl: AccountMethods.LoadByUrl
21 let loadLocalByName: AccountMethods.LoadLocalByName
22 let loadByNameAndHost: AccountMethods.LoadByNameAndHost
23 let listByFollowersUrls: AccountMethods.ListByFollowersUrls
24 let isOwned: AccountMethods.IsOwned
25 let toActivityPubObject: AccountMethods.ToActivityPubObject
26 let toFormattedJSON: AccountMethods.ToFormattedJSON
27 let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
28 let getFollowingUrl: AccountMethods.GetFollowingUrl
29 let getFollowersUrl: AccountMethods.GetFollowersUrl
30 let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
32 export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
33 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
37 defaultValue: DataTypes.UUIDV4,
44 type: DataTypes.STRING,
48 const res = isUserUsernameValid(value)
49 if (res === false) throw new Error('Name is not valid.')
54 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
58 const res = isActivityPubUrlValid(value)
59 if (res === false) throw new Error('URL is not valid.')
64 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
67 publicKeyValid: value => {
68 const res = isAccountPublicKeyValid(value)
69 if (res === false) throw new Error('Public key is not valid.')
74 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
77 privateKeyValid: value => {
78 const res = isAccountPrivateKeyValid(value)
79 if (res === false) throw new Error('Private key is not valid.')
84 type: DataTypes.INTEGER,
87 followersCountValid: value => {
88 const res = isAccountFollowersCountValid(value)
89 if (res === false) throw new Error('Followers count is not valid.')
94 type: DataTypes.INTEGER,
97 followingCountValid: value => {
98 const res = isAccountFollowingCountValid(value)
99 if (res === false) throw new Error('Following count is not valid.')
104 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
107 inboxUrlValid: value => {
108 const res = isActivityPubUrlValid(value)
109 if (res === false) throw new Error('Inbox URL is not valid.')
114 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
117 outboxUrlValid: value => {
118 const res = isActivityPubUrlValid(value)
119 if (res === false) throw new Error('Outbox URL is not valid.')
124 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
127 sharedInboxUrlValid: value => {
128 const res = isActivityPubUrlValid(value)
129 if (res === false) throw new Error('Shared inbox URL is not valid.')
134 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
137 followersUrlValid: value => {
138 const res = isActivityPubUrlValid(value)
139 if (res === false) throw new Error('Followers URL is not valid.')
144 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
147 followingUrlValid: value => {
148 const res = isActivityPubUrlValid(value)
149 if (res === false) throw new Error('Following URL is not valid.')
160 fields: [ 'serverId' ]
163 fields: [ 'userId' ],
167 fields: [ 'applicationId' ],
171 fields: [ 'name', 'serverId', 'applicationId' ],
175 hooks: { afterDestroy }
179 const classMethods = [
189 const instanceMethods = [
193 getFollowerSharedInboxUrls,
198 addMethodsToModel(Account, classMethods, instanceMethods)
203 // ---------------------------------------------------------------------------
205 function associate (models) {
206 Account.belongsTo(models.Server, {
214 Account.belongsTo(models.User, {
222 Account.belongsTo(models.Application, {
224 name: 'applicationId',
230 Account.hasMany(models.VideoChannel, {
239 Account.hasMany(models.AccountFollow, {
247 Account.hasMany(models.AccountFollow, {
249 name: 'targetAccountId',
257 function afterDestroy (account: AccountInstance) {
258 if (account.isOwned()) {
259 return sendDeleteAccount(account, undefined)
265 toFormattedJSON = function (this: AccountInstance) {
266 let host = CONFIG.WEBSERVER.HOST
270 host = this.Server.host
271 score = this.Server.score as number
279 createdAt: this.createdAt,
280 updatedAt: this.updatedAt
286 toActivityPubObject = function (this: AccountInstance) {
287 const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
292 following: this.getFollowingUrl(),
293 followers: this.getFollowersUrl(),
294 inbox: this.inboxUrl,
295 outbox: this.outboxUrl,
296 preferredUsername: this.name,
300 sharedInbox: this.sharedInboxUrl
304 id: this.getPublicKeyUrl(),
306 publicKeyPem: this.publicKey
310 return activityPubContextify(json)
313 isOwned = function (this: AccountInstance) {
314 return this.serverId === null
317 getFollowerSharedInboxUrls = function (this: AccountInstance, t: Sequelize.Transaction) {
318 const query: Sequelize.FindOptions<AccountAttributes> = {
319 attributes: [ 'sharedInboxUrl' ],
322 model: Account['sequelize'].models.AccountFollow,
326 targetAccountId: this.id
333 return Account.findAll(query)
334 .then(accounts => accounts.map(a => a.sharedInboxUrl))
337 getFollowingUrl = function (this: AccountInstance) {
338 return this.url + '/following'
341 getFollowersUrl = function (this: AccountInstance) {
342 return this.url + '/followers'
345 getPublicKeyUrl = function (this: AccountInstance) {
346 return this.url + '#main-key'
349 // ------------------------------ STATICS ------------------------------
351 loadApplication = function () {
352 return Account.findOne({
355 model: Account['sequelize'].models.Application,
362 load = function (id: number) {
363 return Account.findById(id)
366 loadByUUID = function (uuid: string) {
367 const query: Sequelize.FindOptions<AccountAttributes> = {
373 return Account.findOne(query)
376 loadLocalByName = function (name: string) {
377 const query: Sequelize.FindOptions<AccountAttributes> = {
383 [Sequelize.Op.ne]: null
388 [Sequelize.Op.ne]: null
395 return Account.findOne(query)
398 loadByNameAndHost = function (name: string, host: string) {
399 const query: Sequelize.FindOptions<AccountAttributes> = {
405 model: Account['sequelize'].models.Server,
414 return Account.findOne(query)
417 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
418 const query: Sequelize.FindOptions<AccountAttributes> = {
425 return Account.findOne(query)
428 listByFollowersUrls = function (followersUrls: string[], transaction?: Sequelize.Transaction) {
429 const query: Sequelize.FindOptions<AccountAttributes> = {
432 [Sequelize.Op.in]: followersUrls
438 return Account.findAll(query)