qarpeo 0.1.32 → 0.1.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -7
- package/dist/chunks/auth-schema.mjs +12 -10
- package/dist/chunks/wrangler.mjs +1 -1
- package/dist/module.d.ts +1 -0
- package/dist/module.mjs +284 -100
- package/dist/runtime/app/components/Comments.vue +411 -0
- package/dist/runtime/app/composables/cms.d.ts +11 -7
- package/dist/runtime/app/composables/cms.mjs +36 -68
- package/dist/runtime/app/composables/comments.d.ts +45 -0
- package/dist/runtime/app/composables/comments.mjs +150 -0
- package/dist/runtime/app/composables/organization.d.ts +3 -3
- package/dist/runtime/server/db/cms-schema.d.ts +1 -1
- package/dist/runtime/server/db/cms-schema.mjs +1 -2
- package/dist/runtime/server/routes/cms/[collection]/index.get.mjs +2 -0
- package/dist/runtime/server/routes/cms/publish-scheduled.post.mjs +7 -4
- package/dist/runtime/server/routes/cms/subscribers/[id].patch.mjs +9 -2
- package/dist/runtime/server/routes/cms/webhooks/[provider].post.mjs +41 -28
- package/dist/runtime/server/routes/comments/[id]/react.post.d.ts +6 -0
- package/dist/runtime/server/routes/comments/[id]/react.post.mjs +35 -0
- package/dist/runtime/server/routes/{cms/comments/[id].patch.d.ts → comments/[id]/report.post.d.ts} +1 -2
- package/dist/runtime/server/routes/comments/[id]/report.post.mjs +50 -0
- package/dist/runtime/server/routes/{cms/comments → comments}/[id].delete.d.ts +0 -1
- package/dist/runtime/server/routes/comments/[id].delete.mjs +27 -0
- package/dist/runtime/server/routes/{cms/comments → comments}/[id].get.d.ts +1 -1
- package/dist/runtime/server/routes/comments/[id].get.mjs +14 -0
- package/dist/runtime/server/routes/comments/[id].patch.d.ts +4 -0
- package/dist/runtime/server/routes/comments/[id].patch.mjs +37 -0
- package/dist/runtime/server/routes/{cms/comments.get.d.ts → comments/index.get.d.ts} +1 -1
- package/dist/runtime/server/routes/comments/index.get.mjs +34 -0
- package/dist/runtime/server/routes/comments/index.post.d.ts +4 -0
- package/dist/runtime/server/routes/comments/index.post.mjs +36 -0
- package/dist/runtime/server/utils/audit.d.ts +3 -3
- package/dist/runtime/server/utils/audit.mjs +43 -17
- package/dist/runtime/server/utils/auth.d.ts +55 -0
- package/dist/runtime/server/utils/auth.mjs +92 -10
- package/dist/runtime/server/utils/cms/git.mjs +2 -6
- package/dist/runtime/server/utils/cms.d.ts +17 -22
- package/dist/runtime/server/utils/cms.mjs +144 -309
- package/dist/runtime/server/utils/comments.d.ts +112 -0
- package/dist/runtime/server/utils/comments.mjs +450 -0
- package/dist/runtime/server/utils/config.d.ts +2 -1
- package/dist/runtime/server/utils/config.mjs +1 -0
- package/dist/runtime/server/utils/urn.d.ts +15 -0
- package/dist/runtime/server/utils/urn.mjs +20 -0
- package/dist/runtime/server.d.ts +4 -0
- package/dist/runtime/server.mjs +5 -0
- package/dist/runtime/types/auth.d.ts +12 -1
- package/dist/types.d.ts +67 -27
- package/dist/utils/comments-schema.d.ts +13 -0
- package/package.json +4 -4
- package/dist/runtime/app/components/CMSComments.vue +0 -202
- package/dist/runtime/server/routes/cms/comments/[id].delete.mjs +0 -12
- package/dist/runtime/server/routes/cms/comments/[id].get.mjs +0 -13
- package/dist/runtime/server/routes/cms/comments/[id].patch.mjs +0 -17
- package/dist/runtime/server/routes/cms/comments.get.mjs +0 -43
- package/dist/runtime/server/routes/cms/comments.post.d.ts +0 -17
- package/dist/runtime/server/routes/cms/comments.post.mjs +0 -46
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { computed, ref, toValue, watch } from "vue";
|
|
2
|
+
import { useRuntimeConfig } from "#imports";
|
|
3
|
+
function getCommentsPrefix() {
|
|
4
|
+
try {
|
|
5
|
+
const cfg = useRuntimeConfig();
|
|
6
|
+
return cfg.public?.qarpeo?.comments?.prefix || "/api/comments";
|
|
7
|
+
} catch {
|
|
8
|
+
return "/api/comments";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function useComments(targetUrn, options = {}) {
|
|
12
|
+
const { threaded = true, autoFetch = true, status } = options;
|
|
13
|
+
const comments = ref([]);
|
|
14
|
+
const loading = ref(false);
|
|
15
|
+
const error = ref(null);
|
|
16
|
+
const total = ref(0);
|
|
17
|
+
const urn = computed(() => toValue(targetUrn) || "");
|
|
18
|
+
async function fetchComments() {
|
|
19
|
+
const currentUrn = urn.value;
|
|
20
|
+
if (!currentUrn) {
|
|
21
|
+
comments.value = [];
|
|
22
|
+
total.value = 0;
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
loading.value = true;
|
|
26
|
+
error.value = null;
|
|
27
|
+
try {
|
|
28
|
+
const query = {
|
|
29
|
+
targetUrn: currentUrn,
|
|
30
|
+
threaded: threaded ? "true" : "false"
|
|
31
|
+
};
|
|
32
|
+
if (status) {
|
|
33
|
+
query.status = status;
|
|
34
|
+
}
|
|
35
|
+
const res = await $fetch(getCommentsPrefix(), {
|
|
36
|
+
query
|
|
37
|
+
});
|
|
38
|
+
comments.value = res.comments || [];
|
|
39
|
+
total.value = res.total || 0;
|
|
40
|
+
} catch (err) {
|
|
41
|
+
error.value = err;
|
|
42
|
+
} finally {
|
|
43
|
+
loading.value = false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async function postComment(input) {
|
|
47
|
+
const currentUrn = urn.value;
|
|
48
|
+
if (!currentUrn) return null;
|
|
49
|
+
loading.value = true;
|
|
50
|
+
error.value = null;
|
|
51
|
+
try {
|
|
52
|
+
const res = await $fetch(getCommentsPrefix(), {
|
|
53
|
+
method: "POST",
|
|
54
|
+
body: {
|
|
55
|
+
targetUrn: currentUrn,
|
|
56
|
+
parentId: input.parentId,
|
|
57
|
+
authorName: input.authorName,
|
|
58
|
+
authorEmail: input.authorEmail,
|
|
59
|
+
content: input.content,
|
|
60
|
+
turnstileToken: input.turnstileToken,
|
|
61
|
+
metadata: input.metadata
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
await fetchComments();
|
|
65
|
+
return res.comment;
|
|
66
|
+
} catch (err) {
|
|
67
|
+
error.value = err;
|
|
68
|
+
throw err;
|
|
69
|
+
} finally {
|
|
70
|
+
loading.value = false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function deleteComment(id) {
|
|
74
|
+
try {
|
|
75
|
+
await $fetch(`${getCommentsPrefix()}/${encodeURIComponent(id)}`, {
|
|
76
|
+
method: "DELETE"
|
|
77
|
+
});
|
|
78
|
+
await fetchComments();
|
|
79
|
+
return true;
|
|
80
|
+
} catch (err) {
|
|
81
|
+
error.value = err;
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async function reportComment(id, reason, note) {
|
|
86
|
+
try {
|
|
87
|
+
await $fetch(`${getCommentsPrefix()}/${encodeURIComponent(id)}/report`, {
|
|
88
|
+
method: "POST",
|
|
89
|
+
body: { reason, note }
|
|
90
|
+
});
|
|
91
|
+
return true;
|
|
92
|
+
} catch (err) {
|
|
93
|
+
error.value = err;
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async function react(id, reaction) {
|
|
98
|
+
try {
|
|
99
|
+
const res = await $fetch(
|
|
100
|
+
`${getCommentsPrefix()}/${encodeURIComponent(id)}/react`,
|
|
101
|
+
{
|
|
102
|
+
method: "POST",
|
|
103
|
+
body: { reaction }
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
const updateReactionInTree = (items) => {
|
|
107
|
+
for (const item of items) {
|
|
108
|
+
if (item.id === id) {
|
|
109
|
+
item.reactionCounts = res.reactionCounts;
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
if (item.replies?.length) {
|
|
113
|
+
if (updateReactionInTree(item.replies)) return true;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return false;
|
|
117
|
+
};
|
|
118
|
+
updateReactionInTree(comments.value);
|
|
119
|
+
return true;
|
|
120
|
+
} catch (err) {
|
|
121
|
+
error.value = err;
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (autoFetch) {
|
|
126
|
+
watch(
|
|
127
|
+
urn,
|
|
128
|
+
(newUrn) => {
|
|
129
|
+
if (newUrn) {
|
|
130
|
+
fetchComments();
|
|
131
|
+
} else {
|
|
132
|
+
comments.value = [];
|
|
133
|
+
total.value = 0;
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
{ immediate: true }
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
comments,
|
|
141
|
+
loading,
|
|
142
|
+
error,
|
|
143
|
+
total,
|
|
144
|
+
fetchComments,
|
|
145
|
+
postComment,
|
|
146
|
+
deleteComment,
|
|
147
|
+
reportComment,
|
|
148
|
+
react
|
|
149
|
+
};
|
|
150
|
+
}
|
|
@@ -36,7 +36,7 @@ export declare function useOrganization(): {
|
|
|
36
36
|
name?: string | null | undefined;
|
|
37
37
|
avatarUrl?: string | null | undefined;
|
|
38
38
|
emailVerified?: boolean | undefined;
|
|
39
|
-
role?: string | undefined;
|
|
39
|
+
role?: (import("../..").QarpeoRole | string) | undefined;
|
|
40
40
|
} | undefined;
|
|
41
41
|
email?: string | undefined;
|
|
42
42
|
name?: string | undefined;
|
|
@@ -58,7 +58,7 @@ export declare function useOrganization(): {
|
|
|
58
58
|
name?: string | null | undefined;
|
|
59
59
|
avatarUrl?: string | null | undefined;
|
|
60
60
|
emailVerified?: boolean | undefined;
|
|
61
|
-
role?: string | undefined;
|
|
61
|
+
role?: (import("../..").QarpeoRole | string) | undefined;
|
|
62
62
|
} | undefined;
|
|
63
63
|
email?: string | undefined;
|
|
64
64
|
name?: string | undefined;
|
|
@@ -113,7 +113,7 @@ export declare function useOrganization(): {
|
|
|
113
113
|
name?: string | null | undefined;
|
|
114
114
|
avatarUrl?: string | null | undefined;
|
|
115
115
|
emailVerified?: boolean | undefined;
|
|
116
|
-
role?: string | undefined;
|
|
116
|
+
role?: (import("../..").QarpeoRole | string) | undefined;
|
|
117
117
|
} | undefined;
|
|
118
118
|
email?: string | undefined;
|
|
119
119
|
name?: string | undefined;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CMSOptions } from "../../../types";
|
|
2
|
-
export declare const ALL_CMS_TABLE_NAMES: readonly ["_qarpeo_cms_collections", "_qarpeo_cms_entries", "_qarpeo_cms_revisions", "_qarpeo_cms_categories", "_qarpeo_cms_tags", "
|
|
2
|
+
export declare const ALL_CMS_TABLE_NAMES: readonly ["_qarpeo_cms_collections", "_qarpeo_cms_entries", "_qarpeo_cms_revisions", "_qarpeo_cms_categories", "_qarpeo_cms_tags", "_qarpeo_cms_bookmarks", "_qarpeo_cms_subscribers"];
|
|
3
3
|
export declare function getActiveCmsTableNames(cms?: boolean | CMSOptions): string[];
|
|
4
4
|
export declare const cmsCollections: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
5
5
|
name: "_qarpeo_cms_collections";
|
|
@@ -5,7 +5,6 @@ export const ALL_CMS_TABLE_NAMES = [
|
|
|
5
5
|
"_qarpeo_cms_revisions",
|
|
6
6
|
"_qarpeo_cms_categories",
|
|
7
7
|
"_qarpeo_cms_tags",
|
|
8
|
-
"_qarpeo_cms_comments",
|
|
9
8
|
"_qarpeo_cms_bookmarks",
|
|
10
9
|
"_qarpeo_cms_subscribers"
|
|
11
10
|
];
|
|
@@ -159,7 +158,7 @@ export const cmsBookmarks = snakeCase.table(
|
|
|
159
158
|
(t) => [
|
|
160
159
|
index("_qarpeo_cms_bookmarks_user_idx").on(t.userId),
|
|
161
160
|
index("_qarpeo_cms_bookmarks_entry_idx").on(t.entryId),
|
|
162
|
-
|
|
161
|
+
uniqueIndex("_qarpeo_cms_bookmarks_unique_idx").on(t.tenantId, t.userId, t.entryId)
|
|
163
162
|
]
|
|
164
163
|
);
|
|
165
164
|
export const cmsSubscribers = snakeCase.table(
|
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
import { defineEventHandler, getRequestHeader, getQuery, createError } from "nuxt/server";
|
|
2
2
|
import { publishDueScheduledEntries, getCMSConfig } from "../../utils/cms.mjs";
|
|
3
3
|
import { getAuthContext } from "../../utils/context.mjs";
|
|
4
|
+
import { timingSafeEqualString } from "../../utils/crypto.mjs";
|
|
4
5
|
export default defineEventHandler(async (event) => {
|
|
5
6
|
const config = getCMSConfig(event);
|
|
6
|
-
const
|
|
7
|
+
const cfEnv = event?.context?.cloudflare?.env || globalThis.__qarpeo_env__ || {};
|
|
8
|
+
const cronSecret = cfEnv.CRON_SECRET || cfEnv.QARPEO_CRON_SECRET || (typeof process !== "undefined" ? process.env.CRON_SECRET || process.env.QARPEO_CRON_SECRET : void 0);
|
|
7
9
|
const authHeader = getRequestHeader(event, "authorization") || getRequestHeader(event, "x-cron-secret");
|
|
8
10
|
const querySecret = getQuery(event)?.secret;
|
|
11
|
+
const rawToken = authHeader?.replace(/^Bearer\s+/i, "") || querySecret || "";
|
|
9
12
|
let authorized = false;
|
|
10
|
-
if (cronSecret &&
|
|
13
|
+
if (cronSecret && rawToken && timingSafeEqualString(rawToken, cronSecret)) {
|
|
11
14
|
authorized = true;
|
|
12
15
|
} else {
|
|
13
|
-
const auth = await getAuthContext(event);
|
|
16
|
+
const auth = await getAuthContext(event).catch(() => null);
|
|
14
17
|
if (auth?.isAdmin || auth?.role && config.roles?.includes(auth.role)) {
|
|
15
18
|
authorized = true;
|
|
16
19
|
}
|
|
17
20
|
}
|
|
18
|
-
if (!authorized
|
|
21
|
+
if (!authorized) {
|
|
19
22
|
throw createError({
|
|
20
23
|
statusCode: 401,
|
|
21
24
|
message: "Unauthorized: Invalid cron secret or insufficient permissions"
|
|
@@ -9,7 +9,14 @@ export default defineEventHandler(async (event) => {
|
|
|
9
9
|
throw createError({ statusCode: 400, message: "Subscriber ID is required" });
|
|
10
10
|
}
|
|
11
11
|
const body = await readBody(event);
|
|
12
|
-
const
|
|
13
|
-
const
|
|
12
|
+
const ALLOWED_STATUSES = ["active", "unsubscribed", "bounced", "spam"];
|
|
13
|
+
const rawStatus = typeof body?.status === "string" ? body.status.toLowerCase().trim() : "";
|
|
14
|
+
if (!rawStatus || !ALLOWED_STATUSES.includes(rawStatus)) {
|
|
15
|
+
throw createError({
|
|
16
|
+
statusCode: 400,
|
|
17
|
+
message: `Invalid subscriber status '${body?.status}'. Allowed: ${ALLOWED_STATUSES.join(", ")}`
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
const result = await updateCMSSubscriberStatus(event, id, rawStatus);
|
|
14
21
|
return result;
|
|
15
22
|
});
|
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
invalidateCMSEntryCache,
|
|
17
17
|
verifyGitHubSignature
|
|
18
18
|
} from "../../../utils/cms.mjs";
|
|
19
|
+
import { timingSafeEqualString } from "../../../utils/crypto.mjs";
|
|
19
20
|
export default defineEventHandler(async (event) => {
|
|
20
21
|
const provider = (getRouterParam(event, "provider") || "generic").toLowerCase();
|
|
21
22
|
const config = getCMSConfig(event);
|
|
@@ -45,14 +46,18 @@ export default defineEventHandler(async (event) => {
|
|
|
45
46
|
const ghSignature = getH3Header(event, "x-hub-signature-256") || getH3Header(event, "x-hub-signature");
|
|
46
47
|
const cfEnv = event?.context?.cloudflare?.env || globalThis.__qarpeo_env__ || {};
|
|
47
48
|
const webhookSecret2 = config.github?.webhookSecret || config.webhookSecret || cfEnv.GITHUB_WEBHOOK_SECRET || cfEnv.CMS_WEBHOOK_SECRET || (typeof process !== "undefined" ? process.env.GITHUB_WEBHOOK_SECRET || process.env.CMS_WEBHOOK_SECRET : void 0);
|
|
48
|
-
if (webhookSecret2) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
if (!webhookSecret2) {
|
|
50
|
+
throw createError({
|
|
51
|
+
statusCode: 403,
|
|
52
|
+
message: "Webhook secret is not configured for GitHub webhooks."
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const isValid = await verifyGitHubSignature(rawBody, ghSignature, webhookSecret2);
|
|
56
|
+
if (!isValid) {
|
|
57
|
+
throw createError({
|
|
58
|
+
statusCode: 401,
|
|
59
|
+
message: "Invalid or missing GitHub webhook signature (X-Hub-Signature-256)."
|
|
60
|
+
});
|
|
56
61
|
}
|
|
57
62
|
if (ghEvent === "ping") {
|
|
58
63
|
return {
|
|
@@ -125,16 +130,20 @@ export default defineEventHandler(async (event) => {
|
|
|
125
130
|
}
|
|
126
131
|
if (provider === "queue" || provider === "cloudflare") {
|
|
127
132
|
const webhookSecret2 = config.webhookSecret || (typeof process !== "undefined" ? process.env.CMS_WEBHOOK_SECRET || process.env.QARPEO_WEBHOOK_SECRET : void 0);
|
|
128
|
-
if (webhookSecret2) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
133
|
+
if (!webhookSecret2) {
|
|
134
|
+
throw createError({
|
|
135
|
+
statusCode: 403,
|
|
136
|
+
message: "Webhook secret is not configured for Cloudflare Queue CMS webhooks."
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const authHeader2 = getH3Header(event, "authorization");
|
|
140
|
+
const cmsSecretHeader2 = getH3Header(event, "x-cms-secret");
|
|
141
|
+
const token2 = authHeader2?.replace(/^Bearer\s+/i, "") || cmsSecretHeader2 || "";
|
|
142
|
+
if (!timingSafeEqualString(token2, webhookSecret2)) {
|
|
143
|
+
throw createError({
|
|
144
|
+
statusCode: 401,
|
|
145
|
+
message: "Unauthorized Cloudflare Queue CMS webhook request."
|
|
146
|
+
});
|
|
138
147
|
}
|
|
139
148
|
const messages = Array.isArray(body) ? body : Array.isArray(body.messages) ? body.messages : [body];
|
|
140
149
|
const executedActions = [];
|
|
@@ -175,16 +184,20 @@ export default defineEventHandler(async (event) => {
|
|
|
175
184
|
};
|
|
176
185
|
}
|
|
177
186
|
const webhookSecret = config.webhookSecret || (typeof process !== "undefined" ? process.env.CMS_WEBHOOK_SECRET || process.env.QARPEO_WEBHOOK_SECRET : void 0);
|
|
178
|
-
if (webhookSecret) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
187
|
+
if (!webhookSecret) {
|
|
188
|
+
throw createError({
|
|
189
|
+
statusCode: 403,
|
|
190
|
+
message: "Webhook secret is not configured for generic CMS webhooks."
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
const authHeader = getH3Header(event, "authorization");
|
|
194
|
+
const cmsSecretHeader = getH3Header(event, "x-cms-secret");
|
|
195
|
+
const token = authHeader?.replace(/^Bearer\s+/i, "") || cmsSecretHeader || "";
|
|
196
|
+
if (!timingSafeEqualString(token, webhookSecret)) {
|
|
197
|
+
throw createError({
|
|
198
|
+
statusCode: 401,
|
|
199
|
+
message: "Unauthorized generic CMS webhook request."
|
|
200
|
+
});
|
|
188
201
|
}
|
|
189
202
|
const action = body.action || "sync";
|
|
190
203
|
let result = {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createError, defineEventHandler, getRouterParam, readBody } from "nuxt/server";
|
|
2
|
+
import { ensureCommentsTables, getComment, reactToComment } from "../../../utils/comments.mjs";
|
|
3
|
+
import { getAuthContext } from "../../../utils/context.mjs";
|
|
4
|
+
import { assertRateLimit } from "../../../utils/ratelimit.mjs";
|
|
5
|
+
export default defineEventHandler(async (event) => {
|
|
6
|
+
await ensureCommentsTables(event);
|
|
7
|
+
const id = getRouterParam(event, "id");
|
|
8
|
+
if (!id) {
|
|
9
|
+
throw createError({ statusCode: 400, statusMessage: "Missing comment id" });
|
|
10
|
+
}
|
|
11
|
+
await assertRateLimit(event, {
|
|
12
|
+
key: `comments:react:${id}`,
|
|
13
|
+
max: 30,
|
|
14
|
+
window: "1m",
|
|
15
|
+
message: "Too many reactions. Please slow down."
|
|
16
|
+
});
|
|
17
|
+
const existing = await getComment(id, event);
|
|
18
|
+
if (!existing) {
|
|
19
|
+
throw createError({ statusCode: 404, statusMessage: "Comment not found" });
|
|
20
|
+
}
|
|
21
|
+
const body = await readBody(event) || {};
|
|
22
|
+
if (!body.reaction || typeof body.reaction !== "string") {
|
|
23
|
+
throw createError({ statusCode: 400, statusMessage: "Field 'reaction' is required." });
|
|
24
|
+
}
|
|
25
|
+
const auth = await getAuthContext(event).catch(() => null);
|
|
26
|
+
const result = await reactToComment(
|
|
27
|
+
id,
|
|
28
|
+
body.reaction,
|
|
29
|
+
{
|
|
30
|
+
userId: auth?.user?.id
|
|
31
|
+
},
|
|
32
|
+
event
|
|
33
|
+
);
|
|
34
|
+
return { success: true, ...result };
|
|
35
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createError, defineEventHandler, getRouterParam, readBody } from "nuxt/server";
|
|
2
|
+
import {
|
|
3
|
+
ensureCommentsTables,
|
|
4
|
+
getComment,
|
|
5
|
+
reportComment
|
|
6
|
+
} from "../../../utils/comments.mjs";
|
|
7
|
+
import { getAuthContext } from "../../../utils/context.mjs";
|
|
8
|
+
import { assertRateLimit } from "../../../utils/ratelimit.mjs";
|
|
9
|
+
const ALLOWED_REASONS = [
|
|
10
|
+
"spam",
|
|
11
|
+
"harassment",
|
|
12
|
+
"off-topic",
|
|
13
|
+
"misinformation",
|
|
14
|
+
"other"
|
|
15
|
+
];
|
|
16
|
+
export default defineEventHandler(async (event) => {
|
|
17
|
+
await ensureCommentsTables(event);
|
|
18
|
+
const id = getRouterParam(event, "id");
|
|
19
|
+
if (!id) {
|
|
20
|
+
throw createError({ statusCode: 400, statusMessage: "Missing comment id" });
|
|
21
|
+
}
|
|
22
|
+
await assertRateLimit(event, {
|
|
23
|
+
key: `comments:report:${id}`,
|
|
24
|
+
max: 10,
|
|
25
|
+
window: "1m",
|
|
26
|
+
message: "Too many reports submitted. Please wait a moment."
|
|
27
|
+
});
|
|
28
|
+
const existing = await getComment(id, event);
|
|
29
|
+
if (!existing) {
|
|
30
|
+
throw createError({ statusCode: 404, statusMessage: "Comment not found" });
|
|
31
|
+
}
|
|
32
|
+
const body = await readBody(event) || {};
|
|
33
|
+
if (!body.reason || !ALLOWED_REASONS.includes(body.reason)) {
|
|
34
|
+
throw createError({
|
|
35
|
+
statusCode: 400,
|
|
36
|
+
statusMessage: `Invalid reason. Must be one of: ${ALLOWED_REASONS.join(", ")}`
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
const auth = await getAuthContext(event).catch(() => null);
|
|
40
|
+
const report = await reportComment(
|
|
41
|
+
id,
|
|
42
|
+
body.reason,
|
|
43
|
+
{
|
|
44
|
+
note: body.note,
|
|
45
|
+
userId: auth?.user?.id
|
|
46
|
+
},
|
|
47
|
+
event
|
|
48
|
+
);
|
|
49
|
+
return { success: true, report };
|
|
50
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createError, defineEventHandler, getRouterParam } from "nuxt/server";
|
|
2
|
+
import { deleteComment, ensureCommentsTables, getComment } from "../../utils/comments.mjs";
|
|
3
|
+
import { getAuthContext } from "../../utils/context.mjs";
|
|
4
|
+
export default defineEventHandler(async (event) => {
|
|
5
|
+
await ensureCommentsTables(event);
|
|
6
|
+
const id = getRouterParam(event, "id");
|
|
7
|
+
if (!id) {
|
|
8
|
+
throw createError({ statusCode: 400, statusMessage: "Missing comment id" });
|
|
9
|
+
}
|
|
10
|
+
const existing = await getComment(id, event);
|
|
11
|
+
if (!existing) {
|
|
12
|
+
throw createError({ statusCode: 404, statusMessage: "Comment not found" });
|
|
13
|
+
}
|
|
14
|
+
const auth = await getAuthContext(event).catch(() => null);
|
|
15
|
+
const role = auth?.user?.role || "";
|
|
16
|
+
const isModerator = role === "admin" || role === "super_admin" || role === "moderator";
|
|
17
|
+
const isAuthor = auth?.user?.email && existing.authorEmail && auth.user.email === existing.authorEmail;
|
|
18
|
+
if (!isModerator && !isAuthor) {
|
|
19
|
+
throw createError({
|
|
20
|
+
statusCode: 403,
|
|
21
|
+
statusMessage: "Forbidden",
|
|
22
|
+
message: "You do not have permission to delete this comment."
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
await deleteComment(id, event);
|
|
26
|
+
return { success: true };
|
|
27
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createError, defineEventHandler, getRouterParam } from "nuxt/server";
|
|
2
|
+
import { ensureCommentsTables, getComment } from "../../utils/comments.mjs";
|
|
3
|
+
export default defineEventHandler(async (event) => {
|
|
4
|
+
await ensureCommentsTables(event);
|
|
5
|
+
const id = getRouterParam(event, "id");
|
|
6
|
+
if (!id) {
|
|
7
|
+
throw createError({ statusCode: 400, statusMessage: "Missing comment id" });
|
|
8
|
+
}
|
|
9
|
+
const comment = await getComment(id, event);
|
|
10
|
+
if (!comment) {
|
|
11
|
+
throw createError({ statusCode: 404, statusMessage: "Comment not found" });
|
|
12
|
+
}
|
|
13
|
+
return { comment };
|
|
14
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createError, defineEventHandler, getRouterParam, readBody } from "nuxt/server";
|
|
2
|
+
import {
|
|
3
|
+
ensureCommentsTables,
|
|
4
|
+
getComment,
|
|
5
|
+
updateCommentStatus
|
|
6
|
+
} from "../../utils/comments.mjs";
|
|
7
|
+
import { getAuthContext } from "../../utils/context.mjs";
|
|
8
|
+
export default defineEventHandler(async (event) => {
|
|
9
|
+
await ensureCommentsTables(event);
|
|
10
|
+
const id = getRouterParam(event, "id");
|
|
11
|
+
if (!id) {
|
|
12
|
+
throw createError({ statusCode: 400, statusMessage: "Missing comment id" });
|
|
13
|
+
}
|
|
14
|
+
const auth = await getAuthContext(event).catch(() => null);
|
|
15
|
+
const role = auth?.user?.role || "";
|
|
16
|
+
const isModerator = role === "admin" || role === "super_admin" || role === "moderator";
|
|
17
|
+
if (!isModerator) {
|
|
18
|
+
throw createError({
|
|
19
|
+
statusCode: 403,
|
|
20
|
+
statusMessage: "Forbidden",
|
|
21
|
+
message: "Moderator or admin privileges required to update comment status."
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
const body = await readBody(event) || {};
|
|
25
|
+
if (!body.status || !["pending", "approved", "spam", "deleted"].includes(body.status)) {
|
|
26
|
+
throw createError({
|
|
27
|
+
statusCode: 400,
|
|
28
|
+
statusMessage: "Invalid status. Must be pending, approved, spam, or deleted."
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
const existing = await getComment(id, event);
|
|
32
|
+
if (!existing) {
|
|
33
|
+
throw createError({ statusCode: 404, statusMessage: "Comment not found" });
|
|
34
|
+
}
|
|
35
|
+
const updated = await updateCommentStatus(id, body.status, event);
|
|
36
|
+
return { comment: updated };
|
|
37
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { defineEventHandler, getQuery } from "nuxt/server";
|
|
2
|
+
import { queryComments, ensureCommentsTables, validateTargetUrn } from "../../utils/comments.mjs";
|
|
3
|
+
import { getAuthContext } from "../../utils/context.mjs";
|
|
4
|
+
export default defineEventHandler(async (event) => {
|
|
5
|
+
await ensureCommentsTables(event);
|
|
6
|
+
const query = getQuery(event);
|
|
7
|
+
const targetUrn = String(query.targetUrn || "");
|
|
8
|
+
if (!targetUrn) {
|
|
9
|
+
return { comments: [], total: 0 };
|
|
10
|
+
}
|
|
11
|
+
validateTargetUrn(targetUrn);
|
|
12
|
+
const auth = await getAuthContext(event).catch(() => null);
|
|
13
|
+
const isAdmin = auth?.user?.role === "admin" || auth?.user?.role === "super_admin" || auth?.user?.role === "moderator";
|
|
14
|
+
let status = "approved";
|
|
15
|
+
if (isAdmin && query.status) {
|
|
16
|
+
const raw = String(query.status);
|
|
17
|
+
status = raw.includes(",") ? raw.split(",").map((s) => s.trim()) : raw;
|
|
18
|
+
}
|
|
19
|
+
const limit = Math.min(Number(query.limit) || 50, 200);
|
|
20
|
+
const offset = Number(query.offset) || 0;
|
|
21
|
+
const threaded = query.threaded !== "false";
|
|
22
|
+
return queryComments(
|
|
23
|
+
{
|
|
24
|
+
targetUrn,
|
|
25
|
+
status,
|
|
26
|
+
limit,
|
|
27
|
+
offset,
|
|
28
|
+
threaded,
|
|
29
|
+
tenantId: query.tenantId ? String(query.tenantId) : void 0,
|
|
30
|
+
orderBy: query.order === "asc" ? "asc" : "desc"
|
|
31
|
+
},
|
|
32
|
+
event
|
|
33
|
+
);
|
|
34
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createError, defineEventHandler, readBody } from "nuxt/server";
|
|
2
|
+
import {
|
|
3
|
+
createComment,
|
|
4
|
+
ensureCommentsTables,
|
|
5
|
+
validateTargetUrn,
|
|
6
|
+
getCommentsConfig
|
|
7
|
+
} from "../../utils/comments.mjs";
|
|
8
|
+
import { getAuthContext } from "../../utils/context.mjs";
|
|
9
|
+
export default defineEventHandler(async (event) => {
|
|
10
|
+
await ensureCommentsTables(event);
|
|
11
|
+
const body = await readBody(event) || {};
|
|
12
|
+
if (!body.targetUrn) {
|
|
13
|
+
throw createError({ statusCode: 400, statusMessage: "targetUrn is required." });
|
|
14
|
+
}
|
|
15
|
+
validateTargetUrn(body.targetUrn);
|
|
16
|
+
const auth = await getAuthContext(event).catch(() => null);
|
|
17
|
+
const config = getCommentsConfig(event);
|
|
18
|
+
if (config.requireAuth && !auth?.user?.id) {
|
|
19
|
+
throw createError({ statusCode: 401, statusMessage: "Authentication required to comment." });
|
|
20
|
+
}
|
|
21
|
+
const authorName = (body.authorName ?? auth?.user?.name ?? auth?.user?.username ?? "").trim();
|
|
22
|
+
if (!authorName) {
|
|
23
|
+
throw createError({ statusCode: 400, statusMessage: "authorName is required." });
|
|
24
|
+
}
|
|
25
|
+
if (!body.content || !body.content.trim()) {
|
|
26
|
+
throw createError({ statusCode: 400, statusMessage: "content is required." });
|
|
27
|
+
}
|
|
28
|
+
const comment = await createComment(
|
|
29
|
+
{
|
|
30
|
+
...body,
|
|
31
|
+
authorName
|
|
32
|
+
},
|
|
33
|
+
event
|
|
34
|
+
);
|
|
35
|
+
return { comment };
|
|
36
|
+
});
|