Skip to content

Commit

Permalink
sendXmppMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
ceddybi committed Mar 31, 2024
1 parent 252e3b8 commit a6f0585
Show file tree
Hide file tree
Showing 5 changed files with 466 additions and 15 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@roadmanjs/notification",
"name": "@roadmanjs/notifications",
"version": "0.0.1",
"description": "notification",
"description": "notifications module for roadmanjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -48,6 +48,7 @@
"@types/lodash": "^4.14.168",
"@types/mocha": "^8.2.0",
"@types/node": "^10.0.3",
"@types/node-xmpp-client": "^3.1.11",
"@types/source-map-support": "^0.4.0",
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^3.4.0",
Expand Down Expand Up @@ -84,6 +85,7 @@
"debug": "^4.3.1",
"dotenv": "^8.2.0",
"lodash": "^4.17.20",
"node-xmpp-client": "^3.2.0",
"reflect-metadata": "^0.1.13",
"roadman": "^0.3.12"
}
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ import {BadgeResolver} from './badge/Badge.resolver';
import NotificationResolver from './notification/notification.resolver';

export const getNotificationResolvers = () => [NotificationResolver, BadgeResolver];

// notificationRoadman
38 changes: 33 additions & 5 deletions src/notification/notification.methods.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import {Notification, NotificationModel} from './notification.model';
import {compact, flattenDeep, isEmpty} from 'lodash';
import {compact, isEmpty} from 'lodash';

import {awaitTo} from 'couchset/dist/utils';
import {createUpdate} from 'couchset';
import {updateBadgeCount} from '../badge/Badge.methods';
import {UserModel} from '@roadmanjs/auth';
import {sendXmppMessage} from '../xmpp';
import {log} from '@roadmanjs/logs';

export const createNotification = async (
notification: Notification,
silent = false
): Promise<Notification | null> => {
try {
const {owner} = notification;
// create notification
// send notification if not silent, using owner

const newNotification = await createUpdate<Notification>({
model: NotificationModel,
data: {
Expand All @@ -22,6 +21,10 @@ export const createNotification = async (
...notification, // id and owner if it exists
});

if (!silent) {
await broadcastNotification(notification);
}

return newNotification;
} catch (error) {
console.error(error);
Expand Down Expand Up @@ -90,3 +93,28 @@ export const deleteNotification = async (id: string): Promise<boolean> => {
return false;
}
};

export const broadcastNotification = async (notification: Notification): Promise<boolean> => {
try {
const {owner} = notification;

const [error, user] = await awaitTo(UserModel.findById(owner));

if (error || !user) {
throw new Error('error getting user');
}

// send notification to user
// jabber
if (user.jid) {
await sendXmppMessage(user.jid, notification.message);
}
// push notification
// ....

return true;
} catch (error) {
log('error broadcastNotification', error);
return false;
}
};
54 changes: 54 additions & 0 deletions src/xmpp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {log} from '@roadmanjs/logs';
import xmpp from 'node-xmpp-client';

const jid = process.env.XMPP_JID;
const password = process.env.XMPP_PASSWORD;
const host = process.env.XMPP_HOST;
const port = +process.env.XMPP_PORT || 5222;
let xmppClient: xmpp.Client | null = null;

export const getXmppClient = (): xmpp.Client | null => {
if (!jid || !password || !host) {
log(
'No XMPP credentials found, please set XMPP_JID, XMPP_PASSWORD, and XMPP_HOST in your .env file.'
);
return null;
}
if (!xmppClient) {
xmppClient = new xmpp.Client({
jid,
password,
host,
port,
});

// Event listener for successful connection
xmppClient.on('online', () => {
console.log('Connected to XMPP server');
});

// Event listener for errors
xmppClient.on('error', (err) => {
console.error('Error:', err);
});

// Event listener for disconnection
xmppClient.on('offline', () => {
console.log('Disconnected from XMPP server');
});

// Event listener for receiving messages
xmppClient.on('stanza', (stanza) => {
console.log('Received:', stanza.toString());
});
}
return xmppClient;
};

export const sendXmppMessage = (to: string, message: string): void => {
const client = getXmppClient();
if (client) {
// @ts-ignore
client.send(new xmpp.Element('message', {to, type: 'chat'}).c('body').t(message));
}
};
Loading

0 comments on commit a6f0585

Please sign in to comment.