Skip to content

Commit

Permalink
feat(notifications): added server export
Browse files Browse the repository at this point in the history
  • Loading branch information
antonstjernquist committed Sep 21, 2024
1 parent 51b0b78 commit 90c7a5c
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SERVER_EVENT_LISTENER,
} from '../shared/network';
import PhoneService from './services/PhoneService';
import { InsertNotification } from '../shared/Types';

const client = new Client({
debug: true,
Expand Down Expand Up @@ -51,6 +52,10 @@ onNet(BROADCAST_EVENT_LISTENER, (data: { data: unknown; event: string }) => {
global.SendNUIMessage({ type: NUI_BROADCAST_EVENT, payload: data });
});

onNet('npwd:create-notification', (data: InsertNotification) => {
global.SendNUIMessage({ type: 'ADD_NOTIFICATION', payload: data });
});

RegisterCommand('npwd-toggle-phone', () => PhoneService.togglePhone(), false);
RegisterKeyMapping('npwd-toggle-phone', 'Open Phone', 'keyboard', 'M');

Expand Down
8 changes: 8 additions & 0 deletions src/server/exports/Notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { InsertNotification } from '../../shared/Types';
import BroadcastService from '../services/BroadcastService';

const _exports = global.exports;

_exports('createNotification', (src: number, notification: InsertNotification) => {
BroadcastService.createNotification(src, notification);
});
1 change: 1 addition & 0 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { messagesRouter } from './router/messages';
import { conversationsRouter } from './router/conversations';
import { contactsRouter } from './router/contacts';
import { authRouter } from './router/auth';
import './exports/Notifications';

function bootstrap() {
initDB();
Expand Down
1 change: 0 additions & 1 deletion src/server/repositories/AuthRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { set } from 'zod';
import { ResourceConfig, getServerConfig } from '../utils/config';
const _exports = global.exports;

Expand Down
17 changes: 16 additions & 1 deletion src/server/router/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import DeviceService from '../services/DeviceService';
import PlayerService from '../services/PlayerService';
import SimCardService from '../services/SimCardService';
import { handleError } from '../utils/errors';
import BroadcastService from '../services/BroadcastService';
import { InsertNotification } from '../../shared/Types';

export const authRouter = new Router({
prefix: '/auth',
Expand Down Expand Up @@ -82,9 +82,24 @@ authRouter.add('/source-by-device', async (ctx) => {
}
});

// export const emitTo = (source: number, event: string, data: unknown) => {
// if (!isRunningInGame()) return;
// emitNet(event, source, data);
// };

authRouter.add('/debug', async (ctx, next) => {
try {
const authorized = await PlayerService.authorizeDevice(ctx.source, 'debug');
const authNotification: InsertNotification = {
title: 'Auth Notification',
description: `You are ${authorized ? 'authorized' : 'not authorized'} to debug`,
appId: 'settings',
path: '/home',
};

console.log('Emitting: npwd:add-notification', ctx.source, authNotification);
emitNet('npwd:add-notification', ctx.source, JSON.stringify(authNotification));

console.log('Authorized', authorized);

ctx.status = 200;
Expand Down
9 changes: 9 additions & 0 deletions src/server/services/BroadcastService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RouterContext } from 'fivem-router';
import PlayerService from './PlayerService';
import { parseObjectToIsoString } from '../utils/date';
import { InsertNotification } from '../../shared/Types';

interface EmitToNuiParams {
ctx: RouterContext;
Expand All @@ -16,6 +17,10 @@ interface EmitToNuisParams extends Omit<EmitToNuiParams, 'sid'> {
class BroadcastService {
constructor() {}

private emitNet(event: string, source: number, data: string) {
emitNet(event, source, data);
}

private async getSourceFromSid(sid: number) {
return await PlayerService.getSourceFromSid(sid);
}
Expand All @@ -41,6 +46,10 @@ class BroadcastService {
ctx.emitToNui(src, event, parseObjectToIsoString(data));
}
}

public async createNotification(source: number, notification: InsertNotification) {
this.emitNet('npwd:create-notification', source, JSON.stringify(notification));
}
}

export default new BroadcastService();
21 changes: 21 additions & 0 deletions src/shared/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,24 @@ export interface Contact {
sim_card_id: number;
created_at: Date;
}

export interface Notification {
id: string;
path: string;
type: 'call' | 'generic';
title: string;
timeout: number;
description: string;
created_at: string;

appId?: string;
overline?: string;
dismissable?: boolean;
}

export type InsertNotification = Omit<Notification, 'id' | 'created_at' | 'timeout' | 'type'> & {
type?: Notification['type'];
timeout?: Notification['timeout'];
created_at?: Notification['created_at'];
dismissable?: Notification['dismissable'];
};
10 changes: 4 additions & 6 deletions src/ui/src/contexts/NotificationContext/useNotifications.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useContext } from 'react';
import { Notification, NotificationsContext } from '.';
import { NotificationsContext } from '.';
import { DEFAULT_NOTIFICATION_TIMEOUT } from '@/constants';
import { useLocation } from 'react-router';
import { InsertNotification } from '../../../../shared/Types';

export const useNotifications = () => {
const { pathname } = useLocation();
Expand All @@ -11,11 +12,7 @@ export const useNotifications = () => {
throw new Error('useNotifications must be used within a NotificationsProvider');
}

const handleCreateNotification = (
notification: Omit<Notification, 'id' | 'created_at' | 'timeout'> & {
timeout?: number;
},
) => {
const handleCreateNotification = (notification: InsertNotification) => {
/**
* If the notification is for the current path, don't show it.
*/
Expand All @@ -28,6 +25,7 @@ export const useNotifications = () => {
{
id: Math.random().toString(36).substring(7),
...notification,
type: notification.type || 'generic',
timeout: notification.timeout || DEFAULT_NOTIFICATION_TIMEOUT,
created_at: new Date().toISOString(),
},
Expand Down

0 comments on commit 90c7a5c

Please sign in to comment.