feat: Notification support
This commit is contained in:
parent
74d6e86e79
commit
becff2366c
2
TODO.md
2
TODO.md
@ -11,7 +11,7 @@ A more complex version of this list is available [here](https://trello.com/b/kJw
|
|||||||
- [ ] Images/Attatchments support
|
- [ ] Images/Attatchments support
|
||||||
- [x] Installation documentation
|
- [x] Installation documentation
|
||||||
- [ ] LaTeX support
|
- [ ] LaTeX support
|
||||||
- [ ] Notifications
|
- [x] Notifications
|
||||||
- [x] Make the damn textbox stop unfocusing on every message submit
|
- [x] Make the damn textbox stop unfocusing on every message submit
|
||||||
- [ ] Message context menus
|
- [ ] Message context menus
|
||||||
- [ ] Message Timestamps
|
- [ ] Message Timestamps
|
||||||
|
@ -3,11 +3,13 @@ import type { Socket } from 'socket.io-client';
|
|||||||
|
|
||||||
class Websocket {
|
class Websocket {
|
||||||
private socket: Socket;
|
private socket: Socket;
|
||||||
|
private me: string;
|
||||||
public messages: TypeMessage[] = $state([]);
|
public messages: TypeMessage[] = $state([]);
|
||||||
private channel: string = '';
|
private channel: string = '';
|
||||||
|
|
||||||
constructor(socket: Socket) {
|
constructor(socket: Socket, user: string) {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
|
this.me = user;
|
||||||
}
|
}
|
||||||
|
|
||||||
connect() {
|
connect() {
|
||||||
@ -26,7 +28,7 @@ class Websocket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add message to local messages array
|
// Add message to local messages array
|
||||||
loadMessage(newMsg: TypeMessage) {
|
private loadMessage(newMsg: TypeMessage) {
|
||||||
this.messages = [
|
this.messages = [
|
||||||
{
|
{
|
||||||
message: newMsg.message,
|
message: newMsg.message,
|
||||||
@ -35,6 +37,16 @@ class Websocket {
|
|||||||
},
|
},
|
||||||
...this.messages,
|
...this.messages,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Sends a notif if the window is unfocused and
|
||||||
|
// the sender is not the current user
|
||||||
|
if (this.me !== newMsg.user && !document.hasFocus()) {
|
||||||
|
new Notification(`Message in #${this.channel}`, {
|
||||||
|
body: `${newMsg.user}: ${newMsg.message}`,
|
||||||
|
icon: newMsg.imageSrc,
|
||||||
|
tag: `message-in-channel-${this.channel}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send a message
|
// Send a message
|
||||||
|
@ -4,7 +4,13 @@ import type { TypeMessage } from '$lib/types';
|
|||||||
import { error, redirect } from '@sveltejs/kit';
|
import { error, redirect } from '@sveltejs/kit';
|
||||||
import { auth } from '$lib/server/db/auth';
|
import { auth } from '$lib/server/db/auth';
|
||||||
|
|
||||||
export async function load({ params, request }): Promise<{ messages: TypeMessage[]; currentUser: string }> {
|
interface ChannelLoad {
|
||||||
|
messages: TypeMessage[];
|
||||||
|
currentUserID: string;
|
||||||
|
currentUserName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function load({ params, request }): Promise<ChannelLoad> {
|
||||||
const session = await auth.api.getSession({
|
const session = await auth.api.getSession({
|
||||||
headers: request.headers,
|
headers: request.headers,
|
||||||
});
|
});
|
||||||
@ -34,6 +40,7 @@ export async function load({ params, request }): Promise<{ messages: TypeMessage
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
messages: messages ?? [],
|
messages: messages ?? [],
|
||||||
currentUser: session.user.id!,
|
currentUserID: session.user.id!,
|
||||||
|
currentUserName: session.user.username!,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -21,10 +21,20 @@
|
|||||||
let textareaRef: HTMLTextAreaElement | undefined = $state();
|
let textareaRef: HTMLTextAreaElement | undefined = $state();
|
||||||
let formref: HTMLFormElement | undefined = $state();
|
let formref: HTMLFormElement | undefined = $state();
|
||||||
|
|
||||||
|
function askNotificationPermission() {
|
||||||
|
// Check if the browser supports notifications
|
||||||
|
if (!('Notification' in window)) {
|
||||||
|
alert('This browser does not support notifications.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Notification.requestPermission();
|
||||||
|
}
|
||||||
|
|
||||||
function submit(event: Event) {
|
function submit(event: Event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
if (msg.length <= 2000) {
|
if (msg.length <= 2000) {
|
||||||
socket?.sendMessage(data.currentUser, msg);
|
socket?.sendMessage(data.currentUserID, msg);
|
||||||
if (textareaRef) textareaRef.style.height = '40px';
|
if (textareaRef) textareaRef.style.height = '40px';
|
||||||
msg = '';
|
msg = '';
|
||||||
} else {
|
} else {
|
||||||
@ -34,9 +44,12 @@
|
|||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
// Connect on page load
|
// Connect on page load
|
||||||
socket = new Websocket(io());
|
socket = new Websocket(io(), data.currentUserName);
|
||||||
socket.connect();
|
socket.connect();
|
||||||
|
|
||||||
|
// Ask for notification permissions
|
||||||
|
askNotificationPermission();
|
||||||
|
|
||||||
// Submit on textarea enter
|
// Submit on textarea enter
|
||||||
textareaRef?.addEventListener('keypress', (e) => {
|
textareaRef?.addEventListener('keypress', (e) => {
|
||||||
if (e.key === 'Enter' && !e.shiftKey) {
|
if (e.key === 'Enter' && !e.shiftKey) {
|
||||||
|
Loading…
Reference in New Issue
Block a user