From 979ac883657e50fcc27b61ecd4535ef9fbc8b904 Mon Sep 17 00:00:00 2001 From: neobooru <50623835+neobooru@users.noreply.github.com> Date: Sat, 31 Aug 2019 01:15:54 +0200 Subject: [PATCH] Cleanup + future proofing --- src/SzuruTypes.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/SzuruWrapper.ts | 18 ++++++------------ src/popup/App.vue | 40 +++++++++++++++++++++++++++------------- 3 files changed, 73 insertions(+), 25 deletions(-) diff --git a/src/SzuruTypes.ts b/src/SzuruTypes.ts index 740e935..efe8b49 100644 --- a/src/SzuruTypes.ts +++ b/src/SzuruTypes.ts @@ -42,5 +42,45 @@ export interface TagCategory { default: boolean; } +export interface MicroUser { + name: string; + avatarUrl: string; +} + +export interface Post { + id: number; + version: number; + creationTime: Date; + lastEditTime?: Date; + safety: Safety; + source: string; + type: string; + mimeType: string; + checksum: string; + fileSize: number; + canvasWidth: number; + canvasHeight: number; + contentUrl: string; + thumbnailUrl: string; + flags: string[]; + tags: MicroTag[]; + relations: any[]; // MicroPost resource + user: MicroUser; + score: number; + ownScore: number; + ownFavorite: boolean; + tagCount: number; + favoriteCount: number; + commentCount: number; + noteCount: number; + relationCount: number; + featureCount: number; + lastFeatureTime?: Date; + favoritedBy: MicroUser[]; + hasCustomThumbnail: boolean; + notes: any[]; // Note resource + comments: any[]; // Comment resource +} + export type TagsResult = PagedSearchResult export type TagCategoriesResult = UnpagedSearchResult diff --git a/src/SzuruWrapper.ts b/src/SzuruWrapper.ts index 06a03f7..5f85510 100644 --- a/src/SzuruWrapper.ts +++ b/src/SzuruWrapper.ts @@ -1,7 +1,7 @@ import axios, { AxiosRequestConfig } from 'axios'; import { LocalPost, LocalError } from './LocalTypes'; -import { Config } from './Config'; -import { TagsResult, TagCategoriesResult } from './SzuruTypes'; +import { SzuruSiteConfig } from './Config'; +import { TagsResult, TagCategoriesResult, Post } from './SzuruTypes'; /** * A 1:1 wrapper around the szurubooru API. @@ -44,7 +44,7 @@ export default class SzuruWrapper { return (await this.apiGet("tag-categories")).data; } - async createPost(post: LocalPost): Promise { + async createPost(post: LocalPost): Promise { var obj = { tags: post.tags.map(x => x.name), safety: post.safety, @@ -55,7 +55,7 @@ export default class SzuruWrapper { console.log("Create new post object"); console.dir(obj); - await this.apiPost("posts", obj); + return (await this.apiPost("posts", obj)).data; } private async apiGet(url: string, additionalHeaders: any = {}): Promise { @@ -96,13 +96,7 @@ export default class SzuruWrapper { } } - static async createFromConfig(): Promise { - const config = await Config.load(); - - if (config.sites.length == 0) { - return null; - } - - return new SzuruWrapper(config.sites[0].domain, config.sites[0].username, config.sites[0].authToken); + static async createFromConfig(siteConfig: SzuruSiteConfig): Promise { + return new SzuruWrapper(siteConfig.domain, siteConfig.username, siteConfig.authToken); } } diff --git a/src/popup/App.vue b/src/popup/App.vue index ab16089..31d0995 100644 --- a/src/popup/App.vue +++ b/src/popup/App.vue @@ -90,6 +90,8 @@ import Vue from "vue"; import { browser } from "webextension-polyfill-ts"; import { LocalPost, LocalTag, LocalError } from "../LocalTypes"; import SzuruWrapper from "../SzuruWrapper"; +import { Post } from "../SzuruTypes"; +import { Config, SzuruSiteConfig } from "../Config"; type MessageType = "error" | "info"; @@ -106,6 +108,7 @@ class Message { export default Vue.extend({ data() { return { + activeSite: null as SzuruSiteConfig | null, szuru: null as SzuruWrapper | null, post: new LocalPost(), messages: [] as Message[] @@ -130,8 +133,7 @@ export default Vue.extend({ } }, async upload() { - // Clear messages - this.messages = []; + this.clearMessages(); if (!this.post || !this.post.imageUrl) { this.pushError("There is no post to upload!"); @@ -144,9 +146,13 @@ export default Vue.extend({ } try { + this.pushInfo("Uploading..."); + window.scrollTo(0, 0); const createdPost = await this.szuru.createPost(this.post); - // TODO: Add url to post page on szurubooru - this.pushInfo("Uploaded post"); + // HACK: Directly set message because there isn't a better way to update messages + // TODO: Clicking a link doesn't actually open it in a new tab, + // see https://stackoverflow.com/questions/8915845 + this.messages[0].content = `Uploaded post`; } catch (ex) { const error = ex as LocalError; @@ -201,18 +207,26 @@ export default Vue.extend({ }, pushError(message: string) { this.messages.push(new Message(message, "error")); + }, + clearMessages() { + this.messages = []; + }, + getPostUrl(post: Post): string { + if (!this.activeSite) return ""; + return this.activeSite.domain + "/post/" + post.id; } }, - mounted() { - this.grabPost(); + async mounted() { + const cfg = await Config.load(); + this.activeSite = cfg.sites.length > 0 ? cfg.sites[0] : null; - SzuruWrapper.createFromConfig().then(x => { - if (x) { - this.szuru = x; - } else { - this.pushError("No szurubooru server configured!"); - } - }); + if (this.activeSite == null) { + this.pushError("No szurubooru server configured!"); + } else { + this.szuru = await SzuruWrapper.createFromConfig(this.activeSite); + } + + this.grabPost(); } });