Skip to content

Commit

Permalink
greatly simplified reddit integration
Browse files Browse the repository at this point in the history
  • Loading branch information
tetris11 committed Jul 5, 2016
1 parent caa686b commit f49f294
Showing 1 changed file with 13 additions and 35 deletions.
48 changes: 13 additions & 35 deletions qml/components/imgur.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var ENDPOINT_GET_CREDITS = BASEURL + "/credits";
var ENDPOINT_GALLERY_SEARCH = ENDPOINT_GALLERY + "/search";

//Reddit stuff
var redditModeActive = false; // Global
var REDDIT_SUB = "clouds";

var ENDPOINT_GALLERY_ALBUM = ENDPOINT_GALLERY + "/album";
Expand Down Expand Up @@ -65,7 +66,8 @@ showViral optional true | false - Show or hide viral images from the 'user' se
function getGallery(model, page, settings, onSuccess, onFailure) {
var url = ENDPOINT_GALLERY;
url += "/" + settings.section + "/" + settings.sort + "/" + settings.window + "/" + page + "/?showViral=" + settings.showViral;
//console.log("getGallery: " + url);

console.log("getGallery: " + url);
sendJSONRequest(url, 1, model, onSuccess, onFailure);
}

Expand Down Expand Up @@ -110,7 +112,7 @@ function getRedditSubGallery(model, page, settings, onSuccess, onFailure) {

url += "/top/" + settings.window + "/page/" + page;

console.log("URL:", url);
console.log("reddit url:", url);
sendJSONRequest(url, 1, model, onSuccess, onFailure); // 1 --> still a gallery
}

Expand All @@ -132,8 +134,6 @@ function sendJSONRequest(url, actiontype, model, onSuccess, onFailure) {
handleAlbumsJSON(xhr.responseText, model);
} else if (actiontype === "images") {
handleImagesJSON(xhr.responseText, model);
} else if (actiontype === "reddit") {
handleRedditJSON(xhr.responseText, model, onFailure);
}
//console.log("RateLimit: user=" + creditsUserRemaining + ", client=" + creditsClientRemaining);
onSuccess(xhr.status);
Expand Down Expand Up @@ -218,34 +218,9 @@ function getAlbum(id, model, albumModel, onSuccess, onFailure) {
}


//get reddit image (called by GalleryImage first)
function getRedditImage(id, model, albumModel, onSuccess, onFailure) {
var url = ENDPOINT_IMAGE;
url += "/" + id;

var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function () {

if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status == 200) {
handleGalleryImageJSON(xhr.responseText, model, albumModel);
onSuccess(xhr.status);
} else {
var jsonObject = JSON.parse(xhr.responseText);
onFailure(xhr.status, "NOPE: " + url); // jsonObject.data.error);
}
}
}
xhr = createGETHeader(xhr);
xhr.send();
}



// get gallery image
function getGalleryImage(id, model, albumModel, onSuccess, onFailure) {
var url = ENDPOINT_GALLERY_IMAGE;
var url = redditModeActive ? ENDPOINT_IMAGE : ENDPOINT_GALLERY_IMAGE;
url += "/" + id;

var xhr = new XMLHttpRequest();
Expand All @@ -256,11 +231,6 @@ function getGalleryImage(id, model, albumModel, onSuccess, onFailure) {
handleGalleryImageJSON(xhr.responseText, model, albumModel);
onSuccess(xhr.status);

} else if (xhr.status == 404 ) {
// Try again, but under a different URL startpoint.
getRedditImage(id, model, albumModel, onSuccess, onFailure);


} else {
var jsonObject = JSON.parse(xhr.responseText);
onFailure(xhr.status, xhr.statusText + ": " + jsonObject.data.error);
Expand Down Expand Up @@ -703,6 +673,11 @@ function getAuthorizationHeader() {
}

function processGalleryMode(query, model, page, settings, onSuccess, onFailure) {

// Global accesor used to use the correct URL base
// for getGalleryImage()
redditModeActive = false;

if (query) {
getGallerySearch(query, model, page, settings, onSuccess, onFailure);
}
Expand All @@ -713,9 +688,12 @@ function processGalleryMode(query, model, page, settings, onSuccess, onFailure)
} else if (settings.mode === "memes") {
getMemesSubGallery(model, page, settings, onSuccess, onFailure);
} else if (settings.mode === "reddit") {
redditModeActive = true;
REDDIT_SUB = settings.reddit_sub;

console.log("REDDIT SUB=", REDDIT_SUB);
getRedditSubGallery(model, page, settings, onSuccess, onFailure);

} else if (settings.mode === "favorites") {
getFavorites(model, onSuccess, onFailure);
} else if (settings.mode === "albums") {
Expand Down

0 comments on commit f49f294

Please sign in to comment.