0.6 rust重构01
This commit is contained in:
@@ -3,6 +3,7 @@ import { v } from "convex/values";
|
||||
import { getAuthUserId } from "@convex-dev/auth/server";
|
||||
import { nowIso } from "./_utils/time";
|
||||
import type { Id } from "./_generated/dataModel";
|
||||
import { requireCanonicalOwnedDocument, getCanonicalDocumentByBusinessId } from "./_utils/documentRecord";
|
||||
|
||||
const permission = v.union(v.literal("read"), v.literal("edit"));
|
||||
|
||||
@@ -20,16 +21,6 @@ function normalizeUsername(raw: string): string {
|
||||
return username;
|
||||
}
|
||||
|
||||
async function requireOwnedDocument(ctx: any, documentId: string, userId: string) {
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q: any) => q.eq("id", documentId))
|
||||
.first();
|
||||
if (!doc) throw new Error("页面不存在");
|
||||
if (doc.user_id !== userId) throw new Error("无权限");
|
||||
return doc;
|
||||
}
|
||||
|
||||
async function resolveUserIdByUsername(ctx: any, rawUsername: string): Promise<{ userId: string; username: string }> {
|
||||
const username = normalizeUsername(rawUsername);
|
||||
|
||||
@@ -47,7 +38,7 @@ export const listByDocument = query({
|
||||
args: { documentId: v.string() },
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
const doc = await requireOwnedDocument(ctx, args.documentId, userId);
|
||||
const doc = await requireCanonicalOwnedDocument(ctx, args.documentId, userId);
|
||||
|
||||
const shares = await ctx.db
|
||||
.query("document_shares")
|
||||
@@ -84,7 +75,7 @@ export const upsert = mutation({
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
const doc = await requireOwnedDocument(ctx, args.documentId, userId);
|
||||
const doc = await requireCanonicalOwnedDocument(ctx, args.documentId, userId);
|
||||
const { userId: sharedWithUserId } = await resolveUserIdByUsername(ctx, args.username);
|
||||
if (sharedWithUserId === userId) throw new Error("不能共享给自己");
|
||||
|
||||
@@ -149,7 +140,7 @@ export const remove = mutation({
|
||||
args: { documentId: v.string(), sharedWithUserId: v.string() },
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
const doc = await requireOwnedDocument(ctx, args.documentId, userId);
|
||||
const doc = await requireCanonicalOwnedDocument(ctx, args.documentId, userId);
|
||||
|
||||
const existingShare = await ctx.db
|
||||
.query("document_shares")
|
||||
@@ -294,10 +285,7 @@ export const listMyShareRoots = query({
|
||||
if (documentCache.has(documentId)) {
|
||||
return cached ?? { exists: false, deleted: true, title: null };
|
||||
}
|
||||
const doc = await ctx.db
|
||||
.query("documents")
|
||||
.withIndex("by_document_id", (q: any) => q.eq("id", documentId))
|
||||
.first();
|
||||
const doc = await getCanonicalDocumentByBusinessId<any>(ctx, documentId);
|
||||
const info = doc
|
||||
? { exists: true, deleted: doc.deleted_at != null, title: (doc.title ?? null) as string | null }
|
||||
: { exists: false, deleted: true, title: null };
|
||||
|
||||
Reference in New Issue
Block a user