From c79f8b5f8b6865ff5015cea420143473d581b0ff Mon Sep 17 00:00:00 2001 From: vjrj Date: Thu, 18 Jan 2018 10:55:58 +0100 Subject: [PATCH] Added fire - telegram links, for more info --- imports/api/Common/FiresSchema.js | 15 ++++++++--- imports/api/FireAlerts/FireAlerts.js | 29 ++------------------- imports/api/Fires/server/publications.js | 32 ++++++++++++++++-------- imports/startup/server/migrations.js | 13 ++++++++++ imports/ui/pages/Fires/Fires.js | 20 +++++++++------ imports/ui/pages/FiresMap/FiresMap.js | 4 +-- test/encode.test.js | 10 ++++++++ 7 files changed, 72 insertions(+), 51 deletions(-) diff --git a/imports/api/Common/FiresSchema.js b/imports/api/Common/FiresSchema.js index 5bb792b..822a41e 100644 --- a/imports/api/Common/FiresSchema.js +++ b/imports/api/Common/FiresSchema.js @@ -7,15 +7,22 @@ const firesCommonSchema = { ourid: LocationSchema, lat: Number, lon: Number, - address: { type: String, optional: true }, - scan: Number, type: String, when: Date, + + // Neighbour notified fires + + owner: { type: String, optional: true }, + dateformat: { type: String, optional: true }, + + // NASA types + address: { type: String, optional: true }, // reverse geo + scan: { type: Number, optional: true }, track: { type: Number, optional: true }, acq_date: { type: String, optional: true }, acq_time: { type: String, optional: true }, satellite: { type: String, optional: true }, - confidence: { type: Number, optional: true }, + confidence: { type: String, optional: true }, version: { type: String, optional: true }, frp: { type: Number, optional: true }, daynight: { type: String, optional: true }, @@ -23,6 +30,8 @@ const firesCommonSchema = { bright_t31: { type: Number, optional: true }, bright_ti4: { type: Number, optional: true }, bright_ti5: { type: Number, optional: true }, + + // common createdAt: defaultCreatedAt, updatedAt: defaultUpdateAt }; diff --git a/imports/api/FireAlerts/FireAlerts.js b/imports/api/FireAlerts/FireAlerts.js index 640edde..9c4a37d 100644 --- a/imports/api/FireAlerts/FireAlerts.js +++ b/imports/api/FireAlerts/FireAlerts.js @@ -2,6 +2,7 @@ import { Mongo } from 'meteor/mongo'; import SimpleSchema from 'simpl-schema'; +import firesCommonSchema from '../Common/FiresSchema'; const FireAlerts = new Mongo.Collection('avisosfuego', { idGeneration: 'MONGO' }); @@ -17,33 +18,7 @@ FireAlerts.deny({ remove: () => true }); -/* Sample: - * "_id" : ObjectId("5a059c4879095a1adba47507"), - * "chatId" : null, - * "location" : { - * "lat" : 40.2324096, - * "lon" : -3.3514863, - * "text" : null - * }, - * "aviso" : ISODate("2017-11-10T12:32:08.973Z"), - * "dateformat" : "20171110", - * "geo" : { - * "type" : "Point", - * "coordinates" : [ - * -3.3514863, - * 40.2324096 - * ] - * } - * } - * */ - - -FireAlerts.schema = new SimpleSchema({ - location: Object, - 'location.lat': Number, - 'location.lon': Number, - aviso: Date -}); +FireAlerts.schema = new SimpleSchema(firesCommonSchema); FireAlerts.attachSchema(FireAlerts.schema); diff --git a/imports/api/Fires/server/publications.js b/imports/api/Fires/server/publications.js index 6737510..de67f8b 100644 --- a/imports/api/Fires/server/publications.js +++ b/imports/api/Fires/server/publications.js @@ -5,12 +5,12 @@ import { Meteor } from 'meteor/meteor'; import { check } from 'meteor/check'; import urlEnc from '/imports/modules/url-encode'; import { Promise } from 'meteor/promise'; -import FiresCollection from '../Fires'; import NodeGeocoder from 'node-geocoder'; import { gmapServerKey } from '/imports/startup/server/IPGeocoder'; +import FiresCollection from '../Fires'; function findFire(unsealed) { - const fire = FiresCollection.find({ ourid: { type: 'Point', coordinates: [unsealed.lon, unsealed.lat] } }); + const fire = FiresCollection.find({ ourid: { type: 'Point', coordinates: [unsealed.lon, unsealed.lat] }, when: unsealed.when, type: unsealed.type }); return fire; } @@ -30,22 +30,32 @@ Meteor.publish('fireFromHash', function fireFromHash(fireEnc) { // console.log(fireEnc); const unsealed = Promise.await(urlEnc.decrypt(fireEnc)); const w = unsealed.when; + // console.log(w); unsealed.when = new Date(w); + // console.log(unsealed.when); + const c = unsealed.createdAt; + unsealed.createdAt = new Date(c); + const u = unsealed.updatedAt; + unsealed.updatedAt = new Date(u); // console.log(unsealed); FiresCollection.schema.validate(unsealed); const fire = findFire(unsealed); - try { - const rev = Promise.await(geocoder.reverse({ lat: unsealed.lat, lon: unsealed.lon })); - if (rev[0]) { - unsealed.address = rev[0].formattedAddress; + // console.log(`Found: ${fire.count()}`); + if (!unsealed.address) { + try { + const rev = Promise.await(geocoder.reverse({ lat: unsealed.lat, lon: unsealed.lon })); + if (rev[0]) { + unsealed.address = rev[0].formattedAddress; + } + // console.log(unsealed.address); + } catch (reve) { + console.error(reve); } - // console.log(unsealed.address); - } catch (reve) { - console.error(reve); } if (fire.count() === 0) { - const result = FiresCollection.upsert({ ourid: unsealed.ourid }, { $set: unsealed }, { multi: false, upsert: true }); - console.log(JSON.stringify(result)); + // const result = + FiresCollection.upsert({ ourid: unsealed.ourid, when: unsealed.when, type: unsealed.type }, { $set: unsealed }, { multi: false, upsert: true }); + // console.log(JSON.stringify(result)); } return findFire(unsealed); /* console.log(`fires: ${fire.count()}`); diff --git a/imports/startup/server/migrations.js b/imports/startup/server/migrations.js index 80c255d..d8ecff4 100644 --- a/imports/startup/server/migrations.js +++ b/imports/startup/server/migrations.js @@ -4,6 +4,7 @@ import { Meteor } from 'meteor/meteor'; import { Accounts } from 'meteor/accounts-base'; import randomHex from 'crypto-random-hex'; import UserSubsToFiresCollection from '/imports/api/Subscriptions/Subscriptions'; +import FireAlertsCollection from '/imports/api/FireAlerts/FireAlerts'; Meteor.startup(() => { // https://github.com/percolatestudio/meteor-migrations @@ -69,6 +70,18 @@ Meteor.startup(() => { } }); + Migrations.add({ + version: 4, + up: function deleteOldAlertFiresAndIndexes() { + FireAlertsCollection.remove({ createdAd: null }); + const raw = FireAlertsCollection.rawCollection(); + raw.createIndex({ ourid: '2dsphere' }); + raw.createIndex({ when: 1 }); + raw.createIndex({ updatedAt: 1 }); + raw.createIndex({ createdAt: 1 }); + raw.createIndex({ ourid: 1, type: 1 }); + } + }); // Set createdAt in users & subs Migrations.migrateTo('latest'); diff --git a/imports/ui/pages/Fires/Fires.js b/imports/ui/pages/Fires/Fires.js index 3165f9f..b7e6c09 100644 --- a/imports/ui/pages/Fires/Fires.js +++ b/imports/ui/pages/Fires/Fires.js @@ -26,14 +26,17 @@ class Fire extends React.Component { render() { const { loading, fire, t } = this.props; + if (fire && fire.when) { + this.when = moment.tz(fire.when, moment.tz.guess()); + } return (
{!loading &&

{fire.address ? - t('Información adicional sobre fuego detectado en {{where}} el {{when}}', { where: fire.address, when: moment(fire.when).tz(moment.tz.guess()).format('LLLL (z)') }) : - t('Información adicional sobre fuego detectado el {{when}}', { when: moment.utc(fire.when).format('LLLL') })} + t('Información adicional sobre fuego detectado en {{where}} el {{when}}', { where: fire.address, when: this.when.format('LLLL (z)') }) : + t('Información adicional sobre fuego detectado el {{when}}', { when: this.when.format('LLLL (z)') })}

{t('Coordenadas:')} {fire.lat}, {fire.lon}

- {(fire.type === 'modis' || fire.type === 'virrs') && -

{t('Fuego detectado por satélites de la NASA {{when}}', { when: moment(fire.when).fromNow() })}

+ {(fire.type === 'modis' || fire.type === 'viirs') && +

{t('Fuego detectado por satélites de la NASA {{when}}', { when: this.when.fromNow() })}

} + {(fire.type === 'vecinal') && +

{t('Fuego notificado por uno de nuestros usuarios/as {{when}}', { when: this.when.fromNow() })}

+ } {/* TODO: marcar tipo de fuego, industria, etc */}

{t('Comentarios')}

@@ -71,7 +77,7 @@ class Fire extends React.Component {
  • {t('si conoces esta zona y cómo acceder a el fuego (esto puede de ser de ayuda para apagarlo si sigue activo o para investigarlo en un futuro)')}
  • {t('si conoces el motivo por el que comenzó el fuego')}
  • {t('si quieres denunciar algún tipo de ilegalidad, incluso anónimamente')}
  • -
  • {t('etc')}
  • +
  • {t('o cualquier otra información')}
  • diff --git a/imports/ui/pages/FiresMap/FiresMap.js b/imports/ui/pages/FiresMap/FiresMap.js index 08ad493..e21d199 100644 --- a/imports/ui/pages/FiresMap/FiresMap.js +++ b/imports/ui/pages/FiresMap/FiresMap.js @@ -260,9 +260,7 @@ export default translate([], { wait: true })(withTracker(() => { activefires: ActiveFiresCollection.find({}, { reactive: zoom.get() >= MAXZOOMREACTIVE }).fetch(), // activefires: ActiveFiresCollection.find({}).fetch(), activefirestotal: Counter.get('countActiveFires'), - firealerts: FireAlertsCollection.find().fetch().map(doc => ( - { _id: doc._id, lat: doc.location.lat, lon: doc.location.lon } - )), + firealerts: FireAlertsCollection.find().fetch(), center: geolocation.get(), zoom: zoom.get() }; diff --git a/test/encode.test.js b/test/encode.test.js index 1815582..d415693 100644 --- a/test/encode.test.js +++ b/test/encode.test.js @@ -3,6 +3,7 @@ import { chai } from 'meteor/practicalmeteor:chai'; import ActiveFiresCollection from '/imports/api/ActiveFires/ActiveFires'; +import Iron from 'iron'; import urlEnc from '/imports/modules/url-encode'; describe('url encoding', () => { @@ -68,4 +69,13 @@ describe('url encoding', () => { unsealed.updatedAt = new Date(u); chai.expect(unsealed).to.deep.equal(obj); }); + + + it('should decrypt node-red enc objects', async () => { + const sealed = 'Fe26.2**750f04110aef0f20d1093aa3f2a54dac3d7d5cc86e864e2c7d71b0ead88bc5b9*rX653dosl4BiM2L4fmZiiA*xuKj8XojCaS38R6X1EXkXBWjOidpnB0EVgGbdod_4vACHSl1w61hgXkU6SOq8HdURakJKCyIqWrsrdGt6DI6bl1xgfZ4ejlRMV_Keu0-WV0BVqrxbw0NlMN9KPexRDsy15yZou4ExU1yE36PBsfZIYysUrIsXwiBz3D5KvDaW0BnAXZ4sUR5Kyvk8g75QQmBth_LVHaEWE_OMarQUXDvFZ33R2_vM_i89QSj-wzwG5v-lbYrimE_5SMhEngRJFjihtQ_LfQlkH0wrpe5SUIdNL-DzsRxswvY7RIuMKHVLWSy8So66PxCuVJKa-DGvclX7tj7NO9RgNidfqP8U0izTpoV7dJB44Bwi4NOwLKGwNO9NG7jt-KGb2P6FeLTJYq8Mt0qqNsYXWUMpuhgXlKc2SuKT0avh-kYdovFO3YztGg5dz_Asu5hGZtkzRG6oGrvZxU8j7VDDNSQLLHo67Skmqg5_0e6Bp0gqpz13bmCSVvM_IpOkJLIgkRkGAvFoPy-woBqWiBU3NICo0z9X35WJ8j2xFY5niidGPXkP_uo5JbpGkmLH1tL06UUqjXZ5GS7gVhi8ii0vZt5zgaM4z0g4Q**96aa408156b0952f0d90e7c6d3960c06595543ffcfd642274138631b51b03383*BRnu9clkqVeKeXfWVe8i_Dh6TgqstVgV9HafoiLKxks'; + // console.log(Iron.defaults); + const unsealed = await urlEnc.decrypt(sealed); + // console.log(unsealed); + /* chai.expect(unsealed).to.deep.equal(obj); */ + }); });