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,
201 loadLocalAccountByNameAndPod,
203 listFollowerUrlsForApi,
204 listFollowingUrlsForApi,
208 const instanceMethods = [
212 getFollowerSharedInboxUrls,
217 addMethodsToModel(Account, classMethods, instanceMethods)
222 // ---------------------------------------------------------------------------
224 function associate (models) {
225 Account.belongsTo(models.Pod, {
233 Account.belongsTo(models.User, {
241 Account.belongsTo(models.Application, {
249 Account.hasMany(models.VideoChannel, {
258 Account.hasMany(models.AccountFollower, {
267 Account.hasMany(models.AccountFollower, {
269 name: 'targetAccountId',
277 function afterDestroy (account: AccountInstance) {
278 if (account.isOwned()) {
279 return sendDeleteAccount(account, undefined)
285 toFormattedJSON = function (this: AccountInstance) {
295 toActivityPubObject = function (this: AccountInstance) {
296 const type = this.podId ? 'Application' as 'Application' : 'Person' as 'Person'
301 following: this.getFollowingUrl(),
302 followers: this.getFollowersUrl(),
303 inbox: this.inboxUrl,
304 outbox: this.outboxUrl,
305 preferredUsername: this.name,
309 sharedInbox: this.sharedInboxUrl
313 id: this.getPublicKeyUrl(),
315 publicKeyPem: this.publicKey
319 return activityPubContextify(json)
322 isOwned = function (this: AccountInstance) {
323 return this.podId === null
326 getFollowerSharedInboxUrls = function (this: AccountInstance) {
327 const query: Sequelize.FindOptions<AccountAttributes> = {
328 attributes: [ 'sharedInboxUrl' ],
331 model: Account['sequelize'].models.AccountFollower,
333 targetAccountId: this.id
339 return Account.findAll(query)
340 .then(accounts => accounts.map(a => a.sharedInboxUrl))
343 getFollowingUrl = function (this: AccountInstance) {
344 return this.url + '/followers'
347 getFollowersUrl = function (this: AccountInstance) {
348 return this.url + '/followers'
351 getPublicKeyUrl = function (this: AccountInstance) {
352 return this.url + '#main-key'
355 // ------------------------------ STATICS ------------------------------
357 listOwned = function () {
358 const query: Sequelize.FindOptions<AccountAttributes> = {
364 return Account.findAll(query)
367 listFollowerUrlsForApi = function (id: number, start: number, count?: number) {
368 return createListFollowForApiQuery('followers', id, start, count)
371 listFollowingUrlsForApi = function (id: number, start: number, count?: number) {
372 return createListFollowForApiQuery('following', id, start, count)
375 listFollowingForApi = function (id: number, start: number, count: number, sort: string) {
380 order: [ getSort(sort) ],
383 model: Account['sequelize'].models.AccountFollow,
388 model: Account['sequelize'].models.Account,
391 include: [ Account['sequelize'].models.Pod ]
398 return Account.findAndCountAll(query).then(({ rows, count }) => {
406 listFollowersForApi = function (id: number, start: number, count: number, sort: string) {
411 order: [ getSort(sort) ],
414 model: Account['sequelize'].models.AccountFollow,
419 model: Account['sequelize'].models.Account,
422 include: [ Account['sequelize'].models.Pod ]
429 return Account.findAndCountAll(query).then(({ rows, count }) => {
437 loadApplication = function () {
438 return Account.findOne({
441 model: Account['sequelize'].model.Application,
448 load = function (id: number) {
449 return Account.findById(id)
452 loadByUUID = function (uuid: string) {
453 const query: Sequelize.FindOptions<AccountAttributes> = {
459 return Account.findOne(query)
462 loadLocalAccountByNameAndPod = function (name: string, host: string) {
463 const query: Sequelize.FindOptions<AccountAttributes> = {
467 [Sequelize.Op.ne]: null
472 model: Account['sequelize'].models.Pod,
480 return Account.findOne(query)
483 loadByUrl = function (url: string) {
484 const query: Sequelize.FindOptions<AccountAttributes> = {
490 return Account.findOne(query)
493 loadAccountByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) {
494 const query: Sequelize.FindOptions<AccountAttributes> = {
502 return Account.find(query)
505 // ------------------------------ UTILS ------------------------------
507 async function createListFollowForApiQuery (type: 'followers' | 'following', id: number, start: number, count?: number) {
508 let firstJoin: string
509 let secondJoin: string
511 if (type === 'followers') {
512 firstJoin = 'targetAccountId'
513 secondJoin = 'accountId'
515 firstJoin = 'accountId'
516 secondJoin = 'targetAccountId'
519 const selections = [ '"Followers"."url" AS "url"', 'COUNT(*) AS "total"' ]
520 const tasks: Promise<any>[] = []
522 for (const selection of selections) {
523 let query = 'SELECT ' + selection + ' FROM "Account" ' +
524 'INNER JOIN "AccountFollower" ON "AccountFollower"."' + firstJoin + '" = "Account"."id" ' +
525 'INNER JOIN "Account" AS "Follows" ON "Followers"."id" = "Follows"."' + secondJoin + '" ' +
526 'WHERE "Account"."id" = $id ' +
529 if (count !== undefined) query += ', ' + count
533 type: Sequelize.QueryTypes.SELECT
535 tasks.push(Account['sequelize'].query(query, options))
538 const [ followers, [ { total } ]] = await Promise.all(tasks)
539 const urls: string[] = followers.map(f => f.url)
543 total: parseInt(total, 10)