mirror of
https://github.com/xCyanGrizzly/DragonsStash.git
synced 2026-05-11 06:11:15 +00:00
feat: complete remaining features — training, FTS, bot groups, repair, re-tag
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Manual override training (GroupingRule): - Learn patterns from manual group creation (common filename prefix or creator) - Apply learned rules as first auto-grouping pass (highest confidence after albums) - GroupingRule model stores pattern, channel, signal type, confidence Hash verification after upload: - Re-hash upload files on disk before indexing to catch disk corruption - Creates HASH_MISMATCH notification on discrepancy Grouping conflict detection: - After all grouping passes, check if grouped packages match rules from different groups - Creates GROUPING_CONFLICT notification for manual review Per-channel grouping flags: - Add autoGroupEnabled boolean to TelegramChannel (default true) - Auto-grouping passes (all except album) gated behind this flag - Album grouping always runs as it reflects Telegram's native behavior Full-text search (tsvector): - Add searchVector tsvector column with GIN index and auto-update trigger - Backfill 1870 existing packages - FTS with ts_rank for ranked results, ILIKE fallback for short/failed queries - Applied to both web app and bot search Bot group awareness: - /group <query> — view group info or search groups by name - /sendgroup <id> — send all packages in a group to linked Telegram account Bulk repair: - repairPackageAction clears dest info and resets watermark for re-processing - Repair button in notification bell for MISSING_PART and HASH_MISMATCH alerts - /api/notifications/repair endpoint Retroactive category re-tagging: - When channel category changes, auto-update tags on all existing packages - Removes old category tag, adds new one Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -186,6 +186,62 @@ export async function setPreviewFromExtract(
|
||||
}
|
||||
}
|
||||
|
||||
export async function repairPackageAction(
|
||||
packageId: string
|
||||
): Promise<ActionResult> {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) return { success: false, error: "Unauthorized" };
|
||||
|
||||
try {
|
||||
const pkg = await prisma.package.findUnique({
|
||||
where: { id: packageId },
|
||||
select: {
|
||||
id: true,
|
||||
fileName: true,
|
||||
sourceChannelId: true,
|
||||
sourceMessageId: true,
|
||||
destChannelId: true,
|
||||
destMessageId: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!pkg) return { success: false, error: "Package not found" };
|
||||
|
||||
// Clear the destination info so the worker re-processes it
|
||||
await prisma.package.update({
|
||||
where: { id: packageId },
|
||||
data: {
|
||||
destMessageId: null,
|
||||
destMessageIds: [],
|
||||
destChannelId: null,
|
||||
},
|
||||
});
|
||||
|
||||
// Reset the channel watermark to before this message so worker picks it up
|
||||
await prisma.accountChannelMap.updateMany({
|
||||
where: {
|
||||
channelId: pkg.sourceChannelId,
|
||||
lastProcessedMessageId: { gte: pkg.sourceMessageId },
|
||||
},
|
||||
data: { lastProcessedMessageId: pkg.sourceMessageId - BigInt(1) },
|
||||
});
|
||||
|
||||
// Mark related notifications as read
|
||||
await prisma.systemNotification.updateMany({
|
||||
where: {
|
||||
context: { path: ["packageId"], equals: packageId },
|
||||
isRead: false,
|
||||
},
|
||||
data: { isRead: true },
|
||||
});
|
||||
|
||||
revalidatePath("/stls");
|
||||
return { success: true, data: undefined };
|
||||
} catch {
|
||||
return { success: false, error: "Failed to schedule repair" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function retrySkippedPackageAction(
|
||||
id: string
|
||||
): Promise<ActionResult> {
|
||||
|
||||
@@ -291,10 +291,25 @@ export async function setChannelCategory(
|
||||
if (!admin.success) return admin;
|
||||
|
||||
try {
|
||||
const existing = await prisma.telegramChannel.findUnique({
|
||||
where: { id },
|
||||
select: { category: true },
|
||||
});
|
||||
if (!existing) return { success: false, error: "Channel not found" };
|
||||
|
||||
const oldCategory = existing.category;
|
||||
const newCategory = category?.trim() || null;
|
||||
|
||||
await prisma.telegramChannel.update({
|
||||
where: { id },
|
||||
data: { category: category?.trim() || null },
|
||||
data: { category: newCategory },
|
||||
});
|
||||
|
||||
// Retroactively re-tag packages from this channel when category changes
|
||||
if (oldCategory !== newCategory && newCategory) {
|
||||
await retagChannelPackages(id, oldCategory, newCategory);
|
||||
}
|
||||
|
||||
revalidatePath("/telegram");
|
||||
return { success: true, data: undefined };
|
||||
} catch {
|
||||
@@ -302,6 +317,50 @@ export async function setChannelCategory(
|
||||
}
|
||||
}
|
||||
|
||||
export async function retagChannelPackages(
|
||||
channelId: string,
|
||||
oldCategory: string | null,
|
||||
newCategory: string
|
||||
): Promise<ActionResult<{ updated: number }>> {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) return { success: false, error: "Unauthorized" };
|
||||
|
||||
try {
|
||||
// Find packages from this channel that have the old category tag (or no category tag)
|
||||
const packages = await prisma.package.findMany({
|
||||
where: { sourceChannelId: channelId },
|
||||
select: { id: true, tags: true },
|
||||
});
|
||||
|
||||
let updated = 0;
|
||||
for (const pkg of packages) {
|
||||
const tags = [...pkg.tags];
|
||||
// Remove old category tag if present
|
||||
if (oldCategory) {
|
||||
const idx = tags.indexOf(oldCategory);
|
||||
if (idx !== -1) tags.splice(idx, 1);
|
||||
}
|
||||
// Add new category tag if not already present
|
||||
if (!tags.includes(newCategory)) {
|
||||
tags.push(newCategory);
|
||||
}
|
||||
// Only update if tags actually changed
|
||||
if (JSON.stringify(tags) !== JSON.stringify(pkg.tags)) {
|
||||
await prisma.package.update({
|
||||
where: { id: pkg.id },
|
||||
data: { tags },
|
||||
});
|
||||
updated++;
|
||||
}
|
||||
}
|
||||
|
||||
revalidatePath("/stls");
|
||||
return { success: true, data: { updated } };
|
||||
} catch {
|
||||
return { success: false, error: "Failed to re-tag packages" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function setChannelType(
|
||||
id: string,
|
||||
type: "SOURCE" | "DESTINATION"
|
||||
|
||||
43
src/app/api/notifications/repair/route.ts
Normal file
43
src/app/api/notifications/repair/route.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json().catch(() => ({}));
|
||||
const notificationId = body.notificationId as string;
|
||||
if (!notificationId) {
|
||||
return NextResponse.json({ error: "notificationId required" }, { status: 400 });
|
||||
}
|
||||
|
||||
const notification = await prisma.systemNotification.findUnique({
|
||||
where: { id: notificationId },
|
||||
});
|
||||
|
||||
if (!notification) {
|
||||
return NextResponse.json({ error: "Notification not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const context = notification.context as Record<string, unknown> | null;
|
||||
const packageId = context?.packageId as string | undefined;
|
||||
|
||||
if (!packageId) {
|
||||
return NextResponse.json({ error: "Notification has no associated package" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Import and call the repair action
|
||||
const { repairPackageAction } = await import("@/app/(app)/stls/actions");
|
||||
const result = await repairPackageAction(packageId);
|
||||
|
||||
if (!result.success) {
|
||||
return NextResponse.json({ error: result.error }, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
PopoverTrigger,
|
||||
} from "@/components/ui/popover";
|
||||
import { ScrollArea } from "@/components/ui/scroll-area";
|
||||
import { toast } from "sonner";
|
||||
|
||||
interface Notification {
|
||||
id: string;
|
||||
@@ -93,6 +94,22 @@ export function NotificationBell() {
|
||||
}
|
||||
}
|
||||
|
||||
async function handleRepair(notificationId: string) {
|
||||
try {
|
||||
const res = await fetch("/api/notifications/repair", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ notificationId }),
|
||||
});
|
||||
if (res.ok) {
|
||||
toast.success("Repair scheduled — package will be re-processed on next cycle");
|
||||
fetchNotifications();
|
||||
}
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
|
||||
function formatTime(iso: string): string {
|
||||
const d = new Date(iso);
|
||||
const now = new Date();
|
||||
@@ -147,12 +164,19 @@ export function NotificationBell() {
|
||||
const Icon = severityIcon[n.severity] ?? Info;
|
||||
const color = severityColor[n.severity] ?? "text-muted-foreground";
|
||||
return (
|
||||
<button
|
||||
<div
|
||||
key={n.id}
|
||||
className={`flex w-full gap-3 px-4 py-3 text-left hover:bg-muted/50 transition-colors ${
|
||||
!n.isRead ? "bg-muted/20" : ""
|
||||
}`}
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
onClick={() => !n.isRead && handleMarkRead(n.id)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter" || e.key === " ") {
|
||||
if (!n.isRead) handleMarkRead(n.id);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Icon className={`h-4 w-4 mt-0.5 shrink-0 ${color}`} />
|
||||
<div className="flex-1 min-w-0">
|
||||
@@ -170,8 +194,21 @@ export function NotificationBell() {
|
||||
<p className="text-[10px] text-muted-foreground mt-1">
|
||||
{formatTime(n.createdAt)}
|
||||
</p>
|
||||
{(n.type === "MISSING_PART" || n.type === "HASH_MISMATCH") && (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="h-6 px-2 text-xs mt-1"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleRepair(n.id);
|
||||
}}
|
||||
>
|
||||
Repair
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
@@ -340,6 +340,30 @@ export async function listPackageFiles(options: {
|
||||
};
|
||||
}
|
||||
|
||||
async function fullTextSearchPackageIds(query: string, limit: number): Promise<string[]> {
|
||||
// Convert user query to tsquery — handle multi-word by joining with &
|
||||
const tsQuery = query
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.filter((w) => w.length >= 2)
|
||||
.map((w) => w.replace(/[^a-zA-Z0-9]/g, ""))
|
||||
.filter(Boolean)
|
||||
.join(" & ");
|
||||
|
||||
if (!tsQuery) return [];
|
||||
|
||||
const results = await prisma.$queryRawUnsafe<{ id: string }[]>(
|
||||
`SELECT id FROM packages
|
||||
WHERE "searchVector" @@ to_tsquery('english', $1)
|
||||
ORDER BY ts_rank("searchVector", to_tsquery('english', $1)) DESC
|
||||
LIMIT $2`,
|
||||
tsQuery,
|
||||
limit
|
||||
);
|
||||
|
||||
return results.map((r) => r.id);
|
||||
}
|
||||
|
||||
export async function searchPackages(options: {
|
||||
query: string;
|
||||
page: number;
|
||||
@@ -366,14 +390,26 @@ export async function searchPackages(options: {
|
||||
);
|
||||
const fileMatchedIds = fileMatches.map((f) => f.packageId);
|
||||
|
||||
// Try full-text search first (better ranking, handles word stemming)
|
||||
let ftsPackageNameIds: string[] = [];
|
||||
if (options.searchIn === "both" && q.length >= 3) {
|
||||
try {
|
||||
ftsPackageNameIds = await fullTextSearchPackageIds(q, 200);
|
||||
} catch {
|
||||
// FTS failed — fall back to ILIKE below
|
||||
}
|
||||
}
|
||||
|
||||
const packageNameIds =
|
||||
options.searchIn === "both"
|
||||
? (
|
||||
await prisma.package.findMany({
|
||||
where: { fileName: { contains: q, mode: "insensitive" } },
|
||||
select: { id: true },
|
||||
})
|
||||
).map((p) => p.id)
|
||||
? ftsPackageNameIds.length > 0
|
||||
? ftsPackageNameIds
|
||||
: (
|
||||
await prisma.package.findMany({
|
||||
where: { fileName: { contains: q, mode: "insensitive" } },
|
||||
select: { id: true },
|
||||
})
|
||||
).map((p) => p.id)
|
||||
: [];
|
||||
|
||||
// Also match by group name
|
||||
@@ -696,6 +732,53 @@ export async function createManualGroup(name: string, packageIds: string[]) {
|
||||
data: { packageGroupId: group.id },
|
||||
});
|
||||
|
||||
// Learn a grouping rule from the manual override
|
||||
try {
|
||||
const linkedPkgs = await prisma.package.findMany({
|
||||
where: { id: { in: packageIds } },
|
||||
select: { fileName: true, creator: true },
|
||||
});
|
||||
|
||||
// Extract the common filename pattern
|
||||
const fileNames = linkedPkgs.map((p) => p.fileName);
|
||||
let pattern = "";
|
||||
if (fileNames.length > 1) {
|
||||
// Find longest common prefix
|
||||
let prefix = fileNames[0];
|
||||
for (let i = 1; i < fileNames.length; i++) {
|
||||
while (!fileNames[i].startsWith(prefix)) {
|
||||
prefix = prefix.slice(0, -1);
|
||||
if (!prefix) break;
|
||||
}
|
||||
}
|
||||
const trimmed = prefix.replace(/[\s\-_.(]+$/, "");
|
||||
if (trimmed.length >= 4) {
|
||||
pattern = trimmed;
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to shared creator
|
||||
if (!pattern) {
|
||||
const creators = [...new Set(linkedPkgs.map((p) => p.creator).filter(Boolean))];
|
||||
if (creators.length === 1 && creators[0]) {
|
||||
pattern = creators[0];
|
||||
}
|
||||
}
|
||||
|
||||
if (pattern) {
|
||||
await prisma.groupingRule.create({
|
||||
data: {
|
||||
sourceChannelId: firstPkg.sourceChannelId,
|
||||
pattern,
|
||||
signalType: "MANUAL",
|
||||
createdByGroupId: group.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// Best-effort — don't fail the group creation if rule learning fails
|
||||
}
|
||||
|
||||
// Clean up empty groups left behind
|
||||
await prisma.packageGroup.deleteMany({
|
||||
where: { packages: { none: {} }, id: { not: group.id } },
|
||||
|
||||
Reference in New Issue
Block a user