Notifications for GOTO (using a cool algorithm)

This commit is contained in:
Zach Fox 2018-09-18 15:30:43 -07:00
parent 55a0f77697
commit 4987164706
2 changed files with 99 additions and 103 deletions

View file

@ -532,17 +532,12 @@ function notificationPollCallback(historyArray) {
}
if (!ui.isOpen) {
shouldShowDot = shouldShowDot ||
(someoneElsePurchasedArray.length > 0) ||
(proofIssuedArray.length > 0) ||
(moneyReceivedArray.length > 0) ||
(giftReceivedArray.length > 0);
ui.messagesWaiting(shouldShowDot);
var notificationCount = someoneElsePurchasedArray.length +
proofIssuedArray.length +
moneyReceivedArray.length +
giftReceivedArray.length;
shouldShowDot = shouldShowDot || notificationCount > 0;
ui.messagesWaiting(shouldShowDot);
if (notificationCount > 0) {
var message;

View file

@ -15,118 +15,119 @@
//
(function () { // BEGIN LOCAL_SCOPE
var request = Script.require('request').request;
var AppUi = Script.require('appUi');
var DEBUG = false;
function debug() {
if (!DEBUG) {
return;
}
print('tablet-goto.js:', [].map.call(arguments, JSON.stringify));
}
var stories = {}, pingPong = false;
function expire(id) {
var options = {
uri: Account.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);
}
});
}
var PER_PAGE_DEBUG = 10;
var PER_PAGE_NORMAL = 100;
function pollForAnnouncements() {
// We could bail now if !Account.isLoggedIn(), but what if we someday have system-wide announcments?
var actions = 'announcement';
var count = DEBUG ? PER_PAGE_DEBUG : PER_PAGE_NORMAL;
var options = [
'now=' + new Date().toISOString(),
'include_actions=' + actions,
'restriction=' + (Account.isLoggedIn() ? 'open,hifi' : 'open'),
'require_online=true',
'protocol=' + encodeURIComponent(Window.protocolSignature()),
'per_page=' + count
];
var url = Account.metaverseServerURL + '/api/v1/user_stories?' + options.join('&');
request({
uri: url
}, function (error, data) {
debug(url, error, data);
if (error || (data.status !== 'success')) {
print("Error: unable to get", url, error || data.status);
return;
}
var didNotify = false, key;
pingPong = !pingPong;
data.user_stories.forEach(function (story) {
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;
}
stories[story.id] = story;
var message = story.username + " " + story.action_string + " in " +
story.place_name + ". Open GOTO to join them.";
Window.displayAnnouncement(message);
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) {
ui.messagesWaiting(true);
if (HMD.isHandControllerAvailable()) {
var STRENGTH = 1.0, DURATION_MS = 60, HAND = 2; // both hands
Controller.triggerHapticPulse(STRENGTH, DURATION_MS, HAND);
}
} else if (!Object.keys(stories).length) { // If there's nothing being tracked, then any messageWaiting has expired.
ui.messagesWaiting(false);
}
});
}
var MS_PER_SEC = 1000;
var DEBUG_POLL_TIME_SEC = 10;
var NORMAL_POLL_TIME_SEC = 60;
var ANNOUNCEMENTS_POLL_TIME_MS = (DEBUG ? DEBUG_POLL_TIME_SEC : NORMAL_POLL_TIME_SEC) * MS_PER_SEC;
var pollTimer = Script.setInterval(pollForAnnouncements, ANNOUNCEMENTS_POLL_TIME_MS);
function gotoOpened() {
ui.messagesWaiting(false);
}
function notificationDataProcessPage(data) {
return data.user_stories;
}
var shouldShowDot = false;
var pingPong = false;
var storedAnnouncements = {};
var storedFeaturedStories = {};
function notificationPollCallback(userStoriesArray) {
//
// START logic for keeping track of new info
//
pingPong = !pingPong;
var totalCountedStories = 0;
userStoriesArray.forEach(function (story) {
if (story.audience !== "for_connections" &&
story.audience !== "for_feed") {
return;
} else {
totalCountedStories++;
}
var stored = storedAnnouncements[story.id] || storedFeaturedStories[story.id];
var storedOrNew = stored || story;
storedOrNew.pingPong = pingPong;
if (stored) {
return;
}
if (story.audience === "for_connections") {
storedAnnouncements[story.id] = story;
} else if (story.audience === "for_feed") {
storedFeaturedStories[story.id] = story;
}
});
var key;
for (key in storedAnnouncements) {
if (storedAnnouncements[key].pingPong !== pingPong) {
delete storedAnnouncements[key];
}
}
for (key in storedFeaturedStories) {
if (storedFeaturedStories[key].pingPong !== pingPong) {
delete storedFeaturedStories[key];
}
}
//
// END logic for keeping track of new info
//
var notificationCount = Object.keys(storedAnnouncements).length +
Object.keys(storedFeaturedStories).length;
shouldShowDot = totalCountedStories > 0 || (notificationCount > 0 && shouldShowDot);
ui.messagesWaiting(shouldShowDot && !ui.isOpen);
if (notificationCount > 0 && !ui.isOpen) {
var message;
if (!ui.notificationInitialCallbackMade) {
message = "You have " + userStoriesArray.length + "event invitations pending! " +
"Open GOTO to see them.";
ui.notificationDisplayBanner(message);
} else {
for (key in storedAnnouncements) {
message = storedAnnouncements[key].username + " says something is happening in " +
storedAnnouncements[key].place_name + "! Open GOTO to join them.";
ui.notificationDisplayBanner(message);
}
for (key in storedFeaturedStories) {
message = storedFeaturedStories[key].username + " has invited you to an event in " +
storedFeaturedStories[key].place_name + "! Open GOTO to join them.";
ui.notificationDisplayBanner(message);
}
}
}
}
function isReturnedDataEmpty(data) {
var storiesArray = data.user_stories;
return storiesArray.length === 0;
}
var ui;
var GOTO_QML_SOURCE = "hifi/tablet/TabletAddressDialog.qml";
var BUTTON_NAME = "GOTO";
function startup() {
var options = [
'include_actions=announcement',
'restriction=open,hifi',
'require_online=true',
'protocol=' + encodeURIComponent(Window.protocolSignature()),
'per_page=10'
];
var endpoint = '/api/v1/notifications?source=user_stories?' + options.join('&');
ui = new AppUi({
buttonName: BUTTON_NAME,
sortOrder: 8,
onOpened: gotoOpened,
home: GOTO_QML_SOURCE
home: GOTO_QML_SOURCE,
notificationPollEndpoint: endpoint,
notificationPollTimeoutMs: 60000,
notificationDataProcessPage: notificationDataProcessPage,
notificationPollCallback: notificationPollCallback,
notificationPollStopPaginatingConditionMet: isReturnedDataEmpty,
notificationPollCaresAboutSince: true
});
}
function shutdown() {
Script.clearInterval(pollTimer);
}
startup();
Script.scriptEnding.connect(shutdown);
}()); // END LOCAL_SCOPE