2022-12-06 13:20:36 +00:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
import axios from "axios";
|
2023-01-23 13:29:31 +00:00
|
|
|
import { createClient } from "masto";
|
2022-12-07 05:45:05 +00:00
|
|
|
import { webfinger } from "../assets/utils";
|
2022-12-06 13:20:36 +00:00
|
|
|
|
|
|
|
export const useMastodonStore = defineStore("mastodon", {
|
|
|
|
state() {
|
|
|
|
return {
|
2022-12-07 05:45:05 +00:00
|
|
|
authorized: false,
|
2022-12-09 00:21:17 +00:00
|
|
|
oauth: {
|
|
|
|
url: "",
|
|
|
|
token: "",
|
2023-01-23 12:10:21 +00:00
|
|
|
audon: null,
|
2022-12-09 00:21:17 +00:00
|
|
|
},
|
2022-12-07 05:45:05 +00:00
|
|
|
client: null,
|
|
|
|
userinfo: null,
|
2023-01-25 06:37:31 +00:00
|
|
|
avatar: "",
|
2022-12-06 13:20:36 +00:00
|
|
|
};
|
|
|
|
},
|
2022-12-07 05:45:05 +00:00
|
|
|
getters: {
|
|
|
|
myWebfinger() {
|
|
|
|
if (this.userinfo !== null) {
|
|
|
|
return webfinger(this.userinfo);
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
},
|
|
|
|
},
|
2022-12-06 13:20:36 +00:00
|
|
|
actions: {
|
|
|
|
async fetchToken() {
|
2022-12-07 05:45:05 +00:00
|
|
|
const resp = await axios.get("/api/token");
|
|
|
|
this.oauth = resp.data;
|
2023-01-23 13:29:31 +00:00
|
|
|
const client = createClient({
|
2022-12-07 05:45:05 +00:00
|
|
|
url: this.oauth.url,
|
|
|
|
accessToken: this.oauth.token,
|
2023-01-11 20:53:15 +00:00
|
|
|
disableVersionCheck: true,
|
2022-12-07 05:45:05 +00:00
|
|
|
});
|
|
|
|
this.client = client;
|
2023-01-23 12:10:21 +00:00
|
|
|
const user = await client.v1.accounts.verifyCredentials();
|
|
|
|
this.userinfo = user;
|
2022-12-07 05:45:05 +00:00
|
|
|
this.authorized = true;
|
|
|
|
},
|
2023-01-25 06:37:31 +00:00
|
|
|
async updateAvatar(img, filename) {
|
2023-01-23 12:10:21 +00:00
|
|
|
if (this.client === null) return;
|
2023-01-23 13:29:31 +00:00
|
|
|
const avatarBlob = await (await fetch(img)).blob();
|
2023-01-23 12:10:21 +00:00
|
|
|
this.userinfo = await this.client.v1.accounts.updateCredentials({
|
2023-01-25 06:37:31 +00:00
|
|
|
avatar: new File([avatarBlob], `${Date.now()}_${filename}`),
|
2023-01-23 12:10:21 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
async revertAvatar() {
|
2023-01-25 06:37:31 +00:00
|
|
|
const token = await axios.get("/api/token");
|
2023-01-25 20:58:15 +00:00
|
|
|
const rooms = await axios.get("/api/room");
|
|
|
|
if (token.data.audon.avatar && rooms.data.length < 1) {
|
2023-01-25 19:52:05 +00:00
|
|
|
if (this.avatar) {
|
|
|
|
await this.updateAvatar(this.avatar, token.data.audon.avatar);
|
|
|
|
}
|
|
|
|
await axios.delete("/api/room");
|
|
|
|
}
|
2022-12-06 13:20:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|