pull/9/head
neobooru 2020-03-21 15:21:23 +01:00
parent f5bc5c297d
commit 535b83fc68
2 changed files with 52 additions and 6 deletions

View File

@ -249,11 +249,6 @@ export default Vue.extend({
if (!this.activeSite) return ""; if (!this.activeSite) return "";
return this.activeSite.domain + "/post/" + post.id; return this.activeSite.domain + "/post/" + post.id;
}, },
encodeTagName(tagName: string) {
// Searching for posts with re:zero will show an error message about unknown named token.
// Searching for posts with re\:zero will show posts tagged with re:zero.
return encodeURIComponent(tagName.replace(/\:/g, "\\:"));
},
// Add tag to the post's taglist // Add tag to the post's taglist
addTag() { addTag() {
if (this.selectedPost) { if (this.selectedPost) {

View File

@ -1,9 +1,60 @@
import { ScrapedPost } from "neo-scraper"; import { ScrapedPost } from "neo-scraper";
import { browser, Runtime } from "webextension-polyfill-ts"; import { browser, Runtime } from "webextension-polyfill-ts";
import { BrowserCommand } from "../Common"; import { BrowserCommand } from "../Common";
import { Config } from "../Config.ts";
import SzuruWrapper from "../SzuruWrapper.ts";
import { SzuruError } from "../SzuruTypes";
function uploadPost(post: ScrapedPost) { async function uploadPost(post: ScrapedPost) {
// TODO: Upload post and send status updates to popup // TODO: Upload post and send status updates to popup
console.dir(post);
const cfg = await Config.load();
const szuru = (await SzuruWrapper.createFromConfig(cfg.sites[0]))!;
try {
// Create and upload post
console.log("Uploading...");
const createdPost = await szuru.createPost(post);
console.log("Uploaded post " + createdPost.id);
// TODO: Clicking a link doesn't actually open it in a new tab,
// see https://stackoverflow.com/questions/8915845
// uploadMsg.content = `<a href='${this.getPostUrl(createdPost)}' target='_blank'>Uploaded post</a>`;
// uploadMsg.type = "success";
// Find tags with "default" category and update it
// TODO: Make all these categories configurable
const tagsWithCategory = post.tags.filter(x => x.category);
const unsetCategoryTags = createdPost.tags
.filter(x => x.category == "default")
.filter(x => tagsWithCategory.some(y => x.names.includes(y.name)));
if (unsetCategoryTags.length != 0) {
console.log(`${unsetCategoryTags.length} tags need a different 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 => encodeTagName(x.names[0]));
const tags = (await szuru.getTags(query)).results;
for (let i in tags) {
console.log(`Updating tag ${i}/${unsetCategoryTags.length}`);
tags[i].category = tagsWithCategory.find(x => tags[i].names.includes(x.name))!.category!;
await szuru.updateTag(tags[i]);
}
console.log(`Updated ${tags.length} tags`);
}
} catch (ex) {
const error = ex as SzuruError;
if (error) {
console.error(error);
} else {
console.error(ex);
}
}
}
function encodeTagName(tagName: string) {
// Searching for posts with re:zero will show an error message about unknown named token.
// Searching for posts with re\:zero will show posts tagged with re:zero.
return encodeURIComponent(tagName.replace(/\:/g, "\\:"));
} }
async function messageHandler(cmd: BrowserCommand, sender: Runtime.MessageSender): Promise<any> { async function messageHandler(cmd: BrowserCommand, sender: Runtime.MessageSender): Promise<any> {