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 { AvatarModel } from '../avatar'
17 import { addMethodsToModel } from '../utils'
18 import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface'
20 let Account: Sequelize.Model<AccountInstance, AccountAttributes>
21 let load: AccountMethods.Load
22 let loadApplication: AccountMethods.LoadApplication
23 let loadByUUID: AccountMethods.LoadByUUID
24 let loadByUrl: AccountMethods.LoadByUrl
25 let loadLocalByName: AccountMethods.LoadLocalByName
26 let loadByNameAndHost: AccountMethods.LoadByNameAndHost
27 let listByFollowersUrls: AccountMethods.ListByFollowersUrls
28 let isOwned: AccountMethods.IsOwned
29 let toActivityPubObject: AccountMethods.ToActivityPubObject
30 let toFormattedJSON: AccountMethods.ToFormattedJSON
31 let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
32 let getFollowingUrl: AccountMethods.GetFollowingUrl
33 let getFollowersUrl: AccountMethods.GetFollowersUrl
34 let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
36 export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
37 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
41 defaultValue: DataTypes.UUIDV4,
48 type: DataTypes.STRING,
52 const res = isUserUsernameValid(value)
53 if (res === false) throw new Error('Name is not valid.')
58 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
62 const res = isActivityPubUrlValid(value)
63 if (res === false) throw new Error('URL is not valid.')
68 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
71 publicKeyValid: value => {
72 const res = isAccountPublicKeyValid(value)
73 if (res === false) throw new Error('Public key is not valid.')
78 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
81 privateKeyValid: value => {
82 const res = isAccountPrivateKeyValid(value)
83 if (res === false) throw new Error('Private key is not valid.')
88 type: DataTypes.INTEGER,
91 followersCountValid: value => {
92 const res = isAccountFollowersCountValid(value)
93 if (res === false) throw new Error('Followers count is not valid.')
98 type: DataTypes.INTEGER,
101 followingCountValid: value => {
102 const res = isAccountFollowingCountValid(value)
103 if (res === false) throw new Error('Following count is not valid.')
108 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
111 inboxUrlValid: value => {
112 const res = isActivityPubUrlValid(value)
113 if (res === false) throw new Error('Inbox URL is not valid.')
118 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
121 outboxUrlValid: value => {
122 const res = isActivityPubUrlValid(value)
123 if (res === false) throw new Error('Outbox URL is not valid.')
128 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
131 sharedInboxUrlValid: value => {
132 const res = isActivityPubUrlValid(value)
133 if (res === false) throw new Error('Shared inbox URL is not valid.')
138 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
141 followersUrlValid: value => {
142 const res = isActivityPubUrlValid(value)
143 if (res === false) throw new Error('Followers URL is not valid.')
148 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
151 followingUrlValid: value => {
152 const res = isActivityPubUrlValid(value)
153 if (res === false) throw new Error('Following URL is not valid.')
164 fields: [ 'serverId' ]
167 fields: [ 'userId' ],
171 fields: [ 'applicationId' ],
175 fields: [ 'name', 'serverId', 'applicationId' ],
179 hooks: { afterDestroy }
183 const classMethods = [
193 const instanceMethods = [
197 getFollowerSharedInboxUrls,
202 addMethodsToModel(Account, classMethods, instanceMethods)
207 // ---------------------------------------------------------------------------
209 function associate (models) {
210 Account.belongsTo(models.Server, {
218 Account.belongsTo(models.User, {
226 Account.belongsTo(models.Application, {
228 name: 'applicationId',
234 Account.hasMany(models.VideoChannel, {
243 Account.hasMany(models.AccountFollow, {
251 Account.hasMany(models.AccountFollow, {
253 name: 'targetAccountId',
260 Account.hasOne(models.Avatar, {
269 function afterDestroy (account: AccountInstance) {
270 if (account.isOwned()) {
271 return sendDeleteAccount(account, undefined)
277 toFormattedJSON = function (this: AccountInstance) {
278 let host = CONFIG.WEBSERVER.HOST
280 let avatar: Avatar = null
284 path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename),
285 createdAt: this.Avatar.createdAt,
286 updatedAt: this.Avatar.updatedAt
291 host = this.Server.host
292 score = this.Server.score as number
301 followingCount: this.followingCount,
302 followersCount: this.followersCount,
303 createdAt: this.createdAt,
304 updatedAt: this.updatedAt,
311 toActivityPubObject = function (this: AccountInstance) {
312 const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
317 following: this.getFollowingUrl(),
318 followers: this.getFollowersUrl(),
319 inbox: this.inboxUrl,
320 outbox: this.outboxUrl,
321 preferredUsername: this.name,
325 sharedInbox: this.sharedInboxUrl
329 id: this.getPublicKeyUrl(),
331 publicKeyPem: this.publicKey
335 return activityPubContextify(json)
338 isOwned = function (this: AccountInstance) {
339 return this.serverId === null
342 getFollowerSharedInboxUrls = function (this: AccountInstance, t: Sequelize.Transaction) {
343 const query: Sequelize.FindOptions<AccountAttributes> = {
344 attributes: [ 'sharedInboxUrl' ],
347 model: Account['sequelize'].models.AccountFollow,
351 targetAccountId: this.id
358 return Account.findAll(query)
359 .then(accounts => accounts.map(a => a.sharedInboxUrl))
362 getFollowingUrl = function (this: AccountInstance) {
363 return this.url + '/following'
366 getFollowersUrl = function (this: AccountInstance) {
367 return this.url + '/followers'
370 getPublicKeyUrl = function (this: AccountInstance) {
371 return this.url + '#main-key'
374 // ------------------------------ STATICS ------------------------------
376 loadApplication = function () {
377 return Account.findOne({
380 model: Account['sequelize'].models.Application,
387 load = function (id: number) {
388 return Account.findById(id)
391 loadByUUID = function (uuid: string) {
392 const query: Sequelize.FindOptions<AccountAttributes> = {
398 return Account.findOne(query)
401 loadLocalByName = function (name: string) {
402 const query: Sequelize.FindOptions<AccountAttributes> = {
408 [Sequelize.Op.ne]: null
413 [Sequelize.Op.ne]: null
420 return Account.findOne(query)
423 loadByNameAndHost = function (name: string, host: string) {
424 const query: Sequelize.FindOptions<AccountAttributes> = {
430 model: Account['sequelize'].models.Server,
439 return Account.findOne(query)
442 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
443 const query: Sequelize.FindOptions<AccountAttributes> = {
450 return Account.findOne(query)
453 listByFollowersUrls = function (followersUrls: string[], transaction?: Sequelize.Transaction) {
454 const query: Sequelize.FindOptions<AccountAttributes> = {
457 [Sequelize.Op.in]: followersUrls
463 return Account.findAll(query)