2024-06-21 04:55:24 +01:00
|
|
|
import { Instance, server_types } from './instance.js';
|
|
|
|
import * as api from './api.js';
|
2024-06-28 06:19:00 +01:00
|
|
|
import { get, writable } from 'svelte/store';
|
2024-07-02 19:38:20 +01:00
|
|
|
import { last_read_notif_id } from '$lib/notifications.js';
|
2024-07-02 20:21:34 +01:00
|
|
|
import { user, logged_in } from '$lib/stores/user.js';
|
2024-06-19 22:13:16 +01:00
|
|
|
|
2024-07-01 03:41:02 +01:00
|
|
|
export const client = writable(false);
|
2024-06-19 22:13:16 +01:00
|
|
|
|
2024-06-30 17:37:19 +01:00
|
|
|
const save_name = "campfire";
|
2024-06-19 22:13:16 +01:00
|
|
|
|
|
|
|
export class Client {
|
|
|
|
instance;
|
|
|
|
app;
|
|
|
|
#cache;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.instance = null;
|
|
|
|
this.app = null;
|
|
|
|
this.cache = {
|
|
|
|
users: {},
|
|
|
|
emojis: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async init(host) {
|
|
|
|
if (host.startsWith("https://")) host = host.substring(8);
|
2024-06-21 03:02:18 +01:00
|
|
|
const url = `https://${host}/api/v1/instance`;
|
2024-06-21 04:55:24 +01:00
|
|
|
const data = await fetch(url).then(res => res.json()).catch(error => { console.error(error) });
|
2024-06-21 03:02:18 +01:00
|
|
|
if (!data) {
|
|
|
|
console.error(`Failed to connect to ${host}`);
|
2024-06-28 06:19:00 +01:00
|
|
|
return `Failed to connect to ${host}!`;
|
2024-06-21 03:02:18 +01:00
|
|
|
}
|
2024-06-21 04:55:24 +01:00
|
|
|
|
|
|
|
this.instance = new Instance(host, data.version);
|
2024-06-22 16:35:41 +01:00
|
|
|
if (this.instance.type == server_types.UNSUPPORTED) {
|
|
|
|
console.warn(`Server ${host} is unsupported - ${data.version}`);
|
|
|
|
if (!confirm(
|
2024-06-29 23:10:29 +01:00
|
|
|
`This app does not officially support ${host}. ` +
|
|
|
|
`Things may break, or otherwise not work as epxected! ` +
|
|
|
|
`Are you sure you wish to continue?`
|
|
|
|
)) return false;
|
2024-06-22 16:35:41 +01:00
|
|
|
} else {
|
2024-06-29 23:10:29 +01:00
|
|
|
console.log(`Server is "${this.instance.type}" (or compatible) with capabilities: [${this.instance.capabilities}].`);
|
|
|
|
}
|
2024-06-21 03:02:18 +01:00
|
|
|
|
2024-06-21 04:55:24 +01:00
|
|
|
this.app = await api.createApp(host);
|
2024-06-19 22:13:16 +01:00
|
|
|
|
|
|
|
if (!this.app || !this.instance) {
|
|
|
|
console.error("Failed to create app. Check the network logs for details.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-06-21 03:02:18 +01:00
|
|
|
this.save();
|
|
|
|
|
2024-06-28 06:19:00 +01:00
|
|
|
client.set(this);
|
|
|
|
|
2024-06-21 03:02:18 +01:00
|
|
|
return true;
|
2024-06-19 22:13:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getOAuthUrl() {
|
2024-06-21 04:55:24 +01:00
|
|
|
return api.getOAuthUrl(this.app.secret);
|
2024-06-19 22:13:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async getToken(code) {
|
2024-06-21 04:55:24 +01:00
|
|
|
const token = await api.getToken(code);
|
2024-06-19 22:13:16 +01:00
|
|
|
if (!token) {
|
|
|
|
console.error("Failed to obtain access token");
|
|
|
|
return false;
|
|
|
|
}
|
2024-07-01 03:41:02 +01:00
|
|
|
return token;
|
2024-06-19 22:13:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async revokeToken() {
|
2024-06-21 04:55:24 +01:00
|
|
|
return await api.revokeToken();
|
2024-06-19 22:13:16 +01:00
|
|
|
}
|
|
|
|
|
2024-07-02 12:55:51 +01:00
|
|
|
async getNotifications(since_id, limit, types) {
|
|
|
|
return await api.getNotifications(since_id, limit, types);
|
|
|
|
}
|
|
|
|
|
2024-06-19 22:13:16 +01:00
|
|
|
async getTimeline(last_post_id) {
|
2024-06-21 04:55:24 +01:00
|
|
|
return await api.getTimeline(last_post_id);
|
2024-06-19 22:13:16 +01:00
|
|
|
}
|
|
|
|
|
2024-06-28 06:19:00 +01:00
|
|
|
async getPost(post_id, parent_replies, child_replies) {
|
|
|
|
return await api.getPost(post_id, parent_replies, child_replies);
|
2024-06-19 22:13:16 +01:00
|
|
|
}
|
|
|
|
|
2024-07-01 03:41:02 +01:00
|
|
|
async getPostContext(post_id) {
|
|
|
|
return await api.getPostContext(post_id);
|
|
|
|
}
|
|
|
|
|
2024-06-28 08:43:12 +01:00
|
|
|
async boostPost(post_id) {
|
|
|
|
return await api.boostPost(post_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
async unboostPost(post_id) {
|
|
|
|
return await api.unboostPost(post_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
async favouritePost(post_id) {
|
|
|
|
return await api.favouritePost(post_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
async unfavouritePost(post_id) {
|
|
|
|
return await api.unfavouritePost(post_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
async reactPost(post_id, shortcode) {
|
|
|
|
return await api.reactPost(post_id, shortcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
async unreactPost(post_id, shortcode) {
|
|
|
|
return await api.unreactPost(post_id, shortcode);
|
|
|
|
}
|
|
|
|
|
2024-06-19 22:13:16 +01:00
|
|
|
putCacheUser(user) {
|
|
|
|
this.cache.users[user.id] = user;
|
2024-06-28 06:19:00 +01:00
|
|
|
client.set(this);
|
2024-06-19 22:13:16 +01:00
|
|
|
}
|
|
|
|
|
2024-06-28 06:19:00 +01:00
|
|
|
async getCacheUser(user_id) {
|
2024-06-19 22:13:16 +01:00
|
|
|
let user = this.cache.users[user_id];
|
|
|
|
if (user) return user;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getUserByMention(mention) {
|
|
|
|
let users = Object.values(this.cache.users);
|
|
|
|
for (let i in users) {
|
|
|
|
const user = users[i];
|
|
|
|
if (user.mention == mention) return user;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
putCacheEmoji(emoji) {
|
|
|
|
this.cache.emojis[emoji.id] = emoji;
|
2024-06-28 06:19:00 +01:00
|
|
|
client.set(this);
|
2024-06-19 22:13:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getEmoji(emoji_id) {
|
|
|
|
let emoji = this.cache.emojis[emoji_id];
|
|
|
|
if (!emoji) return false;
|
|
|
|
return emoji;
|
|
|
|
}
|
|
|
|
|
2024-07-01 07:39:57 +01:00
|
|
|
async getUser(user_id) {
|
|
|
|
return await api.getUser(user_id);
|
|
|
|
}
|
|
|
|
|
2024-06-19 22:13:16 +01:00
|
|
|
save() {
|
2024-06-29 10:46:27 +01:00
|
|
|
if (typeof localStorage === typeof undefined) return;
|
2024-06-19 22:13:16 +01:00
|
|
|
localStorage.setItem(save_name, JSON.stringify({
|
2024-06-21 07:19:31 +01:00
|
|
|
version: APP_VERSION,
|
2024-06-21 04:55:24 +01:00
|
|
|
instance: {
|
|
|
|
host: this.instance.host,
|
|
|
|
version: this.instance.version,
|
|
|
|
},
|
2024-07-02 19:38:20 +01:00
|
|
|
last_read_notif_id: get(last_read_notif_id),
|
2024-06-19 22:13:16 +01:00
|
|
|
app: this.app,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
load() {
|
2024-06-29 10:46:27 +01:00
|
|
|
if (typeof localStorage === typeof undefined) return;
|
2024-06-19 22:13:16 +01:00
|
|
|
let json = localStorage.getItem(save_name);
|
|
|
|
if (!json) return false;
|
|
|
|
let saved = JSON.parse(json);
|
2024-06-21 07:19:31 +01:00
|
|
|
if (!saved.version || saved.version !== APP_VERSION) {
|
|
|
|
localStorage.removeItem(save_name);
|
|
|
|
return false;
|
|
|
|
}
|
2024-06-21 04:55:24 +01:00
|
|
|
this.instance = new Instance(saved.instance.host, saved.instance.version);
|
2024-07-02 19:38:20 +01:00
|
|
|
last_read_notif_id.set(saved.last_read_notif_id || 0);
|
2024-06-19 22:13:16 +01:00
|
|
|
this.app = saved.app;
|
2024-06-28 06:19:00 +01:00
|
|
|
client.set(this);
|
2024-06-19 22:13:16 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async logout() {
|
2024-06-21 04:55:24 +01:00
|
|
|
if (!this.instance || !this.app) return;
|
2024-06-19 22:13:16 +01:00
|
|
|
if (!await this.revokeToken()) {
|
|
|
|
console.warn("Failed to log out correctly; ditching the old tokens anyways.");
|
|
|
|
}
|
|
|
|
localStorage.removeItem(save_name);
|
2024-07-02 20:21:34 +01:00
|
|
|
logged_in.set(false);
|
2024-07-01 03:41:02 +01:00
|
|
|
client.set(new Client());
|
2024-06-19 22:13:16 +01:00
|
|
|
console.log("Logged out successfully.");
|
|
|
|
}
|
|
|
|
}
|