Merge pull request #10374 from howard-stearns/notification-circumstances

Notification circumstances
This commit is contained in:
Howard Stearns 2017-05-04 16:18:58 -07:00 committed by GitHub
commit fc9e9e1d28
3 changed files with 49 additions and 57 deletions

View file

@ -156,10 +156,8 @@ Column {
function makeFilteredStoryProcessor() { // answer a function(storyData) that adds it to suggestions if it matches function makeFilteredStoryProcessor() { // answer a function(storyData) that adds it to suggestions if it matches
var words = filter.toUpperCase().split(/\s+/).filter(identity); var words = filter.toUpperCase().split(/\s+/).filter(identity);
function suggestable(story) { function suggestable(story) {
if (story.action === 'snapshot') { // We could filter out places we don't want to suggest, such as those where (story.place_name === AddressManager.placename) or (story.username === Account.username).
return true; return true;
}
return story.place_name !== AddressManager.placename; // Not our entry, but do show other entry points to current domain.
} }
function matches(story) { function matches(story) {
if (!words.length) { if (!words.length) {

View file

@ -38,54 +38,7 @@ var METAVERSE_BASE = location.metaverseServerUrl;
// It's totally unnecessary to return to C++ to perform many of these requests, such as DELETEing an old story, // It's totally unnecessary to return to C++ to perform many of these requests, such as DELETEing an old story,
// POSTING a new one, PUTTING a new audience, or GETTING story data. It's far more efficient to do all of that within JS // POSTING a new one, PUTTING a new audience, or GETTING story data. It's far more efficient to do all of that within JS
function request(options, callback) { // cb(error, responseOfCorrectContentType) of url. A subset of npm request. var request = Script.require('request').request;
var httpRequest = new XMLHttpRequest(), key;
// QT bug: apparently doesn't handle onload. Workaround using readyState.
httpRequest.onreadystatechange = function () {
var READY_STATE_DONE = 4;
var HTTP_OK = 200;
if (httpRequest.readyState >= READY_STATE_DONE) {
var error = (httpRequest.status !== HTTP_OK) && httpRequest.status.toString() + ':' + httpRequest.statusText,
response = !error && httpRequest.responseText,
contentType = !error && httpRequest.getResponseHeader('content-type');
if (!error && contentType.indexOf('application/json') === 0) { // ignoring charset, etc.
try {
response = JSON.parse(response);
} catch (e) {
error = e;
}
}
callback(error, response);
}
};
if (typeof options === 'string') {
options = { uri: options };
}
if (options.url) {
options.uri = options.url;
}
if (!options.method) {
options.method = 'GET';
}
if (options.body && (options.method === 'GET')) { // add query parameters
var params = [], appender = (-1 === options.uri.search('?')) ? '?' : '&';
for (key in options.body) {
params.push(key + '=' + options.body[key]);
}
options.uri += appender + params.join('&');
delete options.body;
}
if (options.json) {
options.headers = options.headers || {};
options.headers["Content-type"] = "application/json";
options.body = JSON.stringify(options.body);
}
for (key in options.headers || {}) {
httpRequest.setRequestHeader(key, options.headers[key]);
}
httpRequest.open(options.method, options.uri, true);
httpRequest.send(options.body);
}
function openLoginWindow() { function openLoginWindow() {
if ((HMD.active && Settings.getValue("hmdTabletBecomesToolbar", false)) if ((HMD.active && Settings.getValue("hmdTabletBecomesToolbar", false))

View file

@ -16,6 +16,13 @@
(function () { // BEGIN LOCAL_SCOPE (function () { // BEGIN LOCAL_SCOPE
var request = Script.require('request').request; var request = Script.require('request').request;
var DEBUG = false;
function debug() {
if (!DEBUG) {
return;
}
print('tablet-goto.js:', [].map.call(arguments, JSON.stringify));
}
var gotoQmlSource = "TabletAddressDialog.qml"; var gotoQmlSource = "TabletAddressDialog.qml";
var buttonName = "GOTO"; var buttonName = "GOTO";
@ -39,6 +46,7 @@
switch (message.method) { switch (message.method) {
case 'request': case 'request':
request(message.params, function (error, data) { request(message.params, function (error, data) {
debug('rpc', request, 'error:', error, 'data:', data);
response.error = error; response.error = error;
response.result = data; response.result = data;
tablet.sendToQml(response); tablet.sendToQml(response);
@ -100,10 +108,24 @@
button.clicked.connect(onClicked); button.clicked.connect(onClicked);
tablet.screenChanged.connect(onScreenChanged); tablet.screenChanged.connect(onScreenChanged);
var stories = {}; var stories = {}, pingPong = false;
var DEBUG = false; function expire(id) {
var options = {
uri: location.metaverseServerUrl + '/api/v1/user_stories/' + id,
method: 'PUT',
json: true,
body: {expire: "true"}
};
request(options, function (error, response) {
debug('expired story', options, 'error:', error, 'response:', response);
if (error || (response.status !== 'success')) {
print("ERROR expiring story: ", error || response.status);
}
});
}
function pollForAnnouncements() { function pollForAnnouncements() {
var actions = DEBUG ? 'snapshot' : 'announcement'; // We could bail now if !Account.isLoggedIn(), but what if we someday have system-wide announcments?
var actions = 'announcement';
var count = DEBUG ? 10 : 100; var count = DEBUG ? 10 : 100;
var options = [ var options = [
'now=' + new Date().toISOString(), 'now=' + new Date().toISOString(),
@ -117,13 +139,24 @@
request({ request({
uri: url uri: url
}, function (error, data) { }, function (error, data) {
debug(url, error, data);
if (error || (data.status !== 'success')) { if (error || (data.status !== 'success')) {
print("Error: unable to get", url, error || data.status); print("Error: unable to get", url, error || data.status);
return; return;
} }
var didNotify = false; var didNotify = false, key;
pingPong = !pingPong;
data.user_stories.forEach(function (story) { data.user_stories.forEach(function (story) {
if (stories[story.id]) { // already seen var stored = stories[story.id], storedOrNew = stored || story;
debug('story exists:', !!stored, storedOrNew);
if ((storedOrNew.username === Account.username) && (storedOrNew.place_name !== location.placename)) {
if (storedOrNew.audience == 'for_connections') { // Only expire if we haven't already done so.
expire(story.id);
}
return; // before marking
}
storedOrNew.pingPong = pingPong;
if (stored) { // already seen
return; return;
} }
stories[story.id] = story; stories[story.id] = story;
@ -131,12 +164,20 @@
Window.displayAnnouncement(message); Window.displayAnnouncement(message);
didNotify = true; didNotify = true;
}); });
for (key in stories) { // Any story we were tracking that was not marked, has expired.
if (stories[key].pingPong !== pingPong) {
debug('removing story', key);
delete stories[key];
}
}
if (didNotify) { if (didNotify) {
messagesWaiting(true); messagesWaiting(true);
if (HMD.isHandControllerAvailable()) { if (HMD.isHandControllerAvailable()) {
var STRENGTH = 1.0, DURATION_MS = 60, HAND = 2; // both hands var STRENGTH = 1.0, DURATION_MS = 60, HAND = 2; // both hands
Controller.triggerHapticPulse(STRENGTH, DURATION_MS, HAND); Controller.triggerHapticPulse(STRENGTH, DURATION_MS, HAND);
} }
} else if (!Object.keys(stories).length) { // If there's nothing being tracked, then any messageWaiting has expired.
messagesWaiting(false);
} }
}); });
} }