audon/audon-fe/src/stores/mastodon.js

54 wiersze
1.2 KiB
JavaScript
Czysty Zwykły widok Historia

2022-12-06 13:20:36 +00:00
import { defineStore } from "pinia";
import axios from "axios";
2022-12-07 05:45:05 +00:00
import { login } from "masto";
import router from "../router";
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,
oauth: {
url: "",
token: "",
audon_id: "",
},
2022-12-07 05:45:05 +00:00
client: null,
userinfo: null,
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;
const client = await login({
url: this.oauth.url,
accessToken: this.oauth.token,
disableVersionCheck: true,
2022-12-07 05:45:05 +00:00
});
this.client = client;
this.userinfo = await client.accounts.verifyCredentials();
this.authorized = true;
},
async callMastodonAPI(caller, ...args) {
2022-12-06 13:20:36 +00:00
try {
2022-12-07 05:45:05 +00:00
return await caller(...args);
2022-12-06 13:20:36 +00:00
} catch (error) {
2022-12-07 05:45:05 +00:00
if (error.response?.status === 401) {
this.$reset();
router.push({ name: "login" });
2022-12-06 13:20:36 +00:00
}
2022-12-07 05:45:05 +00:00
throw error;
2022-12-06 13:20:36 +00:00
}
},
},
});