Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-add ability to convert OSM URLs into geo-uris #3161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Updated translations
- #3123: Contacts do not show up online until chat is opened with them.
- #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 and fix issue #1850
- #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
8 changes: 4 additions & 4 deletions docs/source/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -928,17 +928,17 @@ 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: ``/https:\/\/www\.openstreetmap\.org\/\#map=(?<zoom>[0-9]+)\/(?<lat>[\-0-9.]+)\/(?<lon>[\-0-9.]+)/g``

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

geouri_replacement
------------------

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

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

hide_muc_participants
---------------------
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
4 changes: 2 additions & 2 deletions src/headless/shared/settings/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ 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_replacement: 'https://www.openstreetmap.org/?mlat=$1&mlon=$2#map=18/$1/$2',
geouri_regex: /https:\/\/www\.openstreetmap\.org\/\#map=(?<zoom>[0-9]+)\/(?<lat>[\-0-9.]+)\/(?<lon>[\-0-9.]+)/g,
geouri_replacement: 'https://www.openstreetmap.org/?mlat=$<lat>&mlon=$<lon>#map=$<zoom>/$<lat>/$<lon>',
i18n: undefined,
jid: undefined,
keepalive: true,
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:$<lat>,$<lon>;z=$<zoom>';
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
56 changes: 54 additions & 2 deletions src/plugins/chatview/tests/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,25 @@ describe("A Chat Message", function () {
expect(_converse.api.chatboxes.get).not.toHaveBeenCalled();
}));

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

await mock.waitForRoster(_converse, 'current', 1);
const message = "geo:37.786971,-122.399677;z=7";
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/?mlat=37.786971&amp;'+
'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>');
}));

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

await mock.waitForRoster(_converse, 'current', 1);
Expand All @@ -277,7 +295,41 @@ describe("A Chat Message", function () {
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/?mlat=37.786971&amp;'+
'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>');
'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>');
}));

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=7/51.724/6.630";
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:51.724,6.630;z=7');
}));

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

await mock.waitForRoster(_converse, 'current', 1);
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";
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, '') ===
'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');
}));

it("can be a carbon message, as defined in XEP-0280",
Expand Down
2 changes: 1 addition & 1 deletion src/shared/rich-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class RichText extends String {
* the start of the message body text.
*/
addMapURLs (text, offset) {
const regex = /geo:([\-0-9.]+),([\-0-9.]+)(?:,([\-0-9.]+))?(?:\?(.*))?/g;
const regex = /geo:(?<lat>[\-0-9.]+),(?<lon>[\-0-9.]+)(?:,(?:[\-0-9.]+))?(?:;\w+(?<!\bz)=\w+)*(?:;z=(?<zoom>\d+))?(?:;\w+=\w+)*/g;
const matches = text.matchAll(regex);
for (const m of matches) {
this.addTemplateResult(
Expand Down