let listAcceptedFollowerUrlsForApi: AccountFollowMethods.ListAcceptedFollowerUrlsForApi
let listAcceptedFollowingUrlsForApi: AccountFollowMethods.ListAcceptedFollowingUrlsForApi
let listAcceptedFollowerSharedInboxUrls: AccountFollowMethods.ListAcceptedFollowerSharedInboxUrls
+let toFormattedJSON: AccountFollowMethods.ToFormattedJSON
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
AccountFollow = sequelize.define<AccountFollowInstance, AccountFollowAttributes>('AccountFollow',
listAcceptedFollowingUrlsForApi,
listAcceptedFollowerSharedInboxUrls
]
- addMethodsToModel(AccountFollow, classMethods)
+ const instanceMethods = [
+ toFormattedJSON
+ ]
+ addMethodsToModel(AccountFollow, classMethods, instanceMethods)
return AccountFollow
}
})
}
-loadByAccountAndTarget = function (accountId: number, targetAccountId: number) {
+toFormattedJSON = function (this: AccountFollowInstance) {
+ const follower = this.AccountFollower.toFormattedJSON()
+ const following = this.AccountFollowing.toFormattedJSON()
+
+ const json = {
+ id: this.id,
+ follower,
+ following,
+ state: this.state,
+ createdAt: this.createdAt,
+ updatedAt: this.updatedAt
+ }
+
+ return json
+}
+
+loadByAccountAndTarget = function (accountId: number, targetAccountId: number, t?: Sequelize.Transaction) {
const query = {
where: {
accountId,
targetAccountId
- }
+ },
+ include: [
+ {
+ model: AccountFollow[ 'sequelize' ].models.Account,
+ required: true,
+ as: 'AccountFollower'
+ },
+ {
+ model: AccountFollow['sequelize'].models.Account,
+ required: true,
+ as: 'AccountFollowing'
+ }
+ ],
+ transaction: t
}
return AccountFollow.findOne(query)
return AccountFollow.findAndCountAll(query).then(({ rows, count }) => {
return {
- data: rows.map(r => r.AccountFollowing),
+ data: rows,
total: count
}
})
return AccountFollow.findAndCountAll(query).then(({ rows, count }) => {
return {
- data: rows.map(r => r.AccountFollower),
+ data: rows,
total: count
}
})
}
-listAcceptedFollowerUrlsForApi = function (accountIds: number[], start?: number, count?: number) {
- return createListAcceptedFollowForApiQuery('followers', accountIds, start, count)
+listAcceptedFollowerUrlsForApi = function (accountIds: number[], t: Sequelize.Transaction, start?: number, count?: number) {
+ return createListAcceptedFollowForApiQuery('followers', accountIds, t, start, count)
}
-listAcceptedFollowerSharedInboxUrls = function (accountIds: number[]) {
- return createListAcceptedFollowForApiQuery('followers', accountIds, undefined, undefined, 'sharedInboxUrl')
+listAcceptedFollowerSharedInboxUrls = function (accountIds: number[], t: Sequelize.Transaction) {
+ return createListAcceptedFollowForApiQuery('followers', accountIds, t, undefined, undefined, 'sharedInboxUrl')
}
-listAcceptedFollowingUrlsForApi = function (accountIds: number[], start?: number, count?: number) {
- return createListAcceptedFollowForApiQuery('following', accountIds, start, count)
+listAcceptedFollowingUrlsForApi = function (accountIds: number[], t: Sequelize.Transaction, start?: number, count?: number) {
+ return createListAcceptedFollowForApiQuery('following', accountIds, t, start, count)
}
// ------------------------------ UTILS ------------------------------
async function createListAcceptedFollowForApiQuery (
type: 'followers' | 'following',
accountIds: number[],
+ t: Sequelize.Transaction,
start?: number,
count?: number,
columnUrl = 'url'
let query = 'SELECT ' + selection + ' FROM "Accounts" ' +
'INNER JOIN "AccountFollows" ON "AccountFollows"."' + firstJoin + '" = "Accounts"."id" ' +
'INNER JOIN "Accounts" AS "Follows" ON "AccountFollows"."' + secondJoin + '" = "Follows"."id" ' +
- 'WHERE "Accounts"."id" IN ($accountIds) AND "AccountFollows"."state" = \'accepted\' '
+ 'WHERE "Accounts"."id" = ANY ($accountIds) AND "AccountFollows"."state" = \'accepted\' '
- if (start !== undefined) query += 'LIMIT ' + start
- if (count !== undefined) query += ', ' + count
+ if (count !== undefined) query += 'LIMIT ' + count
+ if (start !== undefined) query += ' OFFSET ' + start
const options = {
- bind: { accountIds: accountIds.join(',') },
- type: Sequelize.QueryTypes.SELECT
+ bind: { accountIds },
+ type: Sequelize.QueryTypes.SELECT,
+ transaction: t
}
tasks.push(AccountFollow['sequelize'].query(query, options))
}