fix: update Mastodon data fetching to include hasAnyPosts flag
Build Images and Deploy / Update-PROD-Stack (push) Successful in 1m27s
Build Images and Deploy / Update-PROD-Stack (push) Successful in 1m27s
This commit is contained in:
@@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { getPostsPerHour } from '@/lib/mastodon'
|
||||
import { getPostsData } from '@/lib/mastodon'
|
||||
import { calcPrice } from '@/lib/pricing'
|
||||
import { normalizeTag } from '@/lib/utils'
|
||||
import { priceUpdateQueue } from '@/lib/queue'
|
||||
@@ -49,8 +49,11 @@ export async function POST(req: NextRequest) {
|
||||
|
||||
// Query Mastodon
|
||||
let postsPerHour = 0
|
||||
let hasAnyPosts = false
|
||||
try {
|
||||
postsPerHour = await getPostsPerHour(tag)
|
||||
const data = await getPostsData(tag)
|
||||
postsPerHour = data.postsPerHour
|
||||
hasAnyPosts = data.hasAnyPosts
|
||||
} catch (err) {
|
||||
console.error('[research] Mastodon fetch failed:', err)
|
||||
return NextResponse.json(
|
||||
@@ -59,18 +62,19 @@ export async function POST(req: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
if (postsPerHour === 0) {
|
||||
if (!hasAnyPosts) {
|
||||
// Deduct point for failed research
|
||||
await prisma.user.update({
|
||||
where: { id: session.user.id },
|
||||
data: { researchPoints: { decrement: 1 } },
|
||||
})
|
||||
return NextResponse.json(
|
||||
{ error: 'No recent posts found for this hashtag. Research point spent.' },
|
||||
{ error: 'No posts found for this hashtag anywhere recently. Research point spent.' },
|
||||
{ status: 404 },
|
||||
)
|
||||
}
|
||||
|
||||
// Use the last-hour price, or $0.25 minimum if active but currently quiet
|
||||
const price = calcPrice(postsPerHour)
|
||||
const activeUntil = new Date(Date.now() + parseInt(process.env.HASHTAG_ACTIVE_HOURS ?? '24', 10) * 60 * 60 * 1000)
|
||||
|
||||
|
||||
+3
-3
@@ -80,7 +80,7 @@ export async function getPostsPerHour(tag: string): Promise<number> {
|
||||
*/
|
||||
export async function getPostsData(
|
||||
tag: string,
|
||||
): Promise<{ postsPerHour: number; relatedTags: string[]; displayTag?: string }> {
|
||||
): Promise<{ postsPerHour: number; relatedTags: string[]; displayTag?: string; hasAnyPosts: boolean }> {
|
||||
const maxPages = parseInt(process.env.MAX_PAGES_PER_HASHTAG ?? '5', 10)
|
||||
const postLimit = Math.min(parseInt(process.env.MASTODON_POST_LIMIT ?? '20', 10), 40)
|
||||
const ONE_HOUR_MS = 60 * 60 * 1000
|
||||
@@ -106,7 +106,7 @@ export async function getPostsData(
|
||||
maxId = nextMaxId
|
||||
}
|
||||
|
||||
if (allPosts.length === 0) return { postsPerHour: 0, relatedTags: [] }
|
||||
if (allPosts.length === 0) return { postsPerHour: 0, relatedTags: [], hasAnyPosts: false }
|
||||
|
||||
const times = allPosts.map((p) => new Date(p.created_at).getTime())
|
||||
const newestMs = Math.max(...times)
|
||||
@@ -165,6 +165,6 @@ export async function getPostsData(
|
||||
if (topCount / total >= 0.5) displayTag = topVariant
|
||||
}
|
||||
|
||||
return { postsPerHour, relatedTags, displayTag }
|
||||
return { postsPerHour, relatedTags, displayTag, hasAnyPosts: true }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user