Fix in IPGeocode (download, and fallback). Schema improvements
This commit is contained in:
parent
67a2dfbd42
commit
df05b33e82
6 changed files with 77 additions and 11 deletions
|
|
@ -3,6 +3,8 @@ A boilerplate for products.
|
||||||
|
|
||||||
[Read the Documentation](http://cleverbeagle.com/pup)
|
[Read the Documentation](http://cleverbeagle.com/pup)
|
||||||
|
|
||||||
---
|
#### GeoIP
|
||||||
|
|
||||||
Need help and want to stay accountable building your product? [Check out Clever Beagle](http://cleverbeagle.com).
|
Configure mirror and cron on geoip database. See http://dev.maxmind.com/geoip/geoip2/geolite2/
|
||||||
|
|
||||||
|
---
|
||||||
|
|
|
||||||
|
|
@ -121,7 +121,7 @@ Meteor.publish('activefiresmyloc', function activeInMyLoc(zoom, lat, lng, height
|
||||||
if (lat && lng) {
|
if (lat && lng) {
|
||||||
return activefires(zoom, lat, lng, height, width);
|
return activefires(zoom, lat, lng, height, width);
|
||||||
}
|
}
|
||||||
const location = localize();
|
const geo = localize();
|
||||||
// console.log(`${location.latitude}, ${location.longitude}`);
|
// console.log(`${location.latitude}, ${location.longitude}`);
|
||||||
return activefires(zoom, location.latitude, location.longitude, height, width);
|
return activefires(zoom, geo.location.latitude, geo.location.longitude, height, width);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
/* eslint-disable consistent-return */
|
/* eslint-disable consistent-return */
|
||||||
|
/* eslint-disable import/no-absolute-path */
|
||||||
|
|
||||||
import { Mongo } from 'meteor/mongo';
|
import { Mongo } from 'meteor/mongo';
|
||||||
import SimpleSchema from 'simpl-schema';
|
import SimpleSchema from 'simpl-schema';
|
||||||
|
import { defaultCreatedAt, defaultUpdateAt } from '/imports/api/Utility/Utils.js';
|
||||||
|
|
||||||
const Subscriptions = new Mongo.Collection('subscriptions', { idGeneration: 'MONGO' });
|
const Subscriptions = new Mongo.Collection('subscriptions', { idGeneration: 'MONGO' });
|
||||||
|
|
||||||
|
|
@ -74,8 +76,11 @@ Subscriptions.schema = new SimpleSchema({
|
||||||
'location.lon': Number,
|
'location.lon': Number,
|
||||||
geo: LocationSchema,
|
geo: LocationSchema,
|
||||||
distance: Number,
|
distance: Number,
|
||||||
|
chatId: { type: Number, optional: true }, // only in 'telegram' type
|
||||||
owner: String,
|
owner: String,
|
||||||
type: String
|
type: String,
|
||||||
|
createdAt: defaultCreatedAt,
|
||||||
|
updatedAt: defaultUpdateAt
|
||||||
});
|
});
|
||||||
|
|
||||||
Subscriptions.attachSchema(Subscriptions.schema);
|
Subscriptions.attachSchema(Subscriptions.schema);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
/* eslint-disable consistent-return */
|
/* eslint-disable consistent-return */
|
||||||
/* eslint-disable import/no-absolute-path */
|
/* eslint-disable import/no-absolute-path */
|
||||||
|
|
||||||
import { Meteor } from 'meteor/meteor';
|
import { Meteor } from 'meteor/meteor';
|
||||||
import SimpleSchema from 'simpl-schema';
|
import SimpleSchema from 'simpl-schema';
|
||||||
import { defaultCreatedAt, defaultUpdateAt } from '/imports/api/Utility/Utils.js';
|
import { defaultCreatedAt, defaultUpdateAt } from '/imports/api/Utility/Utils.js';
|
||||||
|
|
@ -105,7 +106,7 @@ const schemaUser = new SimpleSchema({
|
||||||
'name.first': String,
|
'name.first': String,
|
||||||
'name.last': String, */
|
'name.last': String, */
|
||||||
lang: { type: String, optional: true },
|
lang: { type: String, optional: true },
|
||||||
telegramChatId: { type: Number, optional: true },
|
telegramChatId: { type: SimpleSchema.Integer, optional: true },
|
||||||
telegramUsername: { type: String, optional: true },
|
telegramUsername: { type: String, optional: true },
|
||||||
telegramFirstName: { type: String, optional: true },
|
telegramFirstName: { type: String, optional: true },
|
||||||
telegramLanguageCode: { type: String, optional: true },
|
telegramLanguageCode: { type: String, optional: true },
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import { Meteor } from 'meteor/meteor';
|
import { Meteor } from 'meteor/meteor';
|
||||||
import maxmind from 'maxmind';
|
import maxmind from 'maxmind';
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
process.env.HTTP_FORWARDED_COUNT = Meteor.settings.private.proxies_count;
|
process.env.HTTP_FORWARDED_COUNT = Meteor.settings.private.proxies_count;
|
||||||
if (!Meteor.isDevelopment) {
|
if (!Meteor.isDevelopment) {
|
||||||
|
|
@ -17,7 +18,14 @@ function isPrivateIP(ip) {
|
||||||
(parts[0] === '192' && parts[1] === '168');
|
(parts[0] === '192' && parts[1] === '168');
|
||||||
}
|
}
|
||||||
|
|
||||||
const IPGeocoder = maxmind.openSync(`${process.env.PWD}/private/GeoLite2-City.mmdb`);
|
const dbpath = '/usr/local/share/maxmind-geolite2/GeoLite2-City.mmdb';
|
||||||
|
// const dbpath = `${process.env.PWD}/private/GeoLite2-City.mmdb`;
|
||||||
|
|
||||||
|
if (!fs.existsSync(dbpath)) {
|
||||||
|
console.error(`Maxmind db not found ${dbpath}, download via cron with https://www.npmjs.com/package/maxmind-geolite2-mirror`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const IPGeocoder = maxmind.openSync(dbpath);
|
||||||
export default IPGeocoder;
|
export default IPGeocoder;
|
||||||
|
|
||||||
// Warning: Meteor cannot access to this.connection with arrow functions
|
// Warning: Meteor cannot access to this.connection with arrow functions
|
||||||
|
|
@ -37,9 +45,13 @@ export function localize() {
|
||||||
|
|
||||||
// TODO: cron download GeoLite-City
|
// TODO: cron download GeoLite-City
|
||||||
// http://dev.maxmind.com/geoip/geoip2/geolite2/
|
// http://dev.maxmind.com/geoip/geoip2/geolite2/
|
||||||
const location = IPGeocoder.get(clientIP);
|
const geo = IPGeocoder.get(clientIP);
|
||||||
// console.log(location);
|
// console.warn(geo);
|
||||||
return location;
|
if (geo.location && geo.location.latitude && geo.location.longitude) {
|
||||||
|
return geo;
|
||||||
|
}
|
||||||
|
// geoIP fallback, Madrid
|
||||||
|
return { location: { latitude: 40.4146500, longitude: -3.7004000 } };
|
||||||
}
|
}
|
||||||
|
|
||||||
Meteor.methods({
|
Meteor.methods({
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
/* global Migrations */
|
/* global Migrations */
|
||||||
/* eslint-disable import/no-absolute-path */
|
/* eslint-disable import/no-absolute-path */
|
||||||
import { Meteor } from 'meteor/meteor';
|
import { Meteor } from 'meteor/meteor';
|
||||||
|
import { Accounts } from 'meteor/accounts-base';
|
||||||
|
import randomHex from 'crypto-random-hex';
|
||||||
|
import UserSubsToFiresCollection from '/imports/api/Subscriptions/Subscriptions';
|
||||||
|
|
||||||
Meteor.startup(() => {
|
Meteor.startup(() => {
|
||||||
// https://github.com/percolatestudio/meteor-migrations
|
// https://github.com/percolatestudio/meteor-migrations
|
||||||
|
|
@ -25,7 +28,50 @@ Meteor.startup(() => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Migrations.add({
|
||||||
|
version: 2,
|
||||||
|
up: function migrateSubsForeignKey() {
|
||||||
|
UserSubsToFiresCollection.find({ owner: null }).forEach((sub) => {
|
||||||
|
console.log(`Migrating subs of chatId: ${sub.chatId}`);
|
||||||
|
const subsUser = Meteor.users.findOne({ telegramChatId: sub.chatId });
|
||||||
|
if (subsUser) {
|
||||||
|
console.log(`Migrating linking to user: ${JSON.stringify(subsUser)}`);
|
||||||
|
UserSubsToFiresCollection.update({ _id: sub._id }, { $set: { owner: subsUser._id } });
|
||||||
|
} else {
|
||||||
|
// create user with chatId and def language
|
||||||
|
const username = `tel${sub.chatId.toString().replace(/^-/, '')}`;
|
||||||
|
console.log(`Linking to new user: ${username}`);
|
||||||
|
const newUserId = Accounts.createUser({
|
||||||
|
username,
|
||||||
|
password: randomHex(50),
|
||||||
|
profile: { name: {} }
|
||||||
|
});
|
||||||
|
Meteor.users.update({ _id: newUserId }, {
|
||||||
|
$set: {
|
||||||
|
emails: [],
|
||||||
|
roles: ['user'],
|
||||||
|
telegramChatId: sub.chatId,
|
||||||
|
lang: 'es'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
UserSubsToFiresCollection.update({ _id: sub._id }, { $set: { owner: newUserId } });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Migrations.add({
|
||||||
|
version: 3,
|
||||||
|
up: function emptySubsTypes() {
|
||||||
|
UserSubsToFiresCollection.find({ type: null }).forEach((sub) => {
|
||||||
|
UserSubsToFiresCollection.update({ _id: sub._id }, { $set: { type: 'telegram' } });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// Set createdAt in users & subs
|
||||||
Migrations.migrateTo('latest');
|
Migrations.migrateTo('latest');
|
||||||
|
|
||||||
// Migrations.migrateTo('1,rerun');
|
// Migrations.migrateTo('2,rerun');
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue