Improve ISAC libraries, roles, and public profiles
This commit is contained in:
@@ -9,9 +9,11 @@ import {
|
||||
createUserContentReport,
|
||||
recordCommunityModerationScan
|
||||
} from '$lib/server/community-moderation';
|
||||
import { error, fail } from '@sveltejs/kit';
|
||||
import { error, fail, redirect } from '@sveltejs/kit';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { queueCurrentAvatarReview } from '$lib/server/avatars';
|
||||
import { agentProfileHref } from '$lib/agents';
|
||||
import { resolveAgentProfile } from '$lib/server/agent-profiles';
|
||||
import type { Actions, PageServerLoad } from './$types';
|
||||
|
||||
const COMMENT_MAX_CHARACTERS = 2_000;
|
||||
@@ -22,17 +24,25 @@ const unavailable = (row: Record<string, unknown> | null | undefined) => {
|
||||
return !row || Boolean(row.deleted_at || row.blocked_at || banned);
|
||||
};
|
||||
|
||||
const commentHref = (targetType: string, slug: unknown, targetId: string) => {
|
||||
if (targetType === 'profile') return `/agents/${encodeURIComponent(targetId)}#profile-comments`;
|
||||
const commentHref = (targetType: string, slug: unknown, targetId: string, targetPublicSlug: unknown) => {
|
||||
if (targetType === 'profile') {
|
||||
return agentProfileHref({
|
||||
id: targetId,
|
||||
publicSlug: typeof targetPublicSlug === 'string' ? targetPublicSlug : null
|
||||
}, 'profile-comments');
|
||||
}
|
||||
return `/builds/${encodeURIComponent(String(slug ?? ''))}#comments`;
|
||||
};
|
||||
|
||||
export const load: PageServerLoad = async ({ params, cookies }) => {
|
||||
const database = await getDatabase();
|
||||
const viewer = await getSessionUser(cookies);
|
||||
const resolved = await resolveAgentProfile(database, params.id);
|
||||
if (!resolved) error(404, 'Agent profile not found');
|
||||
const agentId = resolved.id;
|
||||
const agent = await (await database.prepare(`SELECT users.id,
|
||||
COALESCE(profile.display_name,users.username) AS display_name,
|
||||
profile.bio_markdown,profile.is_public,profile.bio_updated_at,
|
||||
profile.public_slug,profile.bio_markdown,profile.is_public,profile.bio_updated_at,
|
||||
COALESCE(moderation.bio_hidden,0) AS bio_hidden,
|
||||
avatar.source AS avatar_source,avatar.value AS avatar_value,avatar.updated_at AS avatar_updated_at,
|
||||
status.blocked_at,status.banned_until,deleted.deleted_at
|
||||
@@ -42,26 +52,34 @@ export const load: PageServerLoad = async ({ params, cookies }) => {
|
||||
LEFT JOIN user_avatars avatar ON avatar.user_id=users.id
|
||||
LEFT JOIN user_status status ON status.user_id=users.id
|
||||
LEFT JOIN deleted_accounts deleted ON deleted.user_id=users.id
|
||||
WHERE users.id=?`)).get(params.id);
|
||||
const isSelf = viewer?.id === params.id;
|
||||
WHERE users.id=?`)).get(agentId);
|
||||
const isSelf = viewer?.id === agentId;
|
||||
if (
|
||||
unavailable(agent)
|
||||
|| (!Boolean(agent?.is_public) && !isSelf)
|
||||
) error(404, 'Agent profile not found');
|
||||
if (params.id !== String(agent.public_slug)) {
|
||||
redirect(308, agentProfileHref({ id: agentId, publicSlug: String(agent.public_slug) }));
|
||||
}
|
||||
|
||||
const [buildRows, guideRows, authoredCommentRows, profileCommentRows, authoredCount, profileCommentCount, contentCounts] =
|
||||
const [roleRows, buildRows, guideRows, authoredCommentRows, profileCommentRows, authoredCount, profileCommentCount, contentCounts] =
|
||||
await Promise.all([
|
||||
(await database.prepare(`SELECT roles.name FROM user_roles
|
||||
JOIN roles ON roles.id=user_roles.role_id
|
||||
WHERE user_roles.user_id=?
|
||||
ORDER BY roles.name COLLATE NOCASE,roles.id`)).all(agentId),
|
||||
(await database.prepare(`SELECT id,slug,title,COALESCE(summary,'') AS summary,updated_at
|
||||
FROM builds WHERE owner_id=? AND visibility='public'
|
||||
AND (guide_body IS NULL OR length(trim(guide_body))=0)
|
||||
ORDER BY updated_at DESC LIMIT 60`)).all(params.id),
|
||||
ORDER BY updated_at DESC LIMIT 60`)).all(agentId),
|
||||
(await database.prepare(`SELECT id,slug,title,COALESCE(summary,'') AS summary,updated_at
|
||||
FROM builds WHERE owner_id=? AND visibility='public'
|
||||
AND guide_body IS NOT NULL AND length(trim(guide_body))>0
|
||||
ORDER BY updated_at DESC LIMIT 60`)).all(params.id),
|
||||
ORDER BY updated_at DESC LIMIT 60`)).all(agentId),
|
||||
(await database.prepare(`SELECT comments.id,comments.body_markdown,comments.created_at,
|
||||
thread.target_type,thread.target_id,builds.title AS build_title,builds.slug,
|
||||
COALESCE(target_profile.display_name,target_user.username) AS target_display_name
|
||||
COALESCE(target_profile.display_name,target_user.username) AS target_display_name,
|
||||
target_profile.public_slug AS target_public_slug
|
||||
FROM comments
|
||||
JOIN comment_threads thread ON thread.id=comments.thread_id
|
||||
LEFT JOIN builds ON builds.id=thread.target_id AND thread.target_type IN ('build','guide')
|
||||
@@ -75,9 +93,10 @@ export const load: PageServerLoad = async ({ params, cookies }) => {
|
||||
AND target_deleted.user_id IS NULL AND target_status.blocked_at IS NULL
|
||||
AND (target_status.banned_until IS NULL OR target_status.banned_until<=?))
|
||||
)
|
||||
ORDER BY comments.created_at DESC LIMIT 50`)).all(params.id, new Date().toISOString()),
|
||||
ORDER BY comments.created_at DESC LIMIT 50`)).all(agentId, new Date().toISOString()),
|
||||
(await database.prepare(`SELECT comments.id,comments.author_id,comments.body_markdown,
|
||||
comments.created_at,COALESCE(profile.display_name,users.username,'Deleted Agent') AS display_name,
|
||||
profile.public_slug,
|
||||
avatar.source AS avatar_source,avatar.value AS avatar_value,avatar.updated_at AS avatar_updated_at
|
||||
FROM comment_threads thread
|
||||
JOIN comments ON comments.thread_id=thread.id AND comments.status='visible'
|
||||
@@ -85,7 +104,7 @@ export const load: PageServerLoad = async ({ params, cookies }) => {
|
||||
LEFT JOIN user_profiles profile ON profile.user_id=users.id
|
||||
LEFT JOIN user_avatars avatar ON avatar.user_id=users.id
|
||||
WHERE thread.target_type='profile' AND thread.target_id=?
|
||||
ORDER BY comments.created_at DESC LIMIT 100`)).all(params.id),
|
||||
ORDER BY comments.created_at DESC LIMIT 100`)).all(agentId),
|
||||
(await database.prepare(`SELECT COUNT(*) AS count FROM comments
|
||||
JOIN comment_threads thread ON thread.id=comments.thread_id
|
||||
LEFT JOIN builds ON builds.id=thread.target_id AND thread.target_type IN ('build','guide')
|
||||
@@ -98,28 +117,30 @@ export const load: PageServerLoad = async ({ params, cookies }) => {
|
||||
OR (thread.target_type='profile' AND COALESCE(target_profile.is_public,0)=1
|
||||
AND target_deleted.user_id IS NULL AND target_status.blocked_at IS NULL
|
||||
AND (target_status.banned_until IS NULL OR target_status.banned_until<=?))
|
||||
)`)).get(params.id, new Date().toISOString()),
|
||||
)`)).get(agentId, new Date().toISOString()),
|
||||
(await database.prepare(`SELECT COUNT(*) AS count FROM comments
|
||||
JOIN comment_threads thread ON thread.id=comments.thread_id
|
||||
WHERE thread.target_type='profile' AND thread.target_id=? AND comments.status='visible'`))
|
||||
.get(params.id),
|
||||
.get(agentId),
|
||||
(await database.prepare(`SELECT
|
||||
SUM(CASE WHEN guide_body IS NULL OR length(trim(guide_body))=0 THEN 1 ELSE 0 END) AS builds,
|
||||
SUM(CASE WHEN guide_body IS NOT NULL AND length(trim(guide_body))>0 THEN 1 ELSE 0 END) AS guides
|
||||
FROM builds WHERE owner_id=? AND visibility='public'`)).get(params.id)
|
||||
FROM builds WHERE owner_id=? AND visibility='public'`)).get(agentId)
|
||||
]);
|
||||
|
||||
return {
|
||||
agent: {
|
||||
id: String(agent.id),
|
||||
displayName: String(agent.display_name),
|
||||
publicSlug: String(agent.public_slug),
|
||||
avatarUrl: avatarUrl(
|
||||
agent.avatar_source,
|
||||
agent.avatar_value,
|
||||
String(agent.id),
|
||||
agent.avatar_updated_at
|
||||
),
|
||||
bioHtml: agent.bio_hidden ? '' : renderUserMarkdown(agent.bio_markdown)
|
||||
bioHtml: agent.bio_hidden ? '' : renderUserMarkdown(agent.bio_markdown),
|
||||
roles: roleRows.map((role) => String(role.name))
|
||||
},
|
||||
counts: {
|
||||
builds: Number(contentCounts?.builds ?? 0),
|
||||
@@ -152,7 +173,7 @@ export const load: PageServerLoad = async ({ params, cookies }) => {
|
||||
targetTitle: targetType === 'profile'
|
||||
? `${String(comment.target_display_name ?? 'Agent')} profile`
|
||||
: String(comment.build_title ?? 'Community content'),
|
||||
targetHref: commentHref(targetType, comment.slug, targetId)
|
||||
targetHref: commentHref(targetType, comment.slug, targetId, comment.target_public_slug)
|
||||
};
|
||||
}),
|
||||
profileComments: profileCommentRows.map((comment) => ({
|
||||
@@ -162,6 +183,7 @@ export const load: PageServerLoad = async ({ params, cookies }) => {
|
||||
author: {
|
||||
id: String(comment.author_id),
|
||||
displayName: String(comment.display_name),
|
||||
publicSlug: comment.public_slug ? String(comment.public_slug) : null,
|
||||
avatarUrl: avatarUrl(
|
||||
comment.avatar_source,
|
||||
comment.avatar_value,
|
||||
@@ -195,6 +217,9 @@ export const actions: Actions = {
|
||||
});
|
||||
}
|
||||
const database = await getDatabase();
|
||||
const owner = await resolveAgentProfile(database, params.id);
|
||||
if (!owner) return fail(404, { profileCommentError: 'Agent profile not found.' });
|
||||
const agentId = owner.id;
|
||||
const moderationPolicy = await getCommunityModerationPolicy();
|
||||
const now = new Date();
|
||||
const rows = await (await database.prepare(`SELECT users.id,
|
||||
@@ -204,9 +229,9 @@ export const actions: Actions = {
|
||||
LEFT JOIN user_profiles profile ON profile.user_id=users.id
|
||||
LEFT JOIN user_status status ON status.user_id=users.id
|
||||
LEFT JOIN deleted_accounts deleted ON deleted.user_id=users.id
|
||||
WHERE users.id IN (?,?)`)).all(commenter.id, params.id);
|
||||
WHERE users.id IN (?,?)`)).all(commenter.id, agentId);
|
||||
const commenterState = rows.find((row) => row.id === commenter.id);
|
||||
const ownerState = rows.find((row) => row.id === params.id);
|
||||
const ownerState = rows.find((row) => row.id === agentId);
|
||||
if (unavailable(commenterState)) {
|
||||
return fail(403, { profileCommentError: 'This account cannot post comments.' });
|
||||
}
|
||||
@@ -218,7 +243,7 @@ export const actions: Actions = {
|
||||
FROM comments JOIN comment_threads thread ON thread.id=comments.thread_id
|
||||
WHERE comments.author_id=? AND comments.status IN ('visible','pending_review')
|
||||
AND thread.target_type='profile' AND thread.target_id=?
|
||||
ORDER BY comments.created_at DESC LIMIT 1`)).get(commenter.id, params.id);
|
||||
ORDER BY comments.created_at DESC LIMIT 1`)).get(commenter.id, agentId);
|
||||
if (typeof recent?.created_at === 'string') {
|
||||
const elapsed = (now.getTime() - new Date(recent.created_at).getTime()) / 1_000;
|
||||
if (elapsed < RAPID_COMMENT_SECONDS) {
|
||||
@@ -239,10 +264,10 @@ export const actions: Actions = {
|
||||
const proposedThreadId = randomUUID();
|
||||
await (await database.prepare(`INSERT OR IGNORE INTO comment_threads
|
||||
(id,target_type,target_id,created_at) VALUES(?,'profile',?,?)`))
|
||||
.run(proposedThreadId, params.id, createdAt);
|
||||
.run(proposedThreadId, agentId, createdAt);
|
||||
const thread = await (await database.prepare(
|
||||
"SELECT id FROM comment_threads WHERE target_type='profile' AND target_id=?"
|
||||
)).get(params.id);
|
||||
)).get(agentId);
|
||||
if (!thread?.id) return fail(500, { profileCommentError: 'The profile thread could not be opened.' });
|
||||
const commentId = randomUUID();
|
||||
await (await database.prepare(`INSERT INTO comments
|
||||
@@ -255,17 +280,18 @@ export const actions: Actions = {
|
||||
authorId: commenter.id,
|
||||
result: moderation
|
||||
});
|
||||
if (commentStatus === 'visible' && commenter.id !== params.id) {
|
||||
if (commentStatus === 'visible' && commenter.id !== agentId) {
|
||||
const profileLink = agentProfileHref(owner, 'profile-comments');
|
||||
await createNotification({
|
||||
userId: params.id,
|
||||
userId: agentId,
|
||||
category: 'comments',
|
||||
source: 'agent',
|
||||
title: 'New profile comment',
|
||||
body: `${commenter.displayName} posted a comment on your Agent profile.`,
|
||||
link: `/agents/${encodeURIComponent(params.id)}#profile-comments`,
|
||||
link: profileLink,
|
||||
actions: [{
|
||||
label: 'VIEW COMMENT',
|
||||
href: `/agents/${encodeURIComponent(params.id)}#profile-comments`
|
||||
href: profileLink
|
||||
}]
|
||||
});
|
||||
}
|
||||
@@ -283,10 +309,12 @@ export const actions: Actions = {
|
||||
const commentId = String((await request.formData()).get('commentId') ?? '').trim();
|
||||
if (!commentId) return fail(400, { profileCommentError: 'Choose a comment to remove.' });
|
||||
const database = await getDatabase();
|
||||
const owner = await resolveAgentProfile(database, params.id);
|
||||
if (!owner) return fail(404, { profileCommentError: 'Agent profile not found.' });
|
||||
const comment = await (await database.prepare(`SELECT comments.id,comments.author_id
|
||||
FROM comments JOIN comment_threads thread ON thread.id=comments.thread_id
|
||||
WHERE comments.id=? AND comments.status='visible'
|
||||
AND thread.target_type='profile' AND thread.target_id=?`)).get(commentId, params.id);
|
||||
AND thread.target_type='profile' AND thread.target_id=?`)).get(commentId, owner.id);
|
||||
if (!comment) return fail(404, { profileCommentError: 'That comment is no longer available.' });
|
||||
if (comment.author_id !== viewer.id) {
|
||||
return fail(403, { profileCommentError: 'You can remove only your own comment.' });
|
||||
@@ -326,22 +354,25 @@ export const actions: Actions = {
|
||||
return failure(400, 'Report details may contain up to 500 visible characters.');
|
||||
}
|
||||
const database = await getDatabase();
|
||||
const owner = await resolveAgentProfile(database, params.id);
|
||||
if (!owner) return failure(404, 'Agent profile not found.');
|
||||
const agentId = owner.id;
|
||||
if (contentType === 'comment') {
|
||||
const comment = await (await database.prepare(`SELECT comments.author_id
|
||||
FROM comments JOIN comment_threads thread ON thread.id=comments.thread_id
|
||||
WHERE comments.id=? AND comments.status='visible'
|
||||
AND thread.target_type='profile' AND thread.target_id=?`)).get(contentId, params.id);
|
||||
AND thread.target_type='profile' AND thread.target_id=?`)).get(contentId, agentId);
|
||||
if (!comment) return failure(404, 'That comment is no longer available.');
|
||||
if (String(comment.author_id) === reporter.id) return failure(400, 'You cannot report your own comment.');
|
||||
} else {
|
||||
if (contentId !== params.id) return failure(400, 'That profile content does not match this Agent.');
|
||||
if (reporter.id === params.id) return failure(400, 'You cannot report your own profile content.');
|
||||
if (contentId !== agentId) return failure(400, 'That profile content does not match this Agent.');
|
||||
if (reporter.id === agentId) return failure(400, 'You cannot report your own profile content.');
|
||||
const profile = await (await database.prepare(`SELECT profile.bio_markdown,profile.is_public,
|
||||
COALESCE(moderation.bio_hidden,0) AS bio_hidden,avatar.source AS avatar_source
|
||||
FROM user_profiles profile
|
||||
LEFT JOIN profile_moderation moderation ON moderation.user_id=profile.user_id
|
||||
LEFT JOIN user_avatars avatar ON avatar.user_id=profile.user_id
|
||||
WHERE profile.user_id=?`)).get(params.id);
|
||||
WHERE profile.user_id=?`)).get(agentId);
|
||||
if (!profile?.is_public) return failure(404, 'That profile is no longer available.');
|
||||
if (contentType === 'profile_bio' && (profile.bio_hidden || !String(profile.bio_markdown ?? '').trim())) {
|
||||
return failure(404, 'That biography is no longer available.');
|
||||
@@ -359,7 +390,7 @@ export const actions: Actions = {
|
||||
});
|
||||
if (!report) return failure(409, 'You already have a pending report for this content.');
|
||||
if (contentType === 'avatar') {
|
||||
await queueCurrentAvatarReview(database, params.id, { origin: 'user_report' });
|
||||
await queueCurrentAvatarReview(database, agentId, { origin: 'user_report' });
|
||||
}
|
||||
return {
|
||||
userReportSaved: true,
|
||||
|
||||
Reference in New Issue
Block a user