import * as Bluebird from 'bluebird'
-import { values, difference } from 'lodash'
+import { difference, values } from 'lodash'
import {
AfterCreate,
AfterDestroy,
import { getServerActor } from '../../helpers/utils'
import { ACTOR_FOLLOW_SCORE, FOLLOW_STATES, SERVER_ACTOR_NAME } from '../../initializers/constants'
import { ServerModel } from '../server/server'
-import { createSafeIn, getSort } from '../utils'
+import { createSafeIn, getFollowsSort, getSort } from '../utils'
import { ActorModel, unusedActorAttributesForAPI } from './actor'
import { VideoChannelModel } from '../video/video-channel'
import { AccountModel } from '../account/account'
-import { IncludeOptions, Op, QueryTypes, Transaction } from 'sequelize'
+import { IncludeOptions, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize'
import {
MActorFollowActorsDefault,
MActorFollowActorsDefaultSubscription,
MActorFollowFormattable,
MActorFollowSubscriptions
} from '@server/typings/models'
+import { ActivityPubActorType } from '@shared/models'
+import { VideoModel } from '@server/models/video/video'
@Table({
tableName: 'actorFollow',
@AfterCreate
@AfterUpdate
- static incrementFollowerAndFollowingCount (instance: ActorFollowModel) {
+ static incrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
if (instance.state !== 'accepted') return undefined
return Promise.all([
- ActorModel.incrementFollows(instance.actorId, 'followingCount', 1),
- ActorModel.incrementFollows(instance.targetActorId, 'followersCount', 1)
+ ActorModel.rebuildFollowsCount(instance.actorId, 'following', options.transaction),
+ ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers', options.transaction)
])
}
@AfterDestroy
- static decrementFollowerAndFollowingCount (instance: ActorFollowModel) {
+ static decrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
return Promise.all([
- ActorModel.incrementFollows(instance.actorId, 'followingCount',-1),
- ActorModel.incrementFollows(instance.targetActorId, 'followersCount', -1)
+ ActorModel.rebuildFollowsCount(instance.actorId, 'following', options.transaction),
+ ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers', options.transaction)
])
}
if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved)
}
+ static isFollowedBy (actorId: number, followerActorId: number) {
+ const query = 'SELECT 1 FROM "actorFollow" WHERE "actorId" = $followerActorId AND "targetActorId" = $actorId LIMIT 1'
+ const options = {
+ type: QueryTypes.SELECT as QueryTypes.SELECT,
+ bind: { actorId, followerActorId },
+ raw: true
+ }
+
+ return VideoModel.sequelize.query(query, options)
+ .then(results => results.length === 1)
+ }
+
static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Bluebird<MActorFollowActorsDefault> {
const query = {
where: {
return ActorFollowModel.findOne(query)
.then(result => {
- if (result && result.ActorFollowing.VideoChannel) {
+ if (result?.ActorFollowing.VideoChannel) {
result.ActorFollowing.VideoChannel.Actor = result.ActorFollowing
}
.map(t => {
if (t.host) {
return {
- [ Op.and ]: [
+ [Op.and]: [
{
- '$preferredUsername$': t.name
+ $preferredUsername$: t.name
},
{
- '$host$': t.host
+ $host$: t.host
}
]
}
}
return {
- [ Op.and ]: [
+ [Op.and]: [
{
- '$preferredUsername$': t.name
+ $preferredUsername$: t.name
},
{
- '$serverId$': null
+ $serverId$: null
}
]
}
const query = {
attributes: [],
where: {
- [ Op.and ]: [
+ [Op.and]: [
{
- [ Op.or ]: whereTab
+ [Op.or]: whereTab
},
{
actorId
return ActorFollowModel.findAll(query)
}
- static listFollowingForApi (id: number, start: number, count: number, sort: string, search?: string) {
+ static listFollowingForApi (options: {
+ id: number
+ start: number
+ count: number
+ sort: string
+ state?: FollowState
+ actorType?: ActivityPubActorType
+ search?: string
+ }) {
+ const { id, start, count, sort, search, state, actorType } = options
+
+ const followWhere = state ? { state } : {}
+ const followingWhere: WhereOptions = {}
+ const followingServerWhere: WhereOptions = {}
+
+ if (search) {
+ Object.assign(followingServerWhere, {
+ host: {
+ [Op.iLike]: '%' + search + '%'
+ }
+ })
+ }
+
+ if (actorType) {
+ Object.assign(followingWhere, { type: actorType })
+ }
+
const query = {
distinct: true,
offset: start,
limit: count,
- order: getSort(sort),
+ order: getFollowsSort(sort),
+ where: followWhere,
include: [
{
model: ActorModel,
model: ActorModel,
as: 'ActorFollowing',
required: true,
+ where: followingWhere,
include: [
{
model: ServerModel,
required: true,
- where: search ? {
- host: {
- [Op.iLike]: '%' + search + '%'
- }
- } : undefined
+ where: followingServerWhere
}
]
}
})
}
- static listFollowersForApi (actorId: number, start: number, count: number, sort: string, search?: string) {
+ static listFollowersForApi (options: {
+ actorId: number
+ start: number
+ count: number
+ sort: string
+ state?: FollowState
+ actorType?: ActivityPubActorType
+ search?: string
+ }) {
+ const { actorId, start, count, sort, search, state, actorType } = options
+
+ const followWhere = state ? { state } : {}
+ const followerWhere: WhereOptions = {}
+ const followerServerWhere: WhereOptions = {}
+
+ if (search) {
+ Object.assign(followerServerWhere, {
+ host: {
+ [Op.iLike]: '%' + search + '%'
+ }
+ })
+ }
+
+ if (actorType) {
+ Object.assign(followerWhere, { type: actorType })
+ }
+
const query = {
distinct: true,
offset: start,
limit: count,
- order: getSort(sort),
+ order: getFollowsSort(sort),
+ where: followWhere,
include: [
{
model: ActorModel,
required: true,
as: 'ActorFollower',
+ where: followerWhere,
include: [
{
model: ServerModel,
required: true,
- where: search ? {
- host: {
- [ Op.iLike ]: '%' + search + '%'
- }
- } : undefined
+ where: followerServerWhere
}
]
},
const followerId = (await getServerActor()).id
const query = {
- attributes: [],
+ attributes: [ 'id' ],
where: {
actorId: followerId
},
include: [
{
- attributes: [ ],
+ attributes: [ 'id' ],
model: ActorModel.unscoped(),
required: true,
as: 'ActorFollowing',
}
const res = await ActorFollowModel.findAll(query)
- const followedHosts = res.map(res => res.ActorFollowing.Server.host)
+ const followedHosts = res.map(row => row.ActorFollowing.Server.host)
return difference(hosts, followedHosts)
}
}
const selections: string[] = []
- if (distinct === true) selections.push('DISTINCT("Follows"."' + columnUrl + '") AS "url"')
- else selections.push('"Follows"."' + columnUrl + '" AS "url"')
+ if (distinct === true) selections.push(`DISTINCT("Follows"."${columnUrl}") AS "selectionUrl"`)
+ else selections.push(`"Follows"."${columnUrl}" AS "selectionUrl"`)
selections.push('COUNT(*) AS "total"')
const tasks: Bluebird<any>[] = []
- for (let selection of selections) {
+ for (const selection of selections) {
let query = 'SELECT ' + selection + ' FROM "actor" ' +
'INNER JOIN "actorFollow" ON "actorFollow"."' + firstJoin + '" = "actor"."id" ' +
'INNER JOIN "actor" AS "Follows" ON "actorFollow"."' + secondJoin + '" = "Follows"."id" ' +
- 'WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = \'accepted\' '
+ `WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = 'accepted' AND "Follows"."${columnUrl}" IS NOT NULL `
if (count !== undefined) query += 'LIMIT ' + count
if (start !== undefined) query += ' OFFSET ' + start
}
const [ followers, [ dataTotal ] ] = await Promise.all(tasks)
- const urls: string[] = followers.map(f => f.url)
+ const urls: string[] = followers.map(f => f.selectionUrl)
return {
data: urls,