reset drizzle, get DMs working (partly, usernames aren't resolved)
This commit is contained in:
parent
3a0f096ade
commit
bf679f9ee0
32 changed files with 1150 additions and 2867 deletions
|
|
@ -1,8 +1,5 @@
|
|||
import type { Handle } from '@sveltejs/kit';
|
||||
import * as auth from '$lib/server/auth';
|
||||
import { kvStore } from '$lib/server/db';
|
||||
import { _isUserConnected, _sendToSubscribers } from './routes/api/updates/+server';
|
||||
import { Status } from '$lib';
|
||||
|
||||
const handleAuth: Handle = async ({ event, resolve }) => {
|
||||
const sessionToken = event.cookies.get(auth.sessionCookieName);
|
||||
|
|
|
|||
|
|
@ -1,312 +1,311 @@
|
|||
<script lang="ts">
|
||||
import { type Data, type OverviewUser } from '$lib';
|
||||
import * as Collapsible from '$lib/components/ui/collapsible/index.js';
|
||||
import * as Sidebar from '$lib/components/ui/sidebar/index.js';
|
||||
import * as Dialog from '$lib/components/ui/dialog/index.js';
|
||||
import * as Tabs from '$lib/components/ui/tabs/index.js';
|
||||
import * as Card from '$lib/components/ui/card/index.js';
|
||||
import MessagesSquare from '@lucide/svelte/icons/messages-square';
|
||||
import MinusIcon from '@lucide/svelte/icons/minus';
|
||||
import PlusIcon from '@lucide/svelte/icons/plus';
|
||||
import UserRoundPlus from '@lucide/svelte/icons/user-round-plus';
|
||||
import UsersRound from '@lucide/svelte/icons/users-round';
|
||||
import CirclePlus from '@lucide/svelte/icons/circle-plus';
|
||||
import Input from './ui/input/input.svelte';
|
||||
import Button, { buttonVariants } from './ui/button/button.svelte';
|
||||
import User from './extra/User.svelte';
|
||||
import type { SessionValidationResult } from '$lib/server/auth';
|
||||
import * as Collapsible from '$lib/components/ui/collapsible/index.js';
|
||||
import * as Sidebar from '$lib/components/ui/sidebar/index.js';
|
||||
import * as Dialog from '$lib/components/ui/dialog/index.js';
|
||||
import * as Tabs from '$lib/components/ui/tabs/index.js';
|
||||
import * as Card from '$lib/components/ui/card/index.js';
|
||||
import MessagesSquare from '@lucide/svelte/icons/messages-square';
|
||||
import MinusIcon from '@lucide/svelte/icons/minus';
|
||||
import PlusIcon from '@lucide/svelte/icons/plus';
|
||||
import UserRoundPlus from '@lucide/svelte/icons/user-round-plus';
|
||||
import UsersRound from '@lucide/svelte/icons/users-round';
|
||||
import CirclePlus from '@lucide/svelte/icons/circle-plus';
|
||||
import Input from './ui/input/input.svelte';
|
||||
import Button, { buttonVariants } from './ui/button/button.svelte';
|
||||
import User from './extra/User.svelte';
|
||||
import type { SessionValidationResult } from '$lib/server/auth';
|
||||
|
||||
let {
|
||||
currentPage = $bindable<string | null>(),
|
||||
data,
|
||||
user,
|
||||
...restProps
|
||||
}: { currentPage: string | null; data: Data; user: SessionValidationResult['user'] } = $props();
|
||||
let {
|
||||
currentPage = $bindable<string | null>(),
|
||||
data,
|
||||
user,
|
||||
...restProps
|
||||
}: { currentPage: string | null; data: Data; user: SessionValidationResult['user'] } = $props();
|
||||
</script>
|
||||
|
||||
<Sidebar.Root {...restProps}>
|
||||
<Sidebar.Header>
|
||||
<Sidebar.Menu>
|
||||
<Sidebar.MenuItem>
|
||||
<Sidebar.MenuButton size="lg">
|
||||
<div
|
||||
class="flex aspect-square size-8 items-center justify-center rounded-lg bg-sidebar-primary text-sidebar-primary-foreground"
|
||||
>
|
||||
<MessagesSquare class="size-4" />
|
||||
</div>
|
||||
<div class="flex flex-col gap-0.5 leading-none">
|
||||
<span class="font-medium">chat.sad.ovh</span>
|
||||
</div>
|
||||
</Sidebar.MenuButton>
|
||||
<div class="flex w-full justify-center gap-2">
|
||||
<Dialog.Root>
|
||||
<Dialog.Trigger>
|
||||
<Button
|
||||
variant={user!.friendRequests.length > 0 ? 'destructive' : 'outline'}
|
||||
size="icon"
|
||||
>
|
||||
<UserRoundPlus />
|
||||
</Button>
|
||||
</Dialog.Trigger>
|
||||
<Sidebar.Header>
|
||||
<Sidebar.Menu>
|
||||
<Sidebar.MenuItem>
|
||||
<Sidebar.MenuButton size="lg">
|
||||
<div
|
||||
class="flex aspect-square size-8 items-center justify-center rounded-lg bg-sidebar-primary text-sidebar-primary-foreground"
|
||||
>
|
||||
<MessagesSquare class="size-4" />
|
||||
</div>
|
||||
<div class="flex flex-col gap-0.5 leading-none">
|
||||
<span class="font-medium">chat.sad.ovh</span>
|
||||
</div>
|
||||
</Sidebar.MenuButton>
|
||||
<div class="flex w-full justify-center gap-2">
|
||||
<Dialog.Root>
|
||||
<Dialog.Trigger>
|
||||
<Button
|
||||
variant={user!.friendRequests.length > 0 ? 'destructive' : 'outline'}
|
||||
size="icon"
|
||||
>
|
||||
<UserRoundPlus />
|
||||
</Button>
|
||||
</Dialog.Trigger>
|
||||
|
||||
<Dialog.Content class="sm:max-w-[425px]">
|
||||
<Dialog.Header>
|
||||
<Dialog.Title>Add a friend</Dialog.Title>
|
||||
<Dialog.Description>
|
||||
Add a friend using their username or manage pending requests.
|
||||
</Dialog.Description>
|
||||
</Dialog.Header>
|
||||
<Dialog.Content class="sm:max-w-[425px]">
|
||||
<Dialog.Header>
|
||||
<Dialog.Title>Add a friend</Dialog.Title>
|
||||
<Dialog.Description>
|
||||
Add a friend using their username or manage pending requests.
|
||||
</Dialog.Description>
|
||||
</Dialog.Header>
|
||||
|
||||
<!-- input to add a new friend -->
|
||||
<form method="POST" action="?/addFriend" class="mb-4">
|
||||
<Input name="username" placeholder="username" required class="mb-2" />
|
||||
<Dialog.Footer>
|
||||
<Dialog.Close class={buttonVariants({ variant: 'outline' })}>Cancel</Dialog.Close>
|
||||
<Button type="submit">Send request</Button>
|
||||
</Dialog.Footer>
|
||||
</form>
|
||||
<!-- input to add a new friend -->
|
||||
<form method="POST" action="?/addFriend" class="mb-4">
|
||||
<Input name="username" placeholder="username" required class="mb-2" />
|
||||
<Dialog.Footer>
|
||||
<Dialog.Close class={buttonVariants({ variant: 'outline' })}>Cancel</Dialog.Close>
|
||||
<Button type="submit">Send request</Button>
|
||||
</Dialog.Footer>
|
||||
</form>
|
||||
|
||||
<!-- Tabs for Friend Requests -->
|
||||
<Tabs.Root value="outgoing">
|
||||
<Tabs.List>
|
||||
<Tabs.Trigger value="outgoing">Outgoing</Tabs.Trigger>
|
||||
<Tabs.Trigger value="incoming">Incoming</Tabs.Trigger>
|
||||
</Tabs.List>
|
||||
<!-- Tabs for Friend Requests -->
|
||||
<Tabs.Root value="outgoing">
|
||||
<Tabs.List>
|
||||
<Tabs.Trigger value="outgoing">Outgoing</Tabs.Trigger>
|
||||
<Tabs.Trigger value="incoming">Incoming</Tabs.Trigger>
|
||||
</Tabs.List>
|
||||
|
||||
<!-- Outgoing Requests -->
|
||||
<Tabs.Content value="outgoing">
|
||||
{#if user.friendRequests.filter((r) => r.fromUser === user.id).length === 0}
|
||||
<p class="text-sm text-muted-foreground">No outgoing requests</p>
|
||||
{:else}
|
||||
{#each user.friendRequests.filter((r) => r.fromUser === user.id) as request (request.id)}
|
||||
<Card.Root class="mb-2">
|
||||
<Card.Header>
|
||||
<Card.Title>{request.username}</Card.Title>
|
||||
<Card.Description>Request sent</Card.Description>
|
||||
</Card.Header>
|
||||
<Card.Footer>
|
||||
<form method="POST" action="?/cancelFriendRequest">
|
||||
<input type="hidden" name="requestId" value={request.id} />
|
||||
<Button type="submit" variant="outline" size="sm">Cancel</Button>
|
||||
</form>
|
||||
</Card.Footer>
|
||||
</Card.Root>
|
||||
{/each}
|
||||
{/if}
|
||||
</Tabs.Content>
|
||||
<!-- Outgoing Requests -->
|
||||
<Tabs.Content value="outgoing">
|
||||
{#if user!.friendRequests.filter((r) => r.fromUser === user!.id).length === 0}
|
||||
<p class="text-sm text-muted-foreground">No outgoing requests</p>
|
||||
{:else}
|
||||
{#each user!.friendRequests.filter((r) => r.fromUser === user!.id) as request (request.id)}
|
||||
<Card.Root class="mb-2">
|
||||
<Card.Header>
|
||||
<Card.Title>{request.toUsername}</Card.Title>
|
||||
<Card.Description>Request sent</Card.Description>
|
||||
</Card.Header>
|
||||
<Card.Footer>
|
||||
<form method="POST" action="?/cancelFriendRequest">
|
||||
<input type="hidden" name="requestId" value={request.id} />
|
||||
<Button type="submit" variant="outline" size="sm">Cancel</Button>
|
||||
</form>
|
||||
</Card.Footer>
|
||||
</Card.Root>
|
||||
{/each}
|
||||
{/if}
|
||||
</Tabs.Content>
|
||||
|
||||
<!-- Incoming Requests -->
|
||||
<Tabs.Content value="incoming">
|
||||
{#if user.friendRequests.filter((r) => r.toUser === user.id).length === 0}
|
||||
<p class="text-sm text-muted-foreground">No incoming requests</p>
|
||||
{:else}
|
||||
{#each user.friendRequests.filter((r) => r.toUser === user.id) as request (request.id)}
|
||||
<Card.Root class="mb-2">
|
||||
<Card.Header>
|
||||
<Card.Title>{request.username}</Card.Title>
|
||||
<Card.Description>Sent you a friend request</Card.Description>
|
||||
</Card.Header>
|
||||
<Card.Footer class="flex gap-2">
|
||||
<!-- accept friend -->
|
||||
<form method="POST" action="?/addFriend">
|
||||
<input type="hidden" name="userId" value={request.fromUser} />
|
||||
<Button type="submit" size="sm">Accept</Button>
|
||||
</form>
|
||||
<!-- decline friend -->
|
||||
<form method="POST" action="?/cancelFriendRequest">
|
||||
<input type="hidden" name="requestId" value={request.id} />
|
||||
<Button type="submit" variant="outline" size="sm">Decline</Button>
|
||||
</form>
|
||||
</Card.Footer>
|
||||
</Card.Root>
|
||||
{/each}
|
||||
{/if}
|
||||
</Tabs.Content>
|
||||
</Tabs.Root>
|
||||
</Dialog.Content>
|
||||
</Dialog.Root>
|
||||
<!-- Incoming Requests -->
|
||||
<Tabs.Content value="incoming">
|
||||
{#if user!.friendRequests.filter((r) => r.toUser === user!.id).length === 0}
|
||||
<p class="text-sm text-muted-foreground">No incoming requests</p>
|
||||
{:else}
|
||||
{#each user!.friendRequests.filter((r) => r.toUser === user!.id) as request (request.id)}
|
||||
<Card.Root class="mb-2">
|
||||
<Card.Header>
|
||||
<Card.Title>{request.fromUsername}</Card.Title>
|
||||
<Card.Description>Sent you a friend request</Card.Description>
|
||||
</Card.Header>
|
||||
<Card.Footer class="flex gap-2">
|
||||
<!-- accept friend -->
|
||||
<form method="POST" action="?/addFriend">
|
||||
<input type="hidden" name="userId" value={request.fromUser} />
|
||||
<Button type="submit" size="sm">Accept</Button>
|
||||
</form>
|
||||
<!-- decline friend -->
|
||||
<form method="POST" action="?/cancelFriendRequest">
|
||||
<input type="hidden" name="requestId" value={request.id} />
|
||||
<Button type="submit" variant="outline" size="sm">Decline</Button>
|
||||
</form>
|
||||
</Card.Footer>
|
||||
</Card.Root>
|
||||
{/each}
|
||||
{/if}
|
||||
</Tabs.Content>
|
||||
</Tabs.Root>
|
||||
</Dialog.Content>
|
||||
</Dialog.Root>
|
||||
|
||||
<Dialog.Root>
|
||||
<Dialog.Trigger>
|
||||
<Button variant="outline" size="icon">
|
||||
<UsersRound />
|
||||
</Button>
|
||||
</Dialog.Trigger>
|
||||
<Dialog.Root>
|
||||
<Dialog.Trigger>
|
||||
<Button variant="outline" size="icon">
|
||||
<UsersRound />
|
||||
</Button>
|
||||
</Dialog.Trigger>
|
||||
|
||||
<Dialog.Content class="sm:max-w-[425px]">
|
||||
<form method="POST" action="?/createGroup">
|
||||
<Dialog.Header>
|
||||
<Dialog.Title>Create a group</Dialog.Title>
|
||||
<Dialog.Description>Add friends into your group!</Dialog.Description>
|
||||
</Dialog.Header>
|
||||
<Dialog.Content class="sm:max-w-[425px]">
|
||||
<form method="POST" action="?/createGroup">
|
||||
<Dialog.Header>
|
||||
<Dialog.Title>Create a group</Dialog.Title>
|
||||
<Dialog.Description>Add friends into your group!</Dialog.Description>
|
||||
</Dialog.Header>
|
||||
|
||||
{#each data.friends as friend (friend.id)}
|
||||
<label class="flex items-center gap-2">
|
||||
<input type="checkbox" name="member" value={friend.id} />
|
||||
<User user={friend} />
|
||||
</label>
|
||||
{/each}
|
||||
{#each data.friends as friend (friend.id)}
|
||||
<label class="flex items-center gap-2">
|
||||
<input type="checkbox" name="member" value={friend.id} />
|
||||
<User user={friend} />
|
||||
</label>
|
||||
{/each}
|
||||
|
||||
<Dialog.Footer>
|
||||
<Dialog.Close class={buttonVariants({ variant: 'outline' })}>Cancel</Dialog.Close>
|
||||
<Button type="submit">Create group</Button>
|
||||
</Dialog.Footer>
|
||||
</form>
|
||||
</Dialog.Content>
|
||||
</Dialog.Root>
|
||||
<Dialog.Footer>
|
||||
<Dialog.Close class={buttonVariants({ variant: 'outline' })}>Cancel</Dialog.Close>
|
||||
<Button type="submit">Create group</Button>
|
||||
</Dialog.Footer>
|
||||
</form>
|
||||
</Dialog.Content>
|
||||
</Dialog.Root>
|
||||
|
||||
<Dialog.Root>
|
||||
<Dialog.Trigger>
|
||||
<Button variant="outline" size="icon">
|
||||
<CirclePlus />
|
||||
</Button>
|
||||
</Dialog.Trigger>
|
||||
<Dialog.Root>
|
||||
<Dialog.Trigger>
|
||||
<Button variant="outline" size="icon">
|
||||
<CirclePlus />
|
||||
</Button>
|
||||
</Dialog.Trigger>
|
||||
|
||||
<Dialog.Content class="sm:max-w-[425px]">
|
||||
<form method="POST" action="?/joinServer">
|
||||
<Dialog.Header>
|
||||
<Dialog.Title>Join a server</Dialog.Title>
|
||||
<Dialog.Description>Enter an invite link.</Dialog.Description>
|
||||
</Dialog.Header>
|
||||
<Dialog.Content class="sm:max-w-[425px]">
|
||||
<form method="POST" action="?/joinServer">
|
||||
<Dialog.Header>
|
||||
<Dialog.Title>Join a server</Dialog.Title>
|
||||
<Dialog.Description>Enter an invite link.</Dialog.Description>
|
||||
</Dialog.Header>
|
||||
|
||||
<Input name="invite" placeholder="invite link" required />
|
||||
<Input name="invite" placeholder="invite link" required />
|
||||
|
||||
<Dialog.Footer>
|
||||
<Dialog.Close class={buttonVariants({ variant: 'outline' })}>Cancel</Dialog.Close>
|
||||
<Button type="submit">Join</Button>
|
||||
</Dialog.Footer>
|
||||
</form>
|
||||
</Dialog.Content>
|
||||
</Dialog.Root>
|
||||
<Dialog.Footer>
|
||||
<Dialog.Close class={buttonVariants({ variant: 'outline' })}>Cancel</Dialog.Close>
|
||||
<Button type="submit">Join</Button>
|
||||
</Dialog.Footer>
|
||||
</form>
|
||||
</Dialog.Content>
|
||||
</Dialog.Root>
|
||||
|
||||
<Dialog.Root>
|
||||
<Dialog.Trigger>
|
||||
<Button variant="outline" size="icon">
|
||||
<PlusIcon />
|
||||
</Button>
|
||||
</Dialog.Trigger>
|
||||
<Dialog.Root>
|
||||
<Dialog.Trigger>
|
||||
<Button variant="outline" size="icon">
|
||||
<PlusIcon />
|
||||
</Button>
|
||||
</Dialog.Trigger>
|
||||
|
||||
<Dialog.Content class="sm:max-w-[425px]">
|
||||
<form method="POST" action="?/createServer">
|
||||
<Dialog.Header>
|
||||
<Dialog.Title>Create a server</Dialog.Title>
|
||||
<Dialog.Description>Name your new server.</Dialog.Description>
|
||||
</Dialog.Header>
|
||||
<Dialog.Content class="sm:max-w-[425px]">
|
||||
<form method="POST" action="?/createServer">
|
||||
<Dialog.Header>
|
||||
<Dialog.Title>Create a server</Dialog.Title>
|
||||
<Dialog.Description>Name your new server.</Dialog.Description>
|
||||
</Dialog.Header>
|
||||
|
||||
<Input name="name" placeholder="Server name" required />
|
||||
<Input name="name" placeholder="Server name" required />
|
||||
|
||||
<Dialog.Footer>
|
||||
<Dialog.Close class={buttonVariants({ variant: 'outline' })}>Cancel</Dialog.Close>
|
||||
<Button type="submit">Create</Button>
|
||||
</Dialog.Footer>
|
||||
</form>
|
||||
</Dialog.Content>
|
||||
</Dialog.Root>
|
||||
</div>
|
||||
</Sidebar.MenuItem>
|
||||
</Sidebar.Menu>
|
||||
</Sidebar.Header>
|
||||
<Sidebar.Content>
|
||||
<Sidebar.Group>
|
||||
<Sidebar.Menu>
|
||||
<Collapsible.Root open={true} class="group/collapsible">
|
||||
<Sidebar.MenuItem>
|
||||
<Collapsible.Trigger>
|
||||
<Sidebar.MenuButton>
|
||||
Friends
|
||||
<PlusIcon class="ms-auto group-data-[state=open]/collapsible:hidden" />
|
||||
<MinusIcon class="ms-auto group-data-[state=closed]/collapsible:hidden" />
|
||||
</Sidebar.MenuButton>
|
||||
</Collapsible.Trigger>
|
||||
<Collapsible.Content>
|
||||
<Sidebar.MenuSub>
|
||||
{#each data.friends as friend (friend.id)}
|
||||
<Sidebar.MenuSubItem>
|
||||
<Sidebar.MenuSubButton>
|
||||
<User
|
||||
onclick={(e) => {
|
||||
e.preventDefault();
|
||||
currentPage = friend.id;
|
||||
}}
|
||||
user={friend}
|
||||
></User>
|
||||
</Sidebar.MenuSubButton>
|
||||
</Sidebar.MenuSubItem>
|
||||
{/each}
|
||||
</Sidebar.MenuSub>
|
||||
</Collapsible.Content>
|
||||
</Sidebar.MenuItem>
|
||||
</Collapsible.Root>
|
||||
<Dialog.Footer>
|
||||
<Dialog.Close class={buttonVariants({ variant: 'outline' })}>Cancel</Dialog.Close>
|
||||
<Button type="submit">Create</Button>
|
||||
</Dialog.Footer>
|
||||
</form>
|
||||
</Dialog.Content>
|
||||
</Dialog.Root>
|
||||
</div>
|
||||
</Sidebar.MenuItem>
|
||||
</Sidebar.Menu>
|
||||
</Sidebar.Header>
|
||||
<Sidebar.Content>
|
||||
<Sidebar.Group>
|
||||
<Sidebar.Menu>
|
||||
<Collapsible.Root open={true} class="group/collapsible">
|
||||
<Sidebar.MenuItem>
|
||||
<Collapsible.Trigger>
|
||||
<Sidebar.MenuButton>
|
||||
Friends
|
||||
<PlusIcon class="ms-auto group-data-[state=open]/collapsible:hidden" />
|
||||
<MinusIcon class="ms-auto group-data-[state=closed]/collapsible:hidden" />
|
||||
</Sidebar.MenuButton>
|
||||
</Collapsible.Trigger>
|
||||
<Collapsible.Content>
|
||||
<Sidebar.MenuSub>
|
||||
{#each data.friends as friend (friend.id)}
|
||||
<Sidebar.MenuSubItem>
|
||||
<Sidebar.MenuSubButton>
|
||||
<User
|
||||
onclick={(e) => {
|
||||
e.preventDefault();
|
||||
currentPage = friend.id;
|
||||
}}
|
||||
user={friend}
|
||||
></User>
|
||||
</Sidebar.MenuSubButton>
|
||||
</Sidebar.MenuSubItem>
|
||||
{/each}
|
||||
</Sidebar.MenuSub>
|
||||
</Collapsible.Content>
|
||||
</Sidebar.MenuItem>
|
||||
</Collapsible.Root>
|
||||
|
||||
<Collapsible.Root open={true} class="group/collapsible">
|
||||
<Sidebar.MenuItem>
|
||||
<Collapsible.Trigger>
|
||||
<Sidebar.MenuButton>
|
||||
Groups
|
||||
<PlusIcon class="ms-auto group-data-[state=open]/collapsible:hidden" />
|
||||
<MinusIcon class="ms-auto group-data-[state=closed]/collapsible:hidden" />
|
||||
</Sidebar.MenuButton>
|
||||
</Collapsible.Trigger>
|
||||
<Collapsible.Content>
|
||||
<Sidebar.MenuSub>
|
||||
{#each data.groups as group (group.id)}
|
||||
<Sidebar.MenuSubItem>
|
||||
<Sidebar.MenuSubButton>
|
||||
<a
|
||||
onclick={(e) => {
|
||||
e.preventDefault();
|
||||
currentPage = group.id;
|
||||
}}
|
||||
href="##"
|
||||
>
|
||||
{group.name} ({group.members} members)
|
||||
</a>
|
||||
</Sidebar.MenuSubButton>
|
||||
</Sidebar.MenuSubItem>
|
||||
{/each}
|
||||
</Sidebar.MenuSub>
|
||||
</Collapsible.Content>
|
||||
</Sidebar.MenuItem>
|
||||
</Collapsible.Root>
|
||||
<Collapsible.Root open={true} class="group/collapsible">
|
||||
<Sidebar.MenuItem>
|
||||
<Collapsible.Trigger>
|
||||
<Sidebar.MenuButton>
|
||||
Groups
|
||||
<PlusIcon class="ms-auto group-data-[state=open]/collapsible:hidden" />
|
||||
<MinusIcon class="ms-auto group-data-[state=closed]/collapsible:hidden" />
|
||||
</Sidebar.MenuButton>
|
||||
</Collapsible.Trigger>
|
||||
<Collapsible.Content>
|
||||
<Sidebar.MenuSub>
|
||||
{#each data.groups as group (group.id)}
|
||||
<Sidebar.MenuSubItem>
|
||||
<Sidebar.MenuSubButton>
|
||||
<a
|
||||
onclick={(e) => {
|
||||
e.preventDefault();
|
||||
currentPage = group.id;
|
||||
}}
|
||||
href="##"
|
||||
>
|
||||
{group.name} ({group.members} members)
|
||||
</a>
|
||||
</Sidebar.MenuSubButton>
|
||||
</Sidebar.MenuSubItem>
|
||||
{/each}
|
||||
</Sidebar.MenuSub>
|
||||
</Collapsible.Content>
|
||||
</Sidebar.MenuItem>
|
||||
</Collapsible.Root>
|
||||
|
||||
<Collapsible.Root open={true} class="group/collapsible">
|
||||
<Sidebar.MenuItem>
|
||||
<Collapsible.Trigger>
|
||||
<Sidebar.MenuButton>
|
||||
Servers
|
||||
<PlusIcon class="ms-auto group-data-[state=open]/collapsible:hidden" />
|
||||
<MinusIcon class="ms-auto group-data-[state=closed]/collapsible:hidden" />
|
||||
</Sidebar.MenuButton>
|
||||
</Collapsible.Trigger>
|
||||
<Collapsible.Content>
|
||||
<Sidebar.MenuSub>
|
||||
{#each data.servers as server (server.id)}
|
||||
<Sidebar.MenuSubItem>
|
||||
<Sidebar.MenuSubButton>
|
||||
<a
|
||||
onclick={(e) => {
|
||||
e.preventDefault();
|
||||
currentPage = server.id;
|
||||
}}
|
||||
href="##"
|
||||
class="flex items-center gap-2"
|
||||
>
|
||||
<img
|
||||
src={'https://api.dicebear.com/7.x/pixel-art/svg?seed=' + server.name}
|
||||
alt={server.name}
|
||||
class="size-6 rounded-full"
|
||||
/>
|
||||
{server.name}
|
||||
</a>
|
||||
</Sidebar.MenuSubButton>
|
||||
</Sidebar.MenuSubItem>
|
||||
{/each}
|
||||
</Sidebar.MenuSub>
|
||||
</Collapsible.Content>
|
||||
</Sidebar.MenuItem>
|
||||
</Collapsible.Root>
|
||||
</Sidebar.Menu>
|
||||
</Sidebar.Group>
|
||||
</Sidebar.Content>
|
||||
<Sidebar.Rail />
|
||||
<Collapsible.Root open={true} class="group/collapsible">
|
||||
<Sidebar.MenuItem>
|
||||
<Collapsible.Trigger>
|
||||
<Sidebar.MenuButton>
|
||||
Servers
|
||||
<PlusIcon class="ms-auto group-data-[state=open]/collapsible:hidden" />
|
||||
<MinusIcon class="ms-auto group-data-[state=closed]/collapsible:hidden" />
|
||||
</Sidebar.MenuButton>
|
||||
</Collapsible.Trigger>
|
||||
<Collapsible.Content>
|
||||
<Sidebar.MenuSub>
|
||||
{#each data.servers as server (server.id)}
|
||||
<Sidebar.MenuSubItem>
|
||||
<Sidebar.MenuSubButton>
|
||||
<a
|
||||
onclick={(e) => {
|
||||
e.preventDefault();
|
||||
currentPage = server.id;
|
||||
}}
|
||||
href="##"
|
||||
class="flex items-center gap-2"
|
||||
>
|
||||
<img
|
||||
src={'https://api.dicebear.com/7.x/pixel-art/svg?seed=' + server.name}
|
||||
alt={server.name}
|
||||
class="size-6 rounded-full"
|
||||
/>
|
||||
{server.name}
|
||||
</a>
|
||||
</Sidebar.MenuSubButton>
|
||||
</Sidebar.MenuSubItem>
|
||||
{/each}
|
||||
</Sidebar.MenuSub>
|
||||
</Collapsible.Content>
|
||||
</Sidebar.MenuItem>
|
||||
</Collapsible.Root>
|
||||
</Sidebar.Menu>
|
||||
</Sidebar.Group>
|
||||
</Sidebar.Content>
|
||||
<Sidebar.Rail />
|
||||
</Sidebar.Root>
|
||||
|
|
|
|||
|
|
@ -1,9 +0,0 @@
|
|||
import { MediaQuery } from "svelte/reactivity";
|
||||
|
||||
const DEFAULT_MOBILE_BREAKPOINT = 768;
|
||||
|
||||
export class IsMobile extends MediaQuery {
|
||||
constructor(breakpoint: number = DEFAULT_MOBILE_BREAKPOINT) {
|
||||
super(`max-width: ${breakpoint - 1}px`);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,11 +4,12 @@ export const UserID = definePrefix('user');
|
|||
export const GroupID = definePrefix('group');
|
||||
export const ServerID = definePrefix('srv');
|
||||
export const FriendRequestID = definePrefix('frq');
|
||||
export const DirectMessageID = definePrefix('dmid');
|
||||
|
||||
export type UserId = Puuid<'user'>;
|
||||
export type GroupId = Puuid<'group'>;
|
||||
export type ServerId = Puuid<'srv'>;
|
||||
export type FriendRequestID = Puuid<'frq'>;
|
||||
export type DirectMessageId = Puuid<'dmid'>;
|
||||
|
||||
export const Status: Record<string, 1 | 2 | 3> = {
|
||||
OFFLINE: 1,
|
||||
|
|
@ -20,6 +21,7 @@ export type OverviewUser = {
|
|||
id: string;
|
||||
username: string;
|
||||
image: string;
|
||||
dmId?: string;
|
||||
};
|
||||
|
||||
export type OverviewServer = {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { sha256 } from '@oslojs/crypto/sha2';
|
|||
import { encodeBase64url, encodeHexLowerCase } from '@oslojs/encoding';
|
||||
import { db } from '$lib/server/db';
|
||||
import * as table from '$lib/server/db/schema';
|
||||
import { _findDmId } from '../../routes/api/messages/[[grp_srv_dm]]/[[channelId]]/[[channelId]]/+server';
|
||||
|
||||
const DAY_IN_MS = 1000 * 60 * 60 * 24;
|
||||
|
||||
|
|
@ -63,15 +64,25 @@ export async function validateSessionToken(token: string) {
|
|||
.set({ expiresAt: session.expiresAt })
|
||||
.where(eq(table.session.id, session.id));
|
||||
}
|
||||
const friends = (user.friends as string[]).length
|
||||
? await db
|
||||
.select({
|
||||
id: table.user.id,
|
||||
username: table.user.username
|
||||
})
|
||||
.from(table.user)
|
||||
.where(inArray(table.user.id, user.friends as string[]))
|
||||
: [];
|
||||
const friends = await Promise.all(
|
||||
((user.friends as string[]).length
|
||||
? await db
|
||||
.select({
|
||||
id: table.user.id,
|
||||
username: table.user.username
|
||||
})
|
||||
.from(table.user)
|
||||
.where(inArray(table.user.id, user.friends as string[]))
|
||||
: []
|
||||
).map(async (z) => {
|
||||
const dmid = await _findDmId(z.id, user.id);
|
||||
|
||||
return {
|
||||
...z,
|
||||
dmId: dmid
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
const servers = (user.servers as string[]).length
|
||||
? await db
|
||||
|
|
@ -99,6 +110,8 @@ export async function validateSessionToken(token: string) {
|
|||
.select({
|
||||
id: table.friendRequest.id,
|
||||
fromUser: table.friendRequest.fromUser,
|
||||
fromUsername: table.friendRequest.fromUsername,
|
||||
toUsername: table.friendRequest.toUsername,
|
||||
toUser: table.friendRequest.toUser
|
||||
})
|
||||
.from(table.friendRequest)
|
||||
|
|
|
|||
|
|
@ -37,7 +37,8 @@ export const group = sqliteTable('group', {
|
|||
owner: text('owner')
|
||||
.notNull()
|
||||
.references(() => user.id),
|
||||
members: text('members', { mode: 'json' }).default([]).notNull()
|
||||
members: text('members', { mode: 'json' }).default([]).notNull(),
|
||||
messages: text('messages', { mode: 'json' }).default([]).notNull()
|
||||
});
|
||||
|
||||
export const channel = sqliteTable('channel', {
|
||||
|
|
@ -51,10 +52,12 @@ export const channel = sqliteTable('channel', {
|
|||
|
||||
export const directMessage = sqliteTable('directMessage', {
|
||||
id: text('id').primaryKey(),
|
||||
name: text('name').notNull(),
|
||||
serverId: text('server_id')
|
||||
firstMember: text('first_member')
|
||||
.notNull()
|
||||
.references(() => server.id),
|
||||
.references(() => user.id),
|
||||
secondMember: text('second_member')
|
||||
.notNull()
|
||||
.references(() => user.id),
|
||||
messages: text('messages', { mode: 'json' }).default([]).notNull()
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { twMerge } from 'tailwind-merge';
|
|||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
export function formatTimestamp(dateString: string) {
|
||||
const date = new Date(dateString);
|
||||
const now = new Date();
|
||||
|
|
|
|||
|
|
@ -1,7 +1,59 @@
|
|||
import { fail, json } from '@sveltejs/kit';
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { GroupID, ServerID, UserID } from '$lib';
|
||||
import { db } from '$lib/server/db';
|
||||
import * as table from '$lib/server/db/schema';
|
||||
import { DirectMessageID, GroupID, ServerID } from '$lib';
|
||||
import { eq, or } from 'drizzle-orm';
|
||||
import { _sendToSubscribers } from '../../../../updates/+server';
|
||||
import { and } from 'drizzle-orm';
|
||||
|
||||
interface Message {
|
||||
id: string;
|
||||
authorId: string;
|
||||
content: string;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export async function _findDm(member_one: string, member_two: string) {
|
||||
return await db
|
||||
.select()
|
||||
.from(table.directMessage)
|
||||
.where(
|
||||
or(
|
||||
and(
|
||||
eq(table.directMessage.secondMember, member_two),
|
||||
eq(table.directMessage.firstMember, member_one)
|
||||
),
|
||||
and(
|
||||
eq(table.directMessage.firstMember, member_two),
|
||||
eq(table.directMessage.secondMember, member_one)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
export async function _findDmId(
|
||||
member_one: string,
|
||||
member_two: string
|
||||
): Promise<string | undefined> {
|
||||
return (
|
||||
await db
|
||||
.select({ id: table.directMessage.id })
|
||||
.from(table.directMessage)
|
||||
.where(
|
||||
or(
|
||||
and(
|
||||
eq(table.directMessage.secondMember, member_two),
|
||||
eq(table.directMessage.firstMember, member_one)
|
||||
),
|
||||
and(
|
||||
eq(table.directMessage.firstMember, member_two),
|
||||
eq(table.directMessage.secondMember, member_one)
|
||||
)
|
||||
)
|
||||
)
|
||||
.limit(1)
|
||||
)?.[0]?.id;
|
||||
}
|
||||
export const GET: RequestHandler = async ({ params, locals }) => {
|
||||
if (!locals.user) {
|
||||
return new Response('No authentication', { status: 401 });
|
||||
|
|
@ -11,38 +63,32 @@ export const GET: RequestHandler = async ({ params, locals }) => {
|
|||
if (!grp_srv_dm) {
|
||||
return new Response('Missing group, server, or DM ID.', { status: 400 });
|
||||
}
|
||||
let messages = [];
|
||||
|
||||
let messages: Message[] = [];
|
||||
let type = '';
|
||||
|
||||
if (GroupID.is(grp_srv_dm)) {
|
||||
type = 'group';
|
||||
messages = Array.from({ length: 5 }, (_, i) => ({
|
||||
id: crypto.randomUUID(),
|
||||
authorId: `user_${Math.floor(Math.random() * 10)}`,
|
||||
content: 'group message ' + (i + 1),
|
||||
timestamp: Date.now() - Math.floor(Math.random() * 100000)
|
||||
}));
|
||||
const g = (await db.select().from(table.group).where(eq(table.group.id, grp_srv_dm)))[0];
|
||||
if (!g) return new Response('Group not found.', { status: 404 });
|
||||
|
||||
messages = g.messages ?? [];
|
||||
} else if (ServerID.is(grp_srv_dm)) {
|
||||
type = 'server';
|
||||
if (!channelId) return new Response('Missing channel ID.', { status: 400 });
|
||||
const c = (await db.select().from(table.channel).where(eq(table.channel.id, channelId)))[0];
|
||||
if (!c) return new Response('Channel not found.', { status: 404 });
|
||||
|
||||
if (!channelId) {
|
||||
return new Response('Missing channel ID.', { status: 400 });
|
||||
}
|
||||
|
||||
messages = Array.from({ length: 5 }, (_, i) => ({
|
||||
id: crypto.randomUUID(),
|
||||
authorId: `user_${Math.floor(Math.random() * 10)}`,
|
||||
content: 'server message ' + (i + 1),
|
||||
timestamp: Date.now() - Math.floor(Math.random() * 100000)
|
||||
}));
|
||||
} else if (UserID.is(grp_srv_dm)) {
|
||||
messages = c.messages;
|
||||
} else if (DirectMessageID.is(grp_srv_dm)) {
|
||||
type = 'dms';
|
||||
messages = Array.from({ length: 5 }, (_, i) => ({
|
||||
id: crypto.randomUUID(),
|
||||
authorId: Math.random() > 0.5 ? locals.user.id : grp_srv_dm,
|
||||
content: 'dm message ' + (i + 1),
|
||||
timestamp: Date.now() - Math.floor(Math.random() * 100000)
|
||||
}));
|
||||
const dm = (
|
||||
await db.select().from(table.directMessage).where(eq(table.directMessage.id, grp_srv_dm))
|
||||
)[0];
|
||||
|
||||
if (!dm) return new Response('DM not found.', { status: 404 });
|
||||
|
||||
messages = dm.messages;
|
||||
}
|
||||
|
||||
return json({
|
||||
|
|
@ -51,3 +97,67 @@ export const GET: RequestHandler = async ({ params, locals }) => {
|
|||
messages
|
||||
});
|
||||
};
|
||||
|
||||
export const POST: RequestHandler = async ({ params, request, locals }) => {
|
||||
if (!locals.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
let { grp_srv_dm, channelId } = params;
|
||||
if (!grp_srv_dm) return new Response('Missing Group/Server/DM id.', { status: 400 });
|
||||
const data = await request.json();
|
||||
const { content } = data;
|
||||
|
||||
if (!content) return new Response('Missing message content.', { status: 400 });
|
||||
|
||||
let messages: Message[] = [];
|
||||
let type = '';
|
||||
|
||||
const message = {
|
||||
id: crypto.randomUUID(),
|
||||
authorId: locals.user.id,
|
||||
content,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
if (GroupID.is(grp_srv_dm)) {
|
||||
type = 'group';
|
||||
const g = (await db.select().from(table.group).where(eq(table.group.id, grp_srv_dm)))[0];
|
||||
if (!g) return new Response('Group not found.', { status: 404 });
|
||||
messages = g.messages ?? [];
|
||||
|
||||
messages.push(message);
|
||||
|
||||
await db.update(table.group).set({ messages }).where(eq(table.group.id, grp_srv_dm));
|
||||
} else if (ServerID.is(grp_srv_dm)) {
|
||||
type = 'server';
|
||||
if (!channelId) return new Response('Missing channel ID.', { status: 400 });
|
||||
const c = (await db.select().from(table.channel).where(eq(table.channel.id, channelId)))[0];
|
||||
if (!c) return new Response('Channel not found.', { status: 404 });
|
||||
messages = c.messages ?? [];
|
||||
|
||||
messages.push(message);
|
||||
|
||||
await db.update(table.channel).set({ messages }).where(eq(table.channel.id, channelId));
|
||||
} else if (DirectMessageID.is(grp_srv_dm)) {
|
||||
type = 'dms';
|
||||
const dm = (
|
||||
await db.select().from(table.directMessage).where(eq(table.directMessage.id, grp_srv_dm))
|
||||
)[0];
|
||||
|
||||
if (!dm) return new Response('DM not found.', { status: 404 });
|
||||
|
||||
messages = dm.messages ?? [];
|
||||
|
||||
messages.push(message);
|
||||
|
||||
await db.update(table.directMessage).set({ messages }).where(eq(table.directMessage.id, dm.id));
|
||||
|
||||
_sendToSubscribers(dm.id, { type: 'message', id: dm.id, message });
|
||||
return json({ type, id: dm.id, messages });
|
||||
}
|
||||
|
||||
_sendToSubscribers(grp_srv_dm, { type: 'message', id: grp_srv_dm, message });
|
||||
|
||||
return json({ type, id: grp_srv_dm, messages });
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,17 +1,18 @@
|
|||
import { Status } from '$lib';
|
||||
import { kvStore } from '$lib/server/db/index.js';
|
||||
import { json } from '@sveltejs/kit';
|
||||
|
||||
interface SubscribedTo {
|
||||
subscribed: string[];
|
||||
subscribed: string[]; // any ID (including channels, dms, et cetera can be added in here, to send SSE data to any group of users)
|
||||
userId: string;
|
||||
controller: ReadableStreamDefaultController;
|
||||
}
|
||||
|
||||
export const _clients = new Map<string, SubscribedTo>();
|
||||
|
||||
export function _sendToSubscribers(userId: string, payload: unknown) {
|
||||
export function _sendToSubscribers(id: string, payload: unknown) {
|
||||
for (const [key, client] of _clients) {
|
||||
if (client.subscribed.includes(userId)) {
|
||||
if (client.subscribed.includes(id)) {
|
||||
try {
|
||||
client.controller.enqueue(`data: ${JSON.stringify(payload)}\n\n`);
|
||||
} catch {
|
||||
|
|
@ -30,7 +31,7 @@ export function _isUserConnected(userId: string): boolean {
|
|||
|
||||
export async function GET({ locals, request }) {
|
||||
if (!locals.user) {
|
||||
return new Response('No authentication', { status: 401 });
|
||||
return json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const userId = locals.user.id;
|
||||
|
|
@ -38,14 +39,14 @@ export async function GET({ locals, request }) {
|
|||
const subscribed = locals.user.friends.map((f) => f.id);
|
||||
const overwrite = locals.user.statusOverwrite;
|
||||
|
||||
const reqId = crypto.randomUUID();
|
||||
const sessionId = crypto.randomUUID();
|
||||
|
||||
const stream = new ReadableStream({
|
||||
start(controller) {
|
||||
_clients.set(reqId, { subscribed, userId, controller });
|
||||
_clients.set(sessionId, { subscribed, userId, controller });
|
||||
console.log(`SSE Client opened. total: ${_clients.size}`);
|
||||
|
||||
controller.enqueue(`data: ${JSON.stringify({ type: 'connected' })}\n\n`);
|
||||
controller.enqueue(`data: ${JSON.stringify({ type: 'connected', sessionId })}\n\n`);
|
||||
|
||||
if (overwrite === Status.DND) {
|
||||
kvStore.set(`user-${userId}-state`, Status.DND);
|
||||
|
|
@ -56,7 +57,7 @@ export async function GET({ locals, request }) {
|
|||
}
|
||||
|
||||
request.signal.addEventListener('abort', () => {
|
||||
_clients.delete(reqId);
|
||||
_clients.delete(sessionId);
|
||||
console.log(`SSE Client aborted. total: ${_clients.size}`);
|
||||
|
||||
if (_isUserConnected(userId)) return;
|
||||
|
|
|
|||
111
src/routes/api/updates/[sessionId]/+server.ts
Normal file
111
src/routes/api/updates/[sessionId]/+server.ts
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { _clients } from '../+server';
|
||||
import { DirectMessageID } from '$lib';
|
||||
import { db } from '$lib/server/db';
|
||||
import * as table from '$lib/server/db/schema';
|
||||
import { or } from 'drizzle-orm';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
export const POST: RequestHandler = async ({ params, request, locals }) => {
|
||||
if (!locals.user) {
|
||||
return json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const { sessionId } = params;
|
||||
if (!sessionId) {
|
||||
return json({ error: 'Missing sessionId' }, { status: 400 });
|
||||
}
|
||||
|
||||
const client = _clients.get(sessionId);
|
||||
if (!client) {
|
||||
return json({ error: 'Client not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
if (client.userId !== locals.user.id) {
|
||||
return json({ error: 'Unauthorized sessionID access' }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { subscribeTo } = body;
|
||||
|
||||
if (typeof subscribeTo !== 'string' || !subscribeTo.trim()) {
|
||||
return json({ error: 'Invalid subscribeTo value' }, { status: 400 });
|
||||
}
|
||||
|
||||
let isValidDmid = false;
|
||||
|
||||
if (DirectMessageID.is(subscribeTo)) {
|
||||
const find = await db
|
||||
.select()
|
||||
.from(table.directMessage)
|
||||
.where(
|
||||
or(
|
||||
eq(table.directMessage.firstMember, locals.user.id),
|
||||
eq(table.directMessage.secondMember, locals.user.id)
|
||||
)
|
||||
);
|
||||
|
||||
if (find?.length != 0) {
|
||||
isValidDmid = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
!(
|
||||
isValidDmid ||
|
||||
locals.user.groups.find((z) => z.id === subscribeTo) ||
|
||||
locals.user.servers.find((z) => z.id === subscribeTo)
|
||||
)
|
||||
) {
|
||||
return json({ error: 'Invalid subscription' }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!client.subscribed.includes(subscribeTo)) {
|
||||
client.subscribed.push(subscribeTo);
|
||||
}
|
||||
|
||||
return json({
|
||||
message: `Added ${subscribeTo} to client subscriptions.`,
|
||||
subscribed: client.subscribed
|
||||
});
|
||||
};
|
||||
|
||||
export const DELETE: RequestHandler = async ({ params, request, locals }) => {
|
||||
if (!locals.user) {
|
||||
return json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const { sessionId } = params;
|
||||
if (!sessionId) {
|
||||
return json({ error: 'Missing sessionId' }, { status: 400 });
|
||||
}
|
||||
|
||||
const client = _clients.get(sessionId);
|
||||
if (!client) {
|
||||
return json({ error: 'Client not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
if (client.userId !== locals.user.id) {
|
||||
return json({ error: 'Unauthorized sessionID access' }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { subscribeTo } = body;
|
||||
|
||||
if (typeof subscribeTo !== 'string' || !subscribeTo.trim()) {
|
||||
return json({ error: 'Invalid subscribeTo value' }, { status: 400 });
|
||||
}
|
||||
|
||||
const index = client.subscribed.indexOf(subscribeTo);
|
||||
if (index !== -1) {
|
||||
client.subscribed.splice(index, 1);
|
||||
} else {
|
||||
return json({ error: 'Subscription not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
return json({
|
||||
message: `Removed ${subscribeTo} from client subscriptions.`,
|
||||
subscribed: client.subscribed
|
||||
});
|
||||
};
|
||||
|
|
@ -3,7 +3,7 @@ import { getRequestEvent } from '$app/server';
|
|||
import type { Actions, PageServerLoad } from './$types';
|
||||
import { db } from '$lib/server/db';
|
||||
import * as table from '$lib/server/db/schema';
|
||||
import { FriendRequestID, ServerID } from '$lib';
|
||||
import { DirectMessageID, FriendRequestID, ServerID } from '$lib';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { and } from 'drizzle-orm';
|
||||
import { type User } from '$lib/server/db/schema';
|
||||
|
|
@ -72,6 +72,13 @@ export const actions = {
|
|||
)
|
||||
.limit(1);
|
||||
|
||||
await db.insert(table.directMessage).values({
|
||||
id: DirectMessageID.newV4(),
|
||||
firstMember: locals.user!.id,
|
||||
secondMember: user[0].id,
|
||||
messages: []
|
||||
});
|
||||
|
||||
// add other guy to us
|
||||
await db.transaction(async (tx) => {
|
||||
await tx
|
||||
|
|
@ -99,7 +106,9 @@ export const actions = {
|
|||
await db.insert(table.friendRequest).values({
|
||||
id: FriendRequestID.newV4(),
|
||||
fromUser: locals.user!.id,
|
||||
toUser: user[0].id
|
||||
toUser: user[0].id,
|
||||
toUsername: user[0].username,
|
||||
fromUsername: locals.user!.username
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@
|
|||
import { onMount } from 'svelte';
|
||||
import type { ActionData } from './$types';
|
||||
import { formatTimestamp } from '$lib/utils';
|
||||
import Input from '$lib/components/ui/input/input.svelte';
|
||||
import { Button } from '$lib/components/ui/button';
|
||||
let errorOpen = $state(true);
|
||||
|
||||
let { form, data }: { form: ActionData; data: PageServerData } = $props();
|
||||
|
|
@ -27,6 +29,11 @@
|
|||
let currentPage: OverviewUser | OverviewGroup | OverviewServer | undefined = $state();
|
||||
|
||||
let messages = $state([]);
|
||||
let inputValue = $state();
|
||||
|
||||
let sessionId: string | undefined = $state();
|
||||
let previousSubscription: typeof currentPageID = $state(null);
|
||||
|
||||
const overview_data: OverviewData = $state({
|
||||
friends: [],
|
||||
groups: [],
|
||||
|
|
@ -52,10 +59,26 @@
|
|||
if (ServerID.is(currentPageID)) return;
|
||||
|
||||
async function getMessages() {
|
||||
const req = await fetch('/api/messages/' + currentPageID);
|
||||
const req = await fetch('/api/messages/' + (currentPage!.dmId || currentPageID));
|
||||
const data = await req.json();
|
||||
|
||||
messages = data.messages;
|
||||
|
||||
if (previousSubscription && previousSubscription != (currentPage!.dmId || currentPageID)) {
|
||||
await fetch('/api/updates/' + sessionId, {
|
||||
body: JSON.stringify({ subscribeTo: previousSubscription }),
|
||||
method: 'DELETE'
|
||||
});
|
||||
}
|
||||
|
||||
if (previousSubscription != (currentPage!.dmId || currentPageID)) {
|
||||
await fetch('/api/updates/' + sessionId, {
|
||||
body: JSON.stringify({ subscribeTo: currentPage!.dmId || currentPageID }),
|
||||
method: 'POST'
|
||||
});
|
||||
}
|
||||
|
||||
previousSubscription = currentPageID;
|
||||
}
|
||||
|
||||
getMessages();
|
||||
|
|
@ -68,6 +91,7 @@
|
|||
id: ServerID.parse(z.id),
|
||||
name: z.name,
|
||||
ownerId: z.ownerId,
|
||||
|
||||
image: 'https://api.dicebear.com/7.x/pixel-art/svg?seed=' + z.name
|
||||
};
|
||||
});
|
||||
|
|
@ -95,6 +119,7 @@
|
|||
username: friend.username,
|
||||
status: status.status,
|
||||
statusMessage: status.statusMessage,
|
||||
dmId: friend.dmId,
|
||||
image: 'https://api.dicebear.com/7.x/pixel-art/svg?seed=' + friend.username
|
||||
};
|
||||
})
|
||||
|
|
@ -109,11 +134,12 @@
|
|||
|
||||
sse.addEventListener('message', (e) => {
|
||||
const json = JSON.parse(e.data) as
|
||||
| { type: 'connected' }
|
||||
| { type: 'connected'; sessionId: string }
|
||||
| { type: 'status'; id: string; status: 1 | 2 | 3 };
|
||||
|
||||
if (json.type == 'connected') {
|
||||
console.log('SSE connected.');
|
||||
console.log('SSE connected. We are sessionID ' + json.sessionId);
|
||||
sessionId = json.sessionId;
|
||||
}
|
||||
|
||||
if (json.type == 'status') {
|
||||
|
|
@ -122,6 +148,10 @@
|
|||
friend.status = json.status;
|
||||
}
|
||||
}
|
||||
|
||||
if (json.type == 'message') {
|
||||
messages.push(json.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
|
@ -203,5 +233,29 @@
|
|||
</div>
|
||||
{/if}
|
||||
{/each}
|
||||
|
||||
{#if currentPageID && currentPage}
|
||||
<Input
|
||||
bind:value={inputValue}
|
||||
placeholder="input box for messages (ignore how ugly it is right now please)"
|
||||
></Input>
|
||||
<Button
|
||||
onclick={async () => {
|
||||
const req = await fetch('/api/messages/' + (currentPage!.dmId || currentPageID), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
content: inputValue
|
||||
})
|
||||
});
|
||||
|
||||
if (!req.ok) {
|
||||
console.error('Failed to send message');
|
||||
}
|
||||
}}>send</Button
|
||||
>
|
||||
{/if}
|
||||
</Sidebar.Inset>
|
||||
</Sidebar.Provider>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue