Migrate Synapsis to embedded Turso
CI / smoke (push) Has been cancelled

This commit is contained in:
2026-07-14 15:50:12 -07:00
parent b4a9d82d05
commit 60f7dba46c
184 changed files with 8375 additions and 53945 deletions
@@ -54,10 +54,7 @@ export async function DELETE(
// Verify the conversation belongs to this user
const conversation = await db.query.chatConversations.findFirst({
where: and(
eq(chatConversations.id, id),
eq(chatConversations.participant1Id, session.user.id)
),
where: { AND: [{ id: id }, { participant1Id: session.user.id }] },
});
if (!conversation) {
@@ -113,16 +110,13 @@ export async function DELETE(
} else {
// Local user - find and delete their conversation too
const recipientUser = await db.query.users.findFirst({
where: eq(users.handle, participant2Handle),
where: { handle: participant2Handle },
});
if (recipientUser) {
// Find their conversation with us
const recipientConversation = await db.query.chatConversations.findFirst({
where: and(
eq(chatConversations.participant1Id, recipientUser.id),
eq(chatConversations.participant2Handle, session.user.handle)
),
where: { AND: [{ participant1Id: recipientUser.id }, { participant2Handle: session.user.handle }] },
});
if (recipientConversation) {
@@ -22,11 +22,11 @@ export async function GET(request: NextRequest) {
// Get all conversations for this user
const conversations = await db.query.chatConversations.findMany({
where: eq(chatConversations.participant1Id, session.user.id),
orderBy: [desc(chatConversations.lastMessageAt)],
where: { participant1Id: session.user.id },
orderBy: () => [desc(chatConversations.lastMessageAt)],
with: {
messages: {
orderBy: [desc(chatMessages.createdAt)],
orderBy: () => [desc(chatMessages.createdAt)],
limit: 1,
},
},
@@ -59,7 +59,7 @@ export async function GET(request: NextRequest) {
// Try to get cached user info
let cachedUser = await db.query.users.findFirst({
where: eq(users.handle, participant2Handle),
where: { handle: participant2Handle },
});
// If not found, check if it's a local user with a domain suffix
@@ -67,7 +67,7 @@ export async function GET(request: NextRequest) {
const [handlePart, domainPart] = participant2Handle.split('@');
if (!domainPart || domainPart === process.env.NEXT_PUBLIC_NODE_DOMAIN) {
cachedUser = await db.query.users.findFirst({
where: eq(users.handle, handlePart),
where: { handle: handlePart },
});
}
}
@@ -91,7 +91,7 @@ export async function GET(request: NextRequest) {
// Re-query to get the new cached user
cachedUser = await db.query.users.findFirst({
where: eq(users.handle, participant2Handle),
where: { handle: participant2Handle },
}) as any;
}
} catch (e) {
+2 -5
View File
@@ -53,7 +53,7 @@ export async function POST(request: NextRequest) {
// Find the recipient (local user)
const recipient = await db.query.users.findFirst({
where: eq(users.handle, data.recipientHandle.toLowerCase()),
where: { handle: data.recipientHandle.toLowerCase() },
});
if (!recipient) {
@@ -63,10 +63,7 @@ export async function POST(request: NextRequest) {
// Find the conversation with the sender
const senderFullHandle = `${data.senderHandle}@${senderNodeDomain}`;
const conversation = await db.query.chatConversations.findFirst({
where: and(
eq(chatConversations.participant1Id, recipient.id),
eq(chatConversations.participant2Handle, senderFullHandle)
),
where: { AND: [{ participant1Id: recipient.id }, { participant2Handle: senderFullHandle }] },
});
if (!conversation) {
+7 -14
View File
@@ -52,10 +52,7 @@ export async function GET(request: NextRequest) {
// Verify user has access to this conversation
const conversation = await db.query.chatConversations.findFirst({
where: and(
eq(chatConversations.id, conversationId),
eq(chatConversations.participant1Id, session.user.id)
),
where: { AND: [{ id: conversationId }, { participant1Id: session.user.id }] },
});
if (!conversation) {
@@ -63,15 +60,14 @@ export async function GET(request: NextRequest) {
}
// Build query with cursor-based pagination
const baseCondition = eq(chatMessages.conversationId, conversationId);
const whereCondition = cursor
? and(baseCondition, lt(chatMessages.createdAt, new Date(cursor)))!
: baseCondition;
? { conversationId, createdAt: { lt: new Date(cursor) } }
: { conversationId };
// Get messages
const messages = await db.query.chatMessages.findMany({
where: whereCondition,
orderBy: [desc(chatMessages.createdAt)],
orderBy: () => [desc(chatMessages.createdAt)],
limit,
});
@@ -92,7 +88,7 @@ export async function GET(request: NextRequest) {
if (senderDids.size > 0) {
const found = await db.query.users.findMany({
where: inArray(users.did, Array.from(senderDids))
where: { did: { in: Array.from(senderDids) } }
});
found.forEach(u => usersByDid[u.did] = u);
}
@@ -100,7 +96,7 @@ export async function GET(request: NextRequest) {
// Also fetch local users by handle if needed
if (senderHandles.size > 0) {
const found = await db.query.users.findMany({
where: inArray(users.handle, Array.from(senderHandles))
where: { handle: { in: Array.from(senderHandles) } }
});
found.forEach(u => usersByHandle[u.handle] = u);
}
@@ -164,10 +160,7 @@ export async function PATCH(request: NextRequest) {
// Verify user has access to this conversation
const conversation = await db.query.chatConversations.findFirst({
where: and(
eq(chatConversations.id, conversationId),
eq(chatConversations.participant1Id, session.user.id)
),
where: { AND: [{ id: conversationId }, { participant1Id: session.user.id }] },
});
if (!conversation) {
@@ -56,7 +56,7 @@ export async function POST(request: NextRequest) {
// Find the target user (local user being followed)
const targetUser = await db.query.users.findFirst({
where: eq(users.handle, data.targetHandle.toLowerCase()),
where: { handle: data.targetHandle.toLowerCase() },
});
if (!targetUser) {
@@ -73,10 +73,7 @@ export async function POST(request: NextRequest) {
// Check if this follow already exists
const existingFollow = await db.query.remoteFollowers.findFirst({
where: and(
eq(remoteFollowers.userId, targetUser.id),
eq(remoteFollowers.actorUrl, actorUrl)
),
where: { AND: [{ userId: targetUser.id }, { actorUrl: actorUrl }] },
});
if (existingFollow) {
+2 -6
View File
@@ -52,7 +52,7 @@ export async function POST(request: NextRequest) {
// Find the target post
const post = await db.query.posts.findFirst({
where: eq(posts.id, data.postId),
where: { id: data.postId },
with: { author: true },
});
@@ -66,11 +66,7 @@ export async function POST(request: NextRequest) {
// Check if already liked by this remote user
const existingLike = await db.query.remoteLikes.findFirst({
where: and(
eq(remoteLikes.postId, data.postId),
eq(remoteLikes.actorHandle, data.like.actorHandle),
eq(remoteLikes.actorNodeDomain, data.like.actorNodeDomain)
),
where: { AND: [{ postId: data.postId }, { actorHandle: data.like.actorHandle }, { actorNodeDomain: data.like.actorNodeDomain }] },
});
if (existingLike) {
@@ -54,7 +54,7 @@ export async function POST(request: NextRequest) {
// Find the mentioned user (local user)
const mentionedUser = await db.query.users.findFirst({
where: eq(users.handle, data.mentionedHandle.toLowerCase()),
where: { handle: data.mentionedHandle.toLowerCase() },
});
if (!mentionedUser) {
@@ -53,7 +53,7 @@ export async function POST(request: NextRequest) {
// Find the target post
const post = await db.query.posts.findFirst({
where: eq(posts.id, data.postId),
where: { id: data.postId },
with: { author: true },
});
@@ -66,11 +66,7 @@ export async function POST(request: NextRequest) {
}
const existingRepost = await db.query.remoteReposts.findFirst({
where: and(
eq(remoteReposts.postId, data.postId),
eq(remoteReposts.actorHandle, data.repost.actorHandle),
eq(remoteReposts.actorNodeDomain, data.repost.actorNodeDomain),
),
where: { AND: [{ postId: data.postId }, { actorHandle: data.repost.actorHandle }, { actorNodeDomain: data.repost.actorNodeDomain }] },
});
if (existingRepost) {
@@ -49,7 +49,7 @@ export async function POST(request: NextRequest) {
// Find the target user
const targetUser = await db.query.users.findFirst({
where: eq(users.handle, data.targetHandle.toLowerCase()),
where: { handle: data.targetHandle.toLowerCase() },
});
if (!targetUser) {
@@ -60,10 +60,7 @@ export async function POST(request: NextRequest) {
const actorUrl = `swarm://${data.unfollow.followerNodeDomain}/${data.unfollow.followerHandle}`;
const existingFollow = await db.query.remoteFollowers.findFirst({
where: and(
eq(remoteFollowers.userId, targetUser.id),
eq(remoteFollowers.actorUrl, actorUrl)
),
where: { AND: [{ userId: targetUser.id }, { actorUrl: actorUrl }] },
});
if (!existingFollow) {
@@ -78,7 +75,7 @@ export async function POST(request: NextRequest) {
// Update follower count
await db.update(users)
.set({ followersCount: sql`GREATEST(0, ${users.followersCount} - 1)` })
.set({ followersCount: sql`max(0, ${users.followersCount} - 1)` })
.where(eq(users.id, targetUser.id));
console.log(`[Swarm] Received unfollow from ${data.unfollow.followerHandle}@${data.unfollow.followerNodeDomain} for @${data.targetHandle}`);
@@ -49,7 +49,7 @@ export async function POST(request: NextRequest) {
// Find the target post
const post = await db.query.posts.findFirst({
where: eq(posts.id, data.postId),
where: { id: data.postId },
});
if (!post) {
@@ -49,7 +49,7 @@ export async function POST(request: NextRequest) {
// Find the target post
const post = await db.query.posts.findFirst({
where: eq(posts.id, data.postId),
where: { id: data.postId },
});
if (!post) {
@@ -61,11 +61,7 @@ export async function POST(request: NextRequest) {
}
const existingRepost = await db.query.remoteReposts.findFirst({
where: and(
eq(remoteReposts.postId, data.postId),
eq(remoteReposts.actorHandle, data.unrepost.actorHandle),
eq(remoteReposts.actorNodeDomain, data.unrepost.actorNodeDomain),
),
where: { AND: [{ postId: data.postId }, { actorHandle: data.unrepost.actorHandle }, { actorNodeDomain: data.unrepost.actorNodeDomain }] },
});
if (!existingRepost) {
@@ -77,7 +73,7 @@ export async function POST(request: NextRequest) {
// Decrement repost count
await db.update(posts)
.set({ repostsCount: sql`GREATEST(0, ${posts.repostsCount} - 1)` })
.set({ repostsCount: sql`max(0, ${posts.repostsCount} - 1)` })
.where(eq(posts.id, data.postId));
await db.delete(remoteReposts).where(eq(remoteReposts.id, existingRepost.id));
+4 -11
View File
@@ -59,7 +59,7 @@ export async function GET(request: NextRequest, context: RouteContext) {
// Find the post
const post = await db.query.posts.findFirst({
where: eq(posts.id, postId),
where: { id: postId },
});
if (!post || post.isRemoved) {
@@ -71,11 +71,7 @@ export async function GET(request: NextRequest, context: RouteContext) {
// If domain is provided, check remote likes
if (checkDomain) {
const remoteLike = await db.query.remoteLikes.findFirst({
where: and(
eq(remoteLikes.postId, postId),
eq(remoteLikes.actorHandle, checkHandle),
eq(remoteLikes.actorNodeDomain, checkDomain)
),
where: { AND: [{ postId: postId }, { actorHandle: checkHandle }, { actorNodeDomain: checkDomain }] },
});
return NextResponse.json({
@@ -89,15 +85,12 @@ export async function GET(request: NextRequest, context: RouteContext) {
// No domain = local user
const localUser = await db.query.users.findFirst({
where: eq(users.handle, checkHandle),
where: { handle: checkHandle },
});
if (localUser) {
const liked = await db.query.likes.findFirst({
where: and(
eq(likes.postId, postId),
eq(likes.userId, localUser.id)
),
where: { AND: [{ postId: postId }, { userId: localUser.id }] },
});
return NextResponse.json({
+5 -8
View File
@@ -36,7 +36,7 @@ export async function GET(request: NextRequest, context: RouteContext) {
// Find the post
const post = await db.query.posts.findFirst({
where: eq(posts.id, postId),
where: { id: postId },
with: {
author: true,
media: true,
@@ -45,7 +45,7 @@ export async function GET(request: NextRequest, context: RouteContext) {
if (!post || post.isRemoved) {
const remoteRepost = await db.query.userSwarmReposts.findFirst({
where: eq(userSwarmReposts.id, postId),
where: { id: postId },
});
if (!remoteRepost) {
@@ -53,7 +53,7 @@ export async function GET(request: NextRequest, context: RouteContext) {
}
const repostAuthor = await db.query.users.findFirst({
where: eq(users.id, remoteRepost.userId),
where: { id: remoteRepost.userId },
});
return NextResponse.json({
@@ -104,15 +104,12 @@ export async function GET(request: NextRequest, context: RouteContext) {
// Get replies
const replies = await db.query.posts.findMany({
where: and(
eq(posts.replyToId, postId),
eq(posts.isRemoved, false)
),
where: { AND: [{ replyToId: postId }, { isRemoved: false }] },
with: {
author: true,
media: true,
},
orderBy: [desc(posts.createdAt)],
orderBy: () => [desc(posts.createdAt)],
limit: 50,
});
+5 -8
View File
@@ -34,7 +34,7 @@ const swarmReplySchema = z.object({
async function syncParentReplyCount(postId: string) {
const [{ count }] = await db
.select({ count: sql<number>`count(*)::int` })
.select({ count: sql<number>`count(*)` })
.from(posts)
.where(and(
eq(posts.replyToId, postId),
@@ -82,10 +82,7 @@ export async function POST(request: NextRequest) {
}
const parentPost = await db.query.posts.findFirst({
where: and(
eq(posts.id, data.postId),
eq(posts.isRemoved, false)
),
where: { AND: [{ id: data.postId }, { isRemoved: false }] },
with: {
author: true,
},
@@ -107,7 +104,7 @@ export async function POST(request: NextRequest) {
});
const remoteUser = await db.query.users.findFirst({
where: eq(users.handle, remoteHandle),
where: { handle: remoteHandle },
});
if (!remoteUser) {
@@ -116,7 +113,7 @@ export async function POST(request: NextRequest) {
const replyApId = `swarm:${sourceDomain}:${data.reply.id}`;
const existingReply = await db.query.posts.findFirst({
where: eq(posts.apId, replyApId),
where: { apId: replyApId },
});
if (existingReply) {
@@ -205,7 +202,7 @@ export async function DELETE(request: NextRequest) {
// Find the reply by its swarm ID
const swarmReplyId = `swarm:${nodeDomain}:${replyId}`;
const existingReply = await db.query.posts.findFirst({
where: eq(posts.apId, swarmReplyId),
where: { apId: swarmReplyId },
});
if (!existingReply) {
+1 -1
View File
@@ -128,7 +128,7 @@ export async function GET(request: NextRequest) {
// Get node NSFW status
const node = await db.query.nodes.findFirst({
where: eq(nodes.domain, nodeDomain),
where: { domain: nodeDomain },
});
const nodeIsNsfw = node?.isNsfw ?? false;
@@ -41,7 +41,7 @@ export async function GET(request: NextRequest, context: RouteContext) {
// Find the user
const user = await db.query.users.findFirst({
where: eq(users.handle, cleanHandle),
where: { handle: cleanHandle },
});
if (!user) {
@@ -74,7 +74,7 @@ export async function GET(request: NextRequest, context: RouteContext) {
// Get remote followers
const userRemoteFollowers = await db.query.remoteFollowers.findMany({
where: eq(remoteFollowers.userId, user.id),
where: { userId: user.id },
limit,
});
@@ -40,7 +40,7 @@ export async function GET(request: NextRequest, context: RouteContext) {
// Find the user
const user = await db.query.users.findFirst({
where: eq(users.handle, cleanHandle),
where: { handle: cleanHandle },
});
if (!user) {
@@ -73,7 +73,7 @@ export async function GET(request: NextRequest, context: RouteContext) {
// Get remote following
const userRemoteFollowing = await db.query.remoteFollows.findMany({
where: eq(remoteFollows.followerId, user.id),
where: { followerId: user.id },
limit,
});
+5 -10
View File
@@ -188,7 +188,7 @@ export async function GET(request: NextRequest, context: RouteContext) {
// Find the user
const user = await db.query.users.findFirst({
where: eq(users.handle, cleanHandle),
where: { handle: cleanHandle },
with: {
botOwner: true, // Include bot owner if this is a bot
},
@@ -222,20 +222,15 @@ export async function GET(request: NextRequest, context: RouteContext) {
};
const localPosts = await db.query.posts.findMany({
where: and(
eq(posts.userId, user.id),
eq(posts.isRemoved, false),
isNull(posts.replyToId),
isNull(posts.swarmReplyToId)
),
where: { AND: [{ userId: user.id }, { isRemoved: false }, { replyToId: { isNull: true } }, { swarmReplyToId: { isNull: true } }] },
with: profilePostRelations,
orderBy: [desc(posts.createdAt)],
orderBy: () => [desc(posts.createdAt)],
limit: limit * 2,
});
const remoteRepostRows = await db.query.userSwarmReposts.findMany({
where: eq(userSwarmReposts.userId, user.id),
orderBy: [desc(userSwarmReposts.repostedAt)],
where: { userId: user.id },
orderBy: () => [desc(userSwarmReposts.repostedAt)],
limit: limit * 2,
});