Add conditional append all for search when no knownIds

Add linkprev for bookmarks (and follows, etc)
This commit is contained in:
Mark Washeim 2023-01-05 09:48:26 +01:00
parent ed48eb0a94
commit abd5912f98
3 changed files with 59 additions and 31 deletions

View file

@ -17,7 +17,8 @@ var mastodonAPI = function(config) {
},
/*
* function to retrieve the Link header
* function to retrieve the Link header
* using HEAD, so bookmarks has head followed by GET
*/
getLink: function (endpoint) {

View file

@ -1,6 +1,6 @@
Qt.include("Mastodon.js")
var debug = true;
var loadImages = true;
// used to dedupe on append/insert
var knownIds = [];
@ -59,14 +59,16 @@ WorkerScript.onMessage = function(msg) {
var API = mastodonAPI({ instance: msg.conf.instance, api_user_token: msg.conf.api_user_token});
/* for some actions
/*
* HEAD call for some actions
* we have to retrieve the Link header
* this falls through and continues for GET
*/
if (msg.action === "bookmarks"){
API.getLink(msg.action, msg.params, function(data) {
console.log(JSON.stringify(data))
WorkerScript.sendMessage({ 'Header': data })
if (debug) console.log(JSON.stringify(data))
WorkerScript.sendMessage({ 'LinkHeader': data })
});
}
@ -182,26 +184,27 @@ 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 (debug) console.log("Fetched > " +length + " in " + mode)
if (debug) console.log("ids > " + knownIds.length )
if (mode === "append") {
for(i = 0; i <= length-1; i++) {
if ( knownIds.indexOf( items[i]["id"]) === -1) {
model.append(items[i])
} else {
console.log("nope: " + items[i]["id"] )
}
}
//model.append(items)
console.log("nope: " + items[i]["id"] )
}
}
// search does not use ids
if ( knownIds.length < 1 ) model.append(items)
} else if (mode === "prepend") {
for(i = length-1; i >= 0 ; i--) {
model.insert(0,items[i])
/*if ( knownIds.indexOf( items[i]["id"]) === -1) {
model.insert(0,items[i])
}*/

View file

@ -18,7 +18,10 @@ SilicaListView {
property bool loadStarted: false
property int scrollOffset
property string action: ""
// should consider better names or
// using min_ & max_id
property string linkprev: ""
property string linknext: ""
property variant vars
property variant conf
property bool notifier: false
@ -172,7 +175,7 @@ SilicaListView {
if (debug) console.log(JSON.stringify(messageObject))
} else {
if (debug) console.log(JSON.stringify(messageObject))
loadStarted = false
// loadStarted = false
}
if (messageObject.fireNotification && notifier){
@ -185,13 +188,21 @@ SilicaListView {
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
}
// the api is stupid
if (messageObject.LinkHeader) {
// <https://mastodon.gamedev.place/api/v1/bookmarks?max_id=11041>; rel=\"next\",
// <https://mastodon.gamedev.place/api/v1/bookmarks?min_id=14158>; rel=\"prev\""
var matches = /max_id=([0-9]+)/.exec(messageObject.LinkHeader);
var maxlink = matches[0].split("=")[1];
var matches = /min_id=([0-9]+)/.exec(messageObject.LinkHeader);
var minlink = matches[0].split("=")[1];
if (debug) console.log("maxlink: " + maxlink)
if (debug) console.log("minlink: " + minlink)
linkprev = maxlink
linknext = minlink
}
}
}
@ -204,7 +215,10 @@ SilicaListView {
triggeredOnStart: false;
interval: {
/* this is hamfisted */
/*
* Varied calls so that server isn't hit
* simultaenously ... 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
@ -250,8 +264,8 @@ SilicaListView {
}
//if (debug) console.log(ids)
if (debug) console.log(uniqueItems.length)
if (debug) console.log( "maxminusone?:" + model.get(model.count - 2).id )
if (debug) console.log( "max?:" + model.get(model.count - 1).id )
if (debug) console.log( "max-one?:" + model.get(model.count - 2).id )
if (debug) console.log( "max:" + model.get(model.count - 1).id )
if ( uniqueItems.length < model.count) {
@ -315,17 +329,27 @@ SilicaListView {
for(var i = 0; i<params.length; i++)
p.push(params[i])
}
/*
* for some types, min_id, max_id
* is obtained from link header
*/
if (mode === "append" && model.count) {
// for some types, max_id is obtained from link header
if ( linkprev === "" ) {
if ( linkprev === "" ) {
p.push({name: 'max_id', data: model.get(model.count-1).id})
} else {
p.push({name: 'max_id', data: linkprev})
}
} else {
p.push({name: 'max_id', data: linkprev})
}
}
if (mode === "prepend" && model.count) {
p.push({name:'since_id', data: model.get(0).id})
if ( linknext === "" ) {
p.push({name:'since_id', data: model.get(0).id})
} else {
p.push({name: 'min_id', data: linknext})
}
}
//if (debug) console.log(JSON.stringify(uniqueIds))
if(title === "Local") {
type = "timelines/public"