From 0cc1a698813e50d86b5a2a8400e1e8a9b82461da Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 1 Sep 2023 09:58:13 +0200 Subject: [PATCH] Avoid federation error logs with likes on notes --- .../activitypub/process/process-announce.ts | 18 +++---------- .../activitypub/process/process-dislike.ts | 8 +++--- .../lib/activitypub/process/process-like.ts | 8 +++--- .../lib/activitypub/process/process-undo.ts | 12 ++++----- server/server/lib/activitypub/videos/get.ts | 25 +++++++++++++------ 5 files changed, 32 insertions(+), 39 deletions(-) diff --git a/server/server/lib/activitypub/process/process-announce.ts b/server/server/lib/activitypub/process/process-announce.ts index ed178f73c..b9598c8f9 100644 --- a/server/server/lib/activitypub/process/process-announce.ts +++ b/server/server/lib/activitypub/process/process-announce.ts @@ -1,14 +1,13 @@ import { ActivityAnnounce } from '@peertube/peertube-models' import { getAPId } from '@server/lib/activitypub/activity.js' import { retryTransactionWrapper } from '../../../helpers/database-utils.js' -import { logger } from '../../../helpers/logger.js' import { sequelizeTypescript } from '../../../initializers/database.js' import { VideoShareModel } from '../../../models/video/video-share.js' import { APProcessorOptions } from '../../../types/activitypub-processor.model.js' -import { MActorSignature, MVideoAccountLightBlacklistAllFiles } from '../../../types/models/index.js' +import { MActorSignature } from '../../../types/models/index.js' import { Notifier } from '../../notifier/index.js' import { forwardVideoRelatedActivity } from '../send/shared/send-utils.js' -import { getOrCreateAPVideo } from '../videos/index.js' +import { maybeGetOrCreateAPVideo } from '../videos/index.js' async function processAnnounceActivity (options: APProcessorOptions) { const { activity, byActor: actorAnnouncer } = options @@ -32,17 +31,8 @@ export { async function processVideoShare (actorAnnouncer: MActorSignature, activity: ActivityAnnounce, notify: boolean) { const objectUri = getAPId(activity.object) - let video: MVideoAccountLightBlacklistAllFiles - let videoCreated: boolean - - try { - const result = await getOrCreateAPVideo({ videoObject: objectUri }) - video = result.video - videoCreated = result.created - } catch (err) { - logger.debug('Cannot process share of %s. Maybe this is not a video object, so just skipping.', objectUri, { err }) - return - } + const { video, created: videoCreated } = await maybeGetOrCreateAPVideo({ videoObject: objectUri }) + if (!video) return await sequelizeTypescript.transaction(async t => { // Add share entry diff --git a/server/server/lib/activitypub/process/process-dislike.ts b/server/server/lib/activitypub/process/process-dislike.ts index 8040cad93..9182e6de4 100644 --- a/server/server/lib/activitypub/process/process-dislike.ts +++ b/server/server/lib/activitypub/process/process-dislike.ts @@ -5,7 +5,7 @@ import { sequelizeTypescript } from '../../../initializers/database.js' import { AccountVideoRateModel } from '../../../models/account/account-video-rate.js' import { APProcessorOptions } from '../../../types/activitypub-processor.model.js' import { MActorSignature } from '../../../types/models/index.js' -import { federateVideoIfNeeded, getOrCreateAPVideo } from '../videos/index.js' +import { federateVideoIfNeeded, maybeGetOrCreateAPVideo } from '../videos/index.js' async function processDislikeActivity (options: APProcessorOptions) { const { activity, byActor } = options @@ -26,10 +26,8 @@ async function processDislike (activity: ActivityDislike, byActor: MActorSignatu if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) - const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: dislikeObject, fetchType: 'only-video' }) - - // We don't care about dislikes of remote videos - if (!onlyVideo.isOwned()) return + const { video: onlyVideo } = await maybeGetOrCreateAPVideo({ videoObject: dislikeObject, fetchType: 'only-video' }) + if (!onlyVideo?.isOwned()) return return sequelizeTypescript.transaction(async t => { const video = await VideoModel.loadFull(onlyVideo.id, t) diff --git a/server/server/lib/activitypub/process/process-like.ts b/server/server/lib/activitypub/process/process-like.ts index 03637f9a7..ccdb37a02 100644 --- a/server/server/lib/activitypub/process/process-like.ts +++ b/server/server/lib/activitypub/process/process-like.ts @@ -6,7 +6,7 @@ import { getAPId } from '../../../lib/activitypub/activity.js' import { AccountVideoRateModel } from '../../../models/account/account-video-rate.js' import { APProcessorOptions } from '../../../types/activitypub-processor.model.js' import { MActorSignature } from '../../../types/models/index.js' -import { federateVideoIfNeeded, getOrCreateAPVideo } from '../videos/index.js' +import { federateVideoIfNeeded, maybeGetOrCreateAPVideo } from '../videos/index.js' async function processLikeActivity (options: APProcessorOptions) { const { activity, byActor } = options @@ -28,10 +28,8 @@ async function processLikeVideo (byActor: MActorSignature, activity: ActivityLik const byAccount = byActor.Account if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url) - const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: videoUrl, fetchType: 'only-video' }) - - // We don't care about likes of remote videos - if (!onlyVideo.isOwned()) return + const { video: onlyVideo } = await maybeGetOrCreateAPVideo({ videoObject: videoUrl, fetchType: 'only-video' }) + if (!onlyVideo?.isOwned()) return return sequelizeTypescript.transaction(async t => { const video = await VideoModel.loadFull(onlyVideo.id, t) diff --git a/server/server/lib/activitypub/process/process-undo.ts b/server/server/lib/activitypub/process/process-undo.ts index 20a167f7b..635c4c3b8 100644 --- a/server/server/lib/activitypub/process/process-undo.ts +++ b/server/server/lib/activitypub/process/process-undo.ts @@ -21,7 +21,7 @@ import { APProcessorOptions } from '../../../types/activitypub-processor.model.j import { MActorSignature } from '../../../types/models/index.js' import { fetchAPObjectIfNeeded } from '../activity.js' import { forwardVideoRelatedActivity } from '../send/shared/send-utils.js' -import { federateVideoIfNeeded, getOrCreateAPVideo } from '../videos/index.js' +import { federateVideoIfNeeded, getOrCreateAPVideo, maybeGetOrCreateAPVideo } from '../videos/index.js' async function processUndoActivity (options: APProcessorOptions>) { const { activity, byActor } = options @@ -67,9 +67,8 @@ export { async function processUndoLike (byActor: MActorSignature, activity: ActivityUndo) { const likeActivity = activity.object - const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: likeActivity.object }) - // We don't care about likes of remote videos - if (!onlyVideo.isOwned()) return + const { video: onlyVideo } = await maybeGetOrCreateAPVideo({ videoObject: likeActivity.object }) + if (!onlyVideo?.isOwned()) return return sequelizeTypescript.transaction(async t => { if (!byActor.Account) throw new Error('Unknown account ' + byActor.url) @@ -92,9 +91,8 @@ async function processUndoLike (byActor: MActorSignature, activity: ActivityUndo async function processUndoDislike (byActor: MActorSignature, activity: ActivityUndo) { const dislikeActivity = activity.object - const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: dislikeActivity.object }) - // We don't care about likes of remote videos - if (!onlyVideo.isOwned()) return + const { video: onlyVideo } = await maybeGetOrCreateAPVideo({ videoObject: dislikeActivity.object }) + if (!onlyVideo?.isOwned()) return return sequelizeTypescript.transaction(async t => { if (!byActor.Account) throw new Error('Unknown account ' + byActor.url) diff --git a/server/server/lib/activitypub/videos/get.ts b/server/server/lib/activitypub/videos/get.ts index 6d70a490f..54efcc08f 100644 --- a/server/server/lib/activitypub/videos/get.ts +++ b/server/server/lib/activitypub/videos/get.ts @@ -35,11 +35,10 @@ type GetVideoParamOther = { allowRefresh?: boolean } -function getOrCreateAPVideo (options: GetVideoParamAll): GetVideoResult -function getOrCreateAPVideo (options: GetVideoParamImmutable): GetVideoResult -function getOrCreateAPVideo (options: GetVideoParamOther): GetVideoResult - -async function getOrCreateAPVideo ( +export function getOrCreateAPVideo (options: GetVideoParamAll): GetVideoResult +export function getOrCreateAPVideo (options: GetVideoParamImmutable): GetVideoResult +export function getOrCreateAPVideo (options: GetVideoParamOther): GetVideoResult +export async function getOrCreateAPVideo ( options: GetVideoParamAll | GetVideoParamImmutable | GetVideoParamOther ): GetVideoResult { // Default params @@ -86,12 +85,22 @@ async function getOrCreateAPVideo ( } } -// --------------------------------------------------------------------------- +export function maybeGetOrCreateAPVideo (options: GetVideoParamAll): GetVideoResult +export function maybeGetOrCreateAPVideo (options: GetVideoParamImmutable): GetVideoResult +export function maybeGetOrCreateAPVideo (options: GetVideoParamOther): GetVideoResult +export async function maybeGetOrCreateAPVideo (options: GetVideoParamAll | GetVideoParamImmutable | GetVideoParamOther) { + try { + const result = await getOrCreateAPVideo(options as any) -export { - getOrCreateAPVideo + return result + } catch (err) { + logger.debug('Cannot fetch remote video ' + options.videoObject + ': maybe not a video object?', { err }) + return { video: undefined, created: false } + } } +// --------------------------------------------------------------------------- +// Private // --------------------------------------------------------------------------- async function scheduleRefresh (video: MVideoThumbnail, fetchType: VideoLoadByUrlType, syncParam: SyncParam) { -- 2.41.0