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 } from '../utils'
24 } from './account-interface'
26 let Account: Sequelize.Model<AccountInstance, AccountAttributes>
27 let loadAccountByPodAndUUID: AccountMethods.LoadAccountByPodAndUUID
28 let load: AccountMethods.Load
29 let loadByUUID: AccountMethods.LoadByUUID
30 let loadByUrl: AccountMethods.LoadByUrl
31 let loadLocalAccountByName: AccountMethods.LoadLocalAccountByName
32 let listOwned: AccountMethods.ListOwned
33 let listFollowerUrlsForApi: AccountMethods.ListFollowerUrlsForApi
34 let listFollowingUrlsForApi: AccountMethods.ListFollowingUrlsForApi
35 let isOwned: AccountMethods.IsOwned
36 let toActivityPubObject: AccountMethods.ToActivityPubObject
37 let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
38 let getFollowingUrl: AccountMethods.GetFollowingUrl
39 let getFollowersUrl: AccountMethods.GetFollowersUrl
40 let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
42 export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
43 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
47 defaultValue: DataTypes.UUIDV4,
54 type: DataTypes.STRING,
57 usernameValid: value => {
58 const res = isUserUsernameValid(value)
59 if (res === false) throw new Error('Username is not valid.')
64 type: DataTypes.STRING,
68 const res = isAccountUrlValid(value)
69 if (res === false) throw new Error('URL is not valid.')
74 type: DataTypes.STRING,
77 publicKeyValid: value => {
78 const res = isAccountPublicKeyValid(value)
79 if (res === false) throw new Error('Public key is not valid.')
84 type: DataTypes.STRING,
87 privateKeyValid: value => {
88 const res = isAccountPrivateKeyValid(value)
89 if (res === false) throw new Error('Private key is not valid.')
94 type: DataTypes.INTEGER,
97 followersCountValid: value => {
98 const res = isAccountFollowersCountValid(value)
99 if (res === false) throw new Error('Followers count is not valid.')
104 type: DataTypes.INTEGER,
107 followersCountValid: value => {
108 const res = isAccountFollowingCountValid(value)
109 if (res === false) throw new Error('Following count is not valid.')
114 type: DataTypes.STRING,
117 inboxUrlValid: value => {
118 const res = isAccountInboxValid(value)
119 if (res === false) throw new Error('Inbox URL is not valid.')
124 type: DataTypes.STRING,
127 outboxUrlValid: value => {
128 const res = isAccountOutboxValid(value)
129 if (res === false) throw new Error('Outbox URL is not valid.')
134 type: DataTypes.STRING,
137 sharedInboxUrlValid: value => {
138 const res = isAccountSharedInboxValid(value)
139 if (res === false) throw new Error('Shared inbox URL is not valid.')
144 type: DataTypes.STRING,
147 followersUrlValid: value => {
148 const res = isAccountFollowersValid(value)
149 if (res === false) throw new Error('Followers URL is not valid.')
154 type: DataTypes.STRING,
157 followingUrlValid: value => {
158 const res = isAccountFollowingValid(value)
159 if (res === false) throw new Error('Following URL is not valid.')
173 fields: [ 'userId' ],
177 fields: [ 'applicationId' ],
181 fields: [ 'name', 'podId' ],
185 hooks: { afterDestroy }
189 const classMethods = [
191 loadAccountByPodAndUUID,
194 loadLocalAccountByName,
196 listFollowerUrlsForApi,
197 listFollowingUrlsForApi
199 const instanceMethods = [
202 getFollowerSharedInboxUrls,
207 addMethodsToModel(Account, classMethods, instanceMethods)
212 // ---------------------------------------------------------------------------
214 function associate (models) {
215 Account.belongsTo(models.Pod, {
223 Account.belongsTo(models.User, {
231 Account.belongsTo(models.Application, {
239 Account.hasMany(models.VideoChannel, {
248 Account.hasMany(models.AccountFollower, {
256 Account.hasMany(models.AccountFollower, {
258 name: 'targetAccountId',
265 function afterDestroy (account: AccountInstance) {
266 if (account.isOwned()) {
267 const removeVideoAccountToFriendsParams = {
271 return removeVideoAccountToFriends(removeVideoAccountToFriendsParams)
277 toActivityPubObject = function (this: AccountInstance) {
278 const type = this.podId ? 'Application' : 'Person'
283 following: this.getFollowingUrl(),
284 followers: this.getFollowersUrl(),
285 inbox: this.inboxUrl,
286 outbox: this.outboxUrl,
287 preferredUsername: this.name,
291 sharedInbox: this.sharedInboxUrl
295 id: this.getPublicKeyUrl(),
297 publicKeyPem: this.publicKey
301 return activityPubContextify(json)
304 isOwned = function (this: AccountInstance) {
305 return this.podId === null
308 getFollowerSharedInboxUrls = function (this: AccountInstance) {
309 const query: Sequelize.FindOptions<AccountAttributes> = {
310 attributes: [ 'sharedInboxUrl' ],
313 model: Account['sequelize'].models.AccountFollower,
315 targetAccountId: this.id
321 return Account.findAll(query)
322 .then(accounts => accounts.map(a => a.sharedInboxUrl))
325 getFollowingUrl = function (this: AccountInstance) {
326 return this.url + '/followers'
329 getFollowersUrl = function (this: AccountInstance) {
330 return this.url + '/followers'
333 getPublicKeyUrl = function (this: AccountInstance) {
334 return this.url + '#main-key'
337 // ------------------------------ STATICS ------------------------------
339 listOwned = function () {
340 const query: Sequelize.FindOptions<AccountAttributes> = {
346 return Account.findAll(query)
349 listFollowerUrlsForApi = function (name: string, start: number, count: number) {
350 return createListFollowForApiQuery('followers', name, start, count)
353 listFollowingUrlsForApi = function (name: string, start: number, count: number) {
354 return createListFollowForApiQuery('following', name, start, count)
357 load = function (id: number) {
358 return Account.findById(id)
361 loadByUUID = function (uuid: string) {
362 const query: Sequelize.FindOptions<AccountAttributes> = {
368 return Account.findOne(query)
371 loadLocalAccountByName = function (name: string) {
372 const query: Sequelize.FindOptions<AccountAttributes> = {
376 [Sequelize.Op.ne]: null
381 return Account.findOne(query)
384 loadByUrl = function (url: string) {
385 const query: Sequelize.FindOptions<AccountAttributes> = {
391 return Account.findOne(query)
394 loadAccountByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) {
395 const query: Sequelize.FindOptions<AccountAttributes> = {
403 return Account.find(query)
406 // ------------------------------ UTILS ------------------------------
408 async function createListFollowForApiQuery (type: 'followers' | 'following', name: string, start: number, count: number) {
409 let firstJoin: string
410 let secondJoin: string
412 if (type === 'followers') {
413 firstJoin = 'targetAccountId'
414 secondJoin = 'accountId'
416 firstJoin = 'accountId'
417 secondJoin = 'targetAccountId'
420 const selections = [ '"Followers"."url" AS "url"', 'COUNT(*) AS "total"' ]
421 const tasks: Promise<any>[] = []
423 for (const selection of selections) {
424 const query = 'SELECT ' + selection + ' FROM "Account" ' +
425 'INNER JOIN "AccountFollower" ON "AccountFollower"."' + firstJoin + '" = "Account"."id" ' +
426 'INNER JOIN "Account" AS "Followers" ON "Followers"."id" = "AccountFollower"."' + secondJoin + '" ' +
427 'WHERE "Account"."name" = \'$name\' ' +
428 'LIMIT ' + start + ', ' + count
432 type: Sequelize.QueryTypes.SELECT
434 tasks.push(Account['sequelize'].query(query, options))
437 const [ followers, [ { total } ]] = await Promise.all(tasks)
438 const urls: string[] = followers.map(f => f.url)
442 total: parseInt(total, 10)