@@ -21,6 +21,7 @@ import {
2121import * as Sentry from '@sentry/node' ;
2222import type { KnexAccountRepository } from 'account/account.repository.knex' ;
2323import type { FollowersService } from 'activitypub/followers.service' ;
24+ import { exhaustiveCheck , getError , getValue , isError } from 'core/result' ;
2425import { v4 as uuidv4 } from 'uuid' ;
2526import type { AccountService } from './account/account.service' ;
2627import { mapActorToExternalAccountData } from './account/utils' ;
@@ -371,7 +372,39 @@ export async function handleAnnoucedCreate(
371372 return ;
372373 }
373374 // This handles storing the posts in the posts table
374- const post = await postService . getByApId ( create . objectId ) ;
375+ const postResult = await postService . getByApId ( create . objectId ) ;
376+
377+ if ( isError ( postResult ) ) {
378+ const error = getError ( postResult ) ;
379+ switch ( error ) {
380+ case 'upstream-error' :
381+ ctx . data . logger . info (
382+ 'Upstream error fetching post for create handling' ,
383+ {
384+ postId : create . objectId . href ,
385+ } ,
386+ ) ;
387+ break ;
388+ case 'not-a-post' :
389+ ctx . data . logger . info (
390+ 'Resource is not a post in create handling' ,
391+ {
392+ postId : create . objectId . href ,
393+ } ,
394+ ) ;
395+ break ;
396+ case 'missing-author' :
397+ ctx . data . logger . info (
398+ 'Post has missing author in create handling' ,
399+ {
400+ postId : create . objectId . href ,
401+ } ,
402+ ) ;
403+ break ;
404+ default :
405+ exhaustiveCheck ( error ) ;
406+ }
407+ }
375408
376409 const object = await create . getObject ( ) ;
377410 const replyTarget = await object ?. getReplyTarget ( ) ;
@@ -452,14 +485,45 @@ export const createUndoHandler = (
452485 }
453486
454487 if ( senderAccount !== null ) {
455- const originalPost = await postService . getByApId (
488+ const originalPostResult = await postService . getByApId (
456489 object . objectId ,
457490 ) ;
458491
459- if ( originalPost !== null ) {
460- originalPost . removeRepost ( senderAccount ) ;
461- await postRepository . save ( originalPost ) ;
492+ if ( isError ( originalPostResult ) ) {
493+ const error = getError ( originalPostResult ) ;
494+ switch ( error ) {
495+ case 'upstream-error' :
496+ ctx . data . logger . info (
497+ 'Upstream error fetching post for undoing announce' ,
498+ {
499+ postId : object . objectId . href ,
500+ } ,
501+ ) ;
502+ break ;
503+ case 'not-a-post' :
504+ ctx . data . logger . info (
505+ 'Resource is not a post in undoing announce' ,
506+ {
507+ postId : object . objectId . href ,
508+ } ,
509+ ) ;
510+ break ;
511+ case 'missing-author' :
512+ ctx . data . logger . info (
513+ 'Post has missing author in undoing announce' ,
514+ {
515+ postId : object . objectId . href ,
516+ } ,
517+ ) ;
518+ break ;
519+ default :
520+ return exhaustiveCheck ( error ) ;
521+ }
522+ return ;
462523 }
524+ const originalPost = getValue ( originalPostResult ) ;
525+ originalPost . removeRepost ( senderAccount ) ;
526+ await postRepository . save ( originalPost ) ;
463527 }
464528 }
465529
@@ -590,9 +654,40 @@ export function createAnnounceHandler(
590654
591655 if ( senderAccount !== null ) {
592656 // This will save the post if it doesn't already exist
593- const post = await postService . getByApId ( announce . objectId ) ;
594-
595- if ( post !== null ) {
657+ const postResult = await postService . getByApId ( announce . objectId ) ;
658+
659+ if ( isError ( postResult ) ) {
660+ const error = getError ( postResult ) ;
661+ switch ( error ) {
662+ case 'upstream-error' :
663+ ctx . data . logger . info (
664+ 'Upstream error fetching post for reposting' ,
665+ {
666+ postId : announce . objectId . href ,
667+ } ,
668+ ) ;
669+ break ;
670+ case 'not-a-post' :
671+ ctx . data . logger . info (
672+ 'Resource for reposting is not a post' ,
673+ {
674+ postId : announce . objectId . href ,
675+ } ,
676+ ) ;
677+ break ;
678+ case 'missing-author' :
679+ ctx . data . logger . info (
680+ 'Post for reposting has missing author' ,
681+ {
682+ postId : announce . objectId . href ,
683+ } ,
684+ ) ;
685+ break ;
686+ default :
687+ return exhaustiveCheck ( error ) ;
688+ }
689+ } else {
690+ const post = getValue ( postResult ) ;
596691 post . addRepost ( senderAccount ) ;
597692 await postRepository . save ( post ) ;
598693 }
@@ -637,11 +732,42 @@ export function createLikeHandler(
637732
638733 const account = await accountService . getByApId ( like . actorId ) ;
639734 if ( account !== null ) {
640- const post = await postService . getByApId ( like . objectId ) ;
641-
642- if ( post !== null ) {
735+ const postResult = await postService . getByApId ( like . objectId ) ;
736+
737+ if ( isError ( postResult ) ) {
738+ const error = getError ( postResult ) ;
739+ switch ( error ) {
740+ case 'upstream-error' :
741+ ctx . data . logger . info (
742+ 'Upstream error fetching post for liking' ,
743+ {
744+ postId : like . objectId . href ,
745+ } ,
746+ ) ;
747+ break ;
748+ case 'not-a-post' :
749+ ctx . data . logger . info (
750+ 'Resource for liking is not a post' ,
751+ {
752+ postId : like . objectId . href ,
753+ } ,
754+ ) ;
755+ break ;
756+ case 'missing-author' :
757+ ctx . data . logger . info (
758+ 'Post for liking has missing author' ,
759+ {
760+ postId : like . objectId . href ,
761+ } ,
762+ ) ;
763+ break ;
764+ default : {
765+ return exhaustiveCheck ( error ) ;
766+ }
767+ }
768+ } else {
769+ const post = getValue ( postResult ) ;
643770 post . addLike ( account ) ;
644-
645771 await postRepository . save ( post ) ;
646772 }
647773 }
0 commit comments