From fd92afff8cedf0c9b0d0095671b867eeeb9b669e Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Wed, 31 May 2017 11:00:43 -0700 Subject: [PATCH 1/3] Ensure old snaps don't accidentally get shared for_url --- scripts/system/html/js/SnapshotReview.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/system/html/js/SnapshotReview.js b/scripts/system/html/js/SnapshotReview.js index 2a4d535fee..5fd884fba7 100644 --- a/scripts/system/html/js/SnapshotReview.js +++ b/scripts/system/html/js/SnapshotReview.js @@ -301,7 +301,7 @@ function addImage(image_data, isLoggedIn, canShare, isGifLoading, isShowingPrevi if (!isGifLoading) { appendShareBar(id, isLoggedIn, canShare, isGif, blastButtonDisabled, hifiButtonDisabled, canBlast); } - if (!isGifLoading || (isShowingPreviousImages && !image_data.story_id)) { + if ((!isGifLoading && !isShowingPreviousImages) || (isShowingPreviousImages && !image_data.story_id)) { shareForUrl(id); } if (isShowingPreviousImages && isLoggedIn && image_data.story_id) { From 70fdb4a168372855c9f218711c7a43496252630a Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Wed, 31 May 2017 12:00:44 -0700 Subject: [PATCH 2/3] Fix incorrectly sharing old snapshots --- scripts/system/snapshot.js | 57 +++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/scripts/system/snapshot.js b/scripts/system/snapshot.js index 1c257cfed5..a1ea8e81af 100644 --- a/scripts/system/snapshot.js +++ b/scripts/system/snapshot.js @@ -36,6 +36,8 @@ var shareAfterLogin = false; var snapshotToShareAfterLogin = []; var METAVERSE_BASE = location.metaverseServerUrl; var isLoggedIn; +var numGifSnapshotUploadsPending = 0; +var numStillSnapshotUploadsPending = 0; // 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 @@ -139,6 +141,12 @@ 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"; + if (isGif) { + numGifSnapshotUploadsPending++; + } else { + numStillSnapshotUploadsPending++; + } } else { shareAfterLogin = true; snapshotToShareAfterLogin.push({ path: message.data, href: Settings.getValue("previousSnapshotHref") }); @@ -307,22 +315,39 @@ function onButtonClicked() { function snapshotUploaded(isError, reply) { if (!isError) { - var replyJson = JSON.parse(reply); - var storyID = replyJson.user_story.id; + var replyJson = JSON.parse(reply), + storyID = replyJson.user_story.id, + imageURL = replyJson.user_story.details.image_url, + isGif = imageURL.split('.').pop().toLowerCase() === "gif", + ignoreGifSnapshotData = false, + ignoreStillSnapshotData = false; storyIDsToMaybeDelete.push(storyID); - var imageURL = replyJson.user_story.details.image_url; - var isGif = imageURL.split('.').pop().toLowerCase() === "gif"; - print('SUCCESS: Snapshot uploaded! Story with audience:for_url created! ID:', storyID); - tablet.emitScriptEvent(JSON.stringify({ - type: "snapshot", - action: "snapshotUploadComplete", - story_id: storyID, - image_url: imageURL, - })); if (isGif) { - Settings.setValue("previousAnimatedSnapStoryID", storyID); + numGifSnapshotUploadsPending--; + if (numGifSnapshotUploadsPending !== 0) { + ignoreGifSnapshotData = true; + } } else { - Settings.setValue("previousStillSnapStoryID", storyID); + numStillSnapshotUploadsPending--; + if (numStillSnapshotUploadsPending !== 0) { + ignoreStillSnapshotData = true; + } + } + if ((isGif && !ignoreGifSnapshotData) || (!isGif && !ignoreStillSnapshotData)) { + print('SUCCESS: Snapshot uploaded! Story with audience:for_url created! ID:', storyID); + tablet.emitScriptEvent(JSON.stringify({ + type: "snapshot", + action: "snapshotUploadComplete", + story_id: storyID, + image_url: imageURL, + })); + if (isGif) { + Settings.setValue("previousAnimatedSnapStoryID", storyID); + } else { + Settings.setValue("previousStillSnapStoryID", storyID); + } + } else { + print('Ignoring snapshotUploaded() callback for stale ' + (isGif ? 'GIF' : 'Still' ) + ' snapshot. Stale story ID:', storyID); } } else { print(reply); @@ -568,6 +593,12 @@ 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"; + if (isGif) { + numGifSnapshotUploadsPending++; + } else { + numStillSnapshotUploadsPending++; + } }); } }); From 53701371abf335782d542aa418af626a411519d6 Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Wed, 31 May 2017 16:43:21 -0700 Subject: [PATCH 3/3] 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 += 'GIF'; } 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 {