From 970ceac0a6bf4990b8924738591df4949491ec9b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 1 Aug 2019 10:15:28 +0200 Subject: [PATCH] Fix multiple server tests --- client/angular.json | 15 ++++++++ .../activitypub/process/process-dislike.ts | 21 +++++------ .../lib/activitypub/process/process-like.ts | 24 +++++++------ server/lib/activitypub/video-comments.ts | 2 +- server/lib/video-comment.ts | 6 ++-- server/models/account/account-video-rate.ts | 36 +++++++++++++++++++ server/models/video/video-comment.ts | 19 +++++++++- server/models/video/video-share.ts | 29 ++++++++++----- shared/extra-utils/videos/videos.ts | 3 +- 9 files changed, 119 insertions(+), 36 deletions(-) diff --git a/client/angular.json b/client/angular.json index 8a709e269..cce930b82 100644 --- a/client/angular.json +++ b/client/angular.json @@ -44,6 +44,21 @@ "buildOptimizer": true, "serviceWorker": true, "ngswConfigPath": "src/ngsw-config.json", + "budgets": [ + { + "type": "initial", + "type": "initial", + "maximumWarning": "2mb", + "maximumWarning": "2mb", + "maximumError": "5mb" + "maximumError": "5mb" + }, + { + "type": "anyComponentStyle", + "maximumWarning": "6kb", + "maximumError": "10kb" + } + ], "fileReplacements": [ { "replace": "src/environments/environment.ts", diff --git a/server/lib/activitypub/process/process-dislike.ts b/server/lib/activitypub/process/process-dislike.ts index bfd69e07a..ed8afd3d2 100644 --- a/server/lib/activitypub/process/process-dislike.ts +++ b/server/lib/activitypub/process/process-dislike.ts @@ -29,20 +29,21 @@ async function processDislike (activity: ActivityCreate | ActivityDislike, byAct const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislikeObject }) return sequelizeTypescript.transaction(async t => { - const rate = { + const url = getVideoDislikeActivityPubUrl(byActor, video) + + const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, url) + if (existingRate && existingRate.type === 'dislike') return + + await AccountVideoRateModel.create({ type: 'dislike' as 'dislike', videoId: video.id, - accountId: byAccount.id - } + accountId: byAccount.id, + url + }, { transaction: t }) - const [ , created ] = await AccountVideoRateModel.findOrCreate({ - where: rate, - defaults: Object.assign({}, rate, { url: getVideoDislikeActivityPubUrl(byActor, video) }), - transaction: t - }) - if (created === true) await video.increment('dislikes', { transaction: t }) + await video.increment('dislikes', { transaction: t }) - if (video.isOwned() && created === true) { + if (video.isOwned()) { // Don't resend the activity to the sender const exceptions = [ byActor ] diff --git a/server/lib/activitypub/process/process-like.ts b/server/lib/activitypub/process/process-like.ts index 2a04167d7..8b97aae55 100644 --- a/server/lib/activitypub/process/process-like.ts +++ b/server/lib/activitypub/process/process-like.ts @@ -29,19 +29,21 @@ async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) { const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoUrl }) return sequelizeTypescript.transaction(async t => { - const rate = { + const url = getVideoLikeActivityPubUrl(byActor, video) + + const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, url) + if (existingRate && existingRate.type === 'like') return + + await AccountVideoRateModel.create({ type: 'like' as 'like', videoId: video.id, - accountId: byAccount.id - } - const [ , created ] = await AccountVideoRateModel.findOrCreate({ - where: rate, - defaults: Object.assign({}, rate, { url: getVideoLikeActivityPubUrl(byActor, video) }), - transaction: t - }) - if (created === true) await video.increment('likes', { transaction: t }) - - if (video.isOwned() && created === true) { + accountId: byAccount.id, + url + }, { transaction: t }) + + await video.increment('likes', { transaction: t }) + + if (video.isOwned()) { // Don't resend the activity to the sender const exceptions = [ byActor ] diff --git a/server/lib/activitypub/video-comments.ts b/server/lib/activitypub/video-comments.ts index 2f26ddefd..921abdb8d 100644 --- a/server/lib/activitypub/video-comments.ts +++ b/server/lib/activitypub/video-comments.ts @@ -54,7 +54,7 @@ async function addVideoComment (videoInstance: VideoModel, commentUrl: string) { }) if (sanitizeAndCheckVideoCommentObject(body) === false) { - logger.debug('Remote video comment JSON is not valid.', { body }) + logger.debug('Remote video comment JSON %s is not valid.', commentUrl, { body }) return { created: false } } diff --git a/server/lib/video-comment.ts b/server/lib/video-comment.ts index bfe22d225..449aa74cb 100644 --- a/server/lib/video-comment.ts +++ b/server/lib/video-comment.ts @@ -27,10 +27,10 @@ async function createVideoComment (obj: { inReplyToCommentId, videoId: obj.video.id, accountId: obj.account.id, - url: 'fake url' - }, { transaction: t, validate: false } as any) // FIXME: sequelize typings + url: new Date().toISOString() + }, { transaction: t, validate: false }) - comment.set('url', getVideoCommentActivityPubUrl(obj.video, comment)) + comment.url = getVideoCommentActivityPubUrl(obj.video, comment) const savedComment = await comment.save({ transaction: t }) savedComment.InReplyToVideoComment = obj.inReplyToComment diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts index 85af9e378..d5c214ecb 100644 --- a/server/models/account/account-video-rate.ts +++ b/server/models/account/account-video-rate.ts @@ -89,6 +89,25 @@ export class AccountVideoRateModel extends Model { return AccountVideoRateModel.findOne(options) } + static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, transaction?: Transaction) { + const options: FindOptions = { + where: { + [ Op.or]: [ + { + accountId, + videoId + }, + { + url + } + ] + } + } + if (transaction) options.transaction = transaction + + return AccountVideoRateModel.findOne(options) + } + static listByAccountForApi (options: { start: number, count: number, @@ -202,6 +221,23 @@ export class AccountVideoRateModel extends Model { videoId, type }, + include: [ + { + model: AccountModel.unscoped(), + required: true, + include: [ + { + model: ActorModel.unscoped(), + required: true, + where: { + serverId: { + [Op.ne]: null + } + } + } + ] + } + ], transaction: t } diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 536b6cb3e..28e5818cd 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -472,7 +472,24 @@ export class VideoCommentModel extends Model { [Op.lt]: beforeUpdatedAt }, videoId - } + }, + include: [ + { + required: true, + model: AccountModel.unscoped(), + include: [ + { + required: true, + model: ActorModel.unscoped(), + where: { + serverId: { + [Op.ne]: null + } + } + } + ] + } + ] } return VideoCommentModel.destroy(query) diff --git a/server/models/video/video-share.ts b/server/models/video/video-share.ts index fda2d7cea..3bab3c027 100644 --- a/server/models/video/video-share.ts +++ b/server/models/video/video-share.ts @@ -1,4 +1,3 @@ -import * as Sequelize from 'sequelize' import * as Bluebird from 'bluebird' import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' @@ -8,6 +7,7 @@ import { ActorModel } from '../activitypub/actor' import { throwIfNotValid } from '../utils' import { VideoModel } from './video' import { VideoChannelModel } from './video-channel' +import { Op, Transaction } from 'sequelize' enum ScopeNames { FULL = 'FULL', @@ -88,7 +88,7 @@ export class VideoShareModel extends Model { }) Video: VideoModel - static load (actorId: number, videoId: number, t?: Sequelize.Transaction) { + static load (actorId: number, videoId: number, t?: Transaction) { return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({ where: { actorId, @@ -98,7 +98,7 @@ export class VideoShareModel extends Model { }) } - static loadByUrl (url: string, t: Sequelize.Transaction) { + static loadByUrl (url: string, t: Transaction) { return VideoShareModel.scope(ScopeNames.FULL).findOne({ where: { url @@ -107,7 +107,7 @@ export class VideoShareModel extends Model { }) } - static loadActorsByShare (videoId: number, t: Sequelize.Transaction) { + static loadActorsByShare (videoId: number, t: Transaction) { const query = { where: { videoId @@ -125,7 +125,7 @@ export class VideoShareModel extends Model { .then(res => res.map(r => r.Actor)) } - static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Sequelize.Transaction): Bluebird { + static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Bluebird { const query = { attributes: [], include: [ @@ -163,7 +163,7 @@ export class VideoShareModel extends Model { .then(res => res.map(r => r.Actor)) } - static loadActorsByVideoChannel (videoChannelId: number, t: Sequelize.Transaction): Bluebird { + static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Bluebird { const query = { attributes: [], include: [ @@ -188,7 +188,7 @@ export class VideoShareModel extends Model { .then(res => res.map(r => r.Actor)) } - static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Sequelize.Transaction) { + static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction) { const query = { offset: start, limit: count, @@ -205,10 +205,21 @@ export class VideoShareModel extends Model { const query = { where: { updatedAt: { - [Sequelize.Op.lt]: beforeUpdatedAt + [Op.lt]: beforeUpdatedAt }, videoId - } + }, + include: [ + { + model: ActorModel.unscoped(), + required: true, + where: { + serverId: { + [ Op.ne ]: null + } + } + } + ] } return VideoShareModel.destroy(query) diff --git a/shared/extra-utils/videos/videos.ts b/shared/extra-utils/videos/videos.ts index c78563232..1533f37ab 100644 --- a/shared/extra-utils/videos/videos.ts +++ b/shared/extra-utils/videos/videos.ts @@ -379,7 +379,8 @@ async function uploadVideo (url: string, accessToken: string, videoAttributesArg req.field('licence', attributes.licence.toString()) } - for (let i = 0; i < attributes.tags.length; i++) { + const tags = attributes.tags || [] + for (let i = 0; i < tags.length; i++) { req.field('tags[' + i + ']', attributes.tags[i]) } -- 2.25.1