fix(dm): restore ownerId=NULL semantics for 1-on-1 DM channels

- Add migrateFixOneOnOneOwnerIds migration to NULL-out ownerId on all
  existing 1-on-1 DMs (those with exactly 2 members)
- Fix POST /api/dm to create 1-on-1 channels with ownerId=null instead
  of the creator's ID
- Guard POST /api/dm/:id/members: reject with 400 if channel has no
  owner (i.e. is a 1-on-1), directing callers to POST /api/dm/group
- Guard DELETE /api/dm/:id/members: replace member-count check with
  ownerId check; remove now-duplicate dmChannel query in that handler
- Add CreateGroupDmRequest type to shared types
This commit is contained in:
Jannis Braun
2026-03-26 22:16:55 +01:00
parent 0bcba2aaa9
commit bec4c2446f
3 changed files with 42 additions and 15 deletions
+23
View File
@@ -607,6 +607,8 @@ export function runMigrations(db: Database.Database): void {
console.error('Federation mutation log backfill failed (non-fatal):', err);
}
migrateFixOneOnOneOwnerIds(db);
console.log('Migrations complete.');
}
@@ -1568,3 +1570,24 @@ function migrateDmChannelsFederatedId(db: Database.Database): void {
console.error('migrateDmChannelsFederatedId: owner backfill failed (non-fatal):', err);
}
}
/** Fix ownerId on 1-on-1 DMs: should be NULL, not the creator's ID */
function migrateFixOneOnOneOwnerIds(db: Database.Database): void {
try {
const result = db.prepare(`
UPDATE dm_channels SET owner_id = NULL
WHERE id IN (
SELECT dm_channel_id FROM dm_members
GROUP BY dm_channel_id
HAVING COUNT(*) = 2
)
AND owner_id IS NOT NULL
`).run();
if (result.changes > 0) {
console.log(`[migrate] Fixed ownerId on ${result.changes} 1-on-1 DM channel(s) (set to NULL)`);
}
} catch (err) {
console.error('migrateFixOneOnOneOwnerIds failed (non-fatal):', err);
}
}