From e1789996fbe3d69f779ab20d613a2c768079d8eb Mon Sep 17 00:00:00 2001
From: Zach Fox <fox@highfidelity.io>
Date: Mon, 24 Apr 2017 16:34:58 -0700
Subject: [PATCH] Add announcements support

---
 scripts/system/html/js/SnapshotReview.js | 25 +++++++++++--------
 scripts/system/snapshot.js               | 31 +++++++++++++++++++-----
 2 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/scripts/system/html/js/SnapshotReview.js b/scripts/system/html/js/SnapshotReview.js
index 9fa58e6d33..4c5419869d 100644
--- a/scripts/system/html/js/SnapshotReview.js
+++ b/scripts/system/html/js/SnapshotReview.js
@@ -23,7 +23,7 @@ function clearImages() {
     imageCount = 0;
     idCounter = 0;
 }
-function addImage(image_data, isGifLoading, isShowingPreviousImages, canSharePreviousImages) {
+function addImage(image_data, isGifLoading, isShowingPreviousImages, canSharePreviousImages, hifiShareButtonsDisabled) {
     if (!image_data.localPath) {
         return;
     }
@@ -49,19 +49,19 @@ function addImage(image_data, isGifLoading, isShowingPreviousImages, canSharePre
     if (!isGifLoading && !isShowingPreviousImages) {
         shareForUrl(id);
     } else if (isShowingPreviousImages && canSharePreviousImages) {
-        appendShareBar(id, image_data.story_id, isGif)
+        appendShareBar(id, image_data.story_id, isGif, hifiShareButtonsDisabled)
     }
 }
-function appendShareBar(divID, story_id, isGif) {
+function appendShareBar(divID, story_id, isGif, hifiShareButtonsDisabled) {
     var story_url = "https://highfidelity.com/user_stories/" + story_id;
     var parentDiv = document.getElementById(divID);
     parentDiv.setAttribute('data-story-id', story_id);
-    document.getElementById(divID).appendChild(createShareOverlay(divID, isGif, story_url));
+    document.getElementById(divID).appendChild(createShareOverlay(divID, isGif, story_url, hifiShareButtonsDisabled));
     twttr.events.bind('click', function (event) {
         shareButtonClicked(divID);
     });
 }
-function createShareOverlay(parentID, isGif, shareURL) {
+function createShareOverlay(parentID, isGif, shareURL, hifiShareButtonsDisabled) {
     var shareOverlayContainer = document.createElement("DIV");
     shareOverlayContainer.id = parentID + "shareOverlayContainer";
     shareOverlayContainer.style.position = "absolute";
@@ -115,8 +115,8 @@ function createShareOverlay(parentID, isGif, shareURL) {
         '<br/>' +
         '<div class="shareControls">' +
             '<div class="hifiShareControls">' +
-                '<input type="button" class="shareWithEveryone" id="' + shareWithEveryoneButtonID + '" value="SHARE WITH EVERYONE" onclick="shareWithEveryone(' + parentID + ')" /><br>' +
-                '<input type="checkbox" class="inviteConnections" id="' + inviteConnectionsCheckboxID + '" checked="checked" />' +
+                '<input type="button"' + (hifiShareButtonsDisabled ? ' disabled="disabled"' : '') + ' class="shareWithEveryone" id="' + shareWithEveryoneButtonID + '" value="SHARE WITH EVERYONE" onclick="shareWithEveryone(' + parentID + ', ' + isGif + ')" /><br>' +
+                '<input type="checkbox"' + (hifiShareButtonsDisabled ? ' disabled="disabled"' : '') + ' class="inviteConnections" id="' + inviteConnectionsCheckboxID + '" checked="checked" />' +
                 '<label class="shareButtonLabel" for="' + inviteConnectionsCheckboxID + '">Invite My Connections</label><br>' +
                 '<input type="button" class="cancelShare" value="CANCEL" onclick="cancelSharing(' + parentID + ')" />' +
             '</div>' +
@@ -152,13 +152,18 @@ function shareForUrl(selectedID) {
         data: paths[parseInt(selectedID.substring(1))]
     }));
 }
-function shareWithEveryone(selectedID) {
+function shareWithEveryone(selectedID, isGif) {
     selectedID = selectedID.id; // Why is this necessary?
 
+    document.getElementById(selectedID + "shareWithEveryoneButton").setAttribute("disabled", "disabled");
+    document.getElementById(selectedID + "inviteConnectionsCheckbox").setAttribute("disabled", "disabled");
+
     EventBridge.emitWebEvent(JSON.stringify({
         type: "snapshot",
         action: "shareSnapshotWithEveryone",
-        story_id: document.getElementById(selectedID).getAttribute("data-story-id")
+        story_id: document.getElementById(selectedID).getAttribute("data-story-id"),
+        isAnnouncement: document.getElementById(selectedID + "inviteConnectionsCheckbox").getAttribute("checked"),
+        isGif: isGif
     }));
 }
 function shareButtonClicked(selectedID) {
@@ -233,7 +238,7 @@ window.onload = function () {
                     var messageOptions = message.options;
                     imageCount = message.image_data.length;
                     message.image_data.forEach(function (element, idx, array) {
-                        addImage(element, true, true, message.canShare);
+                        addImage(element, true, true, message.canShare, message.image_data[idx].buttonDisabled);
                     });
                     break;
                 case 'addImages':
diff --git a/scripts/system/snapshot.js b/scripts/system/snapshot.js
index 422c557391..95a76a9188 100644
--- a/scripts/system/snapshot.js
+++ b/scripts/system/snapshot.js
@@ -143,21 +143,38 @@ function onMessage(message) {
         case 'shareSnapshotWithEveryone':
             isLoggedIn = Account.isLoggedIn();
             storyIDsToMaybeDelete.splice(storyIDsToMaybeDelete.indexOf(message.story_id), 1);
+            if (message.isGif) {
+                Settings.setValue("previousAnimatedSnapSharingDisabled", true);
+            } else {
+                Settings.setValue("previousStillSnapSharingDisabled", true);
+            }
+
+            var requestBody = {
+                audience: "for_feed"
+            }
+
+            if (message.isAnnouncement) {
+                requestBody.action = "announcement";
+            }
+
             if (isLoggedIn) {
                 print('Modifying audience of story ID', message.story_id, "to 'for_feed'");
                 request({
                     uri: METAVERSE_BASE + '/api/v1/user_stories/' + message.story_id,
                     method: 'PUT',
                     json: true,
-                    body: {
-                        audience: "for_feed",
-                    }
+                    body: requestBody
                 }, function (error, response) {
                     if (error || (response.status !== 'success')) {
                         print("ERROR changing audience: ", error || response.status);
+                        if (message.isGif) {
+                            Settings.setValue("previousAnimatedSnapSharingDisabled", false);
+                        } else {
+                            Settings.setValue("previousStillSnapSharingDisabled", false);
+                        }
                         return;
                     } else {
-                        print("SUCCESS changing audience!");
+                        print("SUCCESS changing audience" + (message.isAnnouncement ? " and posting announcement!" : "!"));
                     }
                 });
             } else {
@@ -195,8 +212,10 @@ var isInSnapshotReview = false;
 function openSnapApp() {
     var previousStillSnapPath = Settings.getValue("previousStillSnapPath");
     var previousStillSnapStoryID = Settings.getValue("previousStillSnapStoryID");
+    var previousStillSnapSharingDisabled = Settings.getValue("previousStillSnapSharingDisabled");
     var previousAnimatedSnapPath = Settings.getValue("previousAnimatedSnapPath");
     var previousAnimatedSnapStoryID = Settings.getValue("previousAnimatedSnapStoryID");
+    var previousAnimatedSnapSharingDisabled = Settings.getValue("previousAnimatedSnapSharingDisabled");
     snapshotOptions = {
         containsGif: previousAnimatedSnapPath !== "",
         processingGif: false,
@@ -204,10 +223,10 @@ function openSnapApp() {
     }
     imageData = [];
     if (previousAnimatedSnapPath !== "") {
-        imageData.push({ localPath: previousAnimatedSnapPath, story_id: previousAnimatedSnapStoryID });
+        imageData.push({ localPath: previousAnimatedSnapPath, story_id: previousAnimatedSnapStoryID, buttonDisabled: previousAnimatedSnapSharingDisabled });
     }
     if (previousStillSnapPath !== "") {
-        imageData.push({ localPath: previousStillSnapPath, story_id: previousStillSnapStoryID });
+        imageData.push({ localPath: previousStillSnapPath, story_id: previousStillSnapStoryID, buttonDisabled: previousStillSnapSharingDisabled });
     }
     tablet.gotoWebScreen(SNAPSHOT_REVIEW_URL);
     tablet.webEventReceived.connect(onMessage);