]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Avoid federation error logs with likes on notes
authorChocobozzz <me@florianbigard.com>
Fri, 1 Sep 2023 07:58:13 +0000 (09:58 +0200)
committerChocobozzz <me@florianbigard.com>
Fri, 1 Sep 2023 07:58:13 +0000 (09:58 +0200)
server/server/lib/activitypub/process/process-announce.ts
server/server/lib/activitypub/process/process-dislike.ts
server/server/lib/activitypub/process/process-like.ts
server/server/lib/activitypub/process/process-undo.ts
server/server/lib/activitypub/videos/get.ts

index ed178f73c7d8ba2b538f566a21d157573d54e146..b9598c8f9e2dce8ab499123db6e9205ac255efc2 100644 (file)
@@ -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<ActivityAnnounce>) {
   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
index 8040cad93b663e3041fcbbd991790b0b71285102..9182e6de45ff493d56099ffad0b7692f6a92ff47 100644 (file)
@@ -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<ActivityDislike>) {
   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)
index 03637f9a7092a2a8c20c7d4bbbf8bbcb89bc3665..ccdb37a0268fe63859bb735cb2f9e65480bff73c 100644 (file)
@@ -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<ActivityLike>) {
   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)
index 20a167f7b2499a3d784bbd1fa7d486b35939a699..635c4c3b824fd5362f4e6fc749051ccbf7ce6b16 100644 (file)
@@ -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<ActivityUndo<ActivityUndoObject>>) {
   const { activity, byActor } = options
@@ -67,9 +67,8 @@ export {
 async function processUndoLike (byActor: MActorSignature, activity: ActivityUndo<ActivityLike>) {
   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<ActivityDislike>) {
   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)
index 6d70a490f2cb629d716999d8101072fd4d7077d8..54efcc08fa6d05dee2ece4f29d8ab1b62c2e7f80 100644 (file)
@@ -35,11 +35,10 @@ type GetVideoParamOther = {
   allowRefresh?: boolean
 }
 
-function getOrCreateAPVideo (options: GetVideoParamAll): GetVideoResult<MVideoAccountLightBlacklistAllFiles>
-function getOrCreateAPVideo (options: GetVideoParamImmutable): GetVideoResult<MVideoImmutable>
-function getOrCreateAPVideo (options: GetVideoParamOther): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail>
-
-async function getOrCreateAPVideo (
+export function getOrCreateAPVideo (options: GetVideoParamAll): GetVideoResult<MVideoAccountLightBlacklistAllFiles>
+export function getOrCreateAPVideo (options: GetVideoParamImmutable): GetVideoResult<MVideoImmutable>
+export function getOrCreateAPVideo (options: GetVideoParamOther): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail>
+export async function getOrCreateAPVideo (
   options: GetVideoParamAll | GetVideoParamImmutable | GetVideoParamOther
 ): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> {
   // Default params
@@ -86,12 +85,22 @@ async function getOrCreateAPVideo (
   }
 }
 
-// ---------------------------------------------------------------------------
+export function maybeGetOrCreateAPVideo (options: GetVideoParamAll): GetVideoResult<MVideoAccountLightBlacklistAllFiles>
+export function maybeGetOrCreateAPVideo (options: GetVideoParamImmutable): GetVideoResult<MVideoImmutable>
+export function maybeGetOrCreateAPVideo (options: GetVideoParamOther): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail>
+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) {