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