|
| 1 | +import { action, query, mutation, type ActionCtx } from "./_generated/server"; |
| 2 | +import { v } from "convex/values"; |
| 3 | + |
| 4 | +const CACHE_DURATION = 5 * 60 * 1000; // 5分 |
| 5 | + |
| 6 | +type TrendsArgs = { |
| 7 | + forceRefresh?: boolean; |
| 8 | + postType?: "Article" | "Book"; |
| 9 | + articleType?: "tech" | "idea"; |
| 10 | +}; |
| 11 | + |
| 12 | +type TrendsResult = Promise<any[]>; |
| 13 | + |
| 14 | +const getTrendsHandler = async (ctx: ActionCtx, args: TrendsArgs): TrendsResult => { |
| 15 | + const cacheKey = `trends_${args.postType || "all"}_${args.articleType || "all"}`; |
| 16 | + const now = Date.now(); |
| 17 | + |
| 18 | + if (!args.forceRefresh) { |
| 19 | + const cacheInfo = await ctx.runQuery("zennData:getCacheInfo" as any, { cacheKey }); |
| 20 | + if (cacheInfo && cacheInfo.isValid && cacheInfo.expiresAt > now) { |
| 21 | + return await ctx.runQuery("zennData:getTrendPosts" as any, { |
| 22 | + postType: args.postType, |
| 23 | + articleType: args.articleType, |
| 24 | + }); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + const freshData = await ctx.runAction("zennApi:fetchAllTrends" as any, {}); |
| 29 | + |
| 30 | + await ctx.runMutation("trends:syncPostsToDatabase" as any, { posts: freshData }); |
| 31 | + |
| 32 | + await ctx.runMutation("zennData:updateCacheInfo" as any, { |
| 33 | + cacheKey, |
| 34 | + expiresAt: now + CACHE_DURATION, |
| 35 | + }); |
| 36 | + |
| 37 | + return await ctx.runQuery("zennData:getTrendPosts" as any, { |
| 38 | + postType: args.postType, |
| 39 | + articleType: args.articleType, |
| 40 | + }); |
| 41 | +}; |
| 42 | + |
| 43 | +// メイン関数:トレンドデータを取得または更新 |
| 44 | +export const getTrends = action({ |
| 45 | + args: { |
| 46 | + forceRefresh: v.optional(v.boolean()), |
| 47 | + postType: v.optional(v.union(v.literal("Article"), v.literal("Book"))), |
| 48 | + articleType: v.optional(v.union(v.literal("tech"), v.literal("idea"))), |
| 49 | + }, |
| 50 | + handler: getTrendsHandler, |
| 51 | +}); |
| 52 | + |
| 53 | +// データベースに投稿データを同期 |
| 54 | +export const syncPostsToDatabase = mutation({ |
| 55 | + args: { |
| 56 | + posts: v.array(v.any()), |
| 57 | + }, |
| 58 | + handler: async (ctx, args) => { |
| 59 | + for (const post of args.posts) { |
| 60 | + const userId = await ctx.runMutation("zennData:upsertUser" as any, { |
| 61 | + username: post.user.username, |
| 62 | + name: post.user.name, |
| 63 | + avatarSmallUrl: post.user.avatarSmallUrl, |
| 64 | + }); |
| 65 | + |
| 66 | + await ctx.runMutation("zennData:upsertPost" as any, { |
| 67 | + externalId: post.id, |
| 68 | + title: post.title, |
| 69 | + slug: post.slug, |
| 70 | + likedCount: post.likedCount, |
| 71 | + publishedAt: post.publishedAt, |
| 72 | + emoji: post.emoji, |
| 73 | + postType: post.postType, |
| 74 | + articleType: post.articleType, |
| 75 | + price: post.price, |
| 76 | + isFree: post.isFree, |
| 77 | + summary: post.summary, |
| 78 | + userId, |
| 79 | + }); |
| 80 | + } |
| 81 | + }, |
| 82 | +}); |
| 83 | + |
| 84 | +// すべてのトレンドを取得(フロントエンド用) |
| 85 | +export const getAllTrends = query({ |
| 86 | + args: {}, |
| 87 | + handler: async (ctx) => { |
| 88 | + return await ctx.runQuery("zennData:getTrendPosts" as any, {}); |
| 89 | + }, |
| 90 | +}); |
| 91 | + |
| 92 | +// 技術記事のトレンドを取得 |
| 93 | +export const getTechTrends = query({ |
| 94 | + args: {}, |
| 95 | + handler: async (ctx) => { |
| 96 | + return await ctx.runQuery("zennData:getTrendPosts" as any, { |
| 97 | + postType: "Article", |
| 98 | + articleType: "tech", |
| 99 | + }); |
| 100 | + }, |
| 101 | +}); |
| 102 | + |
| 103 | +// アイデア記事のトレンドを取得 |
| 104 | +export const getIdeaTrends = query({ |
| 105 | + args: {}, |
| 106 | + handler: async (ctx) => { |
| 107 | + return await ctx.runQuery("zennData:getTrendPosts" as any, { |
| 108 | + postType: "Article", |
| 109 | + articleType: "idea", |
| 110 | + }); |
| 111 | + }, |
| 112 | +}); |
| 113 | + |
| 114 | +// 本のトレンドを取得 |
| 115 | +export const getBookTrends = query({ |
| 116 | + args: {}, |
| 117 | + handler: async (ctx) => { |
| 118 | + return await ctx.runQuery("zennData:getTrendPosts" as any, { |
| 119 | + postType: "Book", |
| 120 | + }); |
| 121 | + }, |
| 122 | +}); |
| 123 | + |
| 124 | +// 手動でデータを更新 |
| 125 | +export const refreshTrends = action({ |
| 126 | + args: {}, |
| 127 | + handler: async (ctx) => getTrendsHandler(ctx, { forceRefresh: true }), |
| 128 | +}); |
| 129 | + |
| 130 | +// キャッシュをクリア |
| 131 | +export const clearCache = mutation({ |
| 132 | + args: {}, |
| 133 | + handler: async (ctx) => { |
| 134 | + return await ctx.runMutation("zennData:clearAllCache" as any, {}); |
| 135 | + }, |
| 136 | +}); |
0 commit comments