+ return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
+ ' UNION ' +
+ 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
+ 'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
+ 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
+}
+
+function buildBlockedAccountSQLOptimized (columnNameJoin: string, blockerIds: number[]) {
+ const blockerIdsString = blockerIds.join(', ')
+
+ return [
+ literal(
+ `NOT EXISTS (` +
+ ` SELECT 1 FROM "accountBlocklist" ` +
+ ` WHERE "targetAccountId" = ${columnNameJoin} ` +
+ ` AND "accountId" IN (${blockerIdsString})` +
+ `)`
+ ),
+
+ literal(
+ `NOT EXISTS (` +
+ ` SELECT 1 FROM "account" ` +
+ ` INNER JOIN "actor" ON account."actorId" = actor.id ` +
+ ` INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ` +
+ ` WHERE "account"."id" = ${columnNameJoin} ` +
+ ` AND "serverBlocklist"."accountId" IN (${blockerIdsString})` +
+ `)`
+ )
+ ]
+}
+
+function buildServerIdsFollowedBy (actorId: any) {
+ const actorIdNumber = parseInt(actorId + '', 10)
+
+ return '(' +
+ 'SELECT "actor"."serverId" FROM "actorFollow" ' +
+ 'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' +
+ 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
+ ')'
+}
+
+function buildWhereIdOrUUID (id: number | string) {
+ return validator.isInt('' + id) ? { id } : { uuid: id }
+}
+
+function parseAggregateResult (result: any) {
+ if (!result) return 0
+
+ const total = parseInt(result + '', 10)
+ if (isNaN(total)) return 0
+
+ return total
+}
+
+function createSafeIn (sequelize: Sequelize, stringArr: (string | number)[]) {
+ return stringArr.map(t => {
+ return t === null
+ ? null
+ : sequelize.escape('' + t)
+ }).join(', ')
+}
+
+function buildLocalAccountIdsIn () {
+ return literal(
+ '(SELECT "account"."id" FROM "account" INNER JOIN "actor" ON "actor"."id" = "account"."actorId" AND "actor"."serverId" IS NULL)'
+ )
+}
+
+function buildLocalActorIdsIn () {
+ return literal(
+ '(SELECT "actor"."id" FROM "actor" WHERE "actor"."serverId" IS NULL)'
+ )
+}
+
+function buildDirectionAndField (value: string) {
+ let field: string
+ let direction: 'ASC' | 'DESC'
+
+ if (value.substring(0, 1) === '-') {
+ direction = 'DESC'
+ field = value.substring(1)
+ } else {
+ direction = 'ASC'
+ field = value
+ }
+
+ return { direction, field }
+}
+
+function searchAttribute (sourceField?: string, targetField?: string) {
+ if (!sourceField) return {}
+
+ return {
+ [targetField]: {
+ [Op.iLike]: `%${sourceField}%`
+ }