diff --git a/qml/lib/Mastodon.js b/qml/lib/Mastodon.js index 07646df..f09b1c5 100644 --- a/qml/lib/Mastodon.js +++ b/qml/lib/Mastodon.js @@ -16,6 +16,57 @@ var mastodonAPI = function(config) { return config[key]; }, + /* + * function to retrieve the Link header + */ + + getLink: function (endpoint) { + // variables + var queryData, callback, + queryStringAppend = "?"; + + // check with which arguments we're supplied + if (typeof arguments[1] === "function") { + queryData = {}; + callback = arguments[1]; + } else { + queryData = arguments[1]; + callback = arguments[2]; + } + // build queryData Object into a URL Query String + for (var i in queryData) { + if (queryData.hasOwnProperty(i)) { + if (typeof queryData[i] === "string") { + queryStringAppend += queryData[i] + "&"; + } else if (typeof queryData[i] === "object") { + queryStringAppend += queryData[i].name + "="+ queryData[i].data + "&"; + } + } + } + var http = new XMLHttpRequest() + var url = apiBase + endpoint; + console.log("HEAD" + apiBase + endpoint + queryStringAppend) + + http.open("HEAD", apiBase + endpoint + queryStringAppend, true); + // Send the proper header information along with the request + http.setRequestHeader("Authorization", "Bearer " + config.api_user_token); + http.setRequestHeader("Content-Type", "application/json"); + http.setRequestHeader("Connection", "close"); + + http.onreadystatechange = function() { + if (http.readyState === 4) { + if (http.status === 200) { + callback( http.getResponseHeader("Link") , http.status) + console.log("Successful HEAD API request to " +apiBase+endpoint); + } else { + console.log("error: " + http.status) + } + } + } + http.send(); + + }, + get: function (endpoint) { // for GET API calls // can be called with two or three parameters @@ -46,7 +97,7 @@ var mastodonAPI = function(config) { } } } - queryStringAppend += "limit=12" + //queryStringAppend += "limit=20" // ajax function var http = new XMLHttpRequest() var url = apiBase + endpoint; @@ -61,8 +112,8 @@ var mastodonAPI = function(config) { http.onreadystatechange = function() { // Call a function when the state changes. if (http.readyState === 4) { if (http.status === 200) { - console.log("Successful GET API request to " +apiBase+endpoint); callback(JSON.parse(http.response),http.status) + console.log("Successful GET API request to " +apiBase+endpoint); } else { console.log("error: " + http.status) } @@ -220,7 +271,7 @@ var mastodonAPI = function(config) { var http = new XMLHttpRequest() var url = config.instance + "/oauth/token"; var params = 'client_id=' + client_id + '&client_secret=' + client_secret + '&redirect_uri=' + redirect_uri + '&grant_type=authorization_code&code=' + code; - console.log(params) + // console.log(params) http.open("POST", url, true); // Send the proper header information along with the request diff --git a/qml/lib/Worker.js b/qml/lib/Worker.js index 7878cee..b81ee0d 100644 --- a/qml/lib/Worker.js +++ b/qml/lib/Worker.js @@ -17,6 +17,7 @@ WorkerScript.onMessage = function(msg) { */ // this is not elegant. it's max_id and ids from MyList + if (msg.params[1]) { if ( msg.params[0]["name"] === "max_id" ) { max_id = msg.params[0]["data"] @@ -53,9 +54,24 @@ WorkerScript.onMessage = function(msg) { if (typeof msg.conf['loadImages'] !== "undefined") loadImages = msg.conf['loadImages'] - /** POST statuses */ + + /* init API statuses */ + var API = mastodonAPI({ instance: msg.conf.instance, api_user_token: msg.conf.api_user_token}); + /* for some actions + * we have to retrieve the Link header + */ + + if (msg.action === "bookmarks"){ + API.getLink(msg.action, msg.params, function(data) { + console.log(JSON.stringify(data)) + WorkerScript.sendMessage({ 'Header': data }) + }); + } + + /** POST statuses */ + if (msg.method === "POST"){ API.post(msg.action, msg.params, function(data) { if (msg.bgAction){ @@ -102,7 +118,6 @@ WorkerScript.onMessage = function(msg) { item = parseNotification(data[i]); items.push(item); - } else if(msg.action.indexOf("statuses") >-1 && msg.action.indexOf("context") >-1 && i === "ancestors") { // status ancestors toots - conversation console.log("ancestors") @@ -157,6 +172,7 @@ WorkerScript.onMessage = function(msg) { orderNotifications(items)*/ console.log("Get em all?") + WorkerScript.sendMessage({ 'updatedAll': true}) }); } @@ -166,9 +182,12 @@ WorkerScript.onMessage = function(msg) { function addDataToModel (model, mode, items) { var length = items.length; + console.log("Fetched > " +length + " in " + mode) console.log("ids > " + knownIds.length ) + var i + if (mode === "append") { for(i = 0; i <= length-1; i++) { if ( knownIds.indexOf( items[i]["id"]) === -1) { @@ -190,6 +209,7 @@ function addDataToModel (model, mode, items) { } model.sync() } + function findDuplicate(arr,val) { for(var i=0; i < arr.length; i++){ if( arr.indexOf(val) === -1 ) { @@ -198,7 +218,8 @@ function findDuplicate(arr,val) { } return false; } -/** Function: Get Account Data */ + +/* Function: Get Account Data */ function parseAccounts(collection, prefix, data) { var res = collection; diff --git a/qml/pages/components/MyList.qml b/qml/pages/components/MyList.qml index d4c75f5..bed5729 100644 --- a/qml/pages/components/MyList.qml +++ b/qml/pages/components/MyList.qml @@ -18,6 +18,7 @@ SilicaListView { property bool loadStarted: false property int scrollOffset property string action: "" + property string linkprev: "" property variant vars property variant conf property bool notifier: false @@ -177,13 +178,20 @@ SilicaListView { if (messageObject.fireNotification && notifier){ Logic.notifier(messageObject.data) } + // temporary debugging measure - // should be resolved within loadData() if (messageObject.updatedAll){ if (debug) console.log("Got em all.") - if (model.count > 12) deDouble() + if (model.count > 20) deDouble() loadStarted = false } + if (messageObject.Header) { + //if (debug) console.log(JSON.stringify(messageObject)) + var matches = /max_id=([0-9]+)/.exec(messageObject.Header); + var link = matches[0].split("=")[1]; + if (debug) console.log("link: " + link) + linkprev = link + } } } @@ -195,6 +203,8 @@ SilicaListView { Timer { triggeredOnStart: false; interval: { + + /* this is hamfisted */ var listInterval = Math.floor(Math.random() * 60)*10*1000 if( title === "Home" ) listInterval = 20*60*1000 if( title === "Local" ) listInterval = 10*60*1000 @@ -203,6 +213,7 @@ SilicaListView { if( title === "Notifications" ) listInterval = 12*60*1000 if(debug) console.log(title + ' interval: ' + listInterval) + return listInterval } running: true; @@ -305,7 +316,12 @@ SilicaListView { p.push(params[i]) } if (mode === "append" && model.count) { - p.push({name: 'max_id', data: model.get(model.count-1).id}) + // for some types, max_id is obtained from link header + if ( linkprev === "" ) { + p.push({name: 'max_id', data: model.get(model.count-1).id}) + } else { + p.push({name: 'max_id', data: linkprev}) + } } if (mode === "prepend" && model.count) { p.push({name:'since_id', data: model.get(0).id})