Cleanup + future proofing
parent
d44a62f21b
commit
979ac88365
|
|
@ -42,5 +42,45 @@ export interface TagCategory {
|
||||||
default: boolean;
|
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 TagsResult = PagedSearchResult<Tag>
|
||||||
export type TagCategoriesResult = UnpagedSearchResult<TagCategory>
|
export type TagCategoriesResult = UnpagedSearchResult<TagCategory>
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import axios, { AxiosRequestConfig } from 'axios';
|
import axios, { AxiosRequestConfig } from 'axios';
|
||||||
import { LocalPost, LocalError } from './LocalTypes';
|
import { LocalPost, LocalError } from './LocalTypes';
|
||||||
import { Config } from './Config';
|
import { SzuruSiteConfig } from './Config';
|
||||||
import { TagsResult, TagCategoriesResult } from './SzuruTypes';
|
import { TagsResult, TagCategoriesResult, Post } from './SzuruTypes';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A 1:1 wrapper around the szurubooru API.
|
* A 1:1 wrapper around the szurubooru API.
|
||||||
|
|
@ -44,7 +44,7 @@ export default class SzuruWrapper {
|
||||||
return (await this.apiGet("tag-categories")).data;
|
return (await this.apiGet("tag-categories")).data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createPost(post: LocalPost): Promise<void> {
|
async createPost(post: LocalPost): Promise<Post> {
|
||||||
var obj = {
|
var obj = {
|
||||||
tags: post.tags.map(x => x.name),
|
tags: post.tags.map(x => x.name),
|
||||||
safety: post.safety,
|
safety: post.safety,
|
||||||
|
|
@ -55,7 +55,7 @@ export default class SzuruWrapper {
|
||||||
console.log("Create new post object");
|
console.log("Create new post object");
|
||||||
console.dir(obj);
|
console.dir(obj);
|
||||||
|
|
||||||
await this.apiPost("posts", obj);
|
return (await this.apiPost("posts", obj)).data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async apiGet(url: string, additionalHeaders: any = {}): Promise<any> {
|
private async apiGet(url: string, additionalHeaders: any = {}): Promise<any> {
|
||||||
|
|
@ -96,13 +96,7 @@ export default class SzuruWrapper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async createFromConfig(): Promise<SzuruWrapper | null> {
|
static async createFromConfig(siteConfig: SzuruSiteConfig): Promise<SzuruWrapper | null> {
|
||||||
const config = await Config.load();
|
return new SzuruWrapper(siteConfig.domain, siteConfig.username, siteConfig.authToken);
|
||||||
|
|
||||||
if (config.sites.length == 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new SzuruWrapper(config.sites[0].domain, config.sites[0].username, config.sites[0].authToken);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,8 @@ import Vue from "vue";
|
||||||
import { browser } from "webextension-polyfill-ts";
|
import { browser } from "webextension-polyfill-ts";
|
||||||
import { LocalPost, LocalTag, LocalError } from "../LocalTypes";
|
import { LocalPost, LocalTag, LocalError } from "../LocalTypes";
|
||||||
import SzuruWrapper from "../SzuruWrapper";
|
import SzuruWrapper from "../SzuruWrapper";
|
||||||
|
import { Post } from "../SzuruTypes";
|
||||||
|
import { Config, SzuruSiteConfig } from "../Config";
|
||||||
|
|
||||||
type MessageType = "error" | "info";
|
type MessageType = "error" | "info";
|
||||||
|
|
||||||
|
|
@ -106,6 +108,7 @@ class Message {
|
||||||
export default Vue.extend({
|
export default Vue.extend({
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
|
activeSite: null as SzuruSiteConfig | null,
|
||||||
szuru: null as SzuruWrapper | null,
|
szuru: null as SzuruWrapper | null,
|
||||||
post: new LocalPost(),
|
post: new LocalPost(),
|
||||||
messages: [] as Message[]
|
messages: [] as Message[]
|
||||||
|
|
@ -130,8 +133,7 @@ export default Vue.extend({
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async upload() {
|
async upload() {
|
||||||
// Clear messages
|
this.clearMessages();
|
||||||
this.messages = [];
|
|
||||||
|
|
||||||
if (!this.post || !this.post.imageUrl) {
|
if (!this.post || !this.post.imageUrl) {
|
||||||
this.pushError("There is no post to upload!");
|
this.pushError("There is no post to upload!");
|
||||||
|
|
@ -144,9 +146,13 @@ export default Vue.extend({
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
this.pushInfo("Uploading...");
|
||||||
|
window.scrollTo(0, 0);
|
||||||
const createdPost = await this.szuru.createPost(this.post);
|
const createdPost = await this.szuru.createPost(this.post);
|
||||||
// TODO: Add url to post page on szurubooru
|
// HACK: Directly set message because there isn't a better way to update messages
|
||||||
this.pushInfo("Uploaded <a href='#'>post</a>");
|
// 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) {
|
} catch (ex) {
|
||||||
const error = ex as LocalError;
|
const error = ex as LocalError;
|
||||||
|
|
||||||
|
|
@ -201,18 +207,26 @@ export default Vue.extend({
|
||||||
},
|
},
|
||||||
pushError(message: string) {
|
pushError(message: string) {
|
||||||
this.messages.push(new Message(message, "error"));
|
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() {
|
async mounted() {
|
||||||
this.grabPost();
|
const cfg = await Config.load();
|
||||||
|
this.activeSite = cfg.sites.length > 0 ? cfg.sites[0] : null;
|
||||||
|
|
||||||
SzuruWrapper.createFromConfig().then(x => {
|
if (this.activeSite == null) {
|
||||||
if (x) {
|
this.pushError("No szurubooru server configured!");
|
||||||
this.szuru = x;
|
} else {
|
||||||
} else {
|
this.szuru = await SzuruWrapper.createFromConfig(this.activeSite);
|
||||||
this.pushError("No szurubooru server configured!");
|
}
|
||||||
}
|
|
||||||
});
|
this.grabPost();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue