Skip to content

Commit 19d0750

Browse files
committed
Re-add ability to convert OSM URLs into geo-uris
1 parent 0a338fe commit 19d0750

File tree

7 files changed

+52
-5
lines changed

7 files changed

+52
-5
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Remove call to `api.confirm` in `@converse/headless`
99
- Generate TypeScript declaration files into `dist/types`
1010
- #3156: Add function to prevent drag stutter effect over iframes when resize is called in overlay mode
11+
- #3161: Re-add ability to convert OpenStreetMap URLs into `geo:` URIs in sent messages
1112

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

docs/source/configuration.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,9 +934,14 @@ logged in user, otherwise the user's vCard will be fetched.
934934
geouri_regex
935935
------------
936936

937-
* Default: ``/https:\/\/www.openstreetmap.org\/.*#map=[0-9]+\/([\-0-9.]+)\/([\-0-9.]+)\S*/g``
937+
* Default: ``undefined``
938+
939+
Regular expression used to extract geo coordinates from links to OpenStreetMap and similar services.
940+
It allows to convert OpenStreetMap URLs into `geo:` URIs in sent messages.
941+
942+
The default ``undefined`` means that no conversion is done.
938943

939-
Regular expression used to extract geo coordinates from links to openstreetmap.
944+
E.g. ``/https\:\/\/www.openstreetmap.org\/.*#map=[0-9]+\/([\-0-9.]+)\/([\-0-9.]+)\S*/g``
940945

941946
geouri_replacement
942947
------------------

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ 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,
48+
geouri_regex: undefined,
4949
geouri_replacement: 'https://www.openstreetmap.org/?mlat=$1&mlon=$2#map=18/$1/$2',
5050
i18n: undefined,
5151
jid: undefined,

src/headless/utils/core.js

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

487+
u.httpToGeoUri = function(text) {
488+
const replacement = 'geo:$1,$2';
489+
const geouri_regex = settings_api.get("geouri_regex");
490+
return geouri_regex ? text.replace(geouri_regex, replacement) : text;
491+
};
487492

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

src/plugins/chatview/tests/messages.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,42 @@ describe("A Chat Message", function () {
280280
'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>');
281281
}));
282282

283+
it("will not render geo-URI from Openstreetmap-URL",
284+
mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
285+
286+
await mock.waitForRoster(_converse, 'current', 1);
287+
const message = "https://www.openstreetmap.org/#map=18/68.24920/13.61230";
288+
const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
289+
await mock.openChatBoxFor(_converse, contact_jid);
290+
const view = _converse.chatboxviews.get(contact_jid);
291+
spyOn(view.model, 'sendMessage').and.callThrough();
292+
await mock.sendMessage(view, message);
293+
await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-msg').length, 1000);
294+
expect(view.model.sendMessage).toHaveBeenCalled();
295+
const msg = sizzle('.chat-content .chat-msg:last .chat-msg__text', view).pop();
296+
await u.waitUntil(() => msg.innerHTML.replace(/\<!-.*?-\>/g, '') ===
297+
'<a target="_blank" rel="noopener" href='+
298+
'"https://www.openstreetmap.org/#map=18/68.24920/13.61230">https://www.openstreetmap.org/#map=18/68.24920/13.61230</a>');
299+
}));
300+
301+
it("will render geo-URI from Openstreetmap-URL",
302+
mock.initConverse(['chatBoxesFetched'], {'geouri_regex': /https\:\/\/www.openstreetmap.org\/.*#map=[0-9]+\/([\-0-9.]+)\/([\-0-9.]+)\S*/g},
303+
async function (_converse) {
304+
305+
await mock.waitForRoster(_converse, 'current', 1);
306+
const message = "https://www.openstreetmap.org/#map=18/68.24920/13.61230";
307+
const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
308+
await mock.openChatBoxFor(_converse, contact_jid);
309+
const view = _converse.chatboxviews.get(contact_jid);
310+
spyOn(view.model, 'sendMessage').and.callThrough();
311+
await mock.sendMessage(view, message);
312+
await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-msg').length, 1000);
313+
expect(view.model.sendMessage).toHaveBeenCalled();
314+
const msg = sizzle('.chat-content .chat-msg:last .chat-msg__text', view).pop();
315+
await u.waitUntil(() => msg.innerHTML.replace(/\<!-.*?-\>/g, '') ===
316+
'geo:68.24920,13.61230');
317+
}));
318+
283319
it("can be a carbon message, as defined in XEP-0280",
284320
mock.initConverse([], {}, async function (_converse) {
285321

0 commit comments

Comments
 (0)