Cleanup + future proofing
parent
d44a62f21b
commit
979ac88365
|
|
@ -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<Tag>
|
||||
export type TagCategoriesResult = UnpagedSearchResult<TagCategory>
|
||||
|
|
|
|||
|
|
@ -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<void> {
|
||||
async createPost(post: LocalPost): Promise<Post> {
|
||||
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<any> {
|
||||
|
|
@ -96,13 +96,7 @@ export default class SzuruWrapper {
|
|||
}
|
||||
}
|
||||
|
||||
static async createFromConfig(): Promise<SzuruWrapper | null> {
|
||||
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<SzuruWrapper | null> {
|
||||
return new SzuruWrapper(siteConfig.domain, siteConfig.username, siteConfig.authToken);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 <a href='#'>post</a>");
|
||||
// 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 = `<a href='${this.getPostUrl(createdPost)}'>Uploaded post</a>`;
|
||||
} 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 {
|
||||
if (this.activeSite == null) {
|
||||
this.pushError("No szurubooru server configured!");
|
||||
} else {
|
||||
this.szuru = await SzuruWrapper.createFromConfig(this.activeSite);
|
||||
}
|
||||
});
|
||||
|
||||
this.grabPost();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Reference in New Issue