74 lines
2.1 KiB
JavaScript
74 lines
2.1 KiB
JavaScript
import * as api from '$lib/api.js';
|
|
import { server } from '$lib/client/server.js';
|
|
import { app } from '$lib/client/app.js';
|
|
import { get, writable } from 'svelte/store';
|
|
import { parsePost } from '$lib/post.js';
|
|
import Lang from '$lib/lang';
|
|
|
|
export const timeline = writable([]);
|
|
|
|
const lang = Lang();
|
|
|
|
let loading = false;
|
|
let last_post = false;
|
|
|
|
export async function getTimeline(timelineType = "home", clean, localOnly = false, remoteOnly = false) {
|
|
if (loading) return; // no spamming!!
|
|
loading = true;
|
|
|
|
// if (!clean && get(timeline).length > 0)
|
|
// last_post = get(timeline)[get(timeline).length - 1].id;
|
|
|
|
let timeline_data;
|
|
switch(timelineType) {
|
|
case "favourites":
|
|
timeline_data = await api.getFavourites(
|
|
get(server).host,
|
|
get(app).token,
|
|
last_post
|
|
)
|
|
break;
|
|
|
|
default:
|
|
timeline_data = await api.getTimeline(
|
|
get(server).host,
|
|
get(app).token,
|
|
timelineType,
|
|
last_post,
|
|
localOnly,
|
|
remoteOnly
|
|
);
|
|
break;
|
|
}
|
|
|
|
if (!timeline_data) {
|
|
console.error(lang.string('logs.timeline_fetch_failed'));
|
|
loading = false;
|
|
return;
|
|
}
|
|
|
|
if (clean) {
|
|
timeline.set([]);
|
|
last_post = false;
|
|
} else {
|
|
last_post = timeline_data.next.url.searchParams.get("max_id")
|
|
}
|
|
|
|
for (let i in timeline_data.data) {
|
|
const post_data = timeline_data.data[i];
|
|
const post = await parsePost(post_data, 1);
|
|
if (!post) {
|
|
if (post === null || post === undefined) {
|
|
if (post_data.id) {
|
|
console.warn(lang.string('logs.post_parse_failed_id', post_data.id));
|
|
} else {
|
|
console.warn(lang.string('logs.post_parse_failed'));
|
|
console.debug(post_data);
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
timeline.update(current => [...current, post]);
|
|
}
|
|
loading = false;
|
|
}
|