import * as Bluebird from 'bluebird'
-import { values, difference } from 'lodash'
+import { difference, values } from 'lodash'
+import { IncludeOptions, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize'
import {
AfterCreate,
AfterDestroy,
DataType,
Default,
ForeignKey,
+ Is,
IsInt,
Max,
Model,
Table,
UpdatedAt
} from 'sequelize-typescript'
-import { FollowState } from '../../../shared/models/actors'
-import { ActorFollow } from '../../../shared/models/actors/follow.model'
-import { logger } from '../../helpers/logger'
-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, getFollowsSort } from '../utils'
-import { ActorModel, unusedActorAttributesForAPI } from './actor'
-import { VideoChannelModel } from '../video/video-channel'
-import { AccountModel } from '../account/account'
-import { IncludeOptions, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize'
+import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
+import { getServerActor } from '@server/models/application/application'
+import { VideoModel } from '@server/models/video/video'
import {
MActorFollowActorsDefault,
MActorFollowActorsDefaultSubscription,
MActorFollowFollowingHost,
MActorFollowFormattable,
MActorFollowSubscriptions
-} from '@server/typings/models'
+} from '@server/types/models'
import { ActivityPubActorType } from '@shared/models'
-import { afterCommitIfTransaction } from '@server/helpers/database-utils'
+import { FollowState } from '../../../shared/models/actors'
+import { ActorFollow } from '../../../shared/models/actors/follow.model'
+import { logger } from '../../helpers/logger'
+import { ACTOR_FOLLOW_SCORE, CONSTRAINTS_FIELDS, FOLLOW_STATES, SERVER_ACTOR_NAME } from '../../initializers/constants'
+import { AccountModel } from '../account/account'
+import { ServerModel } from '../server/server'
+import { createSafeIn, getFollowsSort, getSort, searchAttribute, throwIfNotValid } from '../utils'
+import { VideoChannelModel } from '../video/video-channel'
+import { ActorModel, unusedActorAttributesForAPI } from './actor'
@Table({
tableName: 'actorFollow',
},
{
fields: [ 'score' ]
+ },
+ {
+ fields: [ 'url' ],
+ unique: true
}
]
})
@Column
score: number
+ // Allow null because we added this column in PeerTube v3, and don't want to generate fake URLs of remote follows
+ @AllowNull(true)
+ @Is('ActorFollowUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
+ @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
+ url: string
+
@CreatedAt
createdAt: Date
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
}
static listFollowingForApi (options: {
- id: number,
- start: number,
- count: number,
- sort: string,
- state?: FollowState,
- actorType?: ActivityPubActorType,
+ id: number
+ start: number
+ count: number
+ sort: string
+ state?: FollowState
+ actorType?: ActivityPubActorType
search?: string
}) {
const { id, start, count, sort, search, state, actorType } = options
if (search) {
Object.assign(followingServerWhere, {
host: {
- [ Op.iLike ]: '%' + search + '%'
+ [Op.iLike]: '%' + search + '%'
}
})
}
}
static listFollowersForApi (options: {
- actorId: number,
- start: number,
- count: number,
- sort: string,
- state?: FollowState,
- actorType?: ActivityPubActorType,
+ actorId: number
+ start: number
+ count: number
+ sort: string
+ state?: FollowState
+ actorType?: ActivityPubActorType
search?: string
}) {
const { actorId, start, count, sort, search, state, actorType } = options
if (search) {
Object.assign(followerServerWhere, {
host: {
- [ Op.iLike ]: '%' + search + '%'
+ [Op.iLike]: '%' + search + '%'
}
})
}
})
}
- static listSubscriptionsForApi (actorId: number, start: number, count: number, sort: string) {
+ static listSubscriptionsForApi (options: {
+ actorId: number
+ start: number
+ count: number
+ sort: string
+ search?: string
+ }) {
+ const { actorId, start, count, sort } = options
+ const where = {
+ actorId: actorId
+ }
+
+ if (options.search) {
+ Object.assign(where, {
+ [Op.or]: [
+ searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
+ searchAttribute(options.search, '$ActorFollowing.VideoChannel.name$')
+ ]
+ })
+ }
+
const query = {
attributes: [],
distinct: true,
offset: start,
limit: count,
order: getSort(sort),
- where: {
- actorId: actorId
- },
+ where,
include: [
{
attributes: [ 'id' ],
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" ' +