Implemented loadMore pagination on pages.meet

This commit is contained in:
Niko 2022-02-17 01:24:26 +01:00
parent 456b9cea18
commit e6e67b2b09
3 changed files with 26 additions and 5 deletions

View File

@ -1,6 +1,10 @@
const crypto = require('crypto')
const mongo = require('mongodb')
module.exports = {
mongo: {
getID: (str) => { return new mongo.ObjectId(str) },
},
v1url: (path) => { return '/api/v1' + path },
isURLValid: (url) => {
try {

View File

@ -1,16 +1,20 @@
const auth = require('../auth.js')
const utils = require('../api-utils.js')
const FEED_LIMIT = 20
const FEED_LIMIT = 10
module.exports = {
meet: {
// TODO: change algorithm later (to actually get posts instead of accounts)
get: [auth.enforceSession, async (req, res) => {
var results = []
var filter = { type: 'Note', inReplyTo: undefined }
if (req.query.skip !== undefined)
filter._id = { $lt: utils.mongo.getID(req.query.skip) }
const notes = await db.table.objects()
.find({ type: 'Note', inReplyTo: undefined })
.find(filter)
.sort({ _id: -1 })
.limit(FEED_LIMIT)
.toArray()
for (var i = 0; i < notes.length; i++) {
const note = notes[i]

View File

@ -13,6 +13,14 @@ app.pages.meet = {
});
});
},
loadMore: function() {
const skip = app.pages.meet.data[app.pages.meet.data.length-1]._id;
http.get(`/api/v1/feed/meet?skip=${skip}`, {}, function(json) {
if (json === undefined || json.length === 0) return;
app.pages.meet.data = app.pages.meet.data.concat(json);
app.pages.meet.paint();
});
},
paint: function(json) {
json = json || app.pages.meet.data;
let _;
@ -79,7 +87,12 @@ app.pages.meet = {
app.pages.meet.carousel.setDragEvent(direction);
},
set: function(i) { return app.carousel.set(app.pages.meet.carousel, i) },
next: function() { return app.carousel.next(app.pages.meet.carousel) },
next: function() {
if (app.pages.meet.carousel._current >=
app.pages.meet.carousel._size() - 2)
app.pages.meet.loadMore();
return app.carousel.next(app.pages.meet.carousel);
},
prev: function() { return app.carousel.prev(app.pages.meet.carousel) },
setDragEvent: function(direction) {
direction = direction || 'none';
@ -88,8 +101,8 @@ app.pages.meet = {
var options = { y: false, return2start: true, fadeOut: true };
if (!app.carousel.hasPrev(app.pages.meet.carousel))
options.right = false;
if (!app.carousel.hasNext(app.pages.meet.carousel))
options.left = false;
/*if (!app.carousel.hasNext(app.pages.meet.carousel))
options.left = false;*/
if (direction === 'left') {
current.classList.add('slide-from-left');
setTimeout(function() {