todos-contra-el-fuego-web/imports/api/Rest/Rest.js
vjrj 637a749e8d meteor3: REST API async migration COMPLETE — smoke green on Meteor 3.1 + Mongo 7
All 12 Flutter REST endpoints byte-identical to the 1.6.1.1 baseline, running on
Meteor 3.1 against dockerized MongoDB 7. The 3.x jump works end-to-end.

- Rest.js: every endpoint action -> async; all Mongo calls -> *Async
  (findOneAsync/countAsync/fetchAsync/upsertAsync/removeAsync).
- Helpers to async: countRealFires (accepts array|cursor, forEachAsync->for-of),
  firesUnion (fetchAsync), fireFromHash/findOrCreateFire (await, dropped Fibers
  Promise.await), subscriptionsInsert/subscriptionsRemove, upsertFalsePositive,
  falsePositives.insert, subscriptions.update method, countFiresInRegions.
- getFires: countAsync() on a $near cursor throws on the mongodb 6 driver
  ($near not allowed in the aggregation countDocuments uses) -> fetch once and
  use array length for total.
- Route order: json-routes 3.0 matches in registration order, so
  mobile/subscriptions/all/:token/:mobileToken is now registered BEFORE
  :token/:mobileToken/:subsId (else GET .../all/x/y hit the delete-only route).
2026-07-14 06:21:35 +02:00

464 lines
14 KiB
JavaScript

/* global Restivus */
/* eslint-disable import/no-absolute-path */
import { Meteor } from 'meteor/meteor';
import { NumberBetween } from '/imports/modules/server/other-checks';
import Fires from '/imports/api/Fires/Fires';
import { check } from 'meteor/check';
import SiteSettings from '/imports/api/SiteSettings/SiteSettings';
import ActiveFiresCollection from '/imports/api/ActiveFires/ActiveFires';
import { countRealFires } from '/imports/api/ActiveFires/server/countFires';
import { whichAreFalsePositives, firesUnion, zoneToUnion } from '/imports/api/FalsePositives/server/publications';
import { fireFromHash } from '/imports/api/Fires/server/publications.js';
import FalsePositives from '/imports/api/FalsePositives/FalsePositives';
import Industries from '/imports/api/Industries/Industries';
import Subscriptions from '/imports/api/Subscriptions/Subscriptions';
import jsend from 'jsend';
import { Random } from 'meteor/random';
import { subscriptionsInsert } from '/imports/api/Subscriptions/methods.js';
import { Mongo } from 'meteor/mongo';
import { upsertFalsePositive } from '/imports/api/FalsePositives/methods.js';
const debug = false;
const uptime = new Date();
const restivusError = (code, message) => ({ status: 'error', statusCode: code, body: message });
function failMsg(msg) {
return restivusError(500, `Unexpected error in REST call: ${msg}`);
}
function fail(e) {
return restivusError(500, `Unexpected error in REST call: ${e}`);
}
function defaultFailParams(e) {
return restivusError(400, `Wrong REST params: ${e}`);
}
function checkAuthToken(token) {
if (!Meteor.settings.private.internalApiToken || token !== Meteor.settings.private.internalApiToken) {
const message = 'Unauthorized auth token in REST API';
console.warn(message);
return restivusError(401, message);
}
return undefined;
}
function checkLatLonDist(km, lat, lng) {
check(lng, NumberBetween(-180, 180));
check(lat, NumberBetween(-90, 90));
check(km, NumberBetween(0, Meteor.isDevelopment ? 1000 : 100));
}
if (!Meteor.settings.private.internalApiToken) {
console.warn('Meteor.settings.private.internalApiToken is not configured so we don\'t enable our REST API');
} else {
// export
const apiV1 = new Restivus({
useDefaultAuth: true,
apiPath: 'api',
version: 'v1',
prettyJson: true
});
// Generates: POST on /api/users and GET, DELETE /api/users/:id for
// Meteor.users collection
/* apiV1.addCollection(Meteor.users, {
* excludedEndpoints: ['getAll', 'put', 'delete', 'patch', 'get'],
* routeOptions: {
* authRequired: true
* },
* endpoints: {
* post: {
* authRequired: false
* },
* delete: {
* roleRequired: 'admin'
* }
* }
* }); */
apiV1.addCollection(Fires, {
excludedEndpoints: ['put', 'post', 'patch', 'delete'],
// excludedEndpoints: ['getAll', 'put', 'post', 'patch', 'delete'],
routeOptions: {
authRequired: false
},
endpoints: {
get: {
action: async function getFire() {
return Fires.findOneAsync(new Meteor.Collection.ObjectID(this.urlParams.id));
}
}
}
});
// Maps to: /api/v1/status/last-fire-check
apiV1.addRoute('status/last-fire-check', { authRequired: false }, {
get: async function get() {
return SiteSettings.findOneAsync({ name: 'last-fire-check' });
}
});
// Maps to: /api/v1/status/last-fire-detected
apiV1.addRoute('status/last-fire-detected', { authRequired: false }, {
get: async function get() {
return ActiveFiresCollection.findOneAsync({}, { sort: { when: -1 } });
}
});
// Maps to: /api/v1/status/active-fires-count
apiV1.addRoute('status/active-fires-count', { authRequired: false }, {
get: async function get() {
return { total: await ActiveFiresCollection.find({}).countAsync() };
}
});
// Maps to: /api/v1/status/uptime
apiV1.addRoute('status/uptime', { authRequired: false }, {
get: function get() {
return { ms: new Date() - uptime };
}
});
async function getFires(route, full) {
const lat = Number(route.urlParams.lat);
const lng = Number(route.urlParams.lng);
const km = Number(route.urlParams.km);
const { token } = route.urlParams;
try {
checkLatLonDist(km, lat, lng);
check(token, String);
} catch (e) {
return defaultFailParams(e);
}
const failed = checkAuthToken(token);
if (failed) return failed;
if (debug) console.log(`Query for fires in ${lat}, ${lng} in ${km} km radius`);
const fires = ActiveFiresCollection.find({
ourid: {
$near: {
$geometry: {
type: 'Point',
coordinates: [lng, lat]
},
$maxDistance: km * 1000,
$minDistance: 0
}
}
}, {
fields: {
lat: 1,
lon: 1,
when: 1,
type: 1, // modis, viirs, vecinal
scan: 1,
track: 1
}
});
// Meteor 3 / mongodb driver 6: countAsync() on a $near cursor fails
// ($near/$geoNear not allowed inside the aggregation countDocuments uses).
// Fetch once and derive the total from the array length instead.
const firesA = await fires.fetchAsync();
const result = { total: firesA.length, real: await countRealFires(firesA) };
if (debug) console.log(`Query for fires in ${lat}, ${lng} in ${km} km radius ${result}`);
if (!full) {
return result;
}
let union;
if (firesA.length > 0) {
union = await firesUnion(firesA);
} else {
union = zoneToUnion(lat, lng, km);
}
const falsePos = whichAreFalsePositives(FalsePositives, union);
const industries = whichAreFalsePositives(Industries, union);
result.fires = firesA;
result.industries = await industries.fetchAsync();
result.falsePos = await falsePos.fetchAsync();
return result;
}
// Maps to: /api/v1/fires-in/:lat/:lng/:km
// 100 km max
// Ex: http://127.0.0.1:3000/api/v1/fires-in/token/38.736946/-9.142685/100
apiV1.addRoute('fires-in/:token/:lat/:lng/:km', { authRequired: false }, {
get: async function get() {
return getFires(this, false);
}
});
apiV1.addRoute('fires-in-full/:token/:lat/:lng/:km', { authRequired: false }, {
get: async function get() {
return getFires(this, true);
}
});
// Add mobile user:
// curl -X POST http://localhost:3000/api/v1/users/mobile -d "token: thisAppAutToken" -d "mobileToken: user-mobile-firebase-token"
// Response:
//
// https://docs.meteor.com/api/passwords.html#Accounts-createUser
apiV1.addRoute('mobile/users', { authRequired: false }, {
post: async function post() {
const { token, mobileToken, lang } = this.bodyParams;
try {
check(token, String);
check(lang, String);
check(mobileToken, String);
} catch (e) {
return defaultFailParams(e);
}
const failed = checkAuthToken(token);
if (failed) return failed;
let username;
const already = Meteor.users.find({ fireBaseToken: mobileToken });
const alreadyCount = await already.countAsync();
if (alreadyCount > 1) {
return restivusError(500, 'Unexpected error in REST call: several users with that mobile token?');
} else if (alreadyCount === 1) {
username = (await already.fetchAsync())[0].username;
} else {
do {
username = Random.id(15);
} while (await Meteor.users.find({ username }).countAsync() !== 0);
}
// FIXME check valid lang
const now = new Date();
const result = await Meteor.users.upsertAsync({
fireBaseToken: mobileToken
}, {
$set: {
username,
fireBaseToken: mobileToken,
lang,
profile: { },
createdAt: now,
updatedAt: now
}
});
if (debug) {
console.log(this.bodyParams);
console.log(this.urlParams);
console.log(this.queryParams);
}
const upsertUser = await Meteor.users.findOneAsync({ username });
if (debug) console.log(upsertUser);
return jsend.success({
upsertResult: result,
username,
userId: upsertUser._id,
lang: upsertUser.lang,
mobileToken: upsertUser.fireBaseToken
});
}
});
// max sitance: 100km
apiV1.addRoute('mobile/subscriptions', { authRequired: false }, {
post: async function post() {
const {
token,
mobileToken,
id,
lat,
lon,
distance
} = this.bodyParams;
let oid;
try {
check(token, String);
check(mobileToken, String);
check(id, String);
oid = new Mongo.ObjectID(id);
check(oid, Meteor.Collection.ObjectID);
check(lat, Number);
check(lon, Number);
check(distance, Number);
checkLatLonDist(distance, lat, lon);
} catch (e) {
if (debug) console.log(e);
return defaultFailParams(e);
}
const failed = checkAuthToken(token);
if (failed) return failed;
const user = await Meteor.users.findOneAsync({ fireBaseToken: mobileToken });
if (!user) return failMsg('User not found');
const newSubs = {};
newSubs._id = new Meteor.Collection.ObjectID(id);
newSubs.location = {};
newSubs.location.lat = lat;
newSubs.location.lon = lon;
newSubs.distance = distance;
let result;
try {
result = await subscriptionsInsert(newSubs, user._id, 'mobile');
} catch (e) {
return fail(e);
}
return jsend.success({ subsId: result._str });
}
});
// NOTE: json-routes 3.0 (Meteor 3) matches routes in registration order, so
// the literal `all/:token/:mobileToken` route MUST be registered before the
// `:token/:mobileToken/:subsId` route — otherwise a GET to
// `.../all/<tok>/<mob>` matches the 3-param route (delete-only) and restivus
// replies "API endpoint does not exist".
apiV1.addRoute('mobile/subscriptions/all/:token/:mobileToken', { authRequired: false }, {
get: async function get() {
const { token, mobileToken } = this.urlParams;
try {
check(token, String);
check(mobileToken, String);
} catch (e) {
return defaultFailParams(e);
}
const failed = checkAuthToken(token);
if (failed) return failed;
const user = await Meteor.users.findOneAsync({ fireBaseToken: mobileToken });
if (!user) return failMsg('User not found');
const result = Subscriptions.find({ owner: user._id });
return jsend.success({ subscriptions: await result.fetchAsync(), count: await result.countAsync() });
},
delete: async function delAll() {
const { token, mobileToken } = this.urlParams;
try {
check(token, String);
check(mobileToken, String);
} catch (e) {
return defaultFailParams(e);
}
const failed = checkAuthToken(token);
if (failed) return failed('Auth api check failed');
if (await Meteor.users.find({ fireBaseToken: mobileToken }).countAsync() !== 1) return fail;
const user = await Meteor.users.findOneAsync({ fireBaseToken: mobileToken });
if (!user) return failMsg('User not found');
const toRemove = await Subscriptions.find({ owner: user._id }).countAsync();
await Subscriptions.removeAsync({ owner: user._id });
return jsend.success({ count: toRemove });
}
});
apiV1.addRoute('mobile/subscriptions/:token/:mobileToken/:subsId', { authRequired: false }, {
delete: async function del() {
const {
token,
mobileToken,
subsId
} = this.urlParams;
try {
check(token, String);
check(mobileToken, String);
check(subsId, String);
} catch (e) {
return defaultFailParams(e);
}
const failed = checkAuthToken(token);
if (failed) return failed;
const user = await Meteor.users.findOneAsync({ fireBaseToken: mobileToken });
if (!user) return failMsg('User not found');
try {
await Subscriptions.removeAsync({ owner: user._id, _id: new Meteor.Collection.ObjectID(subsId) });
} catch (e) {
return fail(e);
}
return jsend.success({});
}
});
apiV1.addRoute('status/subs-public-union/:token', { authRequired: false }, {
get: async function get() {
const { token } = this.urlParams;
try {
check(token, String);
} catch (e) {
return defaultFailParams(e);
}
const failed = checkAuthToken(token);
if (failed) return failed;
const currentUnion = await SiteSettings.findOneAsync({ name: 'subs-public-union' });
const userSubsBounds = await SiteSettings.findOneAsync({ name: 'subs-public-union-bounds' });
return jsend.success({ union: currentUnion, bounds: userSubsBounds });
}
});
apiV1.addRoute('mobile/falsepositive', { authRequired: false }, {
post: async function post() {
const {
token,
mobileToken,
sealed,
type
} = this.bodyParams;
try {
check(token, String);
check(mobileToken, String);
check(sealed, String);
check(type, String);
} catch (e) {
if (debug) console.log(e);
return defaultFailParams(e);
}
const failed = checkAuthToken(token);
if (failed) return failed;
const user = await Meteor.users.findOneAsync({ fireBaseToken: mobileToken });
if (!user) return failMsg('User not found');
console.log(`Marking hash fire as '${type}' false positive: ${sealed}`);
const fireC = await fireFromHash(sealed, { id: sealed });
if (fireC && typeof fireC === 'object') {
const fire = (await fireC.fetchAsync())[0];
console.log(`Marking fire as false positive: ${fire._id}`);
const result = await upsertFalsePositive(type, user._id, fire);
return jsend.success({ upsert: result });
}
return failMsg('Cannot mark fire as false positive');
}
});
}