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 }
);
}
}