From 30d5dc9bf20f1bb7213dd762d0871c2f9c8f2836 Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Sun, 17 Sep 2023 14:40:50 -0700 Subject: [PATCH] feat: rewrite report/inbox handling --- package.json | 2 +- src/lib/auth.ts | 39 ---- .../lemmy/inbox/PrivateMessage.svelte | 44 +++++ .../lemmy/modal/PrivateMessageModal.svelte | 61 ++++++ src/lib/components/lemmy/user/UserLink.svelte | 43 +++-- src/lib/lemmy/inbox.ts | 154 +++++---------- src/lib/lemmy/report.ts | 93 +++++++++ src/routes/inbox/+page.svelte | 4 +- src/routes/inbox/+page.ts | 17 +- src/routes/inbox/InboxItem.svelte | 182 +++++++----------- src/routes/moderation/+page.ts | 22 ++- src/routes/moderation/Report.svelte | 176 +++++++++-------- src/routes/u/[name]/+page.svelte | 58 +----- 13 files changed, 467 insertions(+), 428 deletions(-) create mode 100644 src/lib/components/lemmy/inbox/PrivateMessage.svelte create mode 100644 src/lib/components/lemmy/modal/PrivateMessageModal.svelte create mode 100644 src/lib/lemmy/report.ts diff --git a/package.json b/package.json index ddb98c7a..a31afbd8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "photon-lemmy", - "version": "1.7.3", + "version": "1.8.0", "private": true, "scripts": { "dev": "vite dev", diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 018531e6..9d51d815 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -2,7 +2,6 @@ import { amModOfAny } from '$lib/components/lemmy/moderation/moderation.js' import { toast } from 'mono-svelte' import { DEFAULT_INSTANCE_URL, instance } from '$lib/instance.js' import { getClient, site } from '$lib/lemmy.js' -import { getInbox, getInboxItemPublished } from '$lib/lemmy/inbox.js' import { userSettings } from '$lib/settings.js' import { moveItem } from '$lib/util.js' import { @@ -294,41 +293,3 @@ setInterval(async () => { user: user, })) }, get(userSettings).notifications.pollRate ?? 30 * 1000) - -setFromStorage('seenUntil', Date.now().toString(), false) - -export async function getInboxNotifications(dontUpdate: boolean = false) { - if (!get(profile) || !get(userSettings).notifications.enabled) return - - const { jwt } = get(profile)! - if (!jwt) return - - let until = Number(localStorage.getItem('seenUntil')) - - if (Number.isNaN(until) || until == 0) { - const now = Date.now() - localStorage.setItem('seenUntil', now.toString()) - until = now - } - - const inbox = await getInbox(jwt, until) - - inbox.forEach((item) => { - const notif = new Notification( - item.person.display_name ?? item.person.name, - { - body: item.body, - timestamp: item.created, - icon: item.person.avatar, - } - ) - - notif.onclick = (e) => { - window.open('/inbox') - } - }) - - if (dontUpdate) return - - localStorage.setItem('seenUntil', Date.now().toString()) -} diff --git a/src/lib/components/lemmy/inbox/PrivateMessage.svelte b/src/lib/components/lemmy/inbox/PrivateMessage.svelte new file mode 100644 index 00000000..db2bef3e --- /dev/null +++ b/src/lib/components/lemmy/inbox/PrivateMessage.svelte @@ -0,0 +1,44 @@ + + +
+
+ From + + {#if $profile?.user?.local_user_view.person.id != message.recipient.id} + to + + {/if} +
+ +
diff --git a/src/lib/components/lemmy/modal/PrivateMessageModal.svelte b/src/lib/components/lemmy/modal/PrivateMessageModal.svelte new file mode 100644 index 00000000..f4adb6f2 --- /dev/null +++ b/src/lib/components/lemmy/modal/PrivateMessageModal.svelte @@ -0,0 +1,61 @@ + + + +

Message

+
+

+ Sending a message +

+ + + +
diff --git a/src/lib/components/lemmy/user/UserLink.svelte b/src/lib/components/lemmy/user/UserLink.svelte index 17802c50..d3b13ac5 100644 --- a/src/lib/components/lemmy/user/UserLink.svelte +++ b/src/lib/components/lemmy/user/UserLink.svelte @@ -1,8 +1,10 @@ +{#if replying && item.type == 'private_message'} + +{/if} + - {#if !isPrivateMessage(item)} -
- - {#if isCommentReply(item)} - - {:else} - - {/if} - - -
-
- Reply - {#if $profile?.user && item.post.creator_id != $profile.user.local_user_view.person.id} -
-
+ + + + {#if item.type != 'private_message'} + + {/if} +
+
+ + {#if item.type == 'private_message'} + Message + {:else if item.type == 'person_mention'} + Mention + {:else if item.type == 'comment_reply'} + Reply + {/if} + + {#if item.type != 'private_message' && $profile?.user && item.item.post.creator_id != $profile.user.local_user_view.person.id} +
+
+
+ -
- -
- {/if} +
+ {/if} + {#if item.type == 'private_message'} + + {:else} -
-
- + {/if} +
+
+ - + {/if} + {:else} + -
- {:else} -
-
- - {#if item.creator.id == $profile?.user?.local_user_view.person.id} - You - {:else} - - {/if} - - messaged - - {#if item.recipient.id == $profile?.user?.local_user_view.person.id} - You - {:else} - - {/if} - -
-
-

- -

- {#if item.recipient.id == $profile?.user?.local_user_view.person.id} -
- -
- {/if} - {#if isPrivateMessage(item)} - {#if replying} -
- -
- -
-
- {/if} {/if} - {/if} +
diff --git a/src/routes/moderation/+page.ts b/src/routes/moderation/+page.ts index 81b41a90..cd371701 100644 --- a/src/routes/moderation/+page.ts +++ b/src/routes/moderation/+page.ts @@ -1,6 +1,11 @@ import { profile } from '$lib/auth.js' +import { isAdmin } from '$lib/components/lemmy/moderation/moderation.js' import { getClient } from '$lib/lemmy.js' -import { getItemPublished } from '$lib/lemmy/item.js' +import { + generalizeCommentReport, + generalizePostReport, + generalizePrivateMessageReport, +} from '$lib/lemmy/report.js' import { get } from 'svelte/store' type ReportListType = 'unread' | 'all' @@ -33,18 +38,25 @@ export async function load({ url, fetch }) { unresolved_only: type == 'unread', } - const [posts, comments] = await Promise.all([ + const admin = get(profile)?.user ? isAdmin(get(profile)!.user!) : false + + const [posts, comments, messages] = await Promise.all([ client.listPostReports({ ...params, }), client.listCommentReports({ ...params, }), + admin ? client.listPrivateMessageReports({ ...params }) : undefined, ]) - const everything = [...posts.post_reports, ...comments.comment_reports].sort( - (a, b) => Date.parse(getItemPublished(b)) - Date.parse(getItemPublished(a)) - ) + const everything = [ + ...posts.post_reports.map(generalizePostReport), + ...comments.comment_reports.map(generalizeCommentReport), + ...(messages?.private_message_reports ?? []).map( + generalizePrivateMessageReport + ), + ].sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime()) return { type: type, diff --git a/src/routes/moderation/Report.svelte b/src/routes/moderation/Report.svelte index 233213e2..ac3dde2f 100644 --- a/src/routes/moderation/Report.svelte +++ b/src/routes/moderation/Report.svelte @@ -12,14 +12,10 @@ import { Check, Icon } from 'svelte-hero-icons' import { profile } from '$lib/auth.js' import { Button } from 'mono-svelte' + import type { ReportView } from '$lib/lemmy/report.js' + import PrivateMessage from '$lib/components/lemmy/inbox/PrivateMessage.svelte' - export let item: PostReportView | CommentReportView | PrivateMessageReportView - - $: resolved = isCommentReport(item) - ? item.comment_report.resolved - : isPostReport(item) - ? item.post_report.resolved - : false + export let item: ReportView let resolving = false async function resolve() { @@ -27,31 +23,60 @@ resolving = true try { - if (isCommentReport(item)) { - await getClient().resolveCommentReport({ - auth: $profile.jwt, - report_id: item.comment_report.id, - resolved: !resolved, - }) + switch (item.type) { + case 'comment': { + const res = await getClient().resolveCommentReport({ + auth: $profile.jwt, + report_id: item.id, + resolved: !item.resolved, + }) + + item.resolved = res.comment_report_view.comment_report.resolved + + toast({ + content: `${ + item.resolved ? 'Resolved' : 'Unresolved' + } that report.`, + type: 'success', + }) - resolved = !resolved + break + } + case 'post': { + const res = await getClient().resolvePostReport({ + auth: $profile.jwt, + report_id: item.id, + resolved: !item.resolved, + }) - toast({ - content: `${resolved ? 'Resolved' : 'Unresolved'} that report.`, - type: 'success', - }) - } else if (isPostReport(item)) { - await getClient().resolvePostReport({ - auth: $profile.jwt, - report_id: item.post_report.id, - resolved: !resolved, - }) + item.resolved = res.post_report_view.post_report.resolved + toast({ + content: `${ + item.resolved ? 'Resolved' : 'Unresolved' + } that report.`, + type: 'success', + }) - resolved = !resolved - toast({ - content: `${resolved ? 'Resolved' : 'Unresolved'} that report.`, - type: 'success', - }) + break + } + case 'message': { + const res = await getClient().resolvePrivateMessageReport({ + auth: $profile.jwt, + report_id: item.id, + resolved: !item.resolved, + }) + + item.resolved = + res.private_message_report_view.private_message_report.resolved + toast({ + content: `${ + item.resolved ? 'Resolved' : 'Unresolved' + } that report.`, + type: 'success', + }) + + break + } } const reports = await getClient().getReportCount({ @@ -73,66 +98,39 @@ } -{#if isCommentReport(item)} - +{:else if item.type == 'post'} + +{:else if item.type == 'message'} + -
-
- - Reason - -

- {item.comment_report.reason} -

-
- -
-{:else if isPostReport(item)} - -
-
- - Reason - -

- {item.post_report.reason} -

-
- -
{/if} + +
+
+ + Reason + +

+ {item.reason} +

+
+ +
diff --git a/src/routes/u/[name]/+page.svelte b/src/routes/u/[name]/+page.svelte index 28dc9579..3a83d818 100644 --- a/src/routes/u/[name]/+page.svelte +++ b/src/routes/u/[name]/+page.svelte @@ -35,6 +35,7 @@ import { Button, Modal, Select } from 'mono-svelte' import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte' import { publishedToDate } from '$lib/components/util/date.js' + import PrivateMessageModal from '$lib/components/lemmy/modal/PrivateMessageModal.svelte' export let data @@ -77,37 +78,7 @@ blocking = false } - let loadingMessage = false let messaging = false - let message = '' - - async function sendMessage() { - if (!$profile?.jwt || message == '') return - - loadingMessage = true - - try { - await getClient().createPrivateMessage({ - auth: $profile.jwt, - content: message, - recipient_id: data.person_view.person.id, - }) - - toast({ - content: 'Successfully sent that person a message.', - type: 'success', - }) - - messaging = false - } catch (err) { - toast({ - content: err as any, - type: 'error', - }) - } - - loadingMessage = false - } @@ -115,29 +86,10 @@ {#if $profile?.user} - -

Message

-
-

- Sending a message -

- - - -
+ {/if}