diff options
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/account/account-video-rate.ts | 30 | ||||
-rw-r--r-- | server/models/video/video-comment.ts | 14 | ||||
-rw-r--r-- | server/models/video/video-share.ts | 13 | ||||
-rw-r--r-- | server/models/video/video.ts | 20 |
4 files changed, 58 insertions, 19 deletions
diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts index d9c529491..801f76bba 100644 --- a/server/models/account/account-video-rate.ts +++ b/server/models/account/account-video-rate.ts | |||
@@ -146,10 +146,22 @@ export class AccountVideoRateModel extends Model { | |||
146 | return AccountVideoRateModel.findAndCountAll(query) | 146 | return AccountVideoRateModel.findAndCountAll(query) |
147 | } | 147 | } |
148 | 148 | ||
149 | static listRemoteRateUrlsOfLocalVideos () { | ||
150 | const query = `SELECT "accountVideoRate".url FROM "accountVideoRate" ` + | ||
151 | `INNER JOIN account ON account.id = "accountVideoRate"."accountId" ` + | ||
152 | `INNER JOIN actor ON actor.id = account."actorId" AND actor."serverId" IS NOT NULL ` + | ||
153 | `INNER JOIN video ON video.id = "accountVideoRate"."videoId" AND video.remote IS FALSE` | ||
154 | |||
155 | return AccountVideoRateModel.sequelize.query<{ url: string }>(query, { | ||
156 | type: QueryTypes.SELECT, | ||
157 | raw: true | ||
158 | }).then(rows => rows.map(r => r.url)) | ||
159 | } | ||
160 | |||
149 | static loadLocalAndPopulateVideo ( | 161 | static loadLocalAndPopulateVideo ( |
150 | rateType: VideoRateType, | 162 | rateType: VideoRateType, |
151 | accountName: string, | 163 | accountName: string, |
152 | videoId: number | string, | 164 | videoId: number, |
153 | t?: Transaction | 165 | t?: Transaction |
154 | ): Promise<MAccountVideoRateAccountVideo> { | 166 | ): Promise<MAccountVideoRateAccountVideo> { |
155 | const options: FindOptions = { | 167 | const options: FindOptions = { |
@@ -241,21 +253,7 @@ export class AccountVideoRateModel extends Model { | |||
241 | 253 | ||
242 | await AccountVideoRateModel.destroy(query) | 254 | await AccountVideoRateModel.destroy(query) |
243 | 255 | ||
244 | const field = type === 'like' | 256 | return VideoModel.updateRatesOf(videoId, type, t) |
245 | ? 'likes' | ||
246 | : 'dislikes' | ||
247 | |||
248 | const rawQuery = `UPDATE "video" SET "${field}" = ` + | ||
249 | '(' + | ||
250 | 'SELECT COUNT(id) FROM "accountVideoRate" WHERE "accountVideoRate"."videoId" = "video"."id" AND type = :rateType' + | ||
251 | ') ' + | ||
252 | 'WHERE "video"."id" = :videoId' | ||
253 | |||
254 | return AccountVideoRateModel.sequelize.query(rawQuery, { | ||
255 | transaction: t, | ||
256 | replacements: { videoId, rateType: type }, | ||
257 | type: QueryTypes.UPDATE | ||
258 | }) | ||
259 | }) | 257 | }) |
260 | } | 258 | } |
261 | 259 | ||
diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index dc7556d44..151c2bc81 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import { uniq } from 'lodash' | 1 | import { uniq } from 'lodash' |
2 | import { FindAndCountOptions, FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' | 2 | import { FindAndCountOptions, FindOptions, Op, Order, QueryTypes, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' |
3 | import { | 3 | import { |
4 | AllowNull, | 4 | AllowNull, |
5 | BelongsTo, | 5 | BelongsTo, |
@@ -696,6 +696,18 @@ export class VideoCommentModel extends Model { | |||
696 | } | 696 | } |
697 | } | 697 | } |
698 | 698 | ||
699 | static listRemoteCommentUrlsOfLocalVideos () { | ||
700 | const query = `SELECT "videoComment".url FROM "videoComment" ` + | ||
701 | `INNER JOIN account ON account.id = "videoComment"."accountId" ` + | ||
702 | `INNER JOIN actor ON actor.id = "account"."actorId" AND actor."serverId" IS NOT NULL ` + | ||
703 | `INNER JOIN video ON video.id = "videoComment"."videoId" AND video.remote IS FALSE` | ||
704 | |||
705 | return VideoCommentModel.sequelize.query<{ url: string }>(query, { | ||
706 | type: QueryTypes.SELECT, | ||
707 | raw: true | ||
708 | }).then(rows => rows.map(r => r.url)) | ||
709 | } | ||
710 | |||
699 | static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) { | 711 | static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) { |
700 | const query = { | 712 | const query = { |
701 | where: { | 713 | where: { |
diff --git a/server/models/video/video-share.ts b/server/models/video/video-share.ts index b7f5f3fa3..5059c1fa6 100644 --- a/server/models/video/video-share.ts +++ b/server/models/video/video-share.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | import { literal, Op, Transaction } from 'sequelize' | 1 | import { literal, Op, QueryTypes, Transaction } from 'sequelize' |
2 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' | 2 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' |
3 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | 3 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
4 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' | 4 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' |
@@ -185,6 +185,17 @@ export class VideoShareModel extends Model { | |||
185 | return VideoShareModel.findAndCountAll(query) | 185 | return VideoShareModel.findAndCountAll(query) |
186 | } | 186 | } |
187 | 187 | ||
188 | static listRemoteShareUrlsOfLocalVideos () { | ||
189 | const query = `SELECT "videoShare".url FROM "videoShare" ` + | ||
190 | `INNER JOIN actor ON actor.id = "videoShare"."actorId" AND actor."serverId" IS NOT NULL ` + | ||
191 | `INNER JOIN video ON video.id = "videoShare"."videoId" AND video.remote IS FALSE` | ||
192 | |||
193 | return VideoShareModel.sequelize.query<{ url: string }>(query, { | ||
194 | type: QueryTypes.SELECT, | ||
195 | raw: true | ||
196 | }).then(rows => rows.map(r => r.url)) | ||
197 | } | ||
198 | |||
188 | static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) { | 199 | static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) { |
189 | const query = { | 200 | const query = { |
190 | where: { | 201 | where: { |
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 8894843e0..b4c7da655 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -34,7 +34,7 @@ import { ModelCache } from '@server/models/model-cache' | |||
34 | import { VideoFile } from '@shared/models/videos/video-file.model' | 34 | import { VideoFile } from '@shared/models/videos/video-file.model' |
35 | import { ResultList, UserRight, VideoPrivacy, VideoState } from '../../../shared' | 35 | import { ResultList, UserRight, VideoPrivacy, VideoState } from '../../../shared' |
36 | import { VideoObject } from '../../../shared/models/activitypub/objects' | 36 | import { VideoObject } from '../../../shared/models/activitypub/objects' |
37 | import { Video, VideoDetails } from '../../../shared/models/videos' | 37 | import { Video, VideoDetails, VideoRateType } from '../../../shared/models/videos' |
38 | import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' | 38 | import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' |
39 | import { VideoFilter } from '../../../shared/models/videos/video-query.type' | 39 | import { VideoFilter } from '../../../shared/models/videos/video-query.type' |
40 | import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' | 40 | import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' |
@@ -1509,6 +1509,24 @@ export class VideoModel extends Model { | |||
1509 | }) | 1509 | }) |
1510 | } | 1510 | } |
1511 | 1511 | ||
1512 | static updateRatesOf (videoId: number, type: VideoRateType, t: Transaction) { | ||
1513 | const field = type === 'like' | ||
1514 | ? 'likes' | ||
1515 | : 'dislikes' | ||
1516 | |||
1517 | const rawQuery = `UPDATE "video" SET "${field}" = ` + | ||
1518 | '(' + | ||
1519 | 'SELECT COUNT(id) FROM "accountVideoRate" WHERE "accountVideoRate"."videoId" = "video"."id" AND type = :rateType' + | ||
1520 | ') ' + | ||
1521 | 'WHERE "video"."id" = :videoId' | ||
1522 | |||
1523 | return AccountVideoRateModel.sequelize.query(rawQuery, { | ||
1524 | transaction: t, | ||
1525 | replacements: { videoId, rateType: type }, | ||
1526 | type: QueryTypes.UPDATE | ||
1527 | }) | ||
1528 | } | ||
1529 | |||
1512 | static checkVideoHasInstanceFollow (videoId: number, followerActorId: number) { | 1530 | static checkVideoHasInstanceFollow (videoId: number, followerActorId: number) { |
1513 | // Instances only share videos | 1531 | // Instances only share videos |
1514 | const query = 'SELECT 1 FROM "videoShare" ' + | 1532 | const query = 'SELECT 1 FROM "videoShare" ' + |