forked from blisstown/campfire
54 lines
1.5 KiB
JavaScript
54 lines
1.5 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('en_GB');
|
|
|
|
let loading = false;
|
|
|
|
export async function getTimeline(clean) {
|
|
if (loading) return; // no spamming!!
|
|
loading = true;
|
|
|
|
let last_post = false;
|
|
if (!clean && get(timeline).length > 0)
|
|
last_post = get(timeline)[get(timeline).length - 1].id;
|
|
|
|
const timeline_data = await api.getTimeline(
|
|
get(server).host,
|
|
get(app).token,
|
|
"home",
|
|
last_post
|
|
);
|
|
|
|
if (!timeline_data) {
|
|
console.error(lang.string('logs.timeline_fetch_failed'));
|
|
loading = false;
|
|
return;
|
|
}
|
|
|
|
if (clean) timeline.set([]);
|
|
|
|
for (let i in timeline_data) {
|
|
const post_data = timeline_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;
|
|
}
|