Skip to content

Commit 7819913

Browse files
committed
Re-add ability to convert OSM URLs into geo-uris
1 parent cb1f929 commit 7819913

File tree

8 files changed

+69
-11
lines changed

8 files changed

+69
-11
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Updated translations
1212
- #3123: Contacts do not show up online until chat is opened with them.
1313
- #3156: Add function to prevent drag stutter effect over iframes when resize is called in overlay mode
14+
- #3161: Re-add ability to convert OpenStreetMap URLs into `geo:` URIs in sent messages and fix issue #1850
1415
- #3165: Use configured nickname in profile view in the control box
1516

1617
- New config option [stanza_timeout](https://conversejs.org/docs/html/configuration.html#stanza-timeout)

docs/source/configuration.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -928,17 +928,17 @@ Accepts a string or array of strings. Any query strings from URLs that match thi
928928
geouri_regex
929929
------------
930930

931-
* Default: ``/https:\/\/www.openstreetmap.org\/.*#map=[0-9]+\/([\-0-9.]+)\/([\-0-9.]+)\S*/g``
931+
* Default: ``/https:\/\/www\.openstreetmap\.org\/\#map=(?<zoom>[0-9]+)\/(?<lat>[\-0-9.]+)\/(?<lon>[\-0-9.]+)/g``
932932

933933
Regular expression used to extract geo coordinates from links to openstreetmap.
934934

935935
geouri_replacement
936936
------------------
937937

938-
* Default: ``'https://www.openstreetmap.org/?mlat=$1&mlon=$2#map=18/$1/$2'``
938+
* Default: ``https://www.openstreetmap.org/?mlat=$<lat>&mlon=$<lon>#map=$<zoom>/$<lat>/$<lon>``
939939

940-
String used to replace geo-URIs with. Ought to be a link to osm or similar. ``$1`` and ``$2`` is replaced by
941-
latitude and longitude respectively.
940+
String used to replace geo-URIs with. Ought to be a link to osm or similar. ``$<lat>``, ``$<lon>``, and ``$<zoom>`` is replaced by
941+
latitude, longitude, and zoom level respectively.
942942

943943
hide_muc_participants
944944
---------------------

src/headless/plugins/chat/model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ const ChatBox = ModelWithContact.extend({
840840
const is_spoiler = !!this.get('composing_spoiler');
841841
const origin_id = u.getUniqueId();
842842
const text = attrs?.body;
843-
const body = text ? u.shortnamesToUnicode(text) : undefined;
843+
const body = text ? u.httpToGeoUri(u.shortnamesToUnicode(text), _converse) : undefined;
844844
attrs = Object.assign({}, attrs, {
845845
'from': _converse.bare_jid,
846846
'fullname': _converse.xmppstatus.get('fullname'),

src/headless/plugins/muc/muc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ const ChatRoomMixin = {
10071007
[text, references] = this.parseTextForReferences(attrs.body);
10081008
}
10091009
const origin_id = getUniqueId();
1010-
const body = text ? u.shortnamesToUnicode(text) : undefined;
1010+
const body = text ? u.httpToGeoUri(u.shortnamesToUnicode(text), _converse) : undefined;
10111011
attrs = Object.assign({}, attrs, {
10121012
body,
10131013
is_spoiler,

src/headless/shared/settings/constants.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export const DEFAULT_SETTINGS = {
4545
connection_options: {},
4646
credentials_url: null, // URL from where login credentials can be fetched
4747
discover_connection_methods: true,
48-
geouri_regex: /https\:\/\/www.openstreetmap.org\/.*#map=[0-9]+\/([\-0-9.]+)\/([\-0-9.]+)\S*/g,
49-
geouri_replacement: 'https://www.openstreetmap.org/?mlat=$1&mlon=$2#map=18/$1/$2',
48+
geouri_regex: /https:\/\/www\.openstreetmap\.org\/\#map=(?<zoom>[0-9]+)\/(?<lat>[\-0-9.]+)\/(?<lon>[\-0-9.]+)/g,
49+
geouri_replacement: 'https://www.openstreetmap.org/?mlat=$<lat>&mlon=$<lon>#map=$<zoom>/$<lat>/$<lon>',
5050
i18n: undefined,
5151
jid: undefined,
5252
keepalive: true,

src/headless/utils/core.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,11 @@ export function getUniqueId (suffix) {
485485
}
486486
}
487487

488+
u.httpToGeoUri = function(text) {
489+
const replacement = 'geo:$<lat>,$<lon>;z=$<zoom>';
490+
const geouri_regex = settings_api.get("geouri_regex");
491+
return geouri_regex ? text.replace(geouri_regex, replacement) : text;
492+
};
488493

489494
/**
490495
* Clears the specified timeout and interval.

src/plugins/chatview/tests/messages.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,25 @@ describe("A Chat Message", function () {
262262
expect(_converse.api.chatboxes.get).not.toHaveBeenCalled();
263263
}));
264264

265-
it("will render Openstreetmap-URL from geo-URI",
265+
it("will render Openstreetmap-URL from geo-URI with zoom level",
266+
mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
267+
268+
await mock.waitForRoster(_converse, 'current', 1);
269+
const message = "geo:37.786971,-122.399677;z=7";
270+
const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
271+
await mock.openChatBoxFor(_converse, contact_jid);
272+
const view = _converse.chatboxviews.get(contact_jid);
273+
spyOn(view.model, 'sendMessage').and.callThrough();
274+
await mock.sendMessage(view, message);
275+
await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-msg').length, 1000);
276+
expect(view.model.sendMessage).toHaveBeenCalled();
277+
const msg = sizzle('.chat-content .chat-msg:last .chat-msg__text', view).pop();
278+
await u.waitUntil(() => msg.innerHTML.replace(/\<!-.*?-\>/g, '') ===
279+
'<a target="_blank" rel="noopener" href="https://www.openstreetmap.org/?mlat=37.786971&amp;'+
280+
'mlon=-122.399677#map=7/37.786971/-122.399677">https://www.openstreetmap.org/?mlat=37.786971&amp;mlon=-122.399677#map=7/37.786971/-122.399677</a>');
281+
}));
282+
283+
it("will render Openstreetmap-URL from geo-URI without zoom level",
266284
mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
267285

268286
await mock.waitForRoster(_converse, 'current', 1);
@@ -277,7 +295,41 @@ describe("A Chat Message", function () {
277295
const msg = sizzle('.chat-content .chat-msg:last .chat-msg__text', view).pop();
278296
await u.waitUntil(() => msg.innerHTML.replace(/\<!-.*?-\>/g, '') ===
279297
'<a target="_blank" rel="noopener" href="https://www.openstreetmap.org/?mlat=37.786971&amp;'+
280-
'mlon=-122.399677#map=18/37.786971/-122.399677">https://www.openstreetmap.org/?mlat=37.786971&amp;mlon=-122.399677#map=18/37.786971/-122.399677</a>');
298+
'mlon=-122.399677#map=/37.786971/-122.399677">https://www.openstreetmap.org/?mlat=37.786971&amp;mlon=-122.399677#map=/37.786971/-122.399677</a>');
299+
}));
300+
301+
it("will not render geo-URI from Openstreetmap-URL",
302+
mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
303+
304+
await mock.waitForRoster(_converse, 'current', 1);
305+
const message = "https://www.openstreetmap.org/#map=7/51.724/6.630";
306+
const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
307+
await mock.openChatBoxFor(_converse, contact_jid);
308+
const view = _converse.chatboxviews.get(contact_jid);
309+
spyOn(view.model, 'sendMessage').and.callThrough();
310+
await mock.sendMessage(view, message);
311+
await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-msg').length, 1000);
312+
expect(view.model.sendMessage).toHaveBeenCalled();
313+
const msg = sizzle('.chat-content .chat-msg:last .chat-msg__text', view).pop();
314+
await u.waitUntil(() => msg.innerHTML.replace(/\<!-.*?-\>/g, '') ===
315+
'geo:51.724,6.630;z=7');
316+
}));
317+
318+
it("will not render geo-URI from Openstreetmap-URL if additional information present in URL",
319+
mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
320+
321+
await mock.waitForRoster(_converse, 'current', 1);
322+
const message = "https://www.openstreetmap.org/directions?engine=fossgis_osrm_car&route=51.507%2C-0.128%3B52.517%2C13.389#map=7/51.724/6.630";
323+
const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
324+
await mock.openChatBoxFor(_converse, contact_jid);
325+
const view = _converse.chatboxviews.get(contact_jid);
326+
spyOn(view.model, 'sendMessage').and.callThrough();
327+
await mock.sendMessage(view, message);
328+
await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-msg').length, 1000);
329+
expect(view.model.sendMessage).toHaveBeenCalled();
330+
const msg = sizzle('.chat-content .chat-msg:last .chat-msg__text', view).pop();
331+
await u.waitUntil(() => msg.innerHTML.replace(/\<!-.*?-\>/g, '') ===
332+
'https://www.openstreetmap.org/directions?engine=fossgis_osrm_car&amp;route=51.507%2C-0.128%3B52.517%2C13.389#map=7/51.724/6.630');
281333
}));
282334

283335
it("can be a carbon message, as defined in XEP-0280",

src/shared/rich-text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export class RichText extends String {
159159
* the start of the message body text.
160160
*/
161161
addMapURLs (text, offset) {
162-
const regex = /geo:([\-0-9.]+),([\-0-9.]+)(?:,([\-0-9.]+))?(?:\?(.*))?/g;
162+
const regex = /geo:(?<lat>[\-0-9.]+),(?<lon>[\-0-9.]+)(?:,(?:[\-0-9.]+))?(?:;\w+(?<!\bz)=\w+)*(?:;z=(?<zoom>\d+))?(?:;\w+=\w+)*/g;
163163
const matches = text.matchAll(regex);
164164
for (const m of matches) {
165165
this.addTemplateResult(

0 commit comments

Comments
 (0)