feat: rewrite report/inbox handling
This commit is contained in:
1 parent
d9ab0db31c
commit
30d5dc9bf2
13 files changed
+468
-429
No files matched your search
@@ -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())
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<script lang="ts">
|
||||
import { profile } from '$lib/auth.js'
|
||||
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
|
||||
import Markdown from '$lib/components/markdown/Markdown.svelte'
|
||||
import type { PrivateMessageView } from 'lemmy-js-client'
|
||||
import { Icon, PaperAirplane } from 'svelte-hero-icons'
|
||||
|
||||
export let message: PrivateMessageView
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col gap-2">
|
||||
<div class="flex flex-row gap-2 items-center flex-wrap">
|
||||
<span class="font-medium text-xs">From</span>
|
||||
<UserLink
|
||||
showInstance={false}
|
||||
user={message.creator}
|
||||
avatar
|
||||
avatarSize={20}
|
||||
shownBadges={{
|
||||
admin: true,
|
||||
banned: true,
|
||||
bot: true,
|
||||
you: $profile?.user?.local_user_view.person.id == message.creator.id,
|
||||
}}
|
||||
/>
|
||||
{#if $profile?.user?.local_user_view.person.id != message.recipient.id}
|
||||
to
|
||||
<UserLink
|
||||
showInstance={false}
|
||||
user={message.recipient}
|
||||
avatar
|
||||
avatarSize={20}
|
||||
shownBadges={{
|
||||
admin: true,
|
||||
banned: true,
|
||||
bot: true,
|
||||
you:
|
||||
$profile?.user?.local_user_view.person.id == message.recipient.id,
|
||||
}}
|
||||
/>
|
||||
{/if}
|
||||
</div>
|
||||
<Markdown source={message.private_message.content} />
|
||||
</div>
|
||||
@@ -0,0 +1,61 @@
|
||||
<script lang="ts">
|
||||
import { profile } from '$lib/auth.js'
|
||||
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
|
||||
import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import type { Person } from 'lemmy-js-client'
|
||||
import { Button, Modal, toast } from 'mono-svelte'
|
||||
|
||||
export let open: boolean = false
|
||||
export let user: Person
|
||||
|
||||
let message = ''
|
||||
|
||||
let loading = false
|
||||
|
||||
async function sendMessage() {
|
||||
if (!$profile?.jwt || message == '') return
|
||||
|
||||
loading = true
|
||||
|
||||
try {
|
||||
await getClient().createPrivateMessage({
|
||||
auth: $profile.jwt,
|
||||
content: message,
|
||||
recipient_id: user.id,
|
||||
})
|
||||
|
||||
toast({
|
||||
content: 'Successfully sent that person a message.',
|
||||
type: 'success',
|
||||
})
|
||||
|
||||
open = false
|
||||
} catch (err) {
|
||||
toast({
|
||||
content: err as any,
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
|
||||
loading = false
|
||||
}
|
||||
</script>
|
||||
|
||||
<Modal bind:open>
|
||||
<h1 class="text-2xl font-bold" slot="title">Message</h1>
|
||||
<form on:submit|preventDefault={sendMessage} class="flex flex-col gap-4">
|
||||
<p class="inline-flex flex-row gap-2 items-center">
|
||||
Sending <UserLink avatar {user} /> a message
|
||||
</p>
|
||||
<MarkdownEditor
|
||||
bind:value={message}
|
||||
label="Message"
|
||||
placeholder="your hair looks nice today"
|
||||
rows={4}
|
||||
/>
|
||||
<Button color="primary" size="lg" submit {loading} disabled={loading}>
|
||||
Send
|
||||
</Button>
|
||||
</form>
|
||||
</Modal>
|
||||
@@ -1,8 +1,10 @@
|
||||
<script lang="ts">
|
||||
import { profile } from '$lib/auth.js'
|
||||
import ShieldIcon from '$lib/components/lemmy/moderation/ShieldIcon.svelte'
|
||||
import Avatar from '$lib/components/ui/Avatar.svelte'
|
||||
import { userSettings } from '$lib/settings.js'
|
||||
import type { Person } from 'lemmy-js-client'
|
||||
import { Badge } from 'mono-svelte'
|
||||
import { Icon, NoSymbol } from 'svelte-hero-icons'
|
||||
|
||||
export let user: Person
|
||||
@@ -11,6 +13,12 @@
|
||||
export let badges: boolean = true
|
||||
export let inComment: boolean = false
|
||||
export let showInstance: boolean = false
|
||||
export let shownBadges = {
|
||||
admin: true,
|
||||
you: false,
|
||||
bot: true,
|
||||
banned: true,
|
||||
}
|
||||
|
||||
function linkFromCommunity(user: Person) {
|
||||
const domain = new URL(user.actor_id).hostname
|
||||
@@ -44,21 +52,24 @@
|
||||
</span>
|
||||
{/if}
|
||||
</span>
|
||||
{#if badges && (user.admin || user.banned || user.bot_account)}
|
||||
<span>
|
||||
{#if user.admin}
|
||||
<div class="text-red-500" title="Admin">
|
||||
<ShieldIcon width={12} filled />
|
||||
</div>
|
||||
{/if}
|
||||
{#if user.banned}
|
||||
<div class="text-red-500" title="Banned">
|
||||
<Icon src={NoSymbol} mini size="12" />
|
||||
</div>
|
||||
{/if}
|
||||
{#if user.bot_account}
|
||||
<div class="text-blue-500 font-bold" title="Bot">BOT</div>
|
||||
{/if}
|
||||
</span>
|
||||
{#if badges}
|
||||
{#if shownBadges.you}
|
||||
<Badge color="green-subtle" label="You" class="text-[10px] px-1 py-[1px]">
|
||||
You
|
||||
</Badge>
|
||||
{/if}
|
||||
{#if shownBadges.admin && user.admin}
|
||||
<div class="text-red-500" title="Admin">
|
||||
<ShieldIcon width={12} filled />
|
||||
</div>
|
||||
{/if}
|
||||
{#if shownBadges.banned && user.banned}
|
||||
<div class="text-red-500" title="Banned">
|
||||
<Icon src={NoSymbol} mini size="12" />
|
||||
</div>
|
||||
{/if}
|
||||
{#if shownBadges.bot && user.bot_account}
|
||||
<div class="text-blue-500 font-bold" title="Bot">BOT</div>
|
||||
{/if}
|
||||
{/if}
|
||||
</a>
|
||||
+52
-102
@@ -1,10 +1,12 @@
|
||||
import { publishedToDate } from '$lib/components/util/date.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import type { Result } from '$lib/lemmy/item.js'
|
||||
import type {
|
||||
Comment,
|
||||
CommentReplyView,
|
||||
CommentView,
|
||||
Person,
|
||||
PersonMentionView,
|
||||
PersonView,
|
||||
PrivateMessageView,
|
||||
} from 'lemmy-js-client'
|
||||
|
||||
@@ -13,110 +15,58 @@ export type InboxItemView =
|
||||
| CommentReplyView
|
||||
| PrivateMessageView
|
||||
|
||||
export function getInboxItemPublished(item: InboxItemView): string {
|
||||
if ('comment_reply' in item) {
|
||||
return item.comment_reply.published
|
||||
}
|
||||
export const generalizeCommentReply = (item: CommentReplyView): InboxItem => ({
|
||||
type: 'comment_reply',
|
||||
id: item.comment_reply.id,
|
||||
published: item.comment_reply.published,
|
||||
item: { ...item },
|
||||
creator: item.creator,
|
||||
read: item.comment_reply.read,
|
||||
})
|
||||
|
||||
if ('private_message' in item) {
|
||||
return item.private_message.published
|
||||
}
|
||||
export const generalizePersonMention = (
|
||||
item: PersonMentionView
|
||||
): InboxItem => ({
|
||||
type: 'person_mention',
|
||||
id: item.person_mention.id,
|
||||
published: item.person_mention.published,
|
||||
item: {
|
||||
...item,
|
||||
},
|
||||
creator: item.creator,
|
||||
read: item.person_mention.read,
|
||||
})
|
||||
|
||||
return item.person_mention.published
|
||||
export const generalizePrivateMessage = (
|
||||
item: PrivateMessageView
|
||||
): InboxItem => ({
|
||||
type: 'private_message',
|
||||
id: item.private_message.id,
|
||||
published: item.private_message.published,
|
||||
item: item,
|
||||
creator: item.creator,
|
||||
read: item.private_message.read,
|
||||
})
|
||||
|
||||
interface PrivateMessage {
|
||||
type: 'private_message'
|
||||
item: PrivateMessageView
|
||||
}
|
||||
interface CommentReply {
|
||||
type: 'comment_reply'
|
||||
item: CommentView
|
||||
}
|
||||
interface PersonMention {
|
||||
type: 'person_mention'
|
||||
item: CommentView
|
||||
}
|
||||
|
||||
export function isRead(
|
||||
item: PersonMentionView | CommentReplyView | PrivateMessageView
|
||||
) {
|
||||
if ('person_mention' in item) {
|
||||
return (item as PersonMentionView).person_mention.read
|
||||
}
|
||||
|
||||
if ('comment_reply' in item) {
|
||||
return (item as CommentReplyView).comment_reply.read
|
||||
}
|
||||
|
||||
if ('private_message' in item) {
|
||||
return (item as PrivateMessageView).private_message.read
|
||||
}
|
||||
|
||||
return false
|
||||
interface BaseInboxItem {
|
||||
creator: Person
|
||||
published: string
|
||||
read: boolean
|
||||
id: number
|
||||
}
|
||||
|
||||
export const getInbox = async (
|
||||
jwt: string,
|
||||
until?: number
|
||||
): Promise<InboxItem[]> => {
|
||||
const client = getClient()
|
||||
|
||||
const params = {
|
||||
limit: 50,
|
||||
auth: jwt,
|
||||
unread_only: true,
|
||||
}
|
||||
|
||||
const [replies, mentions, privateMessages] = await Promise.all([
|
||||
client.getReplies({
|
||||
...params,
|
||||
sort: 'New',
|
||||
}),
|
||||
client.getPersonMentions({
|
||||
...params,
|
||||
sort: 'New',
|
||||
}),
|
||||
client.getPrivateMessages({ ...params }),
|
||||
])
|
||||
|
||||
let items = [
|
||||
...replies.replies,
|
||||
...mentions.mentions,
|
||||
...privateMessages.private_messages,
|
||||
]
|
||||
.map(toInboxItem)
|
||||
.sort((a, b) => b.created - a.created)
|
||||
|
||||
if (until) {
|
||||
items = items.filter((item) => item.created > until)
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
const toInboxItem = (item: InboxItemView): InboxItem => {
|
||||
let type: 'private_message' | 'comment_reply' | 'person_mention'
|
||||
let body: string
|
||||
let personView: Person
|
||||
let created: number
|
||||
if ('comment_reply' in item) {
|
||||
type = 'comment_reply'
|
||||
body = item.comment.content
|
||||
personView = item.creator
|
||||
created = Date.parse(`${item.comment_reply.published}Z`)
|
||||
} else if ('person_mention' in item) {
|
||||
type = 'person_mention'
|
||||
body = item.comment.content
|
||||
personView = item.creator
|
||||
created = Date.parse(`${item.person_mention.published}Z`)
|
||||
} else if ('private_message' in item) {
|
||||
type = 'private_message'
|
||||
body = item.private_message.content
|
||||
personView = item.creator
|
||||
created = Date.parse(`${item.private_message.published}Z`)
|
||||
} else {
|
||||
throw new Error('Invalid item')
|
||||
}
|
||||
|
||||
return {
|
||||
type: type,
|
||||
body: body,
|
||||
person: personView,
|
||||
created,
|
||||
}
|
||||
}
|
||||
|
||||
interface InboxItem {
|
||||
type: 'private_message' | 'comment_reply' | 'person_mention'
|
||||
body: string
|
||||
person: Person
|
||||
created: number
|
||||
}
|
||||
export type InboxItem = BaseInboxItem &
|
||||
(PrivateMessage | CommentReply | PersonMention)
|
||||
@@ -0,0 +1,93 @@
|
||||
import { publishedToDate } from '$lib/components/util/date.js'
|
||||
import type {
|
||||
CommentReportView,
|
||||
CommentView,
|
||||
Person,
|
||||
PostReportView,
|
||||
PostView,
|
||||
PrivateMessage,
|
||||
PrivateMessageReportView,
|
||||
PrivateMessageView,
|
||||
} from 'lemmy-js-client'
|
||||
|
||||
interface CommentReport {
|
||||
type: 'comment'
|
||||
item: CommentView
|
||||
}
|
||||
|
||||
interface PostReport {
|
||||
type: 'post'
|
||||
item: PostView
|
||||
}
|
||||
|
||||
interface PrivateMessageReport {
|
||||
type: 'message'
|
||||
item: PrivateMessage
|
||||
reportee: Person
|
||||
}
|
||||
|
||||
interface BaseReport {
|
||||
creator: Person
|
||||
reason: string
|
||||
reportee?: Person
|
||||
timestamp: Date
|
||||
resolved: boolean
|
||||
id: number
|
||||
}
|
||||
|
||||
export const generalizeCommentReport = (
|
||||
report: CommentReportView
|
||||
): ReportView => ({
|
||||
creator: report.creator,
|
||||
item: {
|
||||
...report,
|
||||
subscribed: 'NotSubscribed',
|
||||
saved: false,
|
||||
creator_blocked: false,
|
||||
creator: report.comment_creator,
|
||||
},
|
||||
reason: report.comment_report.reason,
|
||||
type: 'comment',
|
||||
timestamp: publishedToDate(report.comment_report.published),
|
||||
resolved: report.comment_report.resolved,
|
||||
id: report.comment_report.id,
|
||||
})
|
||||
|
||||
export const generalizePostReport = (report: PostReportView): ReportView => ({
|
||||
creator: report.creator,
|
||||
item: {
|
||||
...report,
|
||||
unread_comments: 0,
|
||||
saved: false,
|
||||
creator_blocked: false,
|
||||
read: false,
|
||||
subscribed: 'NotSubscribed',
|
||||
creator: report.post_creator,
|
||||
},
|
||||
reason: report.post_report.reason,
|
||||
type: 'post',
|
||||
timestamp: publishedToDate(report.post_report.published),
|
||||
resolved: report.post_report.resolved,
|
||||
id: report.post_report.id,
|
||||
})
|
||||
|
||||
export const generalizePrivateMessageReport = (
|
||||
report: PrivateMessageReportView
|
||||
): ReportView => ({
|
||||
creator: report.creator,
|
||||
item: report.private_message,
|
||||
reason: report.private_message_report.reason,
|
||||
type: 'message',
|
||||
reportee: report.private_message_creator,
|
||||
timestamp: publishedToDate(report.private_message_report.published),
|
||||
resolved: report.private_message_report.resolved,
|
||||
id: report.private_message_report.id,
|
||||
})
|
||||
|
||||
export type Report =
|
||||
| CommentReportView
|
||||
| PostReportView
|
||||
| PrivateMessageReportView
|
||||
|
||||
export type ReportView = BaseReport &
|
||||
(CommentReport | PostReport | PrivateMessageReport)
|
||||
Reference in new issue
Block a user