{"version":3,"file":"index-CpD5Zktf.js","sources":["../../../../../src/services/server.ts","../../../../../src/services/implies.ts","../../../../../src/services/reviews.ts","../../../../../src/services/index.ts"],"sourcesContent":["\"use server\";\nimport { redirect } from \"@solidjs/router\";\nimport { createId } from \"@paralleldrive/cuid2\";\nimport { SDK } from \"casdoor-nodejs-sdk\";\nimport { type HTTPEvent, useSession } from \"vinxi/http\";\nimport { UserSession, sessionConf } from \"./session\";\nimport * as AccountRepo from \"~/repos/account\";\nimport { fetchUser } from \"~/repos/account\";\nexport { fetchUser };\n\nconst authCfg = {\n endpoint: process.env.VITE_CASDOOR_ENDPOINT || \"https://door.casdoor.com\",\n orgName: process.env.VITE_CASDOOR_ORGNAME || \"org name\",\n appName: process.env.VITE_CASDOOR_APPNAME,\n clientId: process.env.VITE_CASDOOR_APPID || \"app\",\n clientSecret: process.env.CASDOOR_APPSECRET || \"secrect\",\n certificate: process.env.CASDOOR_CERT || \"cert\",\n};\nconst sdk = new SDK(authCfg);\n\nexport async function getSession(event?: HTTPEvent) {\n return await useSession(sessionConf);\n}\n\nexport const isAuthed = async (event?: HTTPEvent) => {\n return Boolean(await currentUserId(event));\n};\n\nexport const currentUserId = async (event?: HTTPEvent) => {\n const session = await getSession(event);\n return session.data?.userId;\n};\n\nexport const getUser = async () => {\n const session = await getSession();\n const userId = session.data.userId;\n if (userId === undefined) return undefined;\n\n try {\n const user = await AccountRepo.find(userId);\n return user;\n } catch {\n // logout();\n return undefined;\n }\n};\n\ninterface WithTimestamp {\n createdAt: Date;\n updatedAt?: Date | null;\n}\n\ntype WithNumericTimestamps = Omit<\n T,\n \"createdAt\" | \"updatedAt\"\n> & {\n createdAt: number;\n updatedAt?: number;\n};\n\nexport function timestampRecord(\n rec: T\n): WithNumericTimestamps {\n return {\n ...rec,\n createdAt: +rec.createdAt / 1000,\n updatedAt: rec.updatedAt ? +rec.updatedAt / 1000 : undefined,\n } as WithNumericTimestamps;\n}\n\nexport const logout = async () => {\n const session = await getSession();\n await session.update((d) => (d.userId = undefined));\n throw redirect(\"/\");\n};\n\nexport const signUser = async (code: string) => {\n const token = await sdk.getAuthToken(code);\n const user = sdk.parseJwtToken(token.access_token);\n\n const account = await AccountRepo.create({\n id: createId(),\n ssoId: user.id,\n avatar: user.avatar,\n name: user.name,\n permanentAvatar: user.permanentAvatar,\n language: user.language,\n displayName: user.displayName,\n isAdmin: user.isAdmin,\n });\n\n const session = await getSession();\n // return new Response(\"hellow\");\n await session.update({ userId: account.id });\n\n return token;\n};\n","\"use server\";\n\nimport type { Imply, ImplyUpdate, Know } from \"~/types/models\";\nimport { timestampRecord } from \"./server\";\nimport { meiliServerImplies, getRedis } from \"~/lib/server\";\nimport { authorSim } from \"~/repos/account\";\nimport * as repo from \"~/repos/imply\";\nimport * as knowRepo from \"~/repos/know\";\nimport { currentUserId } from \"./server\";\nimport { DATA_STORE, HOME_LIST_ID } from \"~/lib/const\";\n\n// const reviews: (Review & { author: Account })[] = await knex(\n// \"reviews\"\n// )\n// .where(\"implyId\", imply.id)\n// .leftJoin(\n// \"accounts as author\",\n// \"reviews.authorId\",\n// \"=\",\n// \"author.id\"\n// )\n// // https://github.com/knex/knex/issues/882#issuecomment-1351747332\n// .select([\n// \"reviews.*\",\n// knex.raw(\"(json_agg(author.*) ->> 0)::json as author\"),\n// ])\n// .orderBy(\"reviews.createdAt\", \"asc\")\n// .groupBy([\"reviews.id\", \"reviews.authorId\", \"author.id\"]);\n\nexport const query = repo.query;\n\nexport const find = async (\n id: string\n): Promise<(Imply & { propositions: Know[]; know?: Know }) | undefined> => {\n const imply = await repo.find(id);\n if (!imply) return undefined;\n const know = await knowRepo.find(imply.knowId);\n const propositions = await knowRepo.getList(imply.propositionIds);\n\n return { ...imply, propositions, know };\n};\n\nexport const create = async (\n knowId: string,\n val: {\n id: string;\n propositionIds: string[];\n inference: string;\n negative: boolean;\n }\n) => {\n const authorId = await currentUserId();\n if (!authorId) throw \"user not authed\";\n const input = { knowId, authorId, ...val };\n const data = await repo.create(input);\n const doc = await getFullImply(data);\n await meiliServerImplies.addDocuments([doc]);\n};\n\nexport const update = async (id: string, val: ImplyUpdate) => {\n const data = await repo.update(id, val);\n const doc = await getFullImply(data);\n if (data.publishedAt) {\n await meiliServerImplies.updateDocuments([doc]);\n const rc = await getRedis();\n console.log(\"update redis cache\");\n await rc.zAdd(HOME_LIST_ID, {\n score: -doc.createdAt,\n value: \"imply:\" + id,\n });\n await rc.hSet(DATA_STORE, \"imply:\" + id, JSON.stringify(doc));\n } else {\n await meiliServerImplies.deleteDocument(id);\n const rc = await getRedis();\n await rc.zRem(HOME_LIST_ID, \"imply:\" + id);\n await rc.hDel(DATA_STORE, \"imply:\" + id);\n }\n};\n\nasync function getFullImply(data: Imply) {\n const know = await knowRepo.get(data.knowId);\n const author = (await authorSim(data.authorId)) || null;\n const propositions = await knowRepo.getList(data.propositionIds);\n const { publishedAt, ...val } = data;\n const imply = {\n ...timestampRecord(val),\n author,\n know,\n propositions,\n publishedAt: publishedAt ? +publishedAt / 1000 : undefined,\n };\n return imply;\n}\n","\"use server\";\n\nimport { createId } from \"@paralleldrive/cuid2\";\nimport { currentUserId, timestampRecord } from \"./server\";\nimport { authorSim } from \"~/repos/account\";\nimport * as reviewRepo from \"~/repos/review\";\nimport * as implyRepo from \"~/repos/imply\";\nimport * as knowRepo from \"~/repos/know\";\n\nexport const create = async (formData: FormData) => {\n const authorId = await currentUserId();\n if (!authorId) throw \"not logged in\";\n const author = await authorSim(authorId);\n if (!author) throw \"author not found\";\n\n const implyId = formData.get(\"implyId\")?.toString() || \"\";\n const imply = await implyRepo.find(implyId);\n if (!imply) throw \"imply not found\";\n\n const content = formData.get(\"content\")?.toString() || \"\";\n await reviewRepo.create({\n id: createId(),\n implyId,\n content,\n authorId,\n });\n // [\"id\", \"content\", \"implyId\", \"authorId\", \"createdAt\", \"updatedAt\"]\n};\n\nexport const query = async (id: string) => {\n const data = await reviewRepo.find(id);\n if (!data) throw \"not found review\";\n const imply = await implyRepo.find(data.implyId);\n let review;\n if (imply) {\n const know = await knowRepo.find(imply.knowId);\n const propositions = await knowRepo.getList(imply.propositionIds);\n review = {\n ...data,\n imply: { ...imply, know, propositions },\n };\n } else {\n review = {\n ...data,\n imply,\n };\n }\n return review;\n};\n\nexport const edit = async (formData: FormData) => {\n const content = formData.get(\"content\")?.toString();\n const id = formData.get(\"id\")?.toString();\n if (!id) throw \"invalid id\";\n if (!content) throw \"invalid content\";\n await reviewRepo.update(id, { content });\n};\n","import { action, cache } from \"@solidjs/router\";\nimport * as server from \"./server\";\nimport * as imply from \"./implies\";\nimport * as know from \"./knows\";\nimport * as review from \"./reviews\";\nimport * as article from \"./articles\";\n\nexport const getUserCache = cache(server.getUser, \"getUserCache\");\nexport const currentUserIdCache = cache(\n server.currentUserId,\n \"currentUserIdCache\"\n);\nexport const isAuthedCache = cache(server.isAuthed, \"isAuthed\");\nexport const fetchUserCache = cache(server.fetchUser, \"fetchUserCache\");\nexport const logoutAct = action(server.logout, \"logoutAct\");\nexport { signUser } from \"./server\";\nexport const signUserAct = action(server.signUser, \"signUserAct\");\n\nexport const createKnowAct = action(know.create);\nexport const updateKnowAct = action(know.update, \"updateKnowAct\");\nexport const queryKnowImpliesCache = cache(know.query, \"queryKnowImpliesCache\");\n\nexport const getArticleCache = cache(article.fetch, \"getArticleCache\");\n\nexport const queryReviewCache = cache(review.query, \"queryReviewCache\");\nexport const createReviewAct = action(review.create, \"createReviewAct\");\nexport const editReviewAct = action(review.edit, \"editReviewAct\");\n\nexport const queryImplyFullCache = cache(imply.find, \"queryImplyFullCache\");\nexport const UpdateImplyAct = action(imply.update, \"UpdateImplyAct\");\nexport const createImplyAct = action(imply.create, \"createImplyAct\");\n"],"names":["fetchUser","createServerReference","isAuthed","currentUserId","getUser","logout","signUser","find","create","update","query","edit","getUserCache","cache","server","logoutAct","action","know","queryKnowImpliesCache","getArticleCache","article","queryReviewCache","review","createReviewAct","editReviewAct","queryImplyFullCache","imply","UpdateImplyAct"],"mappings":"i4B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