Improve replica set setup and add UnifiedPush test endpoint

This commit is contained in:
Your Name 2025-09-25 15:04:45 -06:00
parent cadab4d29b
commit 8db7d53337
4 changed files with 58 additions and 4 deletions

View file

@ -25,7 +25,7 @@ services:
mongo: mongo:
condition: service_healthy condition: service_healthy
environment: environment:
- NODE_DB_URI=mongodb://mongo/habitrpg - NODE_DB_URI=mongodb://mongo/habitrpg?replicaSet=rs
networks: networks:
- habitica - habitica
ports: ports:
@ -38,7 +38,7 @@ services:
restart: unless-stopped restart: unless-stopped
command: ["--replSet", "rs", "--bind_ip_all", "--port", "27017"] command: ["--replSet", "rs", "--bind_ip_all", "--port", "27017"]
healthcheck: healthcheck:
test: echo "try { rs.status() } catch (err) { rs.initiate() }" | mongosh --port 27017 --quiet test: echo "try { rs.status() } catch (err) { rs.initiate({_id: 'rs', members: [{ _id: 0, host: 'mongo:27017' }]}) }" | mongosh --port 27017 --quiet
interval: 10s interval: 10s
timeout: 30s timeout: 30s
start_period: 0s start_period: 0s

View file

@ -8,7 +8,7 @@ services:
depends_on: depends_on:
- mongo - mongo
environment: environment:
- NODE_DB_URI=mongodb://mongo/habitica # this only needs to be adapted if using a separate database - NODE_DB_URI=mongodb://mongo/habitica?replicaSet=rs # this only needs to be adapted if using a separate database
- BASE_URL=http://127.0.0.1:8080 # change this to the URL under which your instance will be reachable - BASE_URL=http://127.0.0.1:8080 # change this to the URL under which your instance will be reachable
- INVITE_ONLY=false # change to `true` after registration of initial users, to restrict further registrations - INVITE_ONLY=false # change to `true` after registration of initial users, to restrict further registrations
networks: networks:
@ -21,7 +21,7 @@ services:
hostname: mongo hostname: mongo
command: ["--replSet", "rs", "--bind_ip_all", "--port", "27017"] command: ["--replSet", "rs", "--bind_ip_all", "--port", "27017"]
healthcheck: healthcheck:
test: echo "try { rs.status() } catch (err) { rs.initiate() }" | mongosh --port 27017 --quiet test: echo "try { rs.status() } catch (err) { rs.initiate({_id: 'rs', members: [{ _id: 0, host: 'mongo:27017' }]}) }" | mongosh --port 27017 --quiet
interval: 10s interval: 10s
timeout: 30s timeout: 30s
start_period: 0s start_period: 0s

View file

@ -3,6 +3,7 @@ import {
NotFound, NotFound,
} from '../../libs/errors'; } from '../../libs/errors';
import { model as PushDevice } from '../../models/pushDevice'; import { model as PushDevice } from '../../models/pushDevice';
import { sendNotification as sendPushNotification } from '../../libs/pushNotifications';
const api = {}; const api = {};
@ -57,6 +58,58 @@ api.addPushDevice = {
}, },
}; };
/**
* @apiIgnore
* @api {post} /api/v3/user/push-devices/test Send a test push notification
* @apiName UserSendTestPushNotification
* @apiGroup User
*
* @apiParam (Body) {String} [regId] The id of a specific push device to target
*
* @apiSuccess {String} message Success message
*/
api.sendUnifiedPushTest = {
method: 'POST',
url: '/user/push-devices/test',
middlewares: [authWithHeaders()],
async handler (req, res) {
const { user } = res.locals;
const regId = req.body?.regId;
const pushDevices = user.pushDevices?.toObject ? user.pushDevices.toObject() : user.pushDevices;
let unifiedPushDevices = (pushDevices || []).filter(device => device?.type === 'unifiedpush');
if (regId) {
unifiedPushDevices = unifiedPushDevices.filter(device => device.regId === regId);
}
if (unifiedPushDevices.length === 0) {
throw new NotFound(res.t('pushDeviceNotFound'));
}
const notificationTitle = req.body?.title
|| res.t('unifiedPushTestTitle', { defaultValue: 'Habitica UnifiedPush Test' });
const notificationMessage = req.body?.message
|| res.t('unifiedPushTestMessage', { defaultValue: 'This is a test UnifiedPush notification from Habitica.' });
const successMessage = res.t('unifiedPushTestSent', { defaultValue: 'UnifiedPush test notification sent.' });
const userForPush = user.toObject ? user.toObject() : { ...user };
userForPush._id = user._id;
userForPush.pushDevices = unifiedPushDevices;
await sendPushNotification(userForPush, {
identifier: 'unifiedPushTestNotification',
title: notificationTitle,
message: notificationMessage,
payload: {
message: notificationMessage,
},
});
res.respond(200, null, successMessage);
},
};
/** /**
* @apiIgnore * @apiIgnore
* @api {delete} /api/v3/user/push-devices/:regId remove a push device from a user * @api {delete} /api/v3/user/push-devices/:regId remove a push device from a user

View file

@ -144,6 +144,7 @@ async function sendUnifiedPushNotification (user, pushDevice, details, payload)
const body = { const body = {
title: details.title, title: details.title,
message: details.message, message: details.message,
body: details.message,
identifier: details.identifier, identifier: details.identifier,
payload, payload,
}; };