1 import { join } from 'path'
2 import * as Sequelize from 'sequelize'
3 import { Avatar } from '../../../shared/models/avatars/avatar.model'
6 isAccountFollowersCountValid,
7 isAccountFollowingCountValid,
8 isAccountPrivateKeyValid,
9 isAccountPublicKeyValid,
11 } from '../../helpers'
12 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
13 import { AVATARS_DIR } from '../../initializers'
14 import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
15 import { sendDeleteAccount } from '../../lib/activitypub/send/send-delete'
16 import { addMethodsToModel } from '../utils'
17 import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface'
19 let Account: Sequelize.Model<AccountInstance, AccountAttributes>
20 let load: AccountMethods.Load
21 let loadApplication: AccountMethods.LoadApplication
22 let loadByUUID: AccountMethods.LoadByUUID
23 let loadByUrl: AccountMethods.LoadByUrl
24 let loadLocalByName: AccountMethods.LoadLocalByName
25 let loadByNameAndHost: AccountMethods.LoadByNameAndHost
26 let listByFollowersUrls: AccountMethods.ListByFollowersUrls
27 let isOwned: AccountMethods.IsOwned
28 let toActivityPubObject: AccountMethods.ToActivityPubObject
29 let toFormattedJSON: AccountMethods.ToFormattedJSON
30 let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
31 let getFollowingUrl: AccountMethods.GetFollowingUrl
32 let getFollowersUrl: AccountMethods.GetFollowersUrl
33 let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
35 export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
36 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
40 defaultValue: DataTypes.UUIDV4,
47 type: DataTypes.STRING,
51 const res = isUserUsernameValid(value)
52 if (res === false) throw new Error('Name is not valid.')
57 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
61 const res = isActivityPubUrlValid(value)
62 if (res === false) throw new Error('URL is not valid.')
67 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
70 publicKeyValid: value => {
71 const res = isAccountPublicKeyValid(value)
72 if (res === false) throw new Error('Public key is not valid.')
77 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
80 privateKeyValid: value => {
81 const res = isAccountPrivateKeyValid(value)
82 if (res === false) throw new Error('Private key is not valid.')
87 type: DataTypes.INTEGER,
90 followersCountValid: value => {
91 const res = isAccountFollowersCountValid(value)
92 if (res === false) throw new Error('Followers count is not valid.')
97 type: DataTypes.INTEGER,
100 followingCountValid: value => {
101 const res = isAccountFollowingCountValid(value)
102 if (res === false) throw new Error('Following count is not valid.')
107 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
110 inboxUrlValid: value => {
111 const res = isActivityPubUrlValid(value)
112 if (res === false) throw new Error('Inbox URL is not valid.')
117 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
120 outboxUrlValid: value => {
121 const res = isActivityPubUrlValid(value)
122 if (res === false) throw new Error('Outbox URL is not valid.')
127 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
130 sharedInboxUrlValid: value => {
131 const res = isActivityPubUrlValid(value)
132 if (res === false) throw new Error('Shared inbox URL is not valid.')
137 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
140 followersUrlValid: value => {
141 const res = isActivityPubUrlValid(value)
142 if (res === false) throw new Error('Followers URL is not valid.')
147 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
150 followingUrlValid: value => {
151 const res = isActivityPubUrlValid(value)
152 if (res === false) throw new Error('Following URL is not valid.')
163 fields: [ 'serverId' ]
166 fields: [ 'userId' ],
170 fields: [ 'applicationId' ],
174 fields: [ 'name', 'serverId', 'applicationId' ],
178 hooks: { afterDestroy }
182 const classMethods = [
192 const instanceMethods = [
196 getFollowerSharedInboxUrls,
201 addMethodsToModel(Account, classMethods, instanceMethods)
206 // ---------------------------------------------------------------------------
208 function associate (models) {
209 Account.belongsTo(models.Server, {
217 Account.belongsTo(models.User, {
225 Account.belongsTo(models.Application, {
227 name: 'applicationId',
233 Account.hasMany(models.VideoChannel, {
242 Account.hasMany(models.AccountFollow, {
250 Account.hasMany(models.AccountFollow, {
252 name: 'targetAccountId',
259 Account.hasOne(models.Avatar, {
268 function afterDestroy (account: AccountInstance) {
269 if (account.isOwned()) {
270 return sendDeleteAccount(account, undefined)
276 toFormattedJSON = function (this: AccountInstance) {
277 let host = CONFIG.WEBSERVER.HOST
279 let avatar: Avatar = null
283 path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename),
284 createdAt: this.Avatar.createdAt,
285 updatedAt: this.Avatar.updatedAt
290 host = this.Server.host
291 score = this.Server.score as number
300 followingCount: this.followingCount,
301 followersCount: this.followersCount,
302 createdAt: this.createdAt,
303 updatedAt: this.updatedAt,
310 toActivityPubObject = function (this: AccountInstance) {
311 const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
316 following: this.getFollowingUrl(),
317 followers: this.getFollowersUrl(),
318 inbox: this.inboxUrl,
319 outbox: this.outboxUrl,
320 preferredUsername: this.name,
324 sharedInbox: this.sharedInboxUrl
328 id: this.getPublicKeyUrl(),
330 publicKeyPem: this.publicKey
334 return activityPubContextify(json)
337 isOwned = function (this: AccountInstance) {
338 return this.serverId === null
341 getFollowerSharedInboxUrls = function (this: AccountInstance, t: Sequelize.Transaction) {
342 const query: Sequelize.FindOptions<AccountAttributes> = {
343 attributes: [ 'sharedInboxUrl' ],
346 model: Account['sequelize'].models.AccountFollow,
350 targetAccountId: this.id
357 return Account.findAll(query)
358 .then(accounts => accounts.map(a => a.sharedInboxUrl))
361 getFollowingUrl = function (this: AccountInstance) {
362 return this.url + '/following'
365 getFollowersUrl = function (this: AccountInstance) {
366 return this.url + '/followers'
369 getPublicKeyUrl = function (this: AccountInstance) {
370 return this.url + '#main-key'
373 // ------------------------------ STATICS ------------------------------
375 loadApplication = function () {
376 return Account.findOne({
379 model: Account['sequelize'].models.Application,
386 load = function (id: number) {
387 return Account.findById(id)
390 loadByUUID = function (uuid: string) {
391 const query: Sequelize.FindOptions<AccountAttributes> = {
397 return Account.findOne(query)
400 loadLocalByName = function (name: string) {
401 const query: Sequelize.FindOptions<AccountAttributes> = {
407 [Sequelize.Op.ne]: null
412 [Sequelize.Op.ne]: null
419 return Account.findOne(query)
422 loadByNameAndHost = function (name: string, host: string) {
423 const query: Sequelize.FindOptions<AccountAttributes> = {
429 model: Account['sequelize'].models.Server,
438 return Account.findOne(query)
441 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
442 const query: Sequelize.FindOptions<AccountAttributes> = {
449 return Account.findOne(query)
452 listByFollowersUrls = function (followersUrls: string[], transaction?: Sequelize.Transaction) {
453 const query: Sequelize.FindOptions<AccountAttributes> = {
456 [Sequelize.Op.in]: followersUrls
462 return Account.findAll(query)