mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-04-14 11:36:45 +00:00
Improve replica set setup and add UnifiedPush test endpoint
This commit is contained in:
parent
cadab4d29b
commit
8db7d53337
4 changed files with 58 additions and 4 deletions
|
|
@ -25,7 +25,7 @@ services:
|
|||
mongo:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
- NODE_DB_URI=mongodb://mongo/habitrpg
|
||||
- NODE_DB_URI=mongodb://mongo/habitrpg?replicaSet=rs
|
||||
networks:
|
||||
- habitica
|
||||
ports:
|
||||
|
|
@ -38,7 +38,7 @@ services:
|
|||
restart: unless-stopped
|
||||
command: ["--replSet", "rs", "--bind_ip_all", "--port", "27017"]
|
||||
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
|
||||
timeout: 30s
|
||||
start_period: 0s
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ services:
|
|||
depends_on:
|
||||
- mongo
|
||||
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
|
||||
- INVITE_ONLY=false # change to `true` after registration of initial users, to restrict further registrations
|
||||
networks:
|
||||
|
|
@ -21,7 +21,7 @@ services:
|
|||
hostname: mongo
|
||||
command: ["--replSet", "rs", "--bind_ip_all", "--port", "27017"]
|
||||
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
|
||||
timeout: 30s
|
||||
start_period: 0s
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import {
|
|||
NotFound,
|
||||
} from '../../libs/errors';
|
||||
import { model as PushDevice } from '../../models/pushDevice';
|
||||
import { sendNotification as sendPushNotification } from '../../libs/pushNotifications';
|
||||
|
||||
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
|
||||
* @api {delete} /api/v3/user/push-devices/:regId remove a push device from a user
|
||||
|
|
|
|||
|
|
@ -144,6 +144,7 @@ async function sendUnifiedPushNotification (user, pushDevice, details, payload)
|
|||
const body = {
|
||||
title: details.title,
|
||||
message: details.message,
|
||||
body: details.message,
|
||||
identifier: details.identifier,
|
||||
payload,
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue