From 53701371abf335782d542aa418af626a411519d6 Mon Sep 17 00:00:00 2001
From: Zach Fox <fox@highfidelity.io>
Date: Wed, 31 May 2017 16:43:21 -0700
Subject: [PATCH] Fix some bugs :(

---
 scripts/system/html/js/SnapshotReview.js | 23 +++++++++++++----------
 scripts/system/snapshot.js               | 10 +++++++---
 2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/scripts/system/html/js/SnapshotReview.js b/scripts/system/html/js/SnapshotReview.js
index 5fd884fba7..36f7124f93 100644
--- a/scripts/system/html/js/SnapshotReview.js
+++ b/scripts/system/html/js/SnapshotReview.js
@@ -21,6 +21,11 @@ var blastShareText = "Blast to my Connections",
     hifiAlreadySharedText = "Already Shared to Snaps Feed",
     facebookShareText = "Share to Facebook",
     twitterShareText = "Share to Twitter";
+
+function fileExtensionMatches(filePath, extension) {
+    return filePath.split('.').pop().toLowerCase() === extension;
+}
+
 function showSetupInstructions() {
     var snapshotImagesDiv = document.getElementById("snapshot-images");
     snapshotImagesDiv.className = "snapshotInstructions";
@@ -276,10 +281,10 @@ function addImage(image_data, isLoggedIn, canShare, isGifLoading, isShowingPrevi
     if (!image_data.localPath) {
         return;
     }
-    var id = "p" + (idCounter++),
-        imageContainer = document.createElement("DIV"),
+    var imageContainer = document.createElement("DIV"),
         img = document.createElement("IMG"),
-        isGif;
+        isGif = fileExtensionMatches(image_data.localPath, "gif"),
+        id = "p" + (isGif ? "1" : "0");
     imageContainer.id = id;
     imageContainer.style.width = "95%";
     imageContainer.style.height = "240px";
@@ -290,18 +295,17 @@ function addImage(image_data, isLoggedIn, canShare, isGifLoading, isShowingPrevi
     imageContainer.style.position = "relative";
     img.id = id + "img";
     img.src = image_data.localPath;
-    isGif = img.src.split('.').pop().toLowerCase() === "gif";
     imageContainer.appendChild(img);
     document.getElementById("snapshot-images").appendChild(imageContainer);
+    paths.push(image_data.localPath);
     img.onload = function () {
-        paths.push(image_data.localPath);
         if (isGif) {
             imageContainer.innerHTML += '<span class="gifLabel">GIF</span>';
         }
         if (!isGifLoading) {
             appendShareBar(id, isLoggedIn, canShare, isGif, blastButtonDisabled, hifiButtonDisabled, canBlast);
         }
-        if ((!isGifLoading && !isShowingPreviousImages) || (isShowingPreviousImages && !image_data.story_id)) {
+        if ((!isShowingPreviousImages && ((isGif && !isGifLoading) || !isGif)) || (isShowingPreviousImages && !image_data.story_id)) {
             shareForUrl(id);
         }
         if (isShowingPreviousImages && isLoggedIn && image_data.story_id) {
@@ -638,9 +642,8 @@ window.onload = function () {
                     // The last element of the message contents list contains a bunch of options,
                     // including whether or not we can share stuff
                     // The other elements of the list contain image paths.
-
-                    if (messageOptions.containsGif) {
-                        if (messageOptions.processingGif) {
+                    if (messageOptions.containsGif === true) {
+                        if (messageOptions.processingGif === true) {
                             imageCount = message.image_data.length + 1; // "+1" for the GIF that'll finish processing soon
                             message.image_data.push({ localPath: messageOptions.loadingGifPath });
                             message.image_data.forEach(function (element, idx) {
@@ -669,7 +672,7 @@ window.onload = function () {
                     handleCaptureSetting(message.setting);
                     break;
                 case 'snapshotUploadComplete':
-                    var isGif = message.image_url.split('.').pop().toLowerCase() === "gif";
+                    var isGif = fileExtensionMatches(message.image_url, "gif");
                     updateShareInfo(isGif ? "p1" : "p0", message.story_id);
                     break;
                 default:
diff --git a/scripts/system/snapshot.js b/scripts/system/snapshot.js
index a1ea8e81af..494ab245b1 100644
--- a/scripts/system/snapshot.js
+++ b/scripts/system/snapshot.js
@@ -58,6 +58,10 @@ function removeFromStoryIDsToMaybeDelete(story_id) {
     print('storyIDsToMaybeDelete[] now:', JSON.stringify(storyIDsToMaybeDelete));
 }
 
+function fileExtensionMatches(filePath, extension) {
+    return filePath.split('.').pop().toLowerCase() === extension;
+}
+
 function onMessage(message) {
     // Receives message from the html dialog via the qwebchannel EventBridge. This is complicated by the following:
     // 1. Although we can send POJOs, we cannot receive a toplevel object. (Arrays of POJOs are fine, though.)
@@ -141,7 +145,7 @@ function onMessage(message) {
                     if (isLoggedIn) {
                         print('Sharing snapshot with audience "for_url":', message.data);
                         Window.shareSnapshot(message.data, Settings.getValue("previousSnapshotHref"));
-                        var isGif = message.data.split('.').pop().toLowerCase() === "gif";
+                        var isGif = fileExtensionMatches(message.data, "gif");
                         if (isGif) {
                             numGifSnapshotUploadsPending++;
                         } else {
@@ -318,7 +322,7 @@ function snapshotUploaded(isError, reply) {
         var replyJson = JSON.parse(reply),
             storyID = replyJson.user_story.id,
             imageURL = replyJson.user_story.details.image_url,
-            isGif = imageURL.split('.').pop().toLowerCase() === "gif",
+            isGif = fileExtensionMatches(imageURL, "gif"),
             ignoreGifSnapshotData = false,
             ignoreStillSnapshotData = false;
         storyIDsToMaybeDelete.push(storyID);
@@ -593,7 +597,7 @@ function onUsernameChanged() {
                     snapshotToShareAfterLogin.forEach(function (element) {
                         print('Uploading snapshot after login:', element.path);
                         Window.shareSnapshot(element.path, element.href);
-                        var isGif = element.path.split('.').pop().toLowerCase() === "gif";
+                        var isGif = fileExtensionMatches(element.path, "gif");
                         if (isGif) {
                             numGifSnapshotUploadsPending++;
                         } else {