MCommentOwnerReplyVideoLight,
MCommentOwnerVideo,
MCommentOwnerVideoFeed,
- MCommentOwnerVideoReply
+ MCommentOwnerVideoReply,
+ MVideoImmutable
} from '../../typings/models/video'
import { AccountModel } from '../account/account'
import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
}
@Scopes(() => ({
- [ScopeNames.ATTRIBUTES_FOR_API]: (serverAccountId: number, userAccountId?: number) => {
+ [ScopeNames.ATTRIBUTES_FOR_API]: (blockerAccountIds: number[]) => {
return {
attributes: {
include: [
[
Sequelize.literal(
'(' +
- 'WITH "blocklist" AS (' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')' +
+ 'WITH "blocklist" AS (' + buildBlockedAccountSQL(blockerAccountIds) + ')' +
'SELECT COUNT("replies"."id") - (' +
'SELECT COUNT("replies"."id") ' +
'FROM "videoComment" AS "replies" ' +
static async listThreadsForApi (parameters: {
videoId: number
+ isVideoOwned: boolean
start: number
count: number
sort: string
user?: MUserAccountId
}) {
- const { videoId, start, count, sort, user } = parameters
+ const { videoId, isVideoOwned, start, count, sort, user } = parameters
- const serverActor = await getServerActor()
- const serverAccountId = serverActor.Account.id
- const userAccountId = user ? user.Account.id : undefined
+ const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
const query = {
offset: start,
{
accountId: {
[Op.notIn]: Sequelize.literal(
- '(' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')'
+ '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
)
}
},
const scopes: (string | ScopeOptions)[] = [
ScopeNames.WITH_ACCOUNT_FOR_API,
{
- method: [ ScopeNames.ATTRIBUTES_FOR_API, serverAccountId, userAccountId ]
+ method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
}
]
static async listThreadCommentsForApi (parameters: {
videoId: number
+ isVideoOwned: boolean
threadId: number
user?: MUserAccountId
}) {
- const { videoId, threadId, user } = parameters
+ const { videoId, threadId, user, isVideoOwned } = parameters
- const serverActor = await getServerActor()
- const serverAccountId = serverActor.Account.id
- const userAccountId = user ? user.Account.id : undefined
+ const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
const query = {
order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
],
accountId: {
[Op.notIn]: Sequelize.literal(
- '(' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')'
+ '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
)
}
}
const scopes: any[] = [
ScopeNames.WITH_ACCOUNT_FOR_API,
{
- method: [ ScopeNames.ATTRIBUTES_FOR_API, serverAccountId, userAccountId ]
+ method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
}
]
.findAll(query)
}
- static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction, order: 'ASC' | 'DESC' = 'ASC') {
+ static async listAndCountByVideoForAP (video: MVideoImmutable, start: number, count: number, t?: Transaction) {
+ const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({
+ videoId: video.id,
+ isVideoOwned: video.isOwned()
+ })
+
const query = {
- order: [ [ 'createdAt', order ] ] as Order,
+ order: [ [ 'createdAt', 'ASC' ] ] as Order,
offset: start,
limit: count,
where: {
- videoId
+ videoId: video.id,
+ accountId: {
+ [Op.notIn]: Sequelize.literal(
+ '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
+ )
+ }
},
transaction: t
}
deletedAt: null,
accountId: {
[Op.notIn]: Sequelize.literal(
- '(' + buildBlockedAccountSQL(serverActor.Account.id) + ')'
+ '(' + buildBlockedAccountSQL([ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]) + ')'
)
}
},
required: true,
where: {
privacy: VideoPrivacy.PUBLIC
- }
+ },
+ include: [
+ {
+ attributes: [ 'accountId' ],
+ model: VideoChannelModel.unscoped(),
+ required: true
+ }
+ ]
}
]
}
tag
}
}
+
+ private static async buildBlockerAccountIds (options: {
+ videoId: number
+ isVideoOwned: boolean
+ user?: MUserAccountId
+ }) {
+ const { videoId, user, isVideoOwned } = options
+
+ const serverActor = await getServerActor()
+ const blockerAccountIds = [ serverActor.Account.id ]
+
+ if (user) blockerAccountIds.push(user.Account.id)
+
+ if (isVideoOwned) {
+ const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId)
+ blockerAccountIds.push(videoOwnerAccount.id)
+ }
+
+ return blockerAccountIds
+ }
}