Various config persistency fixes

gh-actions
neobooru 2023-05-27 16:08:51 +02:00
parent 6d8844bd43
commit d2416d829d
11 changed files with 148 additions and 162 deletions

View File

@ -33,7 +33,6 @@
"neo-scraper": "^0.5.8",
"normalize.css": "^8.0.1",
"pinia": "^2.0.36",
"pinia-plugin-persistedstate": "^3.1.0",
"vue-router": "4"
},
"devDependencies": {

View File

@ -32,7 +32,6 @@ specifiers:
normalize.css: ^8.0.1
npm-run-all: ^4.1.5
pinia: ^2.0.36
pinia-plugin-persistedstate: ^3.1.0
rimraf: ^3.0.2
sass: ^1.57.1
typescript: ^4.9.3
@ -54,7 +53,6 @@ dependencies:
neo-scraper: 0.5.8
normalize.css: 8.0.1
pinia: 2.0.36_hmuptsblhheur2tugfgucj7gc4
pinia-plugin-persistedstate: 3.1.0_pinia@2.0.36
vue-router: 4.1.6_vue@3.2.47
devDependencies:
@ -4974,14 +4972,6 @@ packages:
engines: {node: '>=4'}
dev: true
/pinia-plugin-persistedstate/3.1.0_pinia@2.0.36:
resolution: {integrity: sha512-8UN+vYMEPBdgNLwceY08mi5olI0wkYaEb8b6hD6xW7SnBRuPydWHlEhZvUWgNb/ibuf4PvufpvtS+dmhYjJQOw==}
peerDependencies:
pinia: ^2.0.0
dependencies:
pinia: 2.0.36_hmuptsblhheur2tugfgucj7gc4
dev: false
/pinia/2.0.36_hmuptsblhheur2tugfgucj7gc4:
resolution: {integrity: sha512-4UKApwjlmJH+VuHKgA+zQMddcCb3ezYnyewQ9NVrsDqZ/j9dMv5+rh+1r48whKNdpFkZAWVxhBp5ewYaYX9JcQ==}
peerDependencies:

View File

@ -82,7 +82,7 @@ export default class SzurubooruApi {
async createPost(post: ScrapedPostDetails, contentToken?: string): Promise<Post> {
const obj = <any>{
tags: post.tags.map((x) => x.name),
tags: post.tags.map((x) => x.names[0]),
safety: post.rating,
source: post.source,
notes: post.notes,

View File

@ -46,7 +46,7 @@ async function uploadPost(data: PostUploadCommandData) {
const tagsWithCategory = data.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)));
.filter((x) => tagsWithCategory.some((y) => x.names.includes(y.names[0])));
if (unsetCategoryTags.length != 0) {
info.updateTagsState = {
@ -64,7 +64,7 @@ async function uploadPost(data: PostUploadCommandData) {
info.updateTagsState.current = parseInt(i);
pushInfo();
const wantedCategory = tagsWithCategory.find((x) => tags[i].names.includes(x.name))?.category;
const wantedCategory = tagsWithCategory.find((x) => tags[i].names.includes(x.names[0]))?.category;
if (wantedCategory) {
if (existingCategories.some((x) => x.name == wantedCategory)) {
tags[i].category = wantedCategory;

View File

@ -0,0 +1,28 @@
import { storage } from "webextension-polyfill";
import type { MaybeRef, RemovableRef, StorageLikeAsync, UseStorageAsyncOptions } from "@vueuse/core";
import { useStorageAsync } from "@vueuse/core";
const storageLocal: StorageLikeAsync = {
removeItem(key: string) {
return storage.local.remove(key);
},
setItem(key: string, value: string) {
console.log("setItem");
console.dir(value);
return storage.local.set({ [key]: value });
},
async getItem(key: string) {
console.log("getItem");
const item = (await storage.local.get(key))[key];
console.dir(item);
return item;
},
};
export const useStorageLocal = <T>(
key: string,
initialValue: MaybeRef<T>,
options?: UseStorageAsyncOptions<T>
): RemovableRef<T> => useStorageAsync(key, initialValue, storageLocal, options);

View File

@ -3,11 +3,9 @@ import { library } from "@fortawesome/fontawesome-svg-core";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import { faCog, faChevronDown, faChevronUp, faChevronLeft } from "@fortawesome/free-solid-svg-icons";
import { createPinia } from "pinia";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";
export function setupPinia(app: App) {
const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);
app.use(pinia);
}

View File

@ -2,24 +2,18 @@
import { useColorMode } from "@vueuse/core";
import SzuruWrapper from "~/api";
import { SzuruSiteConfig } from "~/config";
import { useConfigStore } from "~/stores";
import { cfg } from "~/stores";
import { getErrorMessage } from "~/utils";
let autoSearchSimilar = ref(false);
let loadTagCounts = ref(false);
let addPageUrlToSource = ref(false);
let statusText = ref("");
let statusText = ref("Version " + browser.runtime.getManifest().version);
let statusType = ref("status-quiet");
let sites = reactive<SzuruSiteConfig[]>([]);
let selectedSiteId = ref<string | undefined>(undefined);
const selectedSite = computed(() => {
if (selectedSiteId.value) {
return sites.find((x) => x.id == selectedSiteId.value);
if (cfg.value.selectedSiteId) {
return cfg.value.sites.find((x) => x.id == cfg.value.selectedSiteId);
}
});
const configStore = useConfigStore();
type StatusType = "success" | "error" | "quiet";
async function testConnection() {
@ -48,16 +42,6 @@ async function testConnection() {
}
}
async function saveSettings() {
configStore.sites = sites;
configStore.autoSearchSimilar = autoSearchSimilar.value;
configStore.loadTagCounts = loadTagCounts.value;
configStore.addPageUrlToSource = addPageUrlToSource.value;
configStore.selectedSiteId = selectedSiteId.value;
setStatus("Settings successfully saved");
}
function setStatus(text: string, type: StatusType = "success") {
statusText.value = text;
statusType.value = "status-" + type;
@ -65,31 +49,22 @@ function setStatus(text: string, type: StatusType = "success") {
function addSite() {
const site = new SzuruSiteConfig();
sites.push(site);
selectedSiteId.value = site.id;
cfg.value.sites.push(site);
cfg.value.selectedSiteId = site.id;
}
function removeSelectedSite() {
if (selectedSite.value) {
const idx = sites.indexOf(selectedSite.value);
sites.splice(idx, 1);
const idx = cfg.value.sites.indexOf(selectedSite.value);
cfg.value.sites.splice(idx, 1);
}
// Select first site in list, if it exists.
if (cfg.value.sites.length > 0) {
cfg.value.selectedSiteId = cfg.value.sites[0].id;
}
}
onMounted(async () => {
statusText.value = "Version " + browser.runtime.getManifest().version;
for (let site of configStore.sites) {
// This somehow makes `site` reactive.
sites.push(site);
}
autoSearchSimilar.value = configStore.autoSearchSimilar;
loadTagCounts.value = configStore.loadTagCounts;
addPageUrlToSource.value = configStore.addPageUrlToSource;
selectedSiteId.value = configStore.selectedSiteId;
});
let mode = useColorMode({ emitAuto: true });
</script>
@ -105,8 +80,8 @@ let mode = useColorMode({ emitAuto: true });
<!-- This isn't perfectly responsive for very small devices. -->
<div class="fit flex gap-1 flex-fit">
<select v-model="selectedSiteId">
<option v-for="site in sites" :key="site.id" :value="site.id">
<select v-model="cfg.selectedSiteId">
<option v-for="site in cfg.sites" :key="site.id" :value="site.id">
{{ site.username }} @ {{ site.domain }}
</option>
</select>
@ -154,7 +129,7 @@ let mode = useColorMode({ emitAuto: true });
<div class="settings-block">
<label>
<input type="checkbox" v-model="autoSearchSimilar" />
<input type="checkbox" v-model="cfg.autoSearchSimilar" />
Automatically search for similar posts
</label>
<p class="hint">
@ -165,7 +140,7 @@ let mode = useColorMode({ emitAuto: true });
<div class="settings-block">
<label>
<input type="checkbox" v-model="addPageUrlToSource" />
<input type="checkbox" v-model="cfg.addPageUrlToSource" />
Add page URL to the source list
</label>
<p class="hint">
@ -177,7 +152,7 @@ let mode = useColorMode({ emitAuto: true });
<div class="settings-block">
<label>
<input type="checkbox" v-model="loadTagCounts" />
<input type="checkbox" v-model="cfg.loadTagCounts" />
Show how often a tag is used in the selected szurubooru instance
</label>
<p class="hint">
@ -192,7 +167,7 @@ let mode = useColorMode({ emitAuto: true });
<div class="flex gap-1">
<button @click="testConnection">Test connection</button>
<button @click="saveSettings" class="primary">Save settings</button>
<!-- <button @click="saveSettings" class="primary">Save settings</button> -->
</div>
</div>
</div>

View File

@ -3,15 +3,14 @@ import { isEqual, cloneDeep } from "lodash";
import SzurubooruApi from "~/api";
import { Post, UpdatePostRequest } from "~/api/models";
import { BrowserCommand, PostUpdateCommandData, TagDetails } from "~/models";
import { useConfigStore, useMergeStore, usePopupStore } from "~/stores";
import { cfg, useMergeStore, usePopupStore } from "~/stores";
import { emptyPost, getUrl } from "~/utils";
const props = defineProps(["siteId", "postId"]);
const cfg = useConfigStore();
const merge = useMergeStore();
const pop = usePopupStore();
const szuruConfig = cfg.sites.find((x) => x.id == props.siteId)!;
const szuruConfig = cloneDeep(cfg.value.sites.find((x) => x.id == props.siteId))!;
const szuru = SzurubooruApi.createFromConfig(szuruConfig);
// Used for the update post request.
@ -54,7 +53,7 @@ function addTag(tag: TagDetails) {
tagsToAdd.push(tag);
// Add implications for the tag
if (cfg.addTagImplications) {
if (cfg.value.addTagImplications) {
tagsToAdd.push(...tag.implications);
}
}
@ -162,7 +161,7 @@ watch(
);
watch(
() => cfg.merge.addMissingTags,
() => cfg.value.merge.addMissingTags,
(value) => {
if (value) {
addMissingTags();
@ -173,7 +172,7 @@ watch(
);
watch(
() => cfg.merge.appendSource,
() => cfg.value.merge.appendSource,
(value) => {
if (value) {
appendSources();
@ -184,7 +183,7 @@ watch(
);
watch(
() => cfg.merge.mergeSafety,
() => cfg.value.merge.mergeSafety,
(value) => {
if (value) {
mergeSafety();
@ -198,9 +197,9 @@ onMounted(async () => {
existingPostRo = await szuru?.getPost(props.postId);
if (existingPostRo) {
post.value = cloneDeep(existingPostRo);
if (cfg.merge.appendSource) appendSources();
if (cfg.merge.addMissingTags) addMissingTags();
if (cfg.merge.mergeSafety) mergeSafety();
if (cfg.value.merge.appendSource) appendSources();
if (cfg.value.merge.addMissingTags) addMissingTags();
if (cfg.value.merge.mergeSafety) mergeSafety();
if (scrapedPost?.resolution) {
// Set imageToKeep depending on which image has the largest resolution.
@ -257,6 +256,29 @@ onMounted(async () => {
</div>
</div>
<PopupSection header="Changes">
<div class="section-row">
<ul class="compact-tags">
<li v-show="tagsToAdd.length != 0 || tagsRemoved != 0">
Tags: <span class="success" v-show="tagsToAdd.length != 0">+{{ tagsToAdd.length }}</span>
<span class="danger" v-show="tagsRemoved != 0">-{{ tagsRemoved }}</span>
</li>
<li v-show="sourcesAdded != 0 || sourcesRemoved != 0">
Sources: <span class="success" v-show="sourcesAdded != 0">+2</span>
<span class="danger" v-show="sourcesRemoved != 0">-1</span>
</li>
<li v-show="existingPostRo?.safety != post.safety">
Safety: <s class="danger">{{ existingPostRo?.safety }}</s> <span class="success">{{ post.safety }}</span>
</li>
<li v-show="imageToKeep == 'new'">
<span class="success">Updating post content</span>
</li>
<!-- TODO: Add "No changes" text. -->
</ul>
</div>
</PopupSection>
<PopupSection header="Basic info">
<div class="section-row">
<span class="section-label">Safety</span>
@ -349,27 +371,6 @@ onMounted(async () => {
</div>
</div>
</PopupSection>
<PopupSection header="Changes">
<div class="section-row">
<ul class="compact-tags">
<li v-show="tagsToAdd.length != 0 || tagsRemoved != 0">
Tags: <span class="success" v-show="tagsToAdd.length != 0">+{{ tagsToAdd.length }}</span>
<span class="danger" v-show="tagsRemoved != 0">-{{ tagsRemoved }}</span>
</li>
<li v-show="sourcesAdded != 0 || sourcesRemoved != 0">
Sources: <span class="success" v-show="sourcesAdded != 0">+2</span>
<span class="danger" v-show="sourcesRemoved != 0">-1</span>
</li>
<li v-show="existingPostRo?.safety != post.safety">
Safety: <s class="danger">{{ existingPostRo?.safety }}</s> <span class="success">{{ post.safety }}</span>
</li>
<li v-show="imageToKeep == 'new'">
<span class="success">Updating post content</span>
</li>
</ul>
</div>
</PopupSection>
</div>
</div>
</template>

View File

@ -1,5 +1,6 @@
<script setup lang="ts">
import { useDark } from "@vueuse/core";
import { cloneDeep } from "lodash";
import { ScrapeResults } from "neo-scraper";
import { getUrl, encodeTagName, getErrorMessage } from "~/utils";
import {
@ -14,18 +15,29 @@ import {
import { ImageSearchResult } from "~/api/models";
import { isMobile } from "~/env";
import { DeepReadonly } from "vue";
import { useConfigStore, usePopupStore } from "~/stores";
import { cfg, usePopupStore } from "~/stores";
import { SzuruSiteConfig } from "~/config";
import SzurubooruApi from "~/api";
const pop = usePopupStore();
const cfg = useConfigStore();
const isSearchingForSimilarPosts = ref<number>(0);
const selectedSite = computed(() => {
if (cfg.value.selectedSiteId) {
return cfg.value.sites.find((x) => x.id == cfg.value.selectedSiteId);
}
});
const szuru = computed(() => {
return selectedSite.value ? SzurubooruApi.createFromConfig(selectedSite.value) : undefined;
});
// (Reactive) shorthand for `main.selectedPost.instanceSpecificData.get(config.selectedSiteId)`.
// This should only be used as if it were readonly, because the instanceId might change.
const instanceSpecificData = readonly(
computed(() => {
if (pop.selectedPost && cfg.selectedSiteId) {
return pop.selectedPost.instanceSpecificData[cfg.selectedSiteId];
if (pop.selectedPost && cfg.value.selectedSiteId) {
return pop.selectedPost.instanceSpecificData[cfg.value.selectedSiteId];
}
})
);
@ -34,7 +46,7 @@ watch(
() => pop.selectedPostId,
(value) => {
// Call findSimilar if wanted.
if (cfg.autoSearchSimilar) {
if (cfg.value.autoSearchSimilar) {
let selectedPost = pop.posts.find((x) => x.id == value);
if (selectedPost) findSimilar(selectedPost);
}
@ -42,14 +54,14 @@ watch(
);
watch(
() => cfg.selectedSiteId,
() => cfg.value.selectedSiteId,
async (value, oldValue) => {
// If changed
if (value != oldValue) {
// Update selectedSiteId in config
cfg.selectedSiteId = value;
cfg.value.selectedSiteId = value;
// Call findSimilar if wanted.
if (cfg.autoSearchSimilar && pop.selectedPost) {
if (cfg.value.autoSearchSimilar && pop.selectedPost) {
findSimilar(pop.selectedPost);
}
}
@ -93,13 +105,13 @@ async function grabPost() {
// Add current pageUrl to the source if either
// a. user has addPageUrlToSource set to true
// b. source is empty
if (cfg?.addPageUrlToSource || vm.source == "") {
if (cfg.value.addPageUrlToSource || vm.source == "") {
if (vm.source != "") vm.source += "\n";
vm.source += vm.pageUrl;
}
// Initialize instanceSpecificData with an empty object.
for (const site of cfg.sites) {
for (const site of cfg.value.sites) {
vm.instanceSpecificData[site.id] = {};
}
@ -109,7 +121,7 @@ async function grabPost() {
if (pop.posts.length > 0) {
pop.selectedPostId = pop.posts[0].id;
if (cfg?.loadTagCounts) {
if (cfg.value.loadTagCounts) {
loadTagCounts();
}
} else {
@ -118,8 +130,8 @@ async function grabPost() {
}
async function upload() {
if (!cfg.selectedSiteId) return;
let instanceSpecificData = pop.selectedPost?.instanceSpecificData[cfg.selectedSiteId];
if (!cfg.value.selectedSiteId) return;
let instanceSpecificData = pop.selectedPost?.instanceSpecificData[cfg.value.selectedSiteId];
// Don't try to upload if an exact copy is already on the server.
if (instanceSpecificData?.reverseSearchResult?.exactPostId) return;
@ -128,11 +140,18 @@ async function upload() {
instanceSpecificData.uploadState = { state: "uploading" };
}
const post: any = { ...pop.selectedPost };
// @ts-expect-error ...
const cmdData = new PostUploadCommandData(post, cfg.selectedSite);
const cmd = new BrowserCommand("upload_post", cmdData);
await browser.runtime.sendMessage(cmd);
try {
const post: any = cloneDeep(pop.selectedPost);
const cmdData = new PostUploadCommandData(post, <SzuruSiteConfig>cloneDeep(selectedSite.value));
const cmd = new BrowserCommand("upload_post", cmdData);
await browser.runtime.sendMessage(cmd);
} catch (ex: any) {
if (instanceSpecificData?.uploadState) {
instanceSpecificData.uploadState.state = "error";
instanceSpecificData.uploadState.error = ex.toString();
}
throw ex;
}
}
function resolutionToString(resolution: [number, number]) {
@ -152,8 +171,8 @@ function removeTag(tag: TagDetails) {
}
function getActiveSitePostUrl(postId: number): string {
if (!cfg.selectedSite) return "";
return getUrl(cfg.selectedSite.domain, "post", postId.toString());
if (!selectedSite.value) return "";
return getUrl(selectedSite.value.domain, "post", postId.toString());
}
function getSimilarPosts(data?: DeepReadonly<SimpleImageSearchResult>): SimilarPostInfo[] {
@ -179,7 +198,7 @@ function addTag(tag: TagDetails) {
pop.selectedPost.tags.push(tag);
// Add implications for the tag
if (cfg.addTagImplications) {
if (cfg.value.addTagImplications) {
pop.selectedPost.tags.push(...tag.implications);
}
}
@ -191,10 +210,11 @@ async function clickFindSimilar() {
}
async function findSimilar(post: ScrapedPostDetails | undefined) {
if (!post || !cfg.szuru || !cfg.selectedSiteId) return;
if (!post || !szuru.value || !cfg.value.selectedSiteId) return;
const selectedInstance = cfg.szuru; // Get current object, and not reactive.
let instanceSpecificData = post.instanceSpecificData[cfg.selectedSiteId];
// TODO: This code might not work.
const selectedInstance = toRaw(szuru.value); // Get current object, and not reactive.
let instanceSpecificData = post.instanceSpecificData[cfg.value.selectedSiteId];
if (!instanceSpecificData) {
console.error("instanceSpecificData is undefined. This should never happen!");
@ -209,7 +229,7 @@ async function findSimilar(post: ScrapedPostDetails | undefined) {
try {
let res: ImageSearchResult | undefined;
if (cfg.useContentTokens) {
if (cfg.value.useContentTokens) {
let tmpRes = await selectedInstance.uploadTempFile(post.contentUrl);
// TODO: Error handling?
// Save contentToken in PostViewModel so that we can reuse it when creating/uploading the post.
@ -240,7 +260,7 @@ async function loadTagCounts() {
.map((x) => encodeTagName(x.name))
.join();
const resp = await cfg.szuru?.getTags(query);
const resp = await szuru.value?.getTags(query);
// Not the best code, but it works I guess?
if (resp) {
@ -283,10 +303,10 @@ useDark();
-->
<div v-if="cfg.sites.length == 0" class="bg-danger">
<span>No szurubooru server configured!</span>
<span>No szurubooru servers configured!</span>
</div>
<div v-if="!cfg.selectedSite" class="bg-danger">
<div v-if="!szuru && cfg.sites.length != 0" class="bg-danger">
<span>No target server selected!</span>
</div>
@ -453,7 +473,7 @@ useDark();
<PopupSection header="Tags">
<div class="section-row">
<TagInput :szuru="cfg.szuru" @add-tag="addTag" />
<TagInput :szuru="szuru" @add-tag="addTag" />
</div>
<div class="section-row">

View File

@ -1,45 +1,24 @@
import { defineStore } from "pinia";
import { SzuruSiteConfig } from "~/config";
import SzurubooruApi from "~/api";
import type { ScrapedPostDetails, SetPostUploadInfoData } from "~/models";
import { useStorageLocal } from "~/composables/useStorageLocal";
export const useConfigStore = defineStore("config", {
state: () => ({
addPageUrlToSource: true,
autoSearchSimilar: false,
loadTagCounts: true,
useContentTokens: true,
sites: [] as Array<SzuruSiteConfig>,
selectedSiteId: undefined as string | undefined,
addTagImplications: true,
merge: {
expandOptions: true,
expandExistingTags: false,
expandAddTags: true,
addMissingTags: true,
appendSource: true,
mergeSafety: true,
},
}),
getters: {
selectedSite: (state) => {
return state.sites.find((x) => x.id == state.selectedSiteId);
},
szuru(): SzurubooruApi | undefined {
if (this.selectedSite) {
return SzurubooruApi.createFromConfig(this.selectedSite);
}
},
export const cfg = useStorageLocal("config", {
addPageUrlToSource: true,
autoSearchSimilar: false,
loadTagCounts: true,
useContentTokens: true,
sites: [] as Array<SzuruSiteConfig>,
selectedSiteId: undefined as string | undefined,
addTagImplications: true,
merge: {
expandOptions: true,
expandExistingTags: false,
expandAddTags: true,
addMissingTags: true,
appendSource: true,
mergeSafety: true,
},
actions: {
createSzuruApiFromConfig(siteId: string) {
const cfg = this.sites.find((x) => x.id == siteId);
if (cfg) {
return SzurubooruApi.createFromConfig(cfg);
}
},
},
persist: true,
});
export const usePopupStore = defineStore("popup", {

View File

@ -71,10 +71,6 @@ export default defineConfig(({ command }) => ({
outDir: r("extension/dist"),
emptyOutDir: false,
sourcemap: isDev ? "inline" : false,
// https://developer.chrome.com/docs/webstore/program_policies/#:~:text=Code%20Readability%20Requirements
terserOptions: {
mangle: false,
},
rollupOptions: {
input: {
background: r("src/background/index.html"),