Skip to content

Commit 316e983

Browse files
committed
Re-add ability to convert OSM URLs into geo-uris
1 parent cdda140 commit 316e983

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
@@ -10,6 +10,7 @@
1010
- Removed documentation about the no longer implemented `fullname` option.
1111
- Updated translations
1212
- #3156: Add function to prevent drag stutter effect over iframes when resize is called in overlay mode
13+
- #3161: Re-add ability to convert OpenStreetMap URLs into `geo:` URIs in sent messages
1314
- #3165: Use configured nickname in profile view in the control box
1415

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

docs/source/configuration.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,9 +928,14 @@ 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: ``undefined``
932+
933+
Regular expression used to extract geo coordinates from links to OpenStreetMap and similar services.
934+
It allows to convert OpenStreetMap URLs into `geo:` URIs in sent messages.
935+
936+
The default ``undefined`` means that no conversion is done.
932937

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

935940
geouri_replacement
936941
------------------

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
@@ -485,6 +485,11 @@ export function getUniqueId (suffix) {
485485
}
486486
}
487487

488+
u.httpToGeoUri = function(text) {
489+
const replacement = 'geo:$1,$2';
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: 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)