replace geoURIs (e.g. from Convesations) by links to openstreetmap (#1054)

* replace geoURIs by link to openstreetmap

* Added testcase
This commit is contained in:
ChaosKid42 2018-03-31 18:29:01 +02:00 committed by JC Brand
parent 8504722172
commit fce9ee0db9
7 changed files with 62 additions and 2 deletions

View File

@ -6,6 +6,10 @@
The UI is now based on Bootstrap4 and Flexbox is used extensively. The UI is now based on Bootstrap4 and Flexbox is used extensively.
## New Features
- geo-URIs (e.g. from Conversations) are now replaced by links to openstreetmap (works in reverse also)
## Configuration changes ## Configuration changes
* Removed the `xhr_custom_status` and `xhr_custom_status_url` configuration * Removed the `xhr_custom_status` and `xhr_custom_status_url` configuration

View File

@ -651,6 +651,21 @@ logged in user, otherwise the user's vCard will be fetched.
.. _`hide_muc_server`: .. _`hide_muc_server`:
geouri_regex
----------------
* Default: ``/https:\/\/www.openstreetmap.org\/.*#map=[0-9]+\/([\-0-9.]+)\/([\-0-9.]+)\S*/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'``
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.
hide_muc_server hide_muc_server
--------------- ---------------

View File

@ -2687,6 +2687,33 @@
expect($unreadMsgCount.html()).toBe('1'); expect($unreadMsgCount.html()).toBe('1');
done(); done();
})); }));
it("will render Openstreetmap-URL from geo-URI",
mock.initConverseWithPromises(
null, ['rosterGroupsFetched'], {},
function (done, _converse) {
test_utils.createContacts(_converse, 'current');
var base_url = document.URL.split(window.location.pathname)[0];
var message = "geo:37.786971,-122.399677";
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(_converse, contact_jid);
var view = _converse.chatboxviews.get(contact_jid);
spyOn(view, 'sendMessage').and.callThrough();
test_utils.sendMessage(view, message);
test_utils.waitUntil(function () {
return $(view.el).find('.chat-content').find('.chat-message').length;
}, 1000).then(function () {
expect(view.sendMessage).toHaveBeenCalled();
var msg = $(view.el).find('.chat-content').find('.chat-message').last().find('.chat-msg-content');
expect(msg.html()).toEqual(
'<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.7869'+
'71&amp;mlon=-122.399677#map=18/37.786971/-122.399677</a>');
done();
});
}));
}); });
}); });
})); }));

View File

@ -615,6 +615,8 @@
template = attrs.is_spoiler ? tpl_spoiler_message : tpl_message; template = attrs.is_spoiler ? tpl_spoiler_message : tpl_message;
} }
text = u.geoUriToHttp(text, _converse);
const msg_time = moment(attrs.time) || moment; const msg_time = moment(attrs.time) || moment;
const msg = u.stringToElement(template( const msg = u.stringToElement(template(
_.extend(this.getExtraMessageTemplateAttributes(attrs), { _.extend(this.getExtraMessageTemplateAttributes(attrs), {
@ -846,7 +848,7 @@
'fullname': _.isEmpty(fullname) ? _converse.bare_jid : fullname, 'fullname': _.isEmpty(fullname) ? _converse.bare_jid : fullname,
'sender': 'me', 'sender': 'me',
'time': moment().format(), 'time': moment().format(),
'message': emojione.shortnameToUnicode(text), 'message': u.httpToGeoUri(emojione.shortnameToUnicode(text), _converse),
'is_spoiler': is_spoiler 'is_spoiler': is_spoiler
}; };
if (is_spoiler) { if (is_spoiler) {

View File

@ -306,6 +306,8 @@
expose_rid_and_sid: false, expose_rid_and_sid: false,
filter_by_resource: false, filter_by_resource: false,
forward_messages: false, forward_messages: false,
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',
hide_offline_users: false, hide_offline_users: false,
include_offline_state: false, include_offline_state: false,
jid: undefined, jid: undefined,

View File

@ -675,7 +675,7 @@
* Parameters: * Parameters:
* (String) text: The message text to be sent. * (String) text: The message text to be sent.
*/ */
text = emojione.shortnameToUnicode(text) text = u.httpToGeoUri(emojione.shortnameToUnicode(text), _converse)
const msgid = _converse.connection.getUniqueId(); const msgid = _converse.connection.getUniqueId();
const msg = $msg({ const msg = $msg({
to: this.model.get('jid'), to: this.model.get('jid'),

View File

@ -705,5 +705,15 @@
evt.initEvent(name, bubbles, cancelable); evt.initEvent(name, bubbles, cancelable);
el.dispatchEvent(evt); el.dispatchEvent(evt);
}; };
u.geoUriToHttp = function(text, _converse) {
const regex = /geo:([\-0-9.]+),([\-0-9.]+)(?:,([\-0-9.]+))?(?:\?(.*))?/g;
return text.replace(regex, _converse.geouri_replacement);
};
u.httpToGeoUri = function(text, _converse) {
const replacement = 'geo:$1,$2';
return text.replace(_converse.geouri_regex, replacement);
};
return u; return u;
})); }));