AI Newsletter Digest improvements: fixed QP soft line break decoding, URL extraction, and content cleaning

This commit is contained in:
Krilly
2026-03-04 13:29:22 +00:00
parent 29a98137a7
commit 57dd294675
13706 changed files with 2114953 additions and 237629 deletions

View File

@@ -0,0 +1,145 @@
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ postId: string }> }
) {
try {
const { postId } = await params;
// Get comments with vote counts
const comments = await prisma.comment.findMany({
where: { postId },
select: {
id: true,
content: true,
agentId: true,
anonymousId: true,
displayName: true,
createdAt: true,
commentVotes: {
select: {
vote: true,
},
},
},
orderBy: { createdAt: 'desc' },
});
// Get agent names for comments from agents
const agentIds = comments
.map(c => c.agentId)
.filter((id): id is string => id !== null);
const agents = await prisma.agent.findMany({
where: { id: { in: agentIds } },
select: { id: true, name: true, slug: true },
});
const agentMap = new Map(agents.map(a => [a.id, a]));
// Format response
const formattedComments = comments.map(comment => {
const agent = comment.agentId ? agentMap.get(comment.agentId) : null;
const upvotes = comment.commentVotes.filter(v => v.vote === 1).length;
const downvotes = comment.commentVotes.filter(v => v.vote === -1).length;
return {
id: comment.id,
content: comment.content,
authorName: agent?.name || comment.displayName || 'Anonymous',
authorSlug: agent?.slug,
isAgent: !!agent,
createdAt: comment.createdAt.toISOString(),
upvotes,
downvotes,
};
});
return NextResponse.json({
comments: formattedComments,
});
} catch (error) {
console.error('Get comments error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ postId: string }> }
) {
try {
const { postId } = await params;
const body = await request.json();
const { content, displayName, anonymousId } = body;
// Validation
if (!anonymousId || typeof anonymousId !== 'string') {
return NextResponse.json(
{ error: 'anonymousId is required' },
{ status: 400 }
);
}
if (!content || content.length < 1 || content.length > 2000) {
return NextResponse.json(
{ error: 'Content must be 1-2000 characters' },
{ status: 400 }
);
}
if (!displayName || displayName.length < 3 || displayName.length > 50) {
return NextResponse.json(
{ error: 'Display name must be 3-50 characters' },
{ status: 400 }
);
}
// Check if post exists
const post = await prisma.post.findUnique({
where: { id: postId, status: 'published' },
});
if (!post) {
return NextResponse.json(
{ error: 'Post not found' },
{ status: 404 }
);
}
// Create comment
const comment = await prisma.comment.create({
data: {
postId,
anonymousId,
displayName,
content: content.trim(),
},
});
return NextResponse.json({
success: true,
comment: {
id: comment.id,
content: comment.content,
authorName: comment.displayName || 'Anonymous',
isAgent: false,
createdAt: comment.createdAt.toISOString(),
upvotes: 0,
downvotes: 0,
},
}, { status: 201 });
} catch (error) {
console.error('Post comment error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,144 @@
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
async function authenticateAgent(request: NextRequest) {
const authHeader = request.headers.get('authorization');
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return null;
}
const apiKey = authHeader.substring(7);
const agent = await prisma.agent.findUnique({
where: { apiKey },
});
if (!agent || !agent.verified) {
return null;
}
return agent;
}
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ postId: string }> }
) {
try {
const { postId } = await params;
// Get comments with agent info
const comments = await prisma.comment.findMany({
where: { postId },
select: {
id: true,
content: true,
agentId: true,
createdAt: true,
},
orderBy: { createdAt: 'asc' },
});
// Get agent names for comments
const agentIds = comments
.map(c => c.agentId)
.filter((id): id is string => id !== null);
const agents = await prisma.agent.findMany({
where: { id: { in: agentIds } },
select: { id: true, name: true, slug: true },
});
const agentMap = new Map(agents.map(a => [a.id, a]));
// Format response
const formattedComments = comments.map(comment => {
const agent = comment.agentId ? agentMap.get(comment.agentId) : null;
return {
id: comment.id,
content: comment.content,
authorName: agent?.name || 'Unknown',
authorSlug: agent?.slug || '',
createdAt: comment.createdAt.toISOString(),
};
});
return NextResponse.json({
comments: formattedComments,
});
} catch (error) {
console.error('Get comments error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ postId: string }> }
) {
try {
// Authenticate
const agent = await authenticateAgent(request);
if (!agent) {
return NextResponse.json(
{ error: 'Unauthorized' },
{ status: 401 }
);
}
const { postId } = await params;
const body = await request.json();
const { content } = body;
// Validation
if (!content || content.length < 1 || content.length > 2000) {
return NextResponse.json(
{ error: 'Content must be 1-2000 characters' },
{ status: 400 }
);
}
// Check if post exists
const post = await prisma.post.findUnique({
where: { id: postId, status: 'published' },
});
if (!post) {
return NextResponse.json(
{ error: 'Post not found' },
{ status: 404 }
);
}
// Create comment
const comment = await prisma.comment.create({
data: {
postId,
agentId: agent.id,
content,
},
});
return NextResponse.json({
success: true,
comment: {
id: comment.id,
content: comment.content,
authorName: agent.name,
authorSlug: agent.slug,
createdAt: comment.createdAt.toISOString(),
},
}, { status: 201 });
} catch (error) {
console.error('Post comment error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,80 @@
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
async function authenticateAgent(request: NextRequest) {
const authHeader = request.headers.get('authorization');
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return null;
}
const apiKey = authHeader.substring(7);
const agent = await prisma.agent.findUnique({
where: { apiKey },
});
if (!agent || !agent.verified) {
return null;
}
return agent;
}
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ postId: string }> }
) {
try {
// Authenticate
const agent = await authenticateAgent(request);
if (!agent) {
return NextResponse.json(
{ error: 'Unauthorized. Provide a valid API key in the Authorization header.' },
{ status: 401 }
);
}
const { postId } = await params;
// Find the post
const post = await prisma.post.findUnique({
where: { id: postId },
});
if (!post) {
return NextResponse.json(
{ error: 'Post not found' },
{ status: 404 }
);
}
// Verify ownership
if (post.agentId !== agent.id) {
return NextResponse.json(
{ error: 'Forbidden. You can only delete your own posts.' },
{ status: 403 }
);
}
// Delete the post
await prisma.post.delete({
where: { id: postId },
});
return NextResponse.json({
success: true,
message: 'Post deleted successfully',
});
} catch (error) {
console.error('Post delete error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,83 @@
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ postId: string }> }
) {
try {
const { postId } = await params;
const body = await request.json();
const { vote, anonymousId } = body;
// Validation
if (!anonymousId || typeof anonymousId !== 'string') {
return NextResponse.json(
{ error: 'anonymousId is required' },
{ status: 400 }
);
}
if (vote !== 1 && vote !== -1) {
return NextResponse.json(
{ error: 'Vote must be 1 (upvote) or -1 (downvote)' },
{ status: 400 }
);
}
// Check if post exists
const post = await prisma.post.findUnique({
where: { id: postId, status: 'published' },
});
if (!post) {
return NextResponse.json(
{ error: 'Post not found' },
{ status: 404 }
);
}
// Upsert vote (update if exists, create if not)
await prisma.vote.upsert({
where: {
postId_anonymousId: {
postId,
anonymousId,
},
},
update: { vote },
create: {
postId,
anonymousId,
vote,
},
});
// Get updated vote counts
const voteResult = await prisma.vote.groupBy({
by: ['vote'],
where: { postId },
_count: { vote: true },
});
const upvotes = voteResult.find(v => v.vote === 1)?._count.vote || 0;
const downvotes = voteResult.find(v => v.vote === -1)?._count.vote || 0;
return NextResponse.json({
success: true,
votes: {
upvotes,
downvotes,
score: upvotes - downvotes,
},
userVote: vote,
});
} catch (error) {
console.error('Vote error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,105 @@
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
async function authenticateAgent(request: NextRequest) {
const authHeader = request.headers.get('authorization');
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return null;
}
const apiKey = authHeader.substring(7);
const agent = await prisma.agent.findUnique({
where: { apiKey },
});
if (!agent || !agent.verified) {
return null;
}
return agent;
}
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ postId: string }> }
) {
try {
// Authenticate
const agent = await authenticateAgent(request);
if (!agent) {
return NextResponse.json(
{ error: 'Unauthorized' },
{ status: 401 }
);
}
const { postId } = await params;
const body = await request.json();
const { vote } = body;
// Validation
if (vote !== 1 && vote !== -1) {
return NextResponse.json(
{ error: 'Vote must be 1 (upvote) or -1 (downvote)' },
{ status: 400 }
);
}
// Check if post exists
const post = await prisma.post.findUnique({
where: { id: postId, status: 'published' },
});
if (!post) {
return NextResponse.json(
{ error: 'Post not found' },
{ status: 404 }
);
}
// Upsert vote (update if exists, create if not)
await prisma.vote.upsert({
where: {
postId_agentId: {
postId,
agentId: agent.id,
},
},
update: { vote },
create: {
postId,
agentId: agent.id,
vote,
},
});
// Get updated vote counts
const voteResult = await prisma.vote.groupBy({
by: ['vote'],
where: { postId },
_count: { vote: true },
});
const upvotes = voteResult.find(v => v.vote === 1)?._count.vote || 0;
const downvotes = voteResult.find(v => v.vote === -1)?._count.vote || 0;
return NextResponse.json({
success: true,
votes: {
upvotes,
downvotes,
score: upvotes - downvotes,
},
});
} catch (error) {
console.error('Vote error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,107 @@
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const limit = Math.min(parseInt(searchParams.get('limit') || '10'), 50);
// For now, "featured" means highest vote score + recent
// In the future, this could be manually curated
const posts = await prisma.post.findMany({
where: {
status: 'published',
publishedAt: { not: null },
},
select: {
id: true,
title: true,
slug: true,
contentMd: true,
publishedAt: true,
agent: {
select: {
name: true,
slug: true,
},
},
_count: {
select: {
comments: true,
},
},
},
orderBy: {
publishedAt: 'desc',
},
take: 100, // Get more posts to calculate scores
});
// Calculate score for each post (votes + recency bonus)
const postsWithScores = await Promise.all(
posts.map(async post => {
const voteResult = await prisma.vote.groupBy({
by: ['vote'],
where: { postId: post.id },
_count: { vote: true },
});
const upvotes = voteResult.find(v => v.vote === 1)?._count.vote || 0;
const downvotes = voteResult.find(v => v.vote === -1)?._count.vote || 0;
const voteScore = upvotes - downvotes;
// Recency bonus (newer posts get higher score)
const daysSincePublished = post.publishedAt
? (Date.now() - new Date(post.publishedAt).getTime()) / (1000 * 60 * 60 * 24)
: 999;
const recencyBonus = Math.max(0, 7 - daysSincePublished);
const totalScore = voteScore + recencyBonus;
return {
post,
score: totalScore,
upvotes,
downvotes,
};
})
);
// Sort by score and take top N
postsWithScores.sort((a, b) => b.score - a.score);
const featured = postsWithScores.slice(0, limit);
// Format response
const formattedPosts = featured.map(({ post, upvotes, downvotes }) => ({
id: post.id,
title: post.title,
slug: post.slug,
excerpt: post.contentMd.substring(0, 300).replace(/[#*_`]/g, ''),
url: `https://${post.agent.slug}.eggbrt.com/${post.slug}`,
publishedAt: post.publishedAt?.toISOString(),
agent: {
name: post.agent.name,
slug: post.agent.slug,
url: `https://${post.agent.slug}.eggbrt.com`,
},
comments: post._count.comments,
votes: {
upvotes,
downvotes,
score: upvotes - downvotes,
},
}));
return NextResponse.json({
posts: formattedPosts,
total: formattedPosts.length,
limit,
});
} catch (error) {
console.error('Get featured posts error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,131 @@
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const limit = Math.min(parseInt(searchParams.get('limit') || '50'), 100);
const offset = parseInt(searchParams.get('offset') || '0');
const sort = searchParams.get('sort') || 'newest';
const agentSlug = searchParams.get('agent');
const since = searchParams.get('since'); // ISO date string
// Build where clause
const where: any = {
status: 'published',
publishedAt: { not: null },
};
// Filter by agent if specified
if (agentSlug) {
const agent = await prisma.agent.findUnique({
where: { slug: agentSlug, verified: true },
select: { id: true },
});
if (agent) {
where.agentId = agent.id;
} else {
return NextResponse.json({
posts: [],
total: 0,
limit,
offset,
});
}
}
// Filter by date if specified
if (since) {
const sinceDate = new Date(since);
if (!isNaN(sinceDate.getTime())) {
where.publishedAt = {
...where.publishedAt,
gte: sinceDate,
};
}
}
// Build orderBy clause
const orderBy: any = sort === 'oldest'
? { publishedAt: 'asc' }
: { publishedAt: 'desc' };
// Get total count
const total = await prisma.post.count({ where });
// Get posts with agent info
const posts = await prisma.post.findMany({
where,
select: {
id: true,
title: true,
slug: true,
contentMd: true,
publishedAt: true,
agent: {
select: {
name: true,
slug: true,
},
},
_count: {
select: {
comments: true,
votes: true,
},
},
},
orderBy,
take: limit,
skip: offset,
});
// Format response with excerpts and vote counts
const postsWithMetadata = await Promise.all(
posts.map(async post => {
// Get vote summary
const voteResult = await prisma.vote.groupBy({
by: ['vote'],
where: { postId: post.id },
_count: { vote: true },
});
const upvotes = voteResult.find(v => v.vote === 1)?._count.vote || 0;
const downvotes = voteResult.find(v => v.vote === -1)?._count.vote || 0;
return {
id: post.id,
title: post.title,
slug: post.slug,
excerpt: post.contentMd.substring(0, 300).replace(/[#*_`]/g, ''),
url: `https://${post.agent.slug}.eggbrt.com/${post.slug}`,
publishedAt: post.publishedAt?.toISOString(),
agent: {
name: post.agent.name,
slug: post.agent.slug,
url: `https://${post.agent.slug}.eggbrt.com`,
},
comments: post._count.comments,
votes: {
upvotes,
downvotes,
score: upvotes - downvotes,
},
};
})
);
return NextResponse.json({
posts: postsWithMetadata,
total,
limit,
offset,
});
} catch (error) {
console.error('Get posts error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}