From aa0aa93c4a74792e84f820f6a838453468fc1e45 Mon Sep 17 00:00:00 2001
From: IanM <16573496+imorland@users.noreply.github.com>
Date: Wed, 7 Feb 2024 11:10:17 +0000
Subject: [PATCH] fix: error when trying to display map if no zip or lat/lon
(#36)
---
js/src/forum/components/ZipCodeMap.js | 32 +++++++++++++-----------
js/src/forum/extenders/extendPostMeta.js | 28 ++++++++++-----------
js/src/forum/models/IPInfo.ts | 27 +++++++++++---------
resources/locale/en.yml | 1 +
4 files changed, 47 insertions(+), 41 deletions(-)
diff --git a/js/src/forum/components/ZipCodeMap.js b/js/src/forum/components/ZipCodeMap.js
index 8c88ef9..4204ec7 100644
--- a/js/src/forum/components/ZipCodeMap.js
+++ b/js/src/forum/components/ZipCodeMap.js
@@ -21,16 +21,20 @@ export default class ZipCodeMap extends Component {
this.data = null;
- if (this.ipInfo.zipCode()) {
+ if (this.ipInfo.latitude() && this.ipInfo.longitude()) {
+ this.searchLatLon();
+ } else if (this.ipInfo.zipCode()) {
this.searchZip();
} else {
- this.searchLatLon();
+ this.data = { unknown: true };
}
}
view() {
if (this.loading) {
return ;
+ } else if (this.data && this.data.unknown) {
+ return
{app.translator.trans('fof-geoip.forum.map_modal.not_enough_data')}
;
} else if (!this.data) {
return ;
}
@@ -38,7 +42,7 @@ export default class ZipCodeMap extends Component {
return ;
}
- searchZip() {
+ searchLatLon() {
if (this.loading) return;
this.loading = true;
@@ -46,17 +50,16 @@ export default class ZipCodeMap extends Component {
return addResources().then(
app
.request({
- url: `https://nominatim.openstreetmap.org/search`,
+ url: `https://nominatim.openstreetmap.org/reverse`,
method: 'GET',
params: {
- q: this.ipInfo.zipCode(),
- countrycodes: this.ipInfo.countryCode(),
- limit: 1,
+ lat: this.ipInfo.latitude(),
+ lon: this.ipInfo.longitude(),
format: 'json',
},
})
.then((data) => {
- this.data = data[0];
+ this.data = data;
this.loading = false;
m.redraw();
@@ -64,7 +67,7 @@ export default class ZipCodeMap extends Component {
);
}
- searchLatLon() {
+ searchZip() {
if (this.loading) return;
this.loading = true;
@@ -72,16 +75,17 @@ export default class ZipCodeMap extends Component {
return addResources().then(
app
.request({
- url: `https://nominatim.openstreetmap.org/reverse`,
+ url: `https://nominatim.openstreetmap.org/search`,
method: 'GET',
params: {
- lat: this.ipInfo.latitude(),
- lon: this.ipInfo.longitude(),
+ q: this.ipInfo.zipCode(),
+ countrycodes: this.ipInfo.countryCode(),
+ limit: 1,
format: 'json',
},
})
.then((data) => {
- this.data = data;
+ this.data = data[0];
this.loading = false;
m.redraw();
@@ -100,6 +104,6 @@ export default class ZipCodeMap extends Component {
attribution: '© OpenStreetMap contributors',
}).addTo(this.map);
- L.marker([bounding[0], bounding[2]]).addTo(this.map).bindPopup(displayName).openPopup();
+ L.marker([bounding[0], bounding[2]]).addTo(this.map).openPopup();
}
}
diff --git a/js/src/forum/extenders/extendPostMeta.js b/js/src/forum/extenders/extendPostMeta.js
index 485b547..3775f78 100644
--- a/js/src/forum/extenders/extendPostMeta.js
+++ b/js/src/forum/extenders/extendPostMeta.js
@@ -46,7 +46,7 @@ export default function extendPostMeta() {
const ipAddr = post.data.attributes.ipAddress;
if (ipInformation && ipAddr) {
- const { description, threat, image, zip, country } = getIPData(ipInformation);
+ const { description, threat, image } = getIPData(ipInformation);
items.add(
'ipInfo',
@@ -59,20 +59,18 @@ export default function extendPostMeta() {
100
);
- if (zip && country) {
- items.add(
- 'mapButton',
-
- ,
- 90
- );
- }
+ items.add(
+ 'mapButton',
+
+ ,
+ 90
+ );
}
return items;
diff --git a/js/src/forum/models/IPInfo.ts b/js/src/forum/models/IPInfo.ts
index 9bccd03..37a9b18 100644
--- a/js/src/forum/models/IPInfo.ts
+++ b/js/src/forum/models/IPInfo.ts
@@ -6,43 +6,46 @@ export default class IPInfo extends Model {
}
countryCode() {
- return Model.attribute('countryCode').call(this);
+ return Model.attribute('countryCode').call(this);
}
zipCode() {
- return Model.attribute('zipCode').call(this);
+ return Model.attribute('zipCode').call(this);
}
latitude() {
- return Model.attribute('latitude').call(this);
+ return Model.attribute('latitude').call(this);
}
longitude() {
- return Model.attribute('longitude').call(this);
+ return Model.attribute('longitude').call(this);
}
isp() {
- return Model.attribute('isp').call(this);
+ return Model.attribute('isp').call(this);
}
organization() {
- return Model.attribute('organization').call(this);
+ return Model.attribute('organization').call(this);
}
as() {
- return Model.attribute('as').call(this);
+ return Model.attribute('as').call(this);
}
mobile() {
- return Model.attribute('mobile').call(this);
+ return Model.attribute('mobile').call(this);
}
threatLevel() {
- return Model.attribute('threatLevel').call(this);
+ return Model.attribute('threatLevel').call(this);
}
threatTypes() {
- const raw = Model.attribute('threatTypes').call(this);
+ const raw = Model.attribute('threatTypes').call(this);
+ if (!raw) {
+ return [];
+ }
try {
return JSON.parse(raw);
} catch (error) {
@@ -51,11 +54,11 @@ export default class IPInfo extends Model {
}
error() {
- return Model.attribute('error').call(this);
+ return Model.attribute('error').call(this);
}
dataProvider() {
- return Model.attribute('dataProvider').call(this);
+ return Model.attribute('dataProvider').call(this);
}
createdAt() {
diff --git a/resources/locale/en.yml b/resources/locale/en.yml
index 5c97167..133cde3 100644
--- a/resources/locale/en.yml
+++ b/resources/locale/en.yml
@@ -39,3 +39,4 @@ fof-geoip:
threat_level: "Threat Level"
threat_types: "Threat Types"
error: "Error"
+ not_enough_data: "Not enough data to draw a map"