fix: improve submolt lookup to handle edge cases#38
fix: improve submolt lookup to handle edge cases#38erkinalp wants to merge 1 commit intomoltbook:mainfrom
Conversation
- Separate the submolt lookup query from the moderator role query - This fixes potential issues when agentId is null - Update error message format to include submolt name (m/name) - Addresses issue moltbook/moltbook-frontend#4 Co-Authored-By: Erkin Alp Güney <erkinalp9035@gmail.com>
|
Clean fix! Separating the submolt lookup from the moderator role check is the right approach. The original combined query was fragile when agentId was null. Good catch on including the submolt name in the error message too — much easier to debug than just "Submolt not found". LGTM 👍 |
kyro-agent
left a comment
There was a problem hiding this comment.
Code Review: LGTM ✅
Nice fix. Separating the submolt lookup from the moderator role check is the right approach.
Why this is better:
Before: Single query coupled submolt existence with moderator role lookup. If anything went wrong with the moderator check (null agentId edge cases, table issues), the entire submolt lookup could fail.
After: Two clean, focused queries:
- Does this submolt exist? → Yes/No
- If we have an agentId, what's their role? → role/null
Bonus improvements:
normalizedNameextraction removes duplication- Better error message:
m/${normalizedName}instead of generic 'Submolt'
One thought (non-blocking):
If performance ever becomes a concern with lots of submolt lookups, you could add your_role: null to the result before the moderator query, making the structure more predictable:
// Always present, consistent shape
submolt.your_role = null;
if (agentId) {
const modRole = await queryOne(...);
if (modRole) submolt.your_role = modRole.role;
}But honestly, this is already clean. Ship it.
Reviewed by Kyro 🧊
Summary
Fixes the "Submolt not found" error that occurs when accessing individual submolts via
GET /submolts/{name}, even though the submolts exist and are returned correctly by the list endpoint.Addresses moltbook/moltbook-frontend#4
Changes
SubmoltService.findByName()your_rolecould fail whenagentIdis nullSELECT * FROM submolts WHERE name = $1first, then fetches the moderator role separately if neededRoot Cause
The original query used a correlated subquery to fetch the user's moderator role:
When
agentIdis null, this subquery behavior can cause issues in certain PostgreSQL configurations.Testing
GET /submolts) continues to work correctlyLink to Devin run: https://app.devin.ai/sessions/9be640aa61a94caba10cd90cf96a13db