Add touched/unread inbox issue semantics
This commit is contained in:
2
packages/db/src/migrations/0024_far_beast.sql
Normal file
2
packages/db/src/migrations/0024_far_beast.sql
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
CREATE INDEX "issue_comments_company_issue_created_at_idx" ON "issue_comments" USING btree ("company_id","issue_id","created_at");--> statement-breakpoint
|
||||||
|
CREATE INDEX "issue_comments_company_author_issue_created_at_idx" ON "issue_comments" USING btree ("company_id","author_user_id","issue_id","created_at");
|
||||||
5693
packages/db/src/migrations/meta/0024_snapshot.json
Normal file
5693
packages/db/src/migrations/meta/0024_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -169,6 +169,13 @@
|
|||||||
"when": 1772139727599,
|
"when": 1772139727599,
|
||||||
"tag": "0023_fair_lethal_legion",
|
"tag": "0023_fair_lethal_legion",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 24,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1772806603601,
|
||||||
|
"tag": "0024_far_beast",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -18,5 +18,16 @@ export const issueComments = pgTable(
|
|||||||
(table) => ({
|
(table) => ({
|
||||||
issueIdx: index("issue_comments_issue_idx").on(table.issueId),
|
issueIdx: index("issue_comments_issue_idx").on(table.issueId),
|
||||||
companyIdx: index("issue_comments_company_idx").on(table.companyId),
|
companyIdx: index("issue_comments_company_idx").on(table.companyId),
|
||||||
|
companyIssueCreatedAtIdx: index("issue_comments_company_issue_created_at_idx").on(
|
||||||
|
table.companyId,
|
||||||
|
table.issueId,
|
||||||
|
table.createdAt,
|
||||||
|
),
|
||||||
|
companyAuthorIssueCreatedAtIdx: index("issue_comments_company_author_issue_created_at_idx").on(
|
||||||
|
table.companyId,
|
||||||
|
table.authorUserId,
|
||||||
|
table.issueId,
|
||||||
|
table.createdAt,
|
||||||
|
),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -82,6 +82,9 @@ export interface Issue {
|
|||||||
project?: Project | null;
|
project?: Project | null;
|
||||||
goal?: Goal | null;
|
goal?: Goal | null;
|
||||||
mentionedProjects?: Project[];
|
mentionedProjects?: Project[];
|
||||||
|
myLastTouchAt?: Date | null;
|
||||||
|
lastExternalCommentAt?: Date | null;
|
||||||
|
isUnreadForMe?: boolean;
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
}
|
}
|
||||||
|
|||||||
75
server/src/__tests__/issues-user-context.test.ts
Normal file
75
server/src/__tests__/issues-user-context.test.ts
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { deriveIssueUserContext } from "../services/issues.ts";
|
||||||
|
|
||||||
|
function makeIssue(overrides?: Partial<{
|
||||||
|
createdByUserId: string | null;
|
||||||
|
assigneeUserId: string | null;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
}>) {
|
||||||
|
return {
|
||||||
|
createdByUserId: null,
|
||||||
|
assigneeUserId: null,
|
||||||
|
createdAt: new Date("2026-03-06T10:00:00.000Z"),
|
||||||
|
updatedAt: new Date("2026-03-06T11:00:00.000Z"),
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("deriveIssueUserContext", () => {
|
||||||
|
it("marks issue unread when external comments are newer than my latest comment", () => {
|
||||||
|
const context = deriveIssueUserContext(
|
||||||
|
makeIssue({ createdByUserId: "user-1" }),
|
||||||
|
"user-1",
|
||||||
|
{
|
||||||
|
myLastCommentAt: new Date("2026-03-06T12:00:00.000Z"),
|
||||||
|
lastExternalCommentAt: new Date("2026-03-06T13:00:00.000Z"),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(context.myLastTouchAt?.toISOString()).toBe("2026-03-06T12:00:00.000Z");
|
||||||
|
expect(context.lastExternalCommentAt?.toISOString()).toBe("2026-03-06T13:00:00.000Z");
|
||||||
|
expect(context.isUnreadForMe).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("marks issue read when my latest comment is newest", () => {
|
||||||
|
const context = deriveIssueUserContext(
|
||||||
|
makeIssue({ createdByUserId: "user-1" }),
|
||||||
|
"user-1",
|
||||||
|
{
|
||||||
|
myLastCommentAt: new Date("2026-03-06T14:00:00.000Z"),
|
||||||
|
lastExternalCommentAt: new Date("2026-03-06T13:00:00.000Z"),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(context.isUnreadForMe).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses issue creation time as fallback touch point for creator", () => {
|
||||||
|
const context = deriveIssueUserContext(
|
||||||
|
makeIssue({ createdByUserId: "user-1", createdAt: new Date("2026-03-06T09:00:00.000Z") }),
|
||||||
|
"user-1",
|
||||||
|
{
|
||||||
|
myLastCommentAt: null,
|
||||||
|
lastExternalCommentAt: new Date("2026-03-06T10:00:00.000Z"),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(context.myLastTouchAt?.toISOString()).toBe("2026-03-06T09:00:00.000Z");
|
||||||
|
expect(context.isUnreadForMe).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses issue updated time as fallback touch point for assignee", () => {
|
||||||
|
const context = deriveIssueUserContext(
|
||||||
|
makeIssue({ assigneeUserId: "user-1", updatedAt: new Date("2026-03-06T15:00:00.000Z") }),
|
||||||
|
"user-1",
|
||||||
|
{
|
||||||
|
myLastCommentAt: null,
|
||||||
|
lastExternalCommentAt: new Date("2026-03-06T14:59:00.000Z"),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(context.myLastTouchAt?.toISOString()).toBe("2026-03-06T15:00:00.000Z");
|
||||||
|
expect(context.isUnreadForMe).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -188,20 +188,40 @@ export function issueRoutes(db: Db, storage: StorageService) {
|
|||||||
const companyId = req.params.companyId as string;
|
const companyId = req.params.companyId as string;
|
||||||
assertCompanyAccess(req, companyId);
|
assertCompanyAccess(req, companyId);
|
||||||
const assigneeUserFilterRaw = req.query.assigneeUserId as string | undefined;
|
const assigneeUserFilterRaw = req.query.assigneeUserId as string | undefined;
|
||||||
|
const touchedByUserFilterRaw = req.query.touchedByUserId as string | undefined;
|
||||||
|
const unreadForUserFilterRaw = req.query.unreadForUserId as string | undefined;
|
||||||
const assigneeUserId =
|
const assigneeUserId =
|
||||||
assigneeUserFilterRaw === "me" && req.actor.type === "board"
|
assigneeUserFilterRaw === "me" && req.actor.type === "board"
|
||||||
? req.actor.userId
|
? req.actor.userId
|
||||||
: assigneeUserFilterRaw;
|
: assigneeUserFilterRaw;
|
||||||
|
const touchedByUserId =
|
||||||
|
touchedByUserFilterRaw === "me" && req.actor.type === "board"
|
||||||
|
? req.actor.userId
|
||||||
|
: touchedByUserFilterRaw;
|
||||||
|
const unreadForUserId =
|
||||||
|
unreadForUserFilterRaw === "me" && req.actor.type === "board"
|
||||||
|
? req.actor.userId
|
||||||
|
: unreadForUserFilterRaw;
|
||||||
|
|
||||||
if (assigneeUserFilterRaw === "me" && (!assigneeUserId || req.actor.type !== "board")) {
|
if (assigneeUserFilterRaw === "me" && (!assigneeUserId || req.actor.type !== "board")) {
|
||||||
res.status(403).json({ error: "assigneeUserId=me requires board authentication" });
|
res.status(403).json({ error: "assigneeUserId=me requires board authentication" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (touchedByUserFilterRaw === "me" && (!touchedByUserId || req.actor.type !== "board")) {
|
||||||
|
res.status(403).json({ error: "touchedByUserId=me requires board authentication" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (unreadForUserFilterRaw === "me" && (!unreadForUserId || req.actor.type !== "board")) {
|
||||||
|
res.status(403).json({ error: "unreadForUserId=me requires board authentication" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const result = await svc.list(companyId, {
|
const result = await svc.list(companyId, {
|
||||||
status: req.query.status as string | undefined,
|
status: req.query.status as string | undefined,
|
||||||
assigneeAgentId: req.query.assigneeAgentId as string | undefined,
|
assigneeAgentId: req.query.assigneeAgentId as string | undefined,
|
||||||
assigneeUserId,
|
assigneeUserId,
|
||||||
|
touchedByUserId,
|
||||||
|
unreadForUserId,
|
||||||
projectId: req.query.projectId as string | undefined,
|
projectId: req.query.projectId as string | undefined,
|
||||||
labelId: req.query.labelId as string | undefined,
|
labelId: req.query.labelId as string | undefined,
|
||||||
q: req.query.q as string | undefined,
|
q: req.query.q as string | undefined,
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import type { Db } from "@paperclipai/db";
|
import type { Db } from "@paperclipai/db";
|
||||||
import { and, eq, inArray, isNull, sql } from "drizzle-orm";
|
import { and, eq, sql } from "drizzle-orm";
|
||||||
import { issues, joinRequests } from "@paperclipai/db";
|
import { joinRequests } from "@paperclipai/db";
|
||||||
import { sidebarBadgeService } from "../services/sidebar-badges.js";
|
import { sidebarBadgeService } from "../services/sidebar-badges.js";
|
||||||
|
import { issueService } from "../services/issues.js";
|
||||||
import { accessService } from "../services/access.js";
|
import { accessService } from "../services/access.js";
|
||||||
import { assertCompanyAccess } from "./authz.js";
|
import { assertCompanyAccess } from "./authz.js";
|
||||||
|
|
||||||
@@ -11,6 +12,7 @@ const INBOX_ISSUE_STATUSES = ["backlog", "todo", "in_progress", "in_review", "bl
|
|||||||
export function sidebarBadgeRoutes(db: Db) {
|
export function sidebarBadgeRoutes(db: Db) {
|
||||||
const router = Router();
|
const router = Router();
|
||||||
const svc = sidebarBadgeService(db);
|
const svc = sidebarBadgeService(db);
|
||||||
|
const issueSvc = issueService(db);
|
||||||
const access = accessService(db);
|
const access = accessService(db);
|
||||||
|
|
||||||
router.get("/companies/:companyId/sidebar-badges", async (req, res) => {
|
router.get("/companies/:companyId/sidebar-badges", async (req, res) => {
|
||||||
@@ -34,25 +36,18 @@ export function sidebarBadgeRoutes(db: Db) {
|
|||||||
.then((rows) => Number(rows[0]?.count ?? 0))
|
.then((rows) => Number(rows[0]?.count ?? 0))
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
const assignedIssueCount =
|
const unreadTouchedIssueCount =
|
||||||
req.actor.type === "board" && req.actor.userId
|
req.actor.type === "board" && req.actor.userId
|
||||||
? await db
|
? await issueSvc.countUnreadTouchedByUser(
|
||||||
.select({ count: sql<number>`count(*)` })
|
companyId,
|
||||||
.from(issues)
|
req.actor.userId,
|
||||||
.where(
|
INBOX_ISSUE_STATUSES.join(","),
|
||||||
and(
|
)
|
||||||
eq(issues.companyId, companyId),
|
|
||||||
eq(issues.assigneeUserId, req.actor.userId),
|
|
||||||
inArray(issues.status, [...INBOX_ISSUE_STATUSES]),
|
|
||||||
isNull(issues.hiddenAt),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.then((rows) => Number(rows[0]?.count ?? 0))
|
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
const badges = await svc.get(companyId, {
|
const badges = await svc.get(companyId, {
|
||||||
joinRequests: joinRequestCount,
|
joinRequests: joinRequestCount,
|
||||||
assignedIssues: assignedIssueCount,
|
unreadTouchedIssues: unreadTouchedIssueCount,
|
||||||
});
|
});
|
||||||
res.json(badges);
|
res.json(badges);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -49,6 +49,8 @@ export interface IssueFilters {
|
|||||||
status?: string;
|
status?: string;
|
||||||
assigneeAgentId?: string;
|
assigneeAgentId?: string;
|
||||||
assigneeUserId?: string;
|
assigneeUserId?: string;
|
||||||
|
touchedByUserId?: string;
|
||||||
|
unreadForUserId?: string;
|
||||||
projectId?: string;
|
projectId?: string;
|
||||||
labelId?: string;
|
labelId?: string;
|
||||||
q?: string;
|
q?: string;
|
||||||
@@ -68,6 +70,17 @@ type IssueActiveRunRow = {
|
|||||||
};
|
};
|
||||||
type IssueWithLabels = IssueRow & { labels: IssueLabelRow[]; labelIds: string[] };
|
type IssueWithLabels = IssueRow & { labels: IssueLabelRow[]; labelIds: string[] };
|
||||||
type IssueWithLabelsAndRun = IssueWithLabels & { activeRun: IssueActiveRunRow | null };
|
type IssueWithLabelsAndRun = IssueWithLabels & { activeRun: IssueActiveRunRow | null };
|
||||||
|
type IssueUserCommentStats = {
|
||||||
|
issueId: string;
|
||||||
|
myLastCommentAt: Date | null;
|
||||||
|
lastExternalCommentAt: Date | null;
|
||||||
|
};
|
||||||
|
type IssueUserContextInput = {
|
||||||
|
createdByUserId: string | null;
|
||||||
|
assigneeUserId: string | null;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
};
|
||||||
|
|
||||||
function sameRunLock(checkoutRunId: string | null, actorRunId: string | null) {
|
function sameRunLock(checkoutRunId: string | null, actorRunId: string | null) {
|
||||||
if (actorRunId) return checkoutRunId === actorRunId;
|
if (actorRunId) return checkoutRunId === actorRunId;
|
||||||
@@ -80,6 +93,90 @@ function escapeLikePattern(value: string): string {
|
|||||||
return value.replace(/[\\%_]/g, "\\$&");
|
return value.replace(/[\\%_]/g, "\\$&");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function touchedByUserCondition(companyId: string, userId: string) {
|
||||||
|
return sql<boolean>`
|
||||||
|
(
|
||||||
|
${issues.createdByUserId} = ${userId}
|
||||||
|
OR ${issues.assigneeUserId} = ${userId}
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM ${issueComments}
|
||||||
|
WHERE ${issueComments.issueId} = ${issues.id}
|
||||||
|
AND ${issueComments.companyId} = ${companyId}
|
||||||
|
AND ${issueComments.authorUserId} = ${userId}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function myLastCommentAtExpr(companyId: string, userId: string) {
|
||||||
|
return sql<Date | null>`
|
||||||
|
(
|
||||||
|
SELECT MAX(${issueComments.createdAt})
|
||||||
|
FROM ${issueComments}
|
||||||
|
WHERE ${issueComments.issueId} = ${issues.id}
|
||||||
|
AND ${issueComments.companyId} = ${companyId}
|
||||||
|
AND ${issueComments.authorUserId} = ${userId}
|
||||||
|
)
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function myLastTouchAtExpr(companyId: string, userId: string) {
|
||||||
|
const myLastCommentAt = myLastCommentAtExpr(companyId, userId);
|
||||||
|
return sql<Date | null>`
|
||||||
|
COALESCE(
|
||||||
|
${myLastCommentAt},
|
||||||
|
CASE WHEN ${issues.createdByUserId} = ${userId} THEN ${issues.createdAt} ELSE NULL END,
|
||||||
|
CASE WHEN ${issues.assigneeUserId} = ${userId} THEN ${issues.updatedAt} ELSE NULL END
|
||||||
|
)
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function unreadForUserCondition(companyId: string, userId: string) {
|
||||||
|
const touchedCondition = touchedByUserCondition(companyId, userId);
|
||||||
|
const myLastTouchAt = myLastTouchAtExpr(companyId, userId);
|
||||||
|
return sql<boolean>`
|
||||||
|
(
|
||||||
|
${touchedCondition}
|
||||||
|
AND EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM ${issueComments}
|
||||||
|
WHERE ${issueComments.issueId} = ${issues.id}
|
||||||
|
AND ${issueComments.companyId} = ${companyId}
|
||||||
|
AND (
|
||||||
|
${issueComments.authorUserId} IS NULL
|
||||||
|
OR ${issueComments.authorUserId} <> ${userId}
|
||||||
|
)
|
||||||
|
AND ${issueComments.createdAt} > ${myLastTouchAt}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function deriveIssueUserContext(
|
||||||
|
issue: IssueUserContextInput,
|
||||||
|
userId: string,
|
||||||
|
stats: { myLastCommentAt: Date | null; lastExternalCommentAt: Date | null } | null | undefined,
|
||||||
|
) {
|
||||||
|
const myLastCommentAt = stats?.myLastCommentAt ?? null;
|
||||||
|
const myLastTouchAt =
|
||||||
|
myLastCommentAt ??
|
||||||
|
(issue.createdByUserId === userId ? issue.createdAt : null) ??
|
||||||
|
(issue.assigneeUserId === userId ? issue.updatedAt : null);
|
||||||
|
const lastExternalCommentAt = stats?.lastExternalCommentAt ?? null;
|
||||||
|
const isUnreadForMe = Boolean(
|
||||||
|
myLastTouchAt &&
|
||||||
|
lastExternalCommentAt &&
|
||||||
|
lastExternalCommentAt.getTime() > myLastTouchAt.getTime(),
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
myLastTouchAt,
|
||||||
|
lastExternalCommentAt,
|
||||||
|
isUnreadForMe,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async function labelMapForIssues(dbOrTx: any, issueIds: string[]): Promise<Map<string, IssueLabelRow[]>> {
|
async function labelMapForIssues(dbOrTx: any, issueIds: string[]): Promise<Map<string, IssueLabelRow[]>> {
|
||||||
const map = new Map<string, IssueLabelRow[]>();
|
const map = new Map<string, IssueLabelRow[]>();
|
||||||
if (issueIds.length === 0) return map;
|
if (issueIds.length === 0) return map;
|
||||||
@@ -284,6 +381,9 @@ export function issueService(db: Db) {
|
|||||||
return {
|
return {
|
||||||
list: async (companyId: string, filters?: IssueFilters) => {
|
list: async (companyId: string, filters?: IssueFilters) => {
|
||||||
const conditions = [eq(issues.companyId, companyId)];
|
const conditions = [eq(issues.companyId, companyId)];
|
||||||
|
const touchedByUserId = filters?.touchedByUserId?.trim() || undefined;
|
||||||
|
const unreadForUserId = filters?.unreadForUserId?.trim() || undefined;
|
||||||
|
const contextUserId = unreadForUserId ?? touchedByUserId;
|
||||||
const rawSearch = filters?.q?.trim() ?? "";
|
const rawSearch = filters?.q?.trim() ?? "";
|
||||||
const hasSearch = rawSearch.length > 0;
|
const hasSearch = rawSearch.length > 0;
|
||||||
const escapedSearch = hasSearch ? escapeLikePattern(rawSearch) : "";
|
const escapedSearch = hasSearch ? escapeLikePattern(rawSearch) : "";
|
||||||
@@ -313,6 +413,12 @@ export function issueService(db: Db) {
|
|||||||
if (filters?.assigneeUserId) {
|
if (filters?.assigneeUserId) {
|
||||||
conditions.push(eq(issues.assigneeUserId, filters.assigneeUserId));
|
conditions.push(eq(issues.assigneeUserId, filters.assigneeUserId));
|
||||||
}
|
}
|
||||||
|
if (touchedByUserId) {
|
||||||
|
conditions.push(touchedByUserCondition(companyId, touchedByUserId));
|
||||||
|
}
|
||||||
|
if (unreadForUserId) {
|
||||||
|
conditions.push(unreadForUserCondition(companyId, unreadForUserId));
|
||||||
|
}
|
||||||
if (filters?.projectId) conditions.push(eq(issues.projectId, filters.projectId));
|
if (filters?.projectId) conditions.push(eq(issues.projectId, filters.projectId));
|
||||||
if (filters?.labelId) {
|
if (filters?.labelId) {
|
||||||
const labeledIssueIds = await db
|
const labeledIssueIds = await db
|
||||||
@@ -353,7 +459,62 @@ export function issueService(db: Db) {
|
|||||||
.orderBy(hasSearch ? asc(searchOrder) : asc(priorityOrder), asc(priorityOrder), desc(issues.updatedAt));
|
.orderBy(hasSearch ? asc(searchOrder) : asc(priorityOrder), asc(priorityOrder), desc(issues.updatedAt));
|
||||||
const withLabels = await withIssueLabels(db, rows);
|
const withLabels = await withIssueLabels(db, rows);
|
||||||
const runMap = await activeRunMapForIssues(db, withLabels);
|
const runMap = await activeRunMapForIssues(db, withLabels);
|
||||||
return withActiveRuns(withLabels, runMap);
|
const withRuns = withActiveRuns(withLabels, runMap);
|
||||||
|
if (!contextUserId || withRuns.length === 0) {
|
||||||
|
return withRuns;
|
||||||
|
}
|
||||||
|
|
||||||
|
const issueIds = withRuns.map((row) => row.id);
|
||||||
|
const statsRows = await db
|
||||||
|
.select({
|
||||||
|
issueId: issueComments.issueId,
|
||||||
|
myLastCommentAt: sql<Date | null>`
|
||||||
|
MAX(CASE WHEN ${issueComments.authorUserId} = ${contextUserId} THEN ${issueComments.createdAt} END)
|
||||||
|
`,
|
||||||
|
lastExternalCommentAt: sql<Date | null>`
|
||||||
|
MAX(
|
||||||
|
CASE
|
||||||
|
WHEN ${issueComments.authorUserId} IS NULL OR ${issueComments.authorUserId} <> ${contextUserId}
|
||||||
|
THEN ${issueComments.createdAt}
|
||||||
|
END
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
})
|
||||||
|
.from(issueComments)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(issueComments.companyId, companyId),
|
||||||
|
inArray(issueComments.issueId, issueIds),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.groupBy(issueComments.issueId);
|
||||||
|
const statsByIssueId = new Map(statsRows.map((row) => [row.issueId, row]));
|
||||||
|
|
||||||
|
return withRuns.map((row) => ({
|
||||||
|
...row,
|
||||||
|
...deriveIssueUserContext(row, contextUserId, statsByIssueId.get(row.id)),
|
||||||
|
}));
|
||||||
|
},
|
||||||
|
|
||||||
|
countUnreadTouchedByUser: async (companyId: string, userId: string, status?: string) => {
|
||||||
|
const conditions = [
|
||||||
|
eq(issues.companyId, companyId),
|
||||||
|
isNull(issues.hiddenAt),
|
||||||
|
unreadForUserCondition(companyId, userId),
|
||||||
|
];
|
||||||
|
if (status) {
|
||||||
|
const statuses = status.split(",").map((s) => s.trim()).filter(Boolean);
|
||||||
|
if (statuses.length === 1) {
|
||||||
|
conditions.push(eq(issues.status, statuses[0]));
|
||||||
|
} else if (statuses.length > 1) {
|
||||||
|
conditions.push(inArray(issues.status, statuses));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const [row] = await db
|
||||||
|
.select({ count: sql<number>`count(*)` })
|
||||||
|
.from(issues)
|
||||||
|
.where(and(...conditions));
|
||||||
|
return Number(row?.count ?? 0);
|
||||||
},
|
},
|
||||||
|
|
||||||
getById: async (id: string) => {
|
getById: async (id: string) => {
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export function sidebarBadgeService(db: Db) {
|
|||||||
return {
|
return {
|
||||||
get: async (
|
get: async (
|
||||||
companyId: string,
|
companyId: string,
|
||||||
extra?: { joinRequests?: number; assignedIssues?: number },
|
extra?: { joinRequests?: number; unreadTouchedIssues?: number },
|
||||||
): Promise<SidebarBadges> => {
|
): Promise<SidebarBadges> => {
|
||||||
const actionableApprovals = await db
|
const actionableApprovals = await db
|
||||||
.select({ count: sql<number>`count(*)` })
|
.select({ count: sql<number>`count(*)` })
|
||||||
@@ -43,9 +43,9 @@ export function sidebarBadgeService(db: Db) {
|
|||||||
).length;
|
).length;
|
||||||
|
|
||||||
const joinRequests = extra?.joinRequests ?? 0;
|
const joinRequests = extra?.joinRequests ?? 0;
|
||||||
const assignedIssues = extra?.assignedIssues ?? 0;
|
const unreadTouchedIssues = extra?.unreadTouchedIssues ?? 0;
|
||||||
return {
|
return {
|
||||||
inbox: actionableApprovals + failedRuns + joinRequests + assignedIssues,
|
inbox: actionableApprovals + failedRuns + joinRequests + unreadTouchedIssues,
|
||||||
approvals: actionableApprovals,
|
approvals: actionableApprovals,
|
||||||
failedRuns,
|
failedRuns,
|
||||||
joinRequests,
|
joinRequests,
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ export const issuesApi = {
|
|||||||
projectId?: string;
|
projectId?: string;
|
||||||
assigneeAgentId?: string;
|
assigneeAgentId?: string;
|
||||||
assigneeUserId?: string;
|
assigneeUserId?: string;
|
||||||
|
touchedByUserId?: string;
|
||||||
|
unreadForUserId?: string;
|
||||||
labelId?: string;
|
labelId?: string;
|
||||||
q?: string;
|
q?: string;
|
||||||
},
|
},
|
||||||
@@ -18,6 +20,8 @@ export const issuesApi = {
|
|||||||
if (filters?.projectId) params.set("projectId", filters.projectId);
|
if (filters?.projectId) params.set("projectId", filters.projectId);
|
||||||
if (filters?.assigneeAgentId) params.set("assigneeAgentId", filters.assigneeAgentId);
|
if (filters?.assigneeAgentId) params.set("assigneeAgentId", filters.assigneeAgentId);
|
||||||
if (filters?.assigneeUserId) params.set("assigneeUserId", filters.assigneeUserId);
|
if (filters?.assigneeUserId) params.set("assigneeUserId", filters.assigneeUserId);
|
||||||
|
if (filters?.touchedByUserId) params.set("touchedByUserId", filters.touchedByUserId);
|
||||||
|
if (filters?.unreadForUserId) params.set("unreadForUserId", filters.unreadForUserId);
|
||||||
if (filters?.labelId) params.set("labelId", filters.labelId);
|
if (filters?.labelId) params.set("labelId", filters.labelId);
|
||||||
if (filters?.q) params.set("q", filters.q);
|
if (filters?.q) params.set("q", filters.q);
|
||||||
const qs = params.toString();
|
const qs = params.toString();
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ export const queryKeys = {
|
|||||||
search: (companyId: string, q: string, projectId?: string) =>
|
search: (companyId: string, q: string, projectId?: string) =>
|
||||||
["issues", companyId, "search", q, projectId ?? "__all-projects__"] as const,
|
["issues", companyId, "search", q, projectId ?? "__all-projects__"] as const,
|
||||||
listAssignedToMe: (companyId: string) => ["issues", companyId, "assigned-to-me"] as const,
|
listAssignedToMe: (companyId: string) => ["issues", companyId, "assigned-to-me"] as const,
|
||||||
|
listTouchedByMe: (companyId: string) => ["issues", companyId, "touched-by-me"] as const,
|
||||||
|
listUnreadTouchedByMe: (companyId: string) => ["issues", companyId, "unread-touched-by-me"] as const,
|
||||||
labels: (companyId: string) => ["issues", companyId, "labels"] as const,
|
labels: (companyId: string) => ["issues", companyId, "labels"] as const,
|
||||||
listByProject: (companyId: string, projectId: string) =>
|
listByProject: (companyId: string, projectId: string) =>
|
||||||
["issues", companyId, "project", projectId] as const,
|
["issues", companyId, "project", projectId] as const,
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ const ACTIONABLE_APPROVAL_STATUSES = new Set(["pending", "revision_requested"]);
|
|||||||
type InboxTab = "new" | "all";
|
type InboxTab = "new" | "all";
|
||||||
type InboxCategoryFilter =
|
type InboxCategoryFilter =
|
||||||
| "everything"
|
| "everything"
|
||||||
| "assigned_to_me"
|
| "issues_i_touched"
|
||||||
| "join_requests"
|
| "join_requests"
|
||||||
| "approvals"
|
| "approvals"
|
||||||
| "failed_runs"
|
| "failed_runs"
|
||||||
@@ -57,7 +57,7 @@ type InboxCategoryFilter =
|
|||||||
| "stale_work";
|
| "stale_work";
|
||||||
type InboxApprovalFilter = "all" | "actionable" | "resolved";
|
type InboxApprovalFilter = "all" | "actionable" | "resolved";
|
||||||
type SectionKey =
|
type SectionKey =
|
||||||
| "assigned_to_me"
|
| "issues_i_touched"
|
||||||
| "join_requests"
|
| "join_requests"
|
||||||
| "approvals"
|
| "approvals"
|
||||||
| "failed_runs"
|
| "failed_runs"
|
||||||
@@ -350,13 +350,25 @@ export function Inbox() {
|
|||||||
enabled: !!selectedCompanyId,
|
enabled: !!selectedCompanyId,
|
||||||
});
|
});
|
||||||
const {
|
const {
|
||||||
data: assignedToMeIssuesRaw = [],
|
data: touchedIssuesRaw = [],
|
||||||
isLoading: isAssignedToMeLoading,
|
isLoading: isTouchedIssuesLoading,
|
||||||
} = useQuery({
|
} = useQuery({
|
||||||
queryKey: queryKeys.issues.listAssignedToMe(selectedCompanyId!),
|
queryKey: queryKeys.issues.listTouchedByMe(selectedCompanyId!),
|
||||||
queryFn: () =>
|
queryFn: () =>
|
||||||
issuesApi.list(selectedCompanyId!, {
|
issuesApi.list(selectedCompanyId!, {
|
||||||
assigneeUserId: "me",
|
touchedByUserId: "me",
|
||||||
|
status: "backlog,todo,in_progress,in_review,blocked",
|
||||||
|
}),
|
||||||
|
enabled: !!selectedCompanyId,
|
||||||
|
});
|
||||||
|
const {
|
||||||
|
data: unreadTouchedIssuesRaw = [],
|
||||||
|
isLoading: isUnreadTouchedIssuesLoading,
|
||||||
|
} = useQuery({
|
||||||
|
queryKey: queryKeys.issues.listUnreadTouchedByMe(selectedCompanyId!),
|
||||||
|
queryFn: () =>
|
||||||
|
issuesApi.list(selectedCompanyId!, {
|
||||||
|
unreadForUserId: "me",
|
||||||
status: "backlog,todo,in_progress,in_review,blocked",
|
status: "backlog,todo,in_progress,in_review,blocked",
|
||||||
}),
|
}),
|
||||||
enabled: !!selectedCompanyId,
|
enabled: !!selectedCompanyId,
|
||||||
@@ -372,12 +384,20 @@ export function Inbox() {
|
|||||||
() => (issues ? getStaleIssues(issues) : []).filter((i) => !dismissed.has(`stale:${i.id}`)),
|
() => (issues ? getStaleIssues(issues) : []).filter((i) => !dismissed.has(`stale:${i.id}`)),
|
||||||
[issues, dismissed],
|
[issues, dismissed],
|
||||||
);
|
);
|
||||||
const assignedToMeIssues = useMemo(
|
const sortByRecentExternalComment = useCallback((a: Issue, b: Issue) => {
|
||||||
() =>
|
const aExternal = a.lastExternalCommentAt ? new Date(a.lastExternalCommentAt).getTime() : 0;
|
||||||
[...assignedToMeIssuesRaw].sort(
|
const bExternal = b.lastExternalCommentAt ? new Date(b.lastExternalCommentAt).getTime() : 0;
|
||||||
(a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime(),
|
if (aExternal !== bExternal) return bExternal - aExternal;
|
||||||
),
|
return new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime();
|
||||||
[assignedToMeIssuesRaw],
|
}, []);
|
||||||
|
|
||||||
|
const touchedIssues = useMemo(
|
||||||
|
() => [...touchedIssuesRaw].sort(sortByRecentExternalComment),
|
||||||
|
[sortByRecentExternalComment, touchedIssuesRaw],
|
||||||
|
);
|
||||||
|
const unreadTouchedIssues = useMemo(
|
||||||
|
() => [...unreadTouchedIssuesRaw].sort(sortByRecentExternalComment),
|
||||||
|
[sortByRecentExternalComment, unreadTouchedIssuesRaw],
|
||||||
);
|
);
|
||||||
|
|
||||||
const agentById = useMemo(() => {
|
const agentById = useMemo(() => {
|
||||||
@@ -489,10 +509,11 @@ export function Inbox() {
|
|||||||
const hasAlerts = showAggregateAgentError || showBudgetAlert;
|
const hasAlerts = showAggregateAgentError || showBudgetAlert;
|
||||||
const hasStale = staleIssues.length > 0;
|
const hasStale = staleIssues.length > 0;
|
||||||
const hasJoinRequests = joinRequests.length > 0;
|
const hasJoinRequests = joinRequests.length > 0;
|
||||||
const hasAssignedToMe = assignedToMeIssues.length > 0;
|
const hasTouchedIssues = touchedIssues.length > 0;
|
||||||
|
const hasUnreadTouchedIssues = unreadTouchedIssues.length > 0;
|
||||||
|
|
||||||
const newItemCount =
|
const newItemCount =
|
||||||
assignedToMeIssues.length +
|
unreadTouchedIssues.length +
|
||||||
joinRequests.length +
|
joinRequests.length +
|
||||||
actionableApprovals.length +
|
actionableApprovals.length +
|
||||||
failedRuns.length +
|
failedRuns.length +
|
||||||
@@ -502,8 +523,8 @@ export function Inbox() {
|
|||||||
|
|
||||||
const showJoinRequestsCategory =
|
const showJoinRequestsCategory =
|
||||||
allCategoryFilter === "everything" || allCategoryFilter === "join_requests";
|
allCategoryFilter === "everything" || allCategoryFilter === "join_requests";
|
||||||
const showAssignedCategory =
|
const showTouchedCategory =
|
||||||
allCategoryFilter === "everything" || allCategoryFilter === "assigned_to_me";
|
allCategoryFilter === "everything" || allCategoryFilter === "issues_i_touched";
|
||||||
const showApprovalsCategory = allCategoryFilter === "everything" || allCategoryFilter === "approvals";
|
const showApprovalsCategory = allCategoryFilter === "everything" || allCategoryFilter === "approvals";
|
||||||
const showFailedRunsCategory =
|
const showFailedRunsCategory =
|
||||||
allCategoryFilter === "everything" || allCategoryFilter === "failed_runs";
|
allCategoryFilter === "everything" || allCategoryFilter === "failed_runs";
|
||||||
@@ -511,7 +532,8 @@ export function Inbox() {
|
|||||||
const showStaleCategory = allCategoryFilter === "everything" || allCategoryFilter === "stale_work";
|
const showStaleCategory = allCategoryFilter === "everything" || allCategoryFilter === "stale_work";
|
||||||
|
|
||||||
const approvalsToRender = tab === "new" ? actionableApprovals : filteredAllApprovals;
|
const approvalsToRender = tab === "new" ? actionableApprovals : filteredAllApprovals;
|
||||||
const showAssignedSection = tab === "new" ? hasAssignedToMe : showAssignedCategory && hasAssignedToMe;
|
const showTouchedSection =
|
||||||
|
tab === "new" ? hasUnreadTouchedIssues : showTouchedCategory && hasTouchedIssues;
|
||||||
const showJoinRequestsSection =
|
const showJoinRequestsSection =
|
||||||
tab === "new" ? hasJoinRequests : showJoinRequestsCategory && hasJoinRequests;
|
tab === "new" ? hasJoinRequests : showJoinRequestsCategory && hasJoinRequests;
|
||||||
const showApprovalsSection =
|
const showApprovalsSection =
|
||||||
@@ -524,7 +546,7 @@ export function Inbox() {
|
|||||||
const showStaleSection = tab === "new" ? hasStale : showStaleCategory && hasStale;
|
const showStaleSection = tab === "new" ? hasStale : showStaleCategory && hasStale;
|
||||||
|
|
||||||
const visibleSections = [
|
const visibleSections = [
|
||||||
showAssignedSection ? "assigned_to_me" : null,
|
showTouchedSection ? "issues_i_touched" : null,
|
||||||
showApprovalsSection ? "approvals" : null,
|
showApprovalsSection ? "approvals" : null,
|
||||||
showJoinRequestsSection ? "join_requests" : null,
|
showJoinRequestsSection ? "join_requests" : null,
|
||||||
showFailedRunsSection ? "failed_runs" : null,
|
showFailedRunsSection ? "failed_runs" : null,
|
||||||
@@ -537,7 +559,8 @@ export function Inbox() {
|
|||||||
!isApprovalsLoading &&
|
!isApprovalsLoading &&
|
||||||
!isDashboardLoading &&
|
!isDashboardLoading &&
|
||||||
!isIssuesLoading &&
|
!isIssuesLoading &&
|
||||||
!isAssignedToMeLoading &&
|
!isTouchedIssuesLoading &&
|
||||||
|
!isUnreadTouchedIssuesLoading &&
|
||||||
!isRunsLoading;
|
!isRunsLoading;
|
||||||
|
|
||||||
const showSeparatorBefore = (key: SectionKey) => visibleSections.indexOf(key) > 0;
|
const showSeparatorBefore = (key: SectionKey) => visibleSections.indexOf(key) > 0;
|
||||||
@@ -577,7 +600,7 @@ export function Inbox() {
|
|||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
<SelectContent>
|
<SelectContent>
|
||||||
<SelectItem value="everything">All categories</SelectItem>
|
<SelectItem value="everything">All categories</SelectItem>
|
||||||
<SelectItem value="assigned_to_me">Assigned to me</SelectItem>
|
<SelectItem value="issues_i_touched">Issues I touched</SelectItem>
|
||||||
<SelectItem value="join_requests">Join requests</SelectItem>
|
<SelectItem value="join_requests">Join requests</SelectItem>
|
||||||
<SelectItem value="approvals">Approvals</SelectItem>
|
<SelectItem value="approvals">Approvals</SelectItem>
|
||||||
<SelectItem value="failed_runs">Failed runs</SelectItem>
|
<SelectItem value="failed_runs">Failed runs</SelectItem>
|
||||||
@@ -615,19 +638,23 @@ export function Inbox() {
|
|||||||
{allLoaded && visibleSections.length === 0 && (
|
{allLoaded && visibleSections.length === 0 && (
|
||||||
<EmptyState
|
<EmptyState
|
||||||
icon={InboxIcon}
|
icon={InboxIcon}
|
||||||
message={tab === "new" ? "You're all caught up!" : "No inbox items match these filters."}
|
message={
|
||||||
|
tab === "new"
|
||||||
|
? "No unread updates on issues you're involved in."
|
||||||
|
: "No inbox items match these filters."
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{showAssignedSection && (
|
{showTouchedSection && (
|
||||||
<>
|
<>
|
||||||
{showSeparatorBefore("assigned_to_me") && <Separator />}
|
{showSeparatorBefore("issues_i_touched") && <Separator />}
|
||||||
<div>
|
<div>
|
||||||
<h3 className="mb-3 text-sm font-semibold uppercase tracking-wide text-muted-foreground">
|
<h3 className="mb-3 text-sm font-semibold uppercase tracking-wide text-muted-foreground">
|
||||||
Assigned To Me
|
Issues I Touched
|
||||||
</h3>
|
</h3>
|
||||||
<div className="divide-y divide-border border border-border">
|
<div className="divide-y divide-border border border-border">
|
||||||
{assignedToMeIssues.map((issue) => (
|
{(tab === "new" ? unreadTouchedIssues : touchedIssues).map((issue) => (
|
||||||
<Link
|
<Link
|
||||||
key={issue.id}
|
key={issue.id}
|
||||||
to={`/issues/${issue.identifier ?? issue.id}`}
|
to={`/issues/${issue.identifier ?? issue.id}`}
|
||||||
@@ -640,8 +667,21 @@ export function Inbox() {
|
|||||||
{issue.identifier ?? issue.id.slice(0, 8)}
|
{issue.identifier ?? issue.id.slice(0, 8)}
|
||||||
</span>
|
</span>
|
||||||
<span className="flex-1 truncate text-sm">{issue.title}</span>
|
<span className="flex-1 truncate text-sm">{issue.title}</span>
|
||||||
|
{tab === "all" && (
|
||||||
|
<span
|
||||||
|
className={`shrink-0 rounded-full px-2 py-0.5 text-[10px] font-medium ${
|
||||||
|
issue.isUnreadForMe
|
||||||
|
? "bg-blue-500/20 text-blue-600 dark:text-blue-400"
|
||||||
|
: "bg-muted text-muted-foreground"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{issue.isUnreadForMe ? "Unread" : "Read"}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
<span className="shrink-0 text-xs text-muted-foreground">
|
<span className="shrink-0 text-xs text-muted-foreground">
|
||||||
updated {timeAgo(issue.updatedAt)}
|
{issue.lastExternalCommentAt
|
||||||
|
? `commented ${timeAgo(issue.lastExternalCommentAt)}`
|
||||||
|
: `updated ${timeAgo(issue.updatedAt)}`}
|
||||||
</span>
|
</span>
|
||||||
</Link>
|
</Link>
|
||||||
))}
|
))}
|
||||||
|
|||||||
Reference in New Issue
Block a user