𤯠LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
š fix(channel): restore and purge trashed channels deleted by a non-creator admin (#22163)
AmAzing-dbce1265397a
diff --git a/packages/database/src/repositories/channel/__tests__/threadTrash.test.ts b/packages/database/src/repositories/channel/__tests__/threadTrash.test.tsindex 12185016717..bd84fbce259 100644--- a/packages/database/src/repositories/channel/__tests__/threadTrash.test.ts+++ b/packages/database/src/repositories/channel/__tests__/threadTrash.test.ts@@ -4,7 +4,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';import { getTestDB } from '../../../core/getTestDB';import { TrashModel } from '../../../models/trash';-import { channelJobs, channelMessages, channelThreads } from '../../../schemas';+import { channelJobs, channelMembers, channelMessages, channelThreads } from '../../../schemas';import { ChannelExecutionRepository, ChannelRepository } from '..';import { ChannelTrashRepository } from '../trash';import { createChannelFixture, jobsOf, lease } from './testUtils';@@ -137,4 +137,24 @@ describe('Channel thread recycle bin', () => {expect.objectContaining({ isDeleted: true }),]);});+ it('restores and purges a branch of a private workspace channel in the deleting adminās scope', async () => {+ const { channel, member, asMember, asOwner } = await fixture.createTeamChannel();+ await db.update(channelMembers).set({ role: 'admin' }).where(eq(channelMembers.id, member.id));+ const root = await asOwner.send(channel.id, {+ content: 'Root',+ requestKey: 'root',+ routing: 'fallback',+ });+ const thread = await asOwner.createThread(channel.id, { rootMessageId: root.id });+ // The trash handler scopes by the registry row, whose user is the deleter, not the creator.+ const asDeleter = () => new ChannelTrashRepository(db, fixture.memberId, fixture.workspaceId);+ await asMember.removeThread(channel.id, thread.id);+ expect(await asDeleter().restoreThread(thread.id)).toBe('restored');++ await asMember.removeThread(channel.id, thread.id);+ expect(await asDeleter().purgeThread(thread.id)).toBe('purged');+ expect(await db.select().from(channelThreads).where(eq(channelThreads.id, thread.id))).toEqual(+ [],+ );+ });});diff --git a/packages/database/src/repositories/channel/__tests__/trash.test.ts b/packages/database/src/repositories/channel/__tests__/trash.test.tsindex adeeeb2a8da..0aa83ad6cc6 100644--- a/packages/database/src/repositories/channel/__tests__/trash.test.ts+++ b/packages/database/src/repositories/channel/__tests__/trash.test.ts@@ -89,6 +89,38 @@ describe('Channel recycle bin', () => {expect(await asOwner.list()).toHaveLength(1);});+ it('restores and purges a private workspace channel in the deleting adminās scope', async () => {+ const { channel, member, asMember } = await fixture.createTeamChannel();+ expect(channel.visibility).toBe('private');+ await db.update(channelMembers).set({ role: 'admin' }).where(eq(channelMembers.id, member.id));+ // The trash handler scopes by the registry row, whose user is the deleter, not the creator.+ const asDeleter = () => new ChannelTrashRepository(db, fixture.memberId, fixture.workspaceId);+ await asMember.remove(channel.id);+ expect(await asDeleter().restore(channel.id)).toBe(true);+ expect(await asMember.list()).toHaveLength(1);++ await asMember.remove(channel.id);+ await expect(asDeleter().purge(channel.id)).resolves.toBe('purged');+ expect(await db.select().from(channels).where(eq(channels.id, channel.id))).toEqual([]);+ });++ it('drops the registry rows of threads trashed on their own when the channel is purged', async () => {+ const repo = new ChannelRepository(db, fixture.ownerId);+ const { channel } = await repo.create({ title: 'With a trashed branch' });+ const root = await repo.send(channel.id, {+ content: 'Root',+ requestKey: 'root',+ routing: 'fallback',+ });+ const thread = await repo.createThread(channel.id, { rootMessageId: root.id });+ await repo.removeThread(channel.id, thread.id);+ await repo.remove(channel.id);+ expect(await registry().findByResource('channelThread', thread.id)).toBeDefined();++ await expect(trash().purge(channel.id)).resolves.toBe('purged');+ expect(await registry().findByResource('channelThread', thread.id)).toBeUndefined();+ });+it('does not restore or purge another personal userās channel, or purge a live channel', async () => {const repo = new ChannelRepository(db, fixture.ownerId);const { channel } = await repo.create({ title: 'Private' });diff --git a/packages/database/src/repositories/channel/trash.ts b/packages/database/src/repositories/channel/trash.tsindex 898da7f05f2..1bedd46c17d 100644--- a/packages/database/src/repositories/channel/trash.ts+++ b/packages/database/src/repositories/channel/trash.ts@@ -18,20 +18,22 @@ export class ChannelTrashRepository {private readonly workspaceId?: string,) {}+ /**+ * Scope only, without the visibility filter: `userId` is the deleting user from the registry,+ * who may be a channel admin other than the creator of a private workspace channel. The+ * trash router has already authorized the registry row.+ */+ private scopeWhere = () =>+ buildWorkspaceWhere(+ { includeTrashed: true, userId: this.userId, workspaceId: this.workspaceId },+ { userId: channels.userId, workspaceId: channels.workspaceId },+ );+private lockTrashed = async (tx: Transaction, id: string) => {const [channel] = await tx.select().from(channels)- .where(- and(- eq(channels.id, id),- isTrashed(channels.isDeleted),- buildWorkspaceWhere(- { includeTrashed: true, userId: this.userId, workspaceId: this.workspaceId },- channels,- ),- ),- )+ .where(and(eq(channels.id, id), isTrashed(channels.isDeleted), this.scopeWhere())).for('update');return channel;};@@ -46,14 +48,7 @@ export class ChannelTrashRepository {.from(channelThreads).innerJoin(channels, eq(channels.id, channelThreads.channelId)).where(- and(- eq(channelThreads.id, id),- isTrashed(channelThreads.isDeleted),- buildWorkspaceWhere(- { includeTrashed: true, userId: this.userId, workspaceId: this.workspaceId },- channels,- ),- ),+ and(eq(channelThreads.id, id), isTrashed(channelThreads.isDeleted), this.scopeWhere()),);if (!found) return;const channel = await lockChannel(tx, found.channelId);@@ -67,11 +62,11 @@ export class ChannelTrashRepository {private unregister = (tx: Transaction,- id: string,+ ids: string[],resourceType: Extract<TrashResourceType, 'channel' | 'channelThread'> = 'channel',) =>new TrashModel(this.db, this.userId, this.workspaceId).removeByResources(- [{ resourceId: id, resourceType }],+ ids.map((resourceId) => ({ resourceId, resourceType })),tx,);@@ -81,7 +76,7 @@ export class ChannelTrashRepository {if (!(await this.lockTrashed(tx, id))) return false;await tx.update(channels).set(restoreStamp()).where(eq(channels.id, id));await audit(tx, id, 'restored', id);- await this.unregister(tx, id);+ await this.unregister(tx, [id]);return true;});@@ -98,9 +93,19 @@ export class ChannelTrashRepository {.where(and(eq(channelJobs.channelId, id), eq(channelJobs.writerHeld, true))).limit(1);if (writer) return 'busy';+ // Threads trashed on their own are separate roots; the cascade takes their rows with it.+ const threads = await tx+ .select({ id: channelThreads.id })+ .from(channelThreads)+ .where(and(eq(channelThreads.channelId, id), isTrashed(channelThreads.isDeleted)));// Only Channel-owned records cascade; the referenced Agents, files and topics remain.await tx.delete(channels).where(eq(channels.id, id));- await this.unregister(tx, id);+ await this.unregister(tx, [id]);+ await this.unregister(+ tx,+ threads.map((thread) => thread.id),+ 'channelThread',+ );return 'purged';});@@ -129,7 +134,7 @@ export class ChannelTrashRepository {if (live) return 'conflict';await tx.update(channelThreads).set(restoreStamp()).where(eq(channelThreads.id, id));await audit(tx, thread.channelId, 'thread_restored', id);- await this.unregister(tx, id, 'channelThread');+ await this.unregister(tx, [id], 'channelThread');return 'restored';});@@ -163,7 +168,7 @@ export class ChannelTrashRepository {.where(and(eq(channelMessages.channelId, channelId), eq(channelMessages.threadId, id)));await tx.delete(channelThreads).where(eq(channelThreads.id, id));await audit(tx, channelId, 'thread_purged', id);- await this.unregister(tx, id, 'channelThread');+ await this.unregister(tx, [id], 'channelThread');return 'purged';});}