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 LoadApplication = AccountMethods.LoadApplication
26 import { sendDeleteAccount } from '../../lib/activitypub/send-request'
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 loadLocalAccountByNameAndPod: AccountMethods.LoadLocalAccountByNameAndPod
35 let listOwned: AccountMethods.ListOwned
36 let listFollowerUrlsForApi: AccountMethods.ListFollowerUrlsForApi
37 let listFollowingUrlsForApi: AccountMethods.ListFollowingUrlsForApi
38 let listFollowingForApi: AccountMethods.ListFollowingForApi
39 let listFollowersForApi: AccountMethods.ListFollowersForApi
40 let isOwned: AccountMethods.IsOwned
41 let toActivityPubObject: AccountMethods.ToActivityPubObject
42 let toFormattedJSON: AccountMethods.ToFormattedJSON
43 let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
44 let getFollowingUrl: AccountMethods.GetFollowingUrl
45 let getFollowersUrl: AccountMethods.GetFollowersUrl
46 let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
48 export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
49 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
53 defaultValue: DataTypes.UUIDV4,
60 type: DataTypes.STRING,
63 usernameValid: value => {
64 const res = isUserUsernameValid(value)
65 if (res === false) throw new Error('Username is not valid.')
70 type: DataTypes.STRING,
74 const res = isAccountUrlValid(value)
75 if (res === false) throw new Error('URL is not valid.')
80 type: DataTypes.STRING,
83 publicKeyValid: value => {
84 const res = isAccountPublicKeyValid(value)
85 if (res === false) throw new Error('Public key is not valid.')
90 type: DataTypes.STRING,
93 privateKeyValid: value => {
94 const res = isAccountPrivateKeyValid(value)
95 if (res === false) throw new Error('Private key is not valid.')
100 type: DataTypes.INTEGER,
103 followersCountValid: value => {
104 const res = isAccountFollowersCountValid(value)
105 if (res === false) throw new Error('Followers count is not valid.')
110 type: DataTypes.INTEGER,
113 followersCountValid: value => {
114 const res = isAccountFollowingCountValid(value)
115 if (res === false) throw new Error('Following count is not valid.')
120 type: DataTypes.STRING,
123 inboxUrlValid: value => {
124 const res = isAccountInboxValid(value)
125 if (res === false) throw new Error('Inbox URL is not valid.')
130 type: DataTypes.STRING,
133 outboxUrlValid: value => {
134 const res = isAccountOutboxValid(value)
135 if (res === false) throw new Error('Outbox URL is not valid.')
140 type: DataTypes.STRING,
143 sharedInboxUrlValid: value => {
144 const res = isAccountSharedInboxValid(value)
145 if (res === false) throw new Error('Shared inbox URL is not valid.')
150 type: DataTypes.STRING,
153 followersUrlValid: value => {
154 const res = isAccountFollowersValid(value)
155 if (res === false) throw new Error('Followers URL is not valid.')
160 type: DataTypes.STRING,
163 followingUrlValid: value => {
164 const res = isAccountFollowingValid(value)
165 if (res === false) throw new Error('Following URL is not valid.')
179 fields: [ 'userId' ],
183 fields: [ 'applicationId' ],
187 fields: [ 'name', 'podId' ],
191 hooks: { afterDestroy }
195 const classMethods = [
197 loadAccountByPodAndUUID,
202 loadLocalAccountByNameAndPod,
204 listFollowerUrlsForApi,
205 listFollowingUrlsForApi,
209 const instanceMethods = [
213 getFollowerSharedInboxUrls,
218 addMethodsToModel(Account, classMethods, instanceMethods)
223 // ---------------------------------------------------------------------------
225 function associate (models) {
226 Account.belongsTo(models.Pod, {
234 Account.belongsTo(models.User, {
242 Account.belongsTo(models.Application, {
250 Account.hasMany(models.VideoChannel, {
259 Account.hasMany(models.AccountFollower, {
268 Account.hasMany(models.AccountFollower, {
270 name: 'targetAccountId',
278 function afterDestroy (account: AccountInstance) {
279 if (account.isOwned()) {
280 return sendDeleteAccount(account, undefined)
286 toFormattedJSON = function (this: AccountInstance) {
296 toActivityPubObject = function (this: AccountInstance) {
297 const type = this.podId ? 'Application' as 'Application' : 'Person' as 'Person'
302 following: this.getFollowingUrl(),
303 followers: this.getFollowersUrl(),
304 inbox: this.inboxUrl,
305 outbox: this.outboxUrl,
306 preferredUsername: this.name,
310 sharedInbox: this.sharedInboxUrl
314 id: this.getPublicKeyUrl(),
316 publicKeyPem: this.publicKey
320 return activityPubContextify(json)
323 isOwned = function (this: AccountInstance) {
324 return this.podId === null
327 getFollowerSharedInboxUrls = function (this: AccountInstance) {
328 const query: Sequelize.FindOptions<AccountAttributes> = {
329 attributes: [ 'sharedInboxUrl' ],
332 model: Account['sequelize'].models.AccountFollower,
334 targetAccountId: this.id
340 return Account.findAll(query)
341 .then(accounts => accounts.map(a => a.sharedInboxUrl))
344 getFollowingUrl = function (this: AccountInstance) {
345 return this.url + '/followers'
348 getFollowersUrl = function (this: AccountInstance) {
349 return this.url + '/followers'
352 getPublicKeyUrl = function (this: AccountInstance) {
353 return this.url + '#main-key'
356 // ------------------------------ STATICS ------------------------------
358 listOwned = function () {
359 const query: Sequelize.FindOptions<AccountAttributes> = {
365 return Account.findAll(query)
368 listFollowerUrlsForApi = function (id: number, start: number, count?: number) {
369 return createListFollowForApiQuery('followers', id, start, count)
372 listFollowingUrlsForApi = function (id: number, start: number, count?: number) {
373 return createListFollowForApiQuery('following', id, start, count)
376 listFollowingForApi = function (id: number, start: number, count: number, sort: string) {
381 order: [ getSort(sort) ],
384 model: Account['sequelize'].models.AccountFollow,
389 model: Account['sequelize'].models.Account,
392 include: [ Account['sequelize'].models.Pod ]
399 return Account.findAndCountAll(query).then(({ rows, count }) => {
407 listFollowersForApi = function (id: number, start: number, count: number, sort: string) {
412 order: [ getSort(sort) ],
415 model: Account['sequelize'].models.AccountFollow,
420 model: Account['sequelize'].models.Account,
423 include: [ Account['sequelize'].models.Pod ]
430 return Account.findAndCountAll(query).then(({ rows, count }) => {
438 loadApplication = function () {
439 return Account.findOne({
442 model: Account['sequelize'].model.Application,
449 load = function (id: number) {
450 return Account.findById(id)
453 loadByUUID = function (uuid: string) {
454 const query: Sequelize.FindOptions<AccountAttributes> = {
460 return Account.findOne(query)
463 loadLocalAccountByNameAndPod = function (name: string, host: string) {
464 const query: Sequelize.FindOptions<AccountAttributes> = {
468 [Sequelize.Op.ne]: null
473 model: Account['sequelize'].models.Pod,
481 return Account.findOne(query)
484 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
485 const query: Sequelize.FindOptions<AccountAttributes> = {
492 return Account.findOne(query)
495 loadAccountByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) {
496 const query: Sequelize.FindOptions<AccountAttributes> = {
504 return Account.find(query)
507 // ------------------------------ UTILS ------------------------------
509 async function createListFollowForApiQuery (type: 'followers' | 'following', id: number, start: number, count?: number) {
510 let firstJoin: string
511 let secondJoin: string
513 if (type === 'followers') {
514 firstJoin = 'targetAccountId'
515 secondJoin = 'accountId'
517 firstJoin = 'accountId'
518 secondJoin = 'targetAccountId'
521 const selections = [ '"Followers"."url" AS "url"', 'COUNT(*) AS "total"' ]
522 const tasks: Promise<any>[] = []
524 for (const selection of selections) {
525 let query = 'SELECT ' + selection + ' FROM "Account" ' +
526 'INNER JOIN "AccountFollower" ON "AccountFollower"."' + firstJoin + '" = "Account"."id" ' +
527 'INNER JOIN "Account" AS "Follows" ON "Followers"."id" = "Follows"."' + secondJoin + '" ' +
528 'WHERE "Account"."id" = $id ' +
531 if (count !== undefined) query += ', ' + count
535 type: Sequelize.QueryTypes.SELECT
537 tasks.push(Account['sequelize'].query(query, options))
540 const [ followers, [ { total } ]] = await Promise.all(tasks)
541 const urls: string[] = followers.map(f => f.url)
545 total: parseInt(total, 10)