125 lines · 4.9 KB
import { all, first, run } from "./db";
import { accountDb, repoDb } from "./scoped-db";
import type { ScopedDb } from "./scoped-db";
import { locate } from "./scoped-db/locators";
import { badRequest, notFoundError } from "./errors";
import { recordAdminAction } from "./admin-audit";
import { publicUrl } from "./events/emit";
import { invalidateUnread } from "./user-inbox";
import { dropMcpAccess } from "./access-revocation";
import { HIDEABLE_KINDS } from "~/lib/abuse";
import type { AbuseTargetKind } from "~/lib/abuse";
import { issuesChanged } from "./repo-state";

type Hideable = "repo" | "issue" | "comment" | "review_comment" | "discussion_comment";

type Target = { table: string; counter?: string };

/** `counter` keeps the parent's comment count in step, so hidden comments don't show up as a number either. */
const TARGETS: Record<Hideable, Target> = {
  repo: { table: "repos" },
  issue: { table: "issues" },
  comment: {
    table: "comments",
    counter: `UPDATE issues SET comments_count = MAX(comments_count + ?, 0)
              WHERE id = (SELECT issue_id FROM comments WHERE id = ?)`,
  },
  review_comment: { table: "review_comments" },
  discussion_comment: {
    table: "discussion_comments",
    counter: `UPDATE discussions SET comments_count = MAX(comments_count + ?, 0)
              WHERE id = (SELECT discussion_id FROM discussion_comments WHERE id = ?)`,
  },
};

export function isHideable(kind: string): kind is Hideable {
  return HIDEABLE_KINDS.has(kind as AbuseTargetKind);
}

type Store = Pick<ScopedDb, "first" | "run">;