misc: switch to universal vote function

This commit is contained in:
Xyphyn committed 2023-08-19 14:06:16 -07:00
1 parent 8915759031
commit 76b9864a3b
9 files changed
+134 -102

No files matched your search

@@ -12,7 +12,7 @@
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte' import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
import { Fire, Icon, ShieldExclamation, Trash } from 'svelte-hero-icons' import { Fire, Icon, ShieldExclamation, Trash } from 'svelte-hero-icons'
import { Color } from '$lib/ui/colors.js' import { Color } from '$lib/ui/colors.js'
import { isComment } from '$lib/lemmy/item.js' import { isCommentView } from '$lib/lemmy/item.js'
import { profile } from '$lib/auth.js' import { profile } from '$lib/auth.js'
import ShieldIcon from '$lib/components/lemmy/moderation/ShieldIcon.svelte' import ShieldIcon from '$lib/components/lemmy/moderation/ShieldIcon.svelte'
@@ -36,7 +36,7 @@
</li> </li>
<MenuButton color="success" on:click={() => remove(item)}> <MenuButton color="success" on:click={() => remove(item)}>
<Icon src={Trash} size="16" mini /> <Icon src={Trash} size="16" mini />
{#if isComment(item)} {#if isCommentView(item)}
{item.comment.removed ? 'Restore' : 'Remove'} {item.comment.removed ? 'Restore' : 'Remove'}
{:else} {:else}
{item.post.removed ? 'Restore' : 'Remove'} {item.post.removed ? 'Restore' : 'Remove'}
@@ -15,7 +15,7 @@
Trash, Trash,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import { Color } from '$lib/ui/colors.js' import { Color } from '$lib/ui/colors.js'
import { isComment, isPost } from '$lib/lemmy/item.js' import { isCommentView, isPostView } from '$lib/lemmy/item.js'
import { toast } from '$lib/components/ui/toasts/toasts.js' import { toast } from '$lib/components/ui/toasts/toasts.js'
import { profile } from '$lib/auth.js' import { profile } from '$lib/auth.js'
@@ -27,7 +27,7 @@
$: acting = locking || pinning $: acting = locking || pinning
async function lock(lock: boolean) { async function lock(lock: boolean) {
if (!$profile?.jwt || !isPost(item)) return if (!$profile?.jwt || !isPostView(item)) return
locking = true locking = true
try { try {
@@ -56,7 +56,7 @@
} }
async function pin(pinned: boolean, toInstance: boolean = false) { async function pin(pinned: boolean, toInstance: boolean = false) {
if (!$profile?.jwt || !isPost(item)) return if (!$profile?.jwt || !isPostView(item)) return
pinning = true pinning = true
@@ -133,7 +133,8 @@
<MenuButton <MenuButton
color="success" color="success"
on:click={() => pin(isPost(item) ? !item.post.featured_community : false)} on:click={() =>
pin(isPostView(item) ? !item.post.featured_community : false)}
loading={pinning} loading={pinning}
disabled={pinning} disabled={pinning}
> >
@@ -147,7 +148,7 @@
</MenuButton> </MenuButton>
<MenuButton color="dangerSecondary" on:click={() => remove(item)}> <MenuButton color="dangerSecondary" on:click={() => remove(item)}>
<Icon src={Trash} size="16" mini /> <Icon src={Trash} size="16" mini />
{#if isComment(item)} {#if isCommentView(item)}
{item.comment.removed ? 'Restore' : 'Remove'} {item.comment.removed ? 'Restore' : 'Remove'}
{:else} {:else}
{item.post.removed ? 'Restore' : 'Remove'} {item.post.removed ? 'Restore' : 'Remove'}
@@ -176,7 +177,7 @@
<MenuButton <MenuButton
color="success" color="success"
on:click={() => on:click={() =>
pin(isPost(item) ? !item.post.featured_local : false, true)} pin(isPostView(item) ? !item.post.featured_local : false, true)}
> >
<Icon src={InformationCircle} size="16" mini /> <Icon src={InformationCircle} size="16" mini />
<div class="flex flex-col text-left"> <div class="flex flex-col text-left">
@@ -6,7 +6,7 @@
import Modal from '$lib/components/ui/modal/Modal.svelte' import Modal from '$lib/components/ui/modal/Modal.svelte'
import { toast } from '$lib/components/ui/toasts/toasts.js' import { toast } from '$lib/components/ui/toasts/toasts.js'
import { getClient } from '$lib/lemmy.js' import { getClient } from '$lib/lemmy.js'
import { isComment, isPost } from '$lib/lemmy/item.js' import { isCommentView, isPostView } from '$lib/lemmy/item.js'
import type { CommentView, PostView } from 'lemmy-js-client' import type { CommentView, PostView } from 'lemmy-js-client'
import { profile } from '$lib/auth.js' import { profile } from '$lib/auth.js'
import Checkbox from '$lib/components/input/Checkbox.svelte' import Checkbox from '$lib/components/input/Checkbox.svelte'
@@ -27,7 +27,7 @@
let loading = false let loading = false
$: removed = item $: removed = item
? isComment(item) ? isCommentView(item)
? item.comment.removed ? item.comment.removed
: item.post.removed : item.post.removed
: false : false
@@ -56,7 +56,7 @@
try { try {
if (purge) { if (purge) {
if (isComment(item)) { if (isCommentView(item)) {
await getClient(undefined, fetch).purgeComment({ await getClient(undefined, fetch).purgeComment({
auth: $profile.jwt, auth: $profile.jwt,
comment_id: item.comment.id, comment_id: item.comment.id,
@@ -94,7 +94,7 @@
.createPrivateMessage({ .createPrivateMessage({
auth: $profile.jwt, auth: $profile.jwt,
content: replyReason, content: replyReason,
recipient_id: isComment(item) recipient_id: isCommentView(item)
? item.comment.creator_id ? item.comment.creator_id
: item.post.creator_id, : item.post.creator_id,
}) })
@@ -110,7 +110,7 @@
auth: $profile.jwt, auth: $profile.jwt,
content: replyReason, content: replyReason,
post_id: item.post.id, post_id: item.post.id,
parent_id: isComment(item) ? item.comment.id : undefined, parent_id: isCommentView(item) ? item.comment.id : undefined,
}) })
.catch(() => { .catch(() => {
toast({ toast({
@@ -121,7 +121,7 @@
} }
} }
if (isComment(item)) { if (isCommentView(item)) {
await getClient().removeComment({ await getClient().removeComment({
auth: $profile.jwt, auth: $profile.jwt,
comment_id: item.comment.id, comment_id: item.comment.id,
@@ -129,7 +129,7 @@
reason: reason || undefined, reason: reason || undefined,
}) })
item.comment.removed = !removed item.comment.removed = !removed
} else if (isPost(item)) { } else if (isPostView(item)) {
await getClient().removePost({ await getClient().removePost({
auth: $profile.jwt, auth: $profile.jwt,
post_id: item.post.id, post_id: item.post.id,
@@ -180,7 +180,7 @@
on:submit|preventDefault={remove} on:submit|preventDefault={remove}
class="flex flex-col gap-4 list-none" class="flex flex-col gap-4 list-none"
> >
{#if isComment(item)} {#if isCommentView(item)}
<Comment <Comment
node={{ node={{
children: [], children: [],
@@ -190,7 +190,7 @@
postId={item.post.id} postId={item.post.id}
actions={false} actions={false}
/> />
{:else if isPost(item)} {:else if isPostView(item)}
<Post actions={false} post={item} /> <Post actions={false} post={item} />
{/if} {/if}
@@ -1,15 +1,15 @@
import type { Submission } from '$lib/lemmy/contentview.js' import type { SubmissionView } from '$lib/lemmy/contentview.js'
import type { Community, MyUserInfo, Person } from 'lemmy-js-client' import type { Community, MyUserInfo, Person } from 'lemmy-js-client'
import { writable } from 'svelte/store' import { writable } from 'svelte/store'
interface Modals { interface Modals {
reporting: { reporting: {
open: boolean open: boolean
item: Submission | undefined item: SubmissionView | undefined
} }
removing: { removing: {
open: boolean open: boolean
item: Submission | undefined item: SubmissionView | undefined
purge: boolean purge: boolean
} }
banning: { banning: {
@@ -38,7 +38,7 @@ export let modals = writable<Modals>({
}, },
}) })
export function report(item: Submission) { export function report(item: SubmissionView) {
modals.update((m) => ({ modals.update((m) => ({
...m, ...m,
reporting: { reporting: {
@@ -48,7 +48,7 @@ export function report(item: Submission) {
})) }))
} }
export function remove(item: Submission, purge: boolean = false) { export function remove(item: SubmissionView, purge: boolean = false) {
modals.update((m) => ({ modals.update((m) => ({
...m, ...m,
removing: { removing: {
+22 -58
View File
@@ -13,64 +13,12 @@
Icon, Icon,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import { profile } from '$lib/auth.js' import { profile } from '$lib/auth.js'
import { slide } from 'svelte/transition' import { vote as voteItem } from '$lib/lemmy/contentview.js'
export let post: Post export let post: Post
export let vote: number = 0 export let vote: number = 0
export let score: number export let score: number
async function upvote() {
if (!$profile?.jwt) return
if (vote == undefined) vote = 0
const upvoted = vote == 1
if (vote == -1) {
score += 2
} else if (vote == 1) {
score -= 1
} else if (vote == 0) {
score += 1
}
vote = Number(!upvoted)
await getClient()
.likePost({
score: upvoted ? 0 : 1,
auth: $profile.jwt,
post_id: post.id,
})
.catch((_) => undefined)
}
async function downvote() {
if (!$profile?.jwt) return
if (vote == undefined) vote = 0
const upvoted = vote == -1
if (vote == -1) {
score += 1
} else if (vote == 1) {
score -= 2
} else if (vote == 0) {
score -= 1
}
vote = -Number(!upvoted)
await getClient()
.likePost({
score: upvoted ? 0 : -1,
auth: $profile.jwt,
post_id: post.id,
})
.catch((_) => undefined)
}
const voteColor = (vote: number) => const voteColor = (vote: number) =>
vote == 1 vote == 1
? $userSettings.revertColors ? $userSettings.revertColors
@@ -83,13 +31,17 @@
: '' : ''
</script> </script>
<slot {upvote} {downvote} {vote} {score}> <slot {vote} {score}>
{#if $userSettings.newVote} {#if $userSettings.newVote}
<div class="flex items-center text-sm rounded-md border-zinc-700"> <div class="flex items-center text-sm rounded-md border-zinc-700">
<Button <Button
aria-label="Upvote" aria-label="Upvote"
class={vote == 1 ? voteColor(vote) : ''} class={vote == 1 ? voteColor(vote) : ''}
on:click={upvote} on:click={async () => {
if (!$profile?.jwt) return
score = await voteItem(post, vote == 1 ? 0 : 1, $profile.jwt)
vote = vote == 1 ? 0 : 1
}}
size="square-md" size="square-md"
color="tertiary" color="tertiary"
alignment="center" alignment="center"
@@ -102,7 +54,11 @@
<Button <Button
aria-label="Downvote" aria-label="Downvote"
class={vote == -1 ? voteColor(vote) : ''} class={vote == -1 ? voteColor(vote) : ''}
on:click={downvote} on:click={async () => {
if (!$profile?.jwt) return
score = await voteItem(post, vote == -1 ? 0 : -1, $profile.jwt)
vote = vote == -1 ? 0 : -1
}}
size="square-md" size="square-md"
color="tertiary" color="tertiary"
> >
@@ -117,7 +73,11 @@
<Button <Button
aria-label="Upvote" aria-label="Upvote"
class={vote == 1 ? voteColor(vote) : ''} class={vote == 1 ? voteColor(vote) : ''}
on:click={upvote} on:click={async () => {
if (!$profile?.jwt) return
score = await voteItem(post, vote == 1 ? 0 : 1, $profile.jwt)
vote = vote == 1 ? 0 : 1
}}
size="square-sm" size="square-sm"
color="tertiary" color="tertiary"
alignment="center" alignment="center"
@@ -132,7 +92,11 @@
<Button <Button
aria-label="Downvote" aria-label="Downvote"
class={vote == -1 ? voteColor(vote) : ''} class={vote == -1 ? voteColor(vote) : ''}
on:click={downvote} on:click={async () => {
if (!$profile?.jwt) return
score = await voteItem(post, vote == -1 ? 0 : -1, $profile.jwt)
vote = vote == -1 ? 0 : -1
}}
size="square-sm" size="square-sm"
color="tertiary" color="tertiary"
> >
+65 -11
View File
@@ -1,22 +1,33 @@
import { getClient } from '$lib/lemmy.js' import { getClient } from '$lib/lemmy.js'
import { isComment, isPost } from '$lib/lemmy/item.js' import { isComment, isCommentView, isPostView } from '$lib/lemmy/item.js'
import type { CommentView, Person, PostView } from 'lemmy-js-client' import type {
CommentView,
Person,
PostView,
Comment,
Post,
} from 'lemmy-js-client'
export type Submission = PostView | CommentView export type SubmissionView = PostView | CommentView
export type Submission = Post | Comment
export interface ContentView { export interface ContentView {
type: 'post' | 'comment' type: 'post' | 'comment'
title?: string title?: string
body: string body: string
creator: Person creator?: Person
id: number id: number
} }
const isSubmissionView = (
item: SubmissionView | ContentView
): item is SubmissionView => !('type' in item)
const isSubmission = (item: Submission | ContentView): item is Submission => const isSubmission = (item: Submission | ContentView): item is Submission =>
!('type' in item) !('type' in item)
export const contentView = (item: Submission): ContentView => { export const contentView = (item: SubmissionView): ContentView => {
if (isComment(item)) if (isCommentView(item))
return { return {
type: 'comment', type: 'comment',
body: item.comment.content, body: item.comment.content,
@@ -33,12 +44,28 @@ export const contentView = (item: Submission): ContentView => {
} }
} }
export const contentItem = (item: Submission): ContentView => {
if (isComment(item))
return {
type: 'comment',
body: item.content,
id: item.id,
}
else
return {
type: 'post',
body: item.body ?? item.name,
title: item.name,
id: item.id,
}
}
export async function save( export async function save(
item: ContentView | Submission, item: ContentView | SubmissionView,
save: boolean, save: boolean,
jwt: string jwt: string
): Promise<boolean> { ): Promise<boolean> {
if (isSubmission(item)) item = contentView(item) if (isSubmissionView(item)) item = contentView(item)
if (item.type == 'post') { if (item.type == 'post') {
return ( return (
@@ -61,11 +88,11 @@ export async function save(
} }
export async function deleteItem( export async function deleteItem(
item: ContentView | Submission, item: ContentView | SubmissionView,
deleted: boolean, deleted: boolean,
jwt: string jwt: string
): Promise<boolean> { ): Promise<boolean> {
if (isSubmission(item)) item = contentView(item) if (isSubmissionView(item)) item = contentView(item)
if (item.type == 'post') { if (item.type == 'post') {
return ( return (
@@ -82,7 +109,34 @@ export async function deleteItem(
comment_id: item.id, comment_id: item.id,
deleted: deleted, deleted: deleted,
}) })
).comment_view.post.deleted ).comment_view.comment.deleted
} }
return deleted return deleted
} }
export async function vote(
item: ContentView | Submission,
vote: number,
jwt: string
): Promise<number> {
if (isSubmission(item)) item = contentItem(item)
if (item.type == 'post') {
return (
await getClient().likePost({
auth: jwt,
post_id: item.id,
score: vote,
})
).post_view.counts.score
} else if (item.type == 'comment') {
return (
await getClient().likeComment({
auth: jwt,
comment_id: item.id,
score: vote,
})
).comment_view.counts.score
}
return 0
}
+12 -4
View File
@@ -2,17 +2,20 @@ import type {
CommentReportView, CommentReportView,
CommentView, CommentView,
CommunityView, CommunityView,
Comment,
PersonView, PersonView,
PostReportView, PostReportView,
PostView, PostView,
PrivateMessage, Post,
PrivateMessageReportView, PrivateMessageReportView,
PrivateMessageView, PrivateMessageView,
} from 'lemmy-js-client' } from 'lemmy-js-client'
export type Result = export type Result =
| PostView | PostView
| Post
| CommentView | CommentView
| Comment
| PersonView | PersonView
| CommunityView | CommunityView
| PrivateMessageView | PrivateMessageView
@@ -38,13 +41,18 @@ export function getItemPublished(item: Result) {
return '' return ''
} }
export const isPost = (item: Result): item is PostView => export const isPostView = (item: Result): item is PostView =>
'post' in item && !('comment' in item) 'post' in item && !('comment' in item)
export const isComment = (item: Result): item is CommentView => export const isPost = (item: Result): item is Post =>
'name' in item && !('content' in item)
export const isCommentView = (item: Result): item is CommentView =>
'comment' in item 'comment' in item
export const isCommunity = (item: Result): item is CommunityView => export const isComment = (item: Result): item is Comment => 'content' in item
export const isCommunityView = (item: Result): item is CommunityView =>
'community' in item 'community' in item
export const isUser = (item: Result): item is PersonView => 'person' in item export const isUser = (item: Result): item is PersonView => 'person' in item
+9 -4
View File
@@ -11,7 +11,12 @@
import UserItem from '$lib/components/lemmy/user/UserItem.svelte' import UserItem from '$lib/components/lemmy/user/UserItem.svelte'
import Pageination from '$lib/components/ui/Pageination.svelte' import Pageination from '$lib/components/ui/Pageination.svelte'
import Spinner from '$lib/components/ui/loader/Spinner.svelte' import Spinner from '$lib/components/ui/loader/Spinner.svelte'
import { isComment, isCommunity, isPost, isUser } from '$lib/lemmy/item.js' import {
isCommentView,
isCommunityView,
isPostView,
isUser,
} from '$lib/lemmy/item.js'
import { searchParam } from '$lib/util.js' import { searchParam } from '$lib/util.js'
import type { import type {
CommentView, CommentView,
@@ -112,11 +117,11 @@
{/await} {/await}
<div class="flex flex-col gap-4 mt-4"> <div class="flex flex-col gap-4 mt-4">
{#each data.results as result} {#each data.results as result}
{#if isPost(result)} {#if isPostView(result)}
<Post post={result} /> <Post post={result} />
{:else if isComment(result)} {:else if isCommentView(result)}
<CommentItem comment={result} /> <CommentItem comment={result} />
{:else if isCommunity(result)} {:else if isCommunityView(result)}
<CommunityItem community={result} /> <CommunityItem community={result} />
{:else if isUser(result)} {:else if isUser(result)}
<UserItem user={result} /> <UserItem user={result} />
+3 -3
View File
@@ -20,7 +20,7 @@
import Pageination from '$lib/components/ui/Pageination.svelte' import Pageination from '$lib/components/ui/Pageination.svelte'
import { page } from '$app/stores' import { page } from '$app/stores'
import { goto } from '$app/navigation' import { goto } from '$app/navigation'
import { isComment } from '$lib/lemmy/item.js' import { isCommentView } from '$lib/lemmy/item.js'
import { getClient } from '$lib/lemmy.js' import { getClient } from '$lib/lemmy.js'
import { isBlocked } from '$lib/lemmy/user.js' import { isBlocked } from '$lib/lemmy/user.js'
import MultiSelect from '$lib/components/input/MultiSelect.svelte' import MultiSelect from '$lib/components/input/MultiSelect.svelte'
@@ -165,9 +165,9 @@
/> />
</div> </div>
{#each data.items as item (item.counts.id)} {#each data.items as item (item.counts.id)}
{#if isComment(item) && (data.type == 'all' || data.type == 'comments')} {#if isCommentView(item) && (data.type == 'all' || data.type == 'comments')}
<CommentItem comment={item} /> <CommentItem comment={item} />
{:else if !isComment(item) && (data.type == 'all' || data.type == 'posts')} {:else if !isCommentView(item) && (data.type == 'all' || data.type == 'posts')}
<Post post={item} /> <Post post={item} />
{/if} {/if}
{/each} {/each}