0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Stored geolocation for member on creation (#15320)

refs https://github.com/TryGhost/Team/issues/1826

Geolocation was prev. loaded after member was created and updated on existing member. this was mostly due to historical context where we couldn't store data on magic link token.
Since email alerts go out at the time of member creation, this flow missed out on attaching member's location to email. 
This change -

- stores request ip when a member asks for magic link in the token
- loads request ip from token when member uses magic link, and for new members loads their geolocation and stores it with member creation
This commit is contained in:
Rishabh Garg 2022-08-26 00:45:34 +05:30 committed by GitHub
parent b16ad52401
commit 1bf70bf3c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View file

@ -2,6 +2,7 @@ const {Router} = require('express');
const body = require('body-parser'); const body = require('body-parser');
const MagicLink = require('@tryghost/magic-link'); const MagicLink = require('@tryghost/magic-link');
const errors = require('@tryghost/errors'); const errors = require('@tryghost/errors');
const logging = require('@tryghost/logging');
const MemberAnalyticsService = require('@tryghost/member-analytics-service'); const MemberAnalyticsService = require('@tryghost/member-analytics-service');
const MembersAnalyticsIngress = require('@tryghost/members-analytics-ingress'); const MembersAnalyticsIngress = require('@tryghost/members-analytics-ingress');
@ -204,7 +205,7 @@ module.exports = function MembersAPI({
} }
async function getMemberDataFromMagicLinkToken(token) { async function getMemberDataFromMagicLinkToken(token) {
const {email, labels = [], name = '', oldEmail, newsletters, attribution} = await getTokenDataFromMagicLinkToken(token); const {email, labels = [], name = '', oldEmail, newsletters, attribution, reqIp} = await getTokenDataFromMagicLinkToken(token);
if (!email) { if (!email) {
return null; return null;
} }
@ -220,7 +221,19 @@ module.exports = function MembersAPI({
} }
return member; return member;
} }
const newMember = await users.create({name, email, labels, newsletters, attribution});
let geolocation;
if (reqIp) {
try {
geolocation = JSON.stringify(await geolocationService.getGeolocationFromIP(reqIp));
} catch (err) {
logging.warn(err);
// no-op, we don't want to stop anything working due to
// geolocation lookup failing
}
}
const newMember = await users.create({name, email, labels, newsletters, attribution, geolocation});
// Notify staff users of new free member signup // Notify staff users of new free member signup
if (labsService.isSet('emailAlerts')) { if (labsService.isSet('emailAlerts')) {

View file

@ -27,6 +27,7 @@ module.exports = class RouterController {
* @param {import('@tryghost/members-stripe-service')} deps.stripeAPIService * @param {import('@tryghost/members-stripe-service')} deps.stripeAPIService
* @param {import('@tryghost/member-attribution')} deps.memberAttributionService * @param {import('@tryghost/member-attribution')} deps.memberAttributionService
* @param {any} deps.tokenService * @param {any} deps.tokenService
* @param {any} deps.sendEmailWithMagicLink
* @param {{isSet(name: string): boolean}} deps.labsService * @param {{isSet(name: string): boolean}} deps.labsService
*/ */
constructor({ constructor({
@ -401,7 +402,9 @@ module.exports = class RouterController {
} }
} else { } else {
const tokenData = _.pick(req.body, ['labels', 'name', 'newsletters']); const tokenData = _.pick(req.body, ['labels', 'name', 'newsletters']);
if (req.ip) {
tokenData.reqIp = req.ip;
}
// Save attribution data in the tokenData // Save attribution data in the tokenData
tokenData.attribution = this._memberAttributionService.getAttribution(req.body.urlHistory); tokenData.attribution = this._memberAttributionService.getAttribution(req.body.urlHistory);