0.6 rust重构01
This commit is contained in:
@@ -1,21 +1,14 @@
|
||||
import { internalQuery, mutation, query } from "./_generated/server";
|
||||
import { v } from "convex/values";
|
||||
import { getAuthUserId } from "@convex-dev/auth/server";
|
||||
import { requireUserId } from "./_utils/auth";
|
||||
import { nowIso } from "./_utils/time";
|
||||
import { collectSubtree } from "./_utils/documentTree";
|
||||
import { enqueueIngestDocumentJob } from "./_utils/ingestJobs";
|
||||
import { extractTextFromDocumentContent } from "./_utils/text";
|
||||
import { getCanonicalDocumentByBusinessId, getCanonicalParentDocumentId } from "./_utils/documentRecord";
|
||||
|
||||
const accessScope = v.union(v.literal("private"), v.literal("shared"), v.literal("public"));
|
||||
|
||||
async function requireUserId(ctx: any): Promise<string> {
|
||||
const userId = await getAuthUserId(ctx);
|
||||
if (userId === null) {
|
||||
throw new Error("未登录");
|
||||
}
|
||||
return userId;
|
||||
}
|
||||
|
||||
type SharePermission = "read" | "edit";
|
||||
|
||||
type SharePolicy = { permission: SharePermission; disableDownload: boolean; disableCopy: boolean };
|
||||
@@ -58,11 +51,7 @@ async function resolveSharePolicy(ctx: any, doc: any, userId: string): Promise<S
|
||||
};
|
||||
}
|
||||
|
||||
const parent = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q: any) => q.eq("id", parentId))
|
||||
.first();
|
||||
parentId = parent?.parent_id ?? null;
|
||||
parentId = await getCanonicalParentDocumentId(ctx, parentId);
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -127,11 +116,7 @@ async function resolveGroupSharePolicy(ctx: any, doc: any, userId: string): Prom
|
||||
for (let depth = 0; depth < 60 && parentId; depth += 1) {
|
||||
await checkDocId(parentId, true);
|
||||
|
||||
const parent = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q: any) => q.eq("id", parentId))
|
||||
.first();
|
||||
parentId = parent?.parent_id ?? null;
|
||||
parentId = await getCanonicalParentDocumentId(ctx, parentId);
|
||||
}
|
||||
|
||||
if (!best) return null;
|
||||
@@ -331,10 +316,7 @@ export const getMeta = query({
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) return null;
|
||||
if (doc.deleted_at != null) return null;
|
||||
try {
|
||||
@@ -394,10 +376,7 @@ export const getMeta = query({
|
||||
export const getPermissionForUser = query({
|
||||
args: { userId: v.string(), id: v.string() },
|
||||
handler: async (ctx, args) => {
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) return null;
|
||||
if (doc.deleted_at != null) return null;
|
||||
|
||||
@@ -425,10 +404,7 @@ export const getPermissionForUser = query({
|
||||
export const getMetaForIngest = internalQuery({
|
||||
args: { userId: v.string(), id: v.string() },
|
||||
handler: async (ctx, args) => {
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) return null;
|
||||
if (doc.user_id !== args.userId) return null;
|
||||
return {
|
||||
@@ -459,10 +435,7 @@ export const getContent = query({
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) return null;
|
||||
if (doc.deleted_at != null) return null;
|
||||
try {
|
||||
@@ -486,10 +459,7 @@ export const getContent = query({
|
||||
export const getContentForIngest = internalQuery({
|
||||
args: { userId: v.string(), id: v.string() },
|
||||
handler: async (ctx, args) => {
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) return null;
|
||||
if (doc.user_id !== args.userId) return null;
|
||||
return { content: doc.content ?? null };
|
||||
@@ -797,6 +767,10 @@ export const create = mutation({
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
const existing = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (existing && existing.deleted_at == null) {
|
||||
throw new Error("页面已存在");
|
||||
}
|
||||
|
||||
const siblings = await ctx.db
|
||||
.query("documents")
|
||||
@@ -867,10 +841,7 @@ export const updateContent = mutation({
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) throw new Error("页面不存在");
|
||||
if (doc.deleted_at != null) throw new Error("页面不存在");
|
||||
await requireWorkspaceMember(ctx, doc.workspace_id, userId);
|
||||
@@ -896,10 +867,7 @@ export const updateTitle = mutation({
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) throw new Error("页面不存在");
|
||||
if (doc.deleted_at != null) throw new Error("页面不存在");
|
||||
await requireWorkspaceMember(ctx, doc.workspace_id, userId);
|
||||
@@ -920,10 +888,7 @@ export const setTemplate = mutation({
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) throw new Error("页面不存在");
|
||||
if (doc.deleted_at != null) throw new Error("页面不存在");
|
||||
await requireWorkspaceMember(ctx, doc.workspace_id, userId);
|
||||
@@ -950,10 +915,7 @@ export const move = mutation({
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) throw new Error("页面不存在");
|
||||
if (doc.user_id !== userId) throw new Error("无权限");
|
||||
|
||||
@@ -1040,10 +1002,7 @@ export const softDelete = mutation({
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) throw new Error("页面不存在");
|
||||
if (doc.user_id !== userId) throw new Error("无权限");
|
||||
const ts = nowIso();
|
||||
@@ -1075,10 +1034,7 @@ export const restore = mutation({
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) throw new Error("页面不存在");
|
||||
if (doc.user_id !== userId) throw new Error("无权限");
|
||||
const ts = nowIso();
|
||||
@@ -1115,10 +1071,7 @@ export const purge = mutation({
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) throw new Error("页面不存在");
|
||||
if (doc.user_id !== userId) throw new Error("无权限");
|
||||
|
||||
@@ -1194,10 +1147,7 @@ export const updateOptions = mutation({
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) throw new Error("页面不存在");
|
||||
if (doc.deleted_at != null) throw new Error("页面不存在");
|
||||
await requireWorkspaceMember(ctx, doc.workspace_id, userId);
|
||||
@@ -1249,10 +1199,7 @@ export const updateStats = mutation({
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.id))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId(ctx, args.id);
|
||||
if (!doc) throw new Error("页面不存在");
|
||||
if (doc.deleted_at != null) throw new Error("页面不存在");
|
||||
await requireWorkspaceMember(ctx, doc.workspace_id, userId);
|
||||
@@ -1284,13 +1231,15 @@ export const duplicate = mutation({
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
|
||||
const source = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q) => q.eq("id", args.sourceId))
|
||||
.first();
|
||||
const source = await getCanonicalDocumentByBusinessId(ctx, args.sourceId);
|
||||
if (!source) throw new Error("页面不存在或无权限访问");
|
||||
if (source.user_id !== userId) throw new Error("页面不存在或无权限访问");
|
||||
|
||||
const existingTarget = await getCanonicalDocumentByBusinessId(ctx, args.newId);
|
||||
if (existingTarget && existingTarget.deleted_at == null) {
|
||||
throw new Error("目标页面已存在");
|
||||
}
|
||||
|
||||
const siblings = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_workspace_parent", (q) =>
|
||||
|
||||
Reference in New Issue
Block a user