From 6ca334d0347db53c4ddc9ae8108f0433d62c2e03 Mon Sep 17 00:00:00 2001 From: neobooru <50623835+neobooru@users.noreply.github.com> Date: Sun, 1 Sep 2019 12:53:07 +0200 Subject: [PATCH] Finish "import tag category" feature Well the basics of it anyway, there are no options to configure yet so it'll always set the tag to whatever category the source website wants it to be. --- package.json | 2 +- src/SzuruWrapper.ts | 26 +++++++++++++++++++++++--- src/popup/App.vue | 24 ++++++++++++++---------- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 33cca8e..30ee7ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "szuru-chrome", - "version": "0.1.0", + "version": "0.1.1", "description": "Import media from various sites into szurubooru", "author": "neobooru", "license": "MIT", diff --git a/src/SzuruWrapper.ts b/src/SzuruWrapper.ts index 5f85510..c37a45c 100644 --- a/src/SzuruWrapper.ts +++ b/src/SzuruWrapper.ts @@ -1,7 +1,7 @@ import axios, { AxiosRequestConfig } from 'axios'; import { LocalPost, LocalError } from './LocalTypes'; import { SzuruSiteConfig } from './Config'; -import { TagsResult, TagCategoriesResult, Post } from './SzuruTypes'; +import { TagsResult, TagCategoriesResult, Post, Tag } from './SzuruTypes'; /** * A 1:1 wrapper around the szurubooru API. @@ -36,8 +36,16 @@ export default class SzuruWrapper { return (await this.apiGet("info")).data; } - async getTags(): Promise { - return (await this.apiGet("tags")).data; + async updateTag(tag: Tag): Promise { + return (await this.apiPut("tag/" + tag.names[0], tag)).data; + } + + async getTags(query: string): Promise { + let url = "tags"; + if (query) { + url += query; + } + return (await this.apiGet(url)).data; } async getTagCategories(): Promise { @@ -81,6 +89,18 @@ export default class SzuruWrapper { return await this.execute(config); } + private async apiPut(url: string, data: any, additionalHeaders: any = {}): Promise { + const fullUrl = this.apiUrl + url; + const config: AxiosRequestConfig = { + method: "PUT", + url: fullUrl, + data: data + } + + config.headers = { ...this.baseHeaders, ...additionalHeaders }; + return await this.execute(config); + } + private async execute(config: AxiosRequestConfig): Promise { if (this.username && this.authToken) { const token = "Token " + btoa(`${this.username}:${this.authToken}`); diff --git a/src/popup/App.vue b/src/popup/App.vue index 7a6eb0c..a92ca2e 100644 --- a/src/popup/App.vue +++ b/src/popup/App.vue @@ -155,9 +155,6 @@ export default Vue.extend({ // see https://stackoverflow.com/questions/8915845 uploadMsg.content = `Uploaded post`; - let tagsMsg = new Message("Checking tags..."); - this.messages.push(tagsMsg); - // Find tags with "default" category and update it // TODO: Make all these categories configurable const tagsWithCategory = this.post.tags.filter(x => x.category); @@ -165,14 +162,21 @@ export default Vue.extend({ .filter(x => x.category == "default") .filter(x => tagsWithCategory.some(y => x.names.includes(y.name))); - console.dir(unsetCategoryTags); + if (unsetCategoryTags.length != 0) { + let tagsMsg = new Message(`${unsetCategoryTags.length} tags need a different category`); + this.messages.push(tagsMsg); - if (unsetCategoryTags.length == 0) { - tagsMsg.content = "All tags have the correct category"; - } else { - tagsMsg.content = `${unsetCategoryTags.length} tags need a different category`; - // TODO: Call /tags?query={tag1},{tag2},{tag3} - // TODO: Foreach tag call PUT /tag/{tag1} with updated category + // unsetCategoryTags is of type MicroTag[] and we need a Tag resource to update it, so let's get those + const query = "?query=" + unsetCategoryTags.map(x => encodeURIComponent(x.names[0])); + const tags = (await this.szuru.getTags(query)).results; + + for (let i in tags) { + tagsMsg.content = `Updating tag ${i}/${unsetCategoryTags.length}`; + tags[i].category = tagsWithCategory.find(x => tags[i].names.includes(x.name))!.category! + await this.szuru.updateTag(tags[i]); + } + + tagsMsg.content = "Updated all tags"; } } catch (ex) { const error = ex as LocalError;