@@ -11,6 +11,7 @@ import type {
1111 WarningThresholdAction ,
1212} from "@/lib/moderation/warnings" ;
1313
14+ import { isUniqueViolation } from "@/api/lib/db-errors" ;
1415import { canOnReport } from "@/api/lib/moderation/access" ;
1516import { findReportTarget } from "@/api/lib/moderation/registry" ;
1617import {
@@ -535,201 +536,212 @@ export const issueWarning = async (
535536 if ( report . status === "done" ) throw reportConflict ( report , "report_closed" ) ;
536537 }
537538
538- const result = await c . get ( "db" ) . transaction ( async ( tx : Db ) => {
539- await lockUserWarnings ( tx , draft . user . id ) ;
539+ const issue = async ( ) =>
540+ await c . get ( "db" ) . transaction ( async ( tx : Db ) => {
541+ await lockUserWarnings ( tx , draft . user . id ) ;
540542
541- const existing = await findIssuedWarning ( tx , moderator , input ) ;
542- if ( existing ) return existing ;
543+ const existing = await findIssuedWarning ( tx , moderator , input ) ;
544+ if ( existing ) return existing ;
543545
544- const now = new Date ( ) ;
545- const expiresAt = draft . expiresAt ( now ) ;
546- const before = await loadActivePoints ( tx , draft . user . id , now ) ;
547- const counts = expiresAt === null || expiresAt . getTime ( ) > now . getTime ( ) ;
548- const after = before + ( counts ? draft . points : 0 ) ;
549-
550- const [ warning ] = await tx
551- . insert ( core_moderation_warnings )
552- . values ( {
553- contentSnapshot : draft . content ?. snapshot ?? null ,
554- defaultPoints : draft . defaultPoints ,
555- expiresAt,
556- idempotencyKey : input . idempotencyKey ,
557- issuedAt : now ,
558- issuedById : moderator . user . id ,
559- message : input . message . trim ( ) ,
560- overrideNote : overridden ? overrideNote : null ,
561- pluginId : draft . content ?. pluginId ?? null ,
562- points : draft . points ,
563- pointsAfter : after ,
564- pointsBefore : before ,
565- reasonId : draft . reason ?. id ?? null ,
566- reasonSnapshot : draft . reason ? { title : draft . reason . title } : null ,
567- reportId : report ?. id ?? null ,
568- scopeFamily : draft . content ?. scopeFamily ?? null ,
569- scopeId : draft . content ?. scopeId ?? null ,
570- staffNote : staffNote === "" ? null : staffNote ,
571- targetId : draft . content ?. targetId ?? null ,
572- targetType : draft . content ?. targetType ?? null ,
573- userId : draft . user . id ,
574- } )
575- . returning ( { id : core_moderation_warnings . id } ) ;
546+ const now = new Date ( ) ;
547+ const expiresAt = draft . expiresAt ( now ) ;
548+ const before = await loadActivePoints ( tx , draft . user . id , now ) ;
549+ const counts = expiresAt === null || expiresAt . getTime ( ) > now . getTime ( ) ;
550+ const after = before + ( counts ? draft . points : 0 ) ;
551+
552+ const [ warning ] = await tx
553+ . insert ( core_moderation_warnings )
554+ . values ( {
555+ contentSnapshot : draft . content ?. snapshot ?? null ,
556+ defaultPoints : draft . defaultPoints ,
557+ expiresAt,
558+ idempotencyKey : input . idempotencyKey ,
559+ issuedAt : now ,
560+ issuedById : moderator . user . id ,
561+ message : input . message . trim ( ) ,
562+ overrideNote : overridden ? overrideNote : null ,
563+ pluginId : draft . content ?. pluginId ?? null ,
564+ points : draft . points ,
565+ pointsAfter : after ,
566+ pointsBefore : before ,
567+ reasonId : draft . reason ?. id ?? null ,
568+ reasonSnapshot : draft . reason ? { title : draft . reason . title } : null ,
569+ reportId : report ?. id ?? null ,
570+ scopeFamily : draft . content ?. scopeFamily ?? null ,
571+ scopeId : draft . content ?. scopeId ?? null ,
572+ staffNote : staffNote === "" ? null : staffNote ,
573+ targetId : draft . content ?. targetId ?? null ,
574+ targetType : draft . content ?. targetType ?? null ,
575+ userId : draft . user . id ,
576+ } )
577+ . returning ( { id : core_moderation_warnings . id } ) ;
576578
577- await appendModerationAudit ( tx , {
578- actorId : moderator . user . id ,
579- data : {
580- content : draft . content
581- ? {
582- pluginId : draft . content . pluginId ,
583- targetId : draft . content . targetId ,
584- targetType : draft . content . targetType ,
585- }
586- : null ,
587- expiresAt : expiresAt ?. toISOString ( ) ?? null ,
588- points : draft . points ,
589- reasonId : draft . reason ?. id ?? null ,
590- reportId : report ?. id ?? null ,
591- } ,
592- type : "warning_issued" ,
593- userId : draft . user . id ,
594- warningId : warning . id ,
595- } ) ;
596- if ( overridden ) {
597579 await appendModerationAudit ( tx , {
598580 actorId : moderator . user . id ,
599581 data : {
600- defaultExpiresAfterHours : draft . defaultExpiresAfterHours ,
601- defaultPoints : draft . defaultPoints ,
582+ content : draft . content
583+ ? {
584+ pluginId : draft . content . pluginId ,
585+ targetId : draft . content . targetId ,
586+ targetType : draft . content . targetType ,
587+ }
588+ : null ,
602589 expiresAt : expiresAt ?. toISOString ( ) ?? null ,
603- expiration : draft . overrides . expiration ,
604590 points : draft . points ,
605- pointsOverridden : draft . overrides . points ,
591+ reasonId : draft . reason ?. id ?? null ,
592+ reportId : report ?. id ?? null ,
606593 } ,
607- type : "warning_overridden " ,
594+ type : "warning_issued " ,
608595 userId : draft . user . id ,
609596 warningId : warning . id ,
610597 } ) ;
611- }
612-
613- const rules = await loadEnabledThresholds ( tx ) ;
614- const { applied, crossed } = evaluateWarningThresholds ( {
615- after,
616- before,
617- rules,
618- } ) ;
619- await appendModerationAudit ( tx , {
620- actorId : moderator . user . id ,
621- data : {
622- after,
623- appliedThresholdId : applied ?. id ?? null ,
624- before,
625- crossedThresholdIds : crossed . map ( rule => rule . id ) ,
626- } ,
627- type : "threshold_evaluated" ,
628- userId : draft . user . id ,
629- warningId : warning . id ,
630- } ) ;
631-
632- let sanction : IssuedWarning [ "sanction" ] = null ;
633- if ( applied ) {
634- const endsAt = thresholdSuspensionEnd ( applied , now ) ;
635- const [ row ] = await tx
636- . insert ( core_moderation_sanctions )
637- . values ( {
638- createdById : moderator . user . id ,
639- endsAt,
640- source : "threshold" ,
641- startsAt : now ,
642- thresholdId : applied . id ,
643- thresholdSnapshot : {
644- action : applied . action ,
645- durationHours : applied . durationHours ,
646- points : applied . points ,
598+ if ( overridden ) {
599+ await appendModerationAudit ( tx , {
600+ actorId : moderator . user . id ,
601+ data : {
602+ defaultExpiresAfterHours : draft . defaultExpiresAfterHours ,
603+ defaultPoints : draft . defaultPoints ,
604+ expiresAt : expiresAt ?. toISOString ( ) ?? null ,
605+ expiration : draft . overrides . expiration ,
606+ points : draft . points ,
607+ pointsOverridden : draft . overrides . points ,
647608 } ,
648- type : "suspension " ,
609+ type : "warning_overridden " ,
649610 userId : draft . user . id ,
650611 warningId : warning . id ,
651- } )
652- . returning ( { id : core_moderation_sanctions . id } ) ;
653- sanction = { endsAt, id : row . id , thresholdId : applied . id } ;
612+ } ) ;
613+ }
614+
615+ const rules = await loadEnabledThresholds ( tx ) ;
616+ const { applied, crossed } = evaluateWarningThresholds ( {
617+ after,
618+ before,
619+ rules,
620+ } ) ;
654621 await appendModerationAudit ( tx , {
655622 actorId : moderator . user . id ,
656623 data : {
657- action : applied . action ,
658- endsAt : endsAt ?. toISOString ( ) ?? null ,
659- source : "threshold" ,
660- thresholdId : applied . id ,
661- thresholdPoints : applied . points ,
624+ after,
625+ appliedThresholdId : applied ?. id ?? null ,
626+ before,
627+ crossedThresholdIds : crossed . map ( rule => rule . id ) ,
662628 } ,
663- sanctionId : row . id ,
664- type : "sanction_applied" ,
629+ type : "threshold_evaluated" ,
665630 userId : draft . user . id ,
666631 warningId : warning . id ,
667632 } ) ;
668- }
669633
670- if ( report ) {
671- await appendReportEvent ( tx , {
672- actorId : moderator . user . id ,
673- data : {
674- points : draft . points ,
634+ let sanction : IssuedWarning [ "sanction" ] = null ;
635+ if ( applied ) {
636+ const endsAt = thresholdSuspensionEnd ( applied , now ) ;
637+ const [ row ] = await tx
638+ . insert ( core_moderation_sanctions )
639+ . values ( {
640+ createdById : moderator . user . id ,
641+ endsAt,
642+ source : "threshold" ,
643+ startsAt : now ,
644+ thresholdId : applied . id ,
645+ thresholdSnapshot : {
646+ action : applied . action ,
647+ durationHours : applied . durationHours ,
648+ points : applied . points ,
649+ } ,
650+ type : "suspension" ,
651+ userId : draft . user . id ,
652+ warningId : warning . id ,
653+ } )
654+ . returning ( { id : core_moderation_sanctions . id } ) ;
655+ sanction = { endsAt, id : row . id , thresholdId : applied . id } ;
656+ await appendModerationAudit ( tx , {
657+ actorId : moderator . user . id ,
658+ data : {
659+ action : applied . action ,
660+ endsAt : endsAt ?. toISOString ( ) ?? null ,
661+ source : "threshold" ,
662+ thresholdId : applied . id ,
663+ thresholdPoints : applied . points ,
664+ } ,
665+ sanctionId : row . id ,
666+ type : "sanction_applied" ,
675667 userId : draft . user . id ,
676668 warningId : warning . id ,
677- } ,
678- reportId : report . id ,
679- type : "warning" ,
680- } ) ;
681- if ( resolveReport ) {
682- await updateReport ( tx , report , {
683- openKey : null ,
684- resolution : "action_taken" ,
685- resolvedAt : now ,
686- resolvedById : moderator . user . id ,
687- status : "done" ,
688669 } ) ;
670+ }
671+
672+ if ( report ) {
689673 await appendReportEvent ( tx , {
690674 actorId : moderator . user . id ,
691- data : { resolution : "action_taken" , warningId : warning . id } ,
675+ data : {
676+ points : draft . points ,
677+ userId : draft . user . id ,
678+ warningId : warning . id ,
679+ } ,
692680 reportId : report . id ,
693- type : "resolved " ,
681+ type : "warning " ,
694682 } ) ;
683+ if ( resolveReport ) {
684+ await updateReport ( tx , report , {
685+ openKey : null ,
686+ resolution : "action_taken" ,
687+ resolvedAt : now ,
688+ resolvedById : moderator . user . id ,
689+ status : "done" ,
690+ } ) ;
691+ await appendReportEvent ( tx , {
692+ actorId : moderator . user . id ,
693+ data : { resolution : "action_taken" , warningId : warning . id } ,
694+ reportId : report . id ,
695+ type : "resolved" ,
696+ } ) ;
697+ }
695698 }
696- }
697699
698- const notifications = c . get ( "notifications" ) ;
699- await notifications . publish ( {
700- actorId : null ,
701- data : {
702- message : input . message . trim ( ) ,
703- points : draft . points ,
704- reason : draft . reason ?. title ?? null ,
705- warningId : warning . id ,
706- } ,
707- idempotencyKey : `warning:${ warning . id } ` ,
708- recipients : [ draft . user . id ] ,
709- tx,
710- type : warningIssuedNotification ,
711- } ) ;
712- if ( sanction ) {
700+ const notifications = c . get ( "notifications" ) ;
713701 await notifications . publish ( {
714702 actorId : null ,
715703 data : {
716- endsAt : sanction . endsAt ?. toISOString ( ) ?? null ,
717- sanctionId : sanction . id ,
704+ message : input . message . trim ( ) ,
705+ points : draft . points ,
706+ reason : draft . reason ?. title ?? null ,
707+ warningId : warning . id ,
718708 } ,
719- idempotencyKey : `sanction :${ sanction . id } ` ,
709+ idempotencyKey : `warning :${ warning . id } ` ,
720710 recipients : [ draft . user . id ] ,
721711 tx,
722- type : accountSuspendedNotification ,
712+ type : warningIssuedNotification ,
723713 } ) ;
724- }
714+ if ( sanction ) {
715+ await notifications . publish ( {
716+ actorId : null ,
717+ data : {
718+ endsAt : sanction . endsAt ?. toISOString ( ) ?? null ,
719+ sanctionId : sanction . id ,
720+ } ,
721+ idempotencyKey : `sanction:${ sanction . id } ` ,
722+ recipients : [ draft . user . id ] ,
723+ tx,
724+ type : accountSuspendedNotification ,
725+ } ) ;
726+ }
727+
728+ return {
729+ duplicate : false ,
730+ points : { after, before } ,
731+ reportResolved : resolveReport ,
732+ sanction,
733+ warningId : warning . id ,
734+ } satisfies IssuedWarning ;
735+ } ) ;
736+
737+ // The lock is per warned member, so the same key sent at once for two
738+ // members only meets at the unique (issuedById, idempotencyKey) index.
739+ const result = await issue ( ) . catch ( async ( error : unknown ) => {
740+ if ( ! isUniqueViolation ( error ) ) throw error ;
741+ const existing = await findIssuedWarning ( c . get ( "db" ) , moderator , input ) ;
742+ if ( ! existing ) throw error ;
725743
726- return {
727- duplicate : false ,
728- points : { after, before } ,
729- reportResolved : resolveReport ,
730- sanction,
731- warningId : warning . id ,
732- } satisfies IssuedWarning ;
744+ return existing ;
733745 } ) ;
734746
735747 if ( result . duplicate ) return result ;
0 commit comments