Add the HEAD call with a WorkerScript callback to pass
max_id for bookmarks to MyList Add a method to MyList to set the max_id from the retrieved Link header
This commit is contained in:
parent
bd91ee5611
commit
8480c68ce1
3 changed files with 97 additions and 9 deletions
|
@ -16,6 +16,57 @@ var mastodonAPI = function(config) {
|
||||||
return config[key];
|
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) {
|
get: function (endpoint) {
|
||||||
// for GET API calls
|
// for GET API calls
|
||||||
// can be called with two or three parameters
|
// can be called with two or three parameters
|
||||||
|
@ -46,7 +97,7 @@ var mastodonAPI = function(config) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
queryStringAppend += "limit=12"
|
//queryStringAppend += "limit=20"
|
||||||
// ajax function
|
// ajax function
|
||||||
var http = new XMLHttpRequest()
|
var http = new XMLHttpRequest()
|
||||||
var url = apiBase + endpoint;
|
var url = apiBase + endpoint;
|
||||||
|
@ -61,8 +112,8 @@ var mastodonAPI = function(config) {
|
||||||
http.onreadystatechange = function() { // Call a function when the state changes.
|
http.onreadystatechange = function() { // Call a function when the state changes.
|
||||||
if (http.readyState === 4) {
|
if (http.readyState === 4) {
|
||||||
if (http.status === 200) {
|
if (http.status === 200) {
|
||||||
console.log("Successful GET API request to " +apiBase+endpoint);
|
|
||||||
callback(JSON.parse(http.response),http.status)
|
callback(JSON.parse(http.response),http.status)
|
||||||
|
console.log("Successful GET API request to " +apiBase+endpoint);
|
||||||
} else {
|
} else {
|
||||||
console.log("error: " + http.status)
|
console.log("error: " + http.status)
|
||||||
}
|
}
|
||||||
|
@ -220,7 +271,7 @@ var mastodonAPI = function(config) {
|
||||||
var http = new XMLHttpRequest()
|
var http = new XMLHttpRequest()
|
||||||
var url = config.instance + "/oauth/token";
|
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;
|
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);
|
http.open("POST", url, true);
|
||||||
|
|
||||||
// Send the proper header information along with the request
|
// Send the proper header information along with the request
|
||||||
|
|
|
@ -17,6 +17,7 @@ WorkerScript.onMessage = function(msg) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// this is not elegant. it's max_id and ids from MyList
|
// this is not elegant. it's max_id and ids from MyList
|
||||||
|
|
||||||
if (msg.params[1]) {
|
if (msg.params[1]) {
|
||||||
if ( msg.params[0]["name"] === "max_id" ) {
|
if ( msg.params[0]["name"] === "max_id" ) {
|
||||||
max_id = msg.params[0]["data"]
|
max_id = msg.params[0]["data"]
|
||||||
|
@ -53,9 +54,24 @@ WorkerScript.onMessage = function(msg) {
|
||||||
if (typeof msg.conf['loadImages'] !== "undefined")
|
if (typeof msg.conf['loadImages'] !== "undefined")
|
||||||
loadImages = msg.conf['loadImages']
|
loadImages = msg.conf['loadImages']
|
||||||
|
|
||||||
/** POST statuses */
|
|
||||||
|
/* init API statuses */
|
||||||
|
|
||||||
var API = mastodonAPI({ instance: msg.conf.instance, api_user_token: msg.conf.api_user_token});
|
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"){
|
if (msg.method === "POST"){
|
||||||
API.post(msg.action, msg.params, function(data) {
|
API.post(msg.action, msg.params, function(data) {
|
||||||
if (msg.bgAction){
|
if (msg.bgAction){
|
||||||
|
@ -102,7 +118,6 @@ WorkerScript.onMessage = function(msg) {
|
||||||
item = parseNotification(data[i]);
|
item = parseNotification(data[i]);
|
||||||
items.push(item);
|
items.push(item);
|
||||||
|
|
||||||
|
|
||||||
} else if(msg.action.indexOf("statuses") >-1 && msg.action.indexOf("context") >-1 && i === "ancestors") {
|
} else if(msg.action.indexOf("statuses") >-1 && msg.action.indexOf("context") >-1 && i === "ancestors") {
|
||||||
// status ancestors toots - conversation
|
// status ancestors toots - conversation
|
||||||
console.log("ancestors")
|
console.log("ancestors")
|
||||||
|
@ -157,6 +172,7 @@ WorkerScript.onMessage = function(msg) {
|
||||||
orderNotifications(items)*/
|
orderNotifications(items)*/
|
||||||
|
|
||||||
console.log("Get em all?")
|
console.log("Get em all?")
|
||||||
|
|
||||||
WorkerScript.sendMessage({ 'updatedAll': true})
|
WorkerScript.sendMessage({ 'updatedAll': true})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -166,9 +182,12 @@ WorkerScript.onMessage = function(msg) {
|
||||||
function addDataToModel (model, mode, items) {
|
function addDataToModel (model, mode, items) {
|
||||||
|
|
||||||
var length = items.length;
|
var length = items.length;
|
||||||
|
|
||||||
console.log("Fetched > " +length + " in " + mode)
|
console.log("Fetched > " +length + " in " + mode)
|
||||||
console.log("ids > " + knownIds.length )
|
console.log("ids > " + knownIds.length )
|
||||||
|
|
||||||
var i
|
var i
|
||||||
|
|
||||||
if (mode === "append") {
|
if (mode === "append") {
|
||||||
for(i = 0; i <= length-1; i++) {
|
for(i = 0; i <= length-1; i++) {
|
||||||
if ( knownIds.indexOf( items[i]["id"]) === -1) {
|
if ( knownIds.indexOf( items[i]["id"]) === -1) {
|
||||||
|
@ -190,6 +209,7 @@ function addDataToModel (model, mode, items) {
|
||||||
}
|
}
|
||||||
model.sync()
|
model.sync()
|
||||||
}
|
}
|
||||||
|
|
||||||
function findDuplicate(arr,val) {
|
function findDuplicate(arr,val) {
|
||||||
for(var i=0; i < arr.length; i++){
|
for(var i=0; i < arr.length; i++){
|
||||||
if( arr.indexOf(val) === -1 ) {
|
if( arr.indexOf(val) === -1 ) {
|
||||||
|
@ -198,7 +218,8 @@ function findDuplicate(arr,val) {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
/** Function: Get Account Data */
|
|
||||||
|
/* Function: Get Account Data */
|
||||||
function parseAccounts(collection, prefix, data) {
|
function parseAccounts(collection, prefix, data) {
|
||||||
|
|
||||||
var res = collection;
|
var res = collection;
|
||||||
|
|
|
@ -18,6 +18,7 @@ SilicaListView {
|
||||||
property bool loadStarted: false
|
property bool loadStarted: false
|
||||||
property int scrollOffset
|
property int scrollOffset
|
||||||
property string action: ""
|
property string action: ""
|
||||||
|
property string linkprev: ""
|
||||||
property variant vars
|
property variant vars
|
||||||
property variant conf
|
property variant conf
|
||||||
property bool notifier: false
|
property bool notifier: false
|
||||||
|
@ -177,13 +178,20 @@ SilicaListView {
|
||||||
if (messageObject.fireNotification && notifier){
|
if (messageObject.fireNotification && notifier){
|
||||||
Logic.notifier(messageObject.data)
|
Logic.notifier(messageObject.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// temporary debugging measure
|
// temporary debugging measure
|
||||||
// should be resolved within loadData()
|
|
||||||
if (messageObject.updatedAll){
|
if (messageObject.updatedAll){
|
||||||
if (debug) console.log("Got em all.")
|
if (debug) console.log("Got em all.")
|
||||||
if (model.count > 12) deDouble()
|
if (model.count > 20) deDouble()
|
||||||
loadStarted = false
|
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 {
|
Timer {
|
||||||
triggeredOnStart: false;
|
triggeredOnStart: false;
|
||||||
interval: {
|
interval: {
|
||||||
|
|
||||||
|
/* this is hamfisted */
|
||||||
var listInterval = Math.floor(Math.random() * 60)*10*1000
|
var listInterval = Math.floor(Math.random() * 60)*10*1000
|
||||||
if( title === "Home" ) listInterval = 20*60*1000
|
if( title === "Home" ) listInterval = 20*60*1000
|
||||||
if( title === "Local" ) listInterval = 10*60*1000
|
if( title === "Local" ) listInterval = 10*60*1000
|
||||||
|
@ -203,6 +213,7 @@ SilicaListView {
|
||||||
if( title === "Notifications" ) listInterval = 12*60*1000
|
if( title === "Notifications" ) listInterval = 12*60*1000
|
||||||
|
|
||||||
if(debug) console.log(title + ' interval: ' + listInterval)
|
if(debug) console.log(title + ' interval: ' + listInterval)
|
||||||
|
|
||||||
return listInterval
|
return listInterval
|
||||||
}
|
}
|
||||||
running: true;
|
running: true;
|
||||||
|
@ -305,7 +316,12 @@ SilicaListView {
|
||||||
p.push(params[i])
|
p.push(params[i])
|
||||||
}
|
}
|
||||||
if (mode === "append" && model.count) {
|
if (mode === "append" && model.count) {
|
||||||
|
// for some types, max_id is obtained from link header
|
||||||
|
if ( linkprev === "" ) {
|
||||||
p.push({name: 'max_id', data: model.get(model.count-1).id})
|
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) {
|
if (mode === "prepend" && model.count) {
|
||||||
p.push({name:'since_id', data: model.get(0).id})
|
p.push({name:'since_id', data: model.get(0).id})
|
||||||
|
|
Loading…
Reference in a new issue