2024-06-21 07:19:31 +01:00
|
|
|
import { version as APP_VERSION } from '../../package.json';
|
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-06-19 22:13:16 +01:00
|
|
|
|
2024-06-28 06:19:00 +01:00
|
|
|
let client = writable(false);
|
2024-06-19 22:13:16 +01:00
|
|
|
|
|
|
|
const save_name = "spacesocial";
|
|
|
|
|
|
|
|
export class Client {
|
|
|
|
instance;
|
|
|
|
app;
|
2024-06-28 06:19:00 +01:00
|
|
|
user;
|
2024-06-19 22:13:16 +01:00
|
|
|
#cache;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.instance = null;
|
|
|
|
this.app = null;
|
|
|
|
this.cache = {
|
|
|
|
users: {},
|
|
|
|
emojis: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static get() {
|
2024-06-28 06:19:00 +01:00
|
|
|
if (get(client)) return client;
|
|
|
|
let new_client = new Client();
|
|
|
|
window.peekie = new_client;
|
|
|
|
new_client.load();
|
|
|
|
client.set(new_client);
|
2024-06-19 22:13:16 +01:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
|
|
|
`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;
|
|
|
|
} else {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
this.app.token = token;
|
2024-06-28 06:19:00 +01:00
|
|
|
client.set(this);
|
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-06-28 06:19:00 +01:00
|
|
|
async verifyCredentials() {
|
|
|
|
const data = await api.verifyCredentials();
|
|
|
|
if (!data) return false;
|
|
|
|
this.user = await api.parseUser(data);
|
|
|
|
client.set(this);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
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-06-19 22:13:16 +01:00
|
|
|
app: this.app,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
load() {
|
|
|
|
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.setItem(save_name + '-backup', json);
|
|
|
|
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-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-06-28 06:19:00 +01:00
|
|
|
client.set(false);
|
2024-06-19 22:13:16 +01:00
|
|
|
console.log("Logged out successfully.");
|
|
|
|
}
|
|
|
|
}
|