Skip to content

Commit

Permalink
Re-add ability to convert OSM URLs into geo-uris
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaosKid42 committed Jun 9, 2023
1 parent 1e4937b commit f190518
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Removed documentation about the no longer implemented `fullname` option.
- Updated translations
- #3156: Add function to prevent drag stutter effect over iframes when resize is called in overlay mode
- #3161: Re-add ability to convert OpenStreetMap URLs into `geo:` URIs in sent messages
- #3165: Use configured nickname in profile view in the control box

- New config option [stanza_timeout](https://conversejs.org/docs/html/configuration.html#stanza-timeout)
Expand Down
9 changes: 7 additions & 2 deletions docs/source/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,14 @@ Accepts a string or array of strings. Any query strings from URLs that match thi
geouri_regex
------------

* Default: ``/https:\/\/www.openstreetmap.org\/.*#map=[0-9]+\/([\-0-9.]+)\/([\-0-9.]+)\S*/g``
* Default: ``undefined``

Regular expression used to extract geo coordinates from links to OpenStreetMap and similar services.
It allows to convert OpenStreetMap URLs into `geo:` URIs in sent messages.

The default ``undefined`` means that no conversion is done.

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

geouri_replacement
------------------
Expand Down
2 changes: 1 addition & 1 deletion src/headless/plugins/chat/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ const ChatBox = ModelWithContact.extend({
const is_spoiler = !!this.get('composing_spoiler');
const origin_id = u.getUniqueId();
const text = attrs?.body;
const body = text ? u.shortnamesToUnicode(text) : undefined;
const body = text ? u.httpToGeoUri(u.shortnamesToUnicode(text), _converse) : undefined;
attrs = Object.assign({}, attrs, {
'from': _converse.bare_jid,
'fullname': _converse.xmppstatus.get('fullname'),
Expand Down
2 changes: 1 addition & 1 deletion src/headless/plugins/muc/muc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ const ChatRoomMixin = {
[text, references] = this.parseTextForReferences(attrs.body);
}
const origin_id = getUniqueId();
const body = text ? u.shortnamesToUnicode(text) : undefined;
const body = text ? u.httpToGeoUri(u.shortnamesToUnicode(text), _converse) : undefined;
attrs = Object.assign({}, attrs, {
body,
is_spoiler,
Expand Down
2 changes: 1 addition & 1 deletion src/headless/shared/settings/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const DEFAULT_SETTINGS = {
connection_options: {},
credentials_url: null, // URL from where login credentials can be fetched
discover_connection_methods: true,
geouri_regex: /https\:\/\/www.openstreetmap.org\/.*#map=[0-9]+\/([\-0-9.]+)\/([\-0-9.]+)\S*/g,
geouri_regex: undefined,
geouri_replacement: 'https://www.openstreetmap.org/?mlat=$1&mlon=$2#map=18/$1/$2',
i18n: undefined,
jid: undefined,
Expand Down
5 changes: 5 additions & 0 deletions src/headless/utils/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ export function getUniqueId (suffix) {
}
}

u.httpToGeoUri = function(text) {
const replacement = 'geo:$1,$2';
const geouri_regex = settings_api.get("geouri_regex");
return geouri_regex ? text.replace(geouri_regex, replacement) : text;
};

/**
* Clears the specified timeout and interval.
Expand Down
36 changes: 36 additions & 0 deletions src/plugins/chatview/tests/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,42 @@ describe("A Chat Message", function () {
'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>');
}));

it("will not render geo-URI from Openstreetmap-URL",
mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {

await mock.waitForRoster(_converse, 'current', 1);
const message = "https://www.openstreetmap.org/#map=18/68.24920/13.61230";
const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
await mock.openChatBoxFor(_converse, contact_jid);
const view = _converse.chatboxviews.get(contact_jid);
spyOn(view.model, 'sendMessage').and.callThrough();
await mock.sendMessage(view, message);
await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-msg').length, 1000);
expect(view.model.sendMessage).toHaveBeenCalled();
const msg = sizzle('.chat-content .chat-msg:last .chat-msg__text', view).pop();
await u.waitUntil(() => msg.innerHTML.replace(/\<!-.*?-\>/g, '') ===
'<a target="_blank" rel="noopener" href='+
'"https://www.openstreetmap.org/#map=18/68.24920/13.61230">https://www.openstreetmap.org/#map=18/68.24920/13.61230</a>');
}));

it("will render geo-URI from Openstreetmap-URL",
mock.initConverse(['chatBoxesFetched'], {'geouri_regex': /https\:\/\/www.openstreetmap.org\/.*#map=[0-9]+\/([\-0-9.]+)\/([\-0-9.]+)\S*/g},
async function (_converse) {

await mock.waitForRoster(_converse, 'current', 1);
const message = "https://www.openstreetmap.org/#map=18/68.24920/13.61230";
const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
await mock.openChatBoxFor(_converse, contact_jid);
const view = _converse.chatboxviews.get(contact_jid);
spyOn(view.model, 'sendMessage').and.callThrough();
await mock.sendMessage(view, message);
await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-msg').length, 1000);
expect(view.model.sendMessage).toHaveBeenCalled();
const msg = sizzle('.chat-content .chat-msg:last .chat-msg__text', view).pop();
await u.waitUntil(() => msg.innerHTML.replace(/\<!-.*?-\>/g, '') ===
'geo:68.24920,13.61230');
}));

it("can be a carbon message, as defined in XEP-0280",
mock.initConverse([], {}, async function (_converse) {

Expand Down

0 comments on commit f190518

Please sign in to comment.