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 // FIXME: remove account in followers
272 // return removeVideoAccountToFriends(removeVideoAccountToFriendsParams)
278 toActivityPubObject = function (this: AccountInstance) {
279 const type = this.podId ? 'Application' as 'Application' : 'Person' as 'Person'
284 following: this.getFollowingUrl(),
285 followers: this.getFollowersUrl(),
286 inbox: this.inboxUrl,
287 outbox: this.outboxUrl,
288 preferredUsername: this.name,
292 sharedInbox: this.sharedInboxUrl
296 id: this.getPublicKeyUrl(),
298 publicKeyPem: this.publicKey
302 return activityPubContextify(json)
305 isOwned = function (this: AccountInstance) {
306 return this.podId === null
309 getFollowerSharedInboxUrls = function (this: AccountInstance) {
310 const query: Sequelize.FindOptions<AccountAttributes> = {
311 attributes: [ 'sharedInboxUrl' ],
314 model: Account['sequelize'].models.AccountFollower,
316 targetAccountId: this.id
322 return Account.findAll(query)
323 .then(accounts => accounts.map(a => a.sharedInboxUrl))
326 getFollowingUrl = function (this: AccountInstance) {
327 return this.url + '/followers'
330 getFollowersUrl = function (this: AccountInstance) {
331 return this.url + '/followers'
334 getPublicKeyUrl = function (this: AccountInstance) {
335 return this.url + '#main-key'
338 // ------------------------------ STATICS ------------------------------
340 listOwned = function () {
341 const query: Sequelize.FindOptions<AccountAttributes> = {
347 return Account.findAll(query)
350 listFollowerUrlsForApi = function (name: string, start: number, count?: number) {
351 return createListFollowForApiQuery('followers', name, start, count)
354 listFollowingUrlsForApi = function (name: string, start: number, count?: number) {
355 return createListFollowForApiQuery('following', name, start, count)
358 load = function (id: number) {
359 return Account.findById(id)
362 loadByUUID = function (uuid: string) {
363 const query: Sequelize.FindOptions<AccountAttributes> = {
369 return Account.findOne(query)
372 loadLocalAccountByName = function (name: string) {
373 const query: Sequelize.FindOptions<AccountAttributes> = {
377 [Sequelize.Op.ne]: null
382 return Account.findOne(query)
385 loadByUrl = function (url: string) {
386 const query: Sequelize.FindOptions<AccountAttributes> = {
392 return Account.findOne(query)
395 loadAccountByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) {
396 const query: Sequelize.FindOptions<AccountAttributes> = {
404 return Account.find(query)
407 // ------------------------------ UTILS ------------------------------
409 async function createListFollowForApiQuery (type: 'followers' | 'following', name: string, start: number, count?: number) {
410 let firstJoin: string
411 let secondJoin: string
413 if (type === 'followers') {
414 firstJoin = 'targetAccountId'
415 secondJoin = 'accountId'
417 firstJoin = 'accountId'
418 secondJoin = 'targetAccountId'
421 const selections = [ '"Followers"."url" AS "url"', 'COUNT(*) AS "total"' ]
422 const tasks: Promise<any>[] = []
424 for (const selection of selections) {
425 let query = 'SELECT ' + selection + ' FROM "Account" ' +
426 'INNER JOIN "AccountFollower" ON "AccountFollower"."' + firstJoin + '" = "Account"."id" ' +
427 'INNER JOIN "Account" AS "Followers" ON "Followers"."id" = "AccountFollower"."' + secondJoin + '" ' +
428 'WHERE "Account"."name" = \'$name\' ' +
431 if (count !== undefined) query += ', ' + count
435 type: Sequelize.QueryTypes.SELECT
437 tasks.push(Account['sequelize'].query(query, options))
440 const [ followers, [ { total } ]] = await Promise.all(tasks)
441 const urls: string[] = followers.map(f => f.url)
445 total: parseInt(total, 10)