Fix in IPGeocode (download, and fallback). Schema improvements

This commit is contained in:
vjrj 2018-01-11 12:28:55 +01:00
parent 67a2dfbd42
commit df05b33e82
6 changed files with 77 additions and 11 deletions

View file

@ -2,6 +2,7 @@
import { Meteor } from 'meteor/meteor';
import maxmind from 'maxmind';
import fs from 'fs';
process.env.HTTP_FORWARDED_COUNT = Meteor.settings.private.proxies_count;
if (!Meteor.isDevelopment) {
@ -17,7 +18,14 @@ function isPrivateIP(ip) {
(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;
// Warning: Meteor cannot access to this.connection with arrow functions
@ -37,9 +45,13 @@ export function localize() {
// TODO: cron download GeoLite-City
// http://dev.maxmind.com/geoip/geoip2/geolite2/
const location = IPGeocoder.get(clientIP);
// console.log(location);
return location;
const geo = IPGeocoder.get(clientIP);
// console.warn(geo);
if (geo.location && geo.location.latitude && geo.location.longitude) {
return geo;
}
// geoIP fallback, Madrid
return { location: { latitude: 40.4146500, longitude: -3.7004000 } };
}
Meteor.methods({

View file

@ -1,6 +1,9 @@
/* global Migrations */
/* eslint-disable import/no-absolute-path */
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(() => {
// 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('1,rerun');
// Migrations.migrateTo('2,rerun');
});