X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Faccount-follow.ts;h=724f37baa6b6038815611ab1d8f5cf652f70e8b0;hb=25ed141c7c7631ef21d8764c1163fbf8a6591391;hp=cc9b7c42b873604234c1e248d31627c4b1ff05ec;hpb=d7d5611c8a23de9b483f0437ad3469afef7b8805;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/account-follow.ts b/server/models/account/account-follow.ts index cc9b7c42b..724f37baa 100644 --- a/server/models/account/account-follow.ts +++ b/server/models/account/account-follow.ts @@ -12,6 +12,7 @@ let listFollowersForApi: AccountFollowMethods.ListFollowersForApi 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('AccountFollow', @@ -46,7 +47,10 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da listAcceptedFollowingUrlsForApi, listAcceptedFollowerSharedInboxUrls ] - addMethodsToModel(AccountFollow, classMethods) + const instanceMethods = [ + toFormattedJSON + ] + addMethodsToModel(AccountFollow, classMethods, instanceMethods) return AccountFollow } @@ -73,12 +77,41 @@ function associate (models) { }) } -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) @@ -110,7 +143,7 @@ listFollowingForApi = function (id: number, start: number, count: number, sort: return AccountFollow.findAndCountAll(query).then(({ rows, count }) => { return { - data: rows.map(r => r.AccountFollowing), + data: rows, total: count } }) @@ -142,22 +175,22 @@ listFollowersForApi = function (id: number, start: number, count: number, sort: 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 ------------------------------ @@ -165,6 +198,7 @@ listAcceptedFollowingUrlsForApi = function (accountIds: number[], start?: number async function createListAcceptedFollowForApiQuery ( type: 'followers' | 'following', accountIds: number[], + t: Sequelize.Transaction, start?: number, count?: number, columnUrl = 'url' @@ -189,12 +223,13 @@ async function createListAcceptedFollowForApiQuery ( 'INNER JOIN "Accounts" AS "Follows" ON "AccountFollows"."' + secondJoin + '" = "Follows"."id" ' + '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 }, - type: Sequelize.QueryTypes.SELECT + type: Sequelize.QueryTypes.SELECT, + transaction: t } tasks.push(AccountFollow['sequelize'].query(query, options)) }