1- "use server" ;
2-
31import { headers } from "next/headers" ;
42import { getAuthServer } from "./auth" ;
53import { getDrizzle } from "./drizzle" ;
64import { chat , diff , message , section } from "@/schema/chat" ;
75import { and , asc , eq , exists } from "drizzle-orm" ;
86import { Auth } from "better-auth" ;
9- import { revalidateTag , unstable_cacheLife } from "next/cache" ;
7+ import { revalidateTag } from "next/cache" ;
108import { isCloudflare } from "./detectCloudflare" ;
11- import { unstable_cacheTag } from "next/cache" ;
129import { PagePath , SectionId } from "./docs" ;
1310
1411export interface CreateChatMessage {
@@ -24,7 +21,7 @@ export interface CreateChatDiff {
2421
2522// cacheに使うキーで、実際のURLではない
2623const CACHE_KEY_BASE = "https://my-code.utcode.net/chatHistory" ;
27- function cacheKeyForPage ( path : PagePath , userId : string ) {
24+ export function cacheKeyForPage ( path : PagePath , userId : string ) {
2825 return `${ CACHE_KEY_BASE } /getChat?path=${ path . lang } /${ path . page } &userId=${ userId } ` ;
2926}
3027
@@ -34,6 +31,8 @@ interface Context {
3431 userId ?: string ;
3532}
3633/**
34+ * drizzleとbetterAuthをまとめて初期化する関数
35+ *
3736 * drizzleが初期化されてなければ初期化し、
3837 * authが初期化されてなければ初期化し、
3938 * userIdがなければセッションから取得してセットする。
@@ -64,9 +63,9 @@ export async function addChat(
6463 sectionId : SectionId ,
6564 messages : CreateChatMessage [ ] ,
6665 diffRaw : CreateChatDiff [ ] ,
67- context ?: Partial < Context >
66+ context : Context
6867) {
69- const { drizzle, userId } = await initContext ( context ) ;
68+ const { drizzle, userId } = context ;
7069 if ( ! userId ) {
7170 throw new Error ( "Not authenticated" ) ;
7271 }
@@ -121,11 +120,11 @@ export async function addChat(
121120
122121export type ChatWithMessages = Awaited < ReturnType < typeof addChat > > ;
123122
124- export async function getChat (
123+ export async function getAllChat (
125124 path : PagePath ,
126- context ?: Partial < Context >
125+ context : Context
127126) : Promise < ChatWithMessages [ ] > {
128- const { drizzle, userId } = await initContext ( context ) ;
127+ const { drizzle, userId } = context ;
129128 if ( ! userId ) {
130129 return [ ] ;
131130 }
@@ -167,32 +166,29 @@ export async function getChat(
167166 // @ts -expect-error なぜかchatsの型にsectionとmessagesが含まれていないことになっているが、正しくwithを指定しているし、console.logしてみるとちゃんと含まれている
168167 return chats ;
169168}
170- export async function getChatFromCache ( path : PagePath , context : Context ) {
171- "use cache" ;
172- unstable_cacheLife ( "days" ) ;
173169
174- // cacheされる関数の中でheader()にはアクセスできない。
175- // なので外でinitContext()を呼んだものを引数に渡す必要がある。
176- // しかし、drizzleオブジェクトは外から渡せないのでgetChatの中で改めてinitContext()を呼んでdrizzleだけ再初期化している
177- // こんな意味不明な仕様になっているのはactionから呼ばれる関数とレンダリング時に呼ばれる関数を1ファイルでまとめて定義し共通化しようとしているせい。あとでなんとかする
178- const { auth, userId } = context ;
170+ export async function getChatOne ( chatId : string , context : Context ) {
171+ const { drizzle, userId } = context ;
179172 if ( ! userId ) {
180- return [ ] ;
173+ throw new Error ( "Not authenticated" ) ;
181174 }
182- unstable_cacheTag ( cacheKeyForPage ( path , userId ) ) ;
183175
184- if ( isCloudflare ( ) ) {
185- const cache = await caches . open ( "chatHistory" ) ;
186- const cachedResponse = await cache . match ( cacheKeyForPage ( path , userId ) ) ;
187- if ( cachedResponse ) {
188- console . log ( "Cache hit for chatHistory/getChat" ) ;
189- const data = ( await cachedResponse . json ( ) ) as ChatWithMessages [ ] ;
190- return data ;
191- } else {
192- console . log ( "Cache miss for chatHistory/getChat" ) ;
193- }
194- }
195- return await getChat ( path , { auth, userId } ) ;
176+ return ( await drizzle . query . chat . findFirst ( {
177+ where : and ( eq ( chat . chatId , chatId ) , eq ( chat . userId , userId ) ) ,
178+ with : {
179+ section : true ,
180+ messages : {
181+ orderBy : [ asc ( message . createdAt ) ] ,
182+ } ,
183+ diff : {
184+ orderBy : [ asc ( diff . createdAt ) ] ,
185+ } ,
186+ } ,
187+ } ) ) as typeof chat . $inferSelect & {
188+ section : typeof section . $inferSelect ;
189+ messages : ( typeof message . $inferSelect ) [ ] ;
190+ diff : ( typeof diff . $inferSelect ) [ ] ;
191+ } ;
196192}
197193
198194export async function migrateChatUser ( oldUserId : string , newUserId : string ) {
0 commit comments