diff --git a/src/background/main.ts b/src/background/main.ts index 852f3a5..fdea49f 100644 --- a/src/background/main.ts +++ b/src/background/main.ts @@ -35,7 +35,7 @@ async function uploadPost(data: PostUploadCommandData) { // Create and upload post browser.runtime.sendMessage( - new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.post.id, info)) + new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.siteId, data.post.id, info)) ); const createdPost = await szuru.createPost(data.post); @@ -44,7 +44,7 @@ async function uploadPost(data: PostUploadCommandData) { info.instancePostId = createdPost.id; browser.runtime.sendMessage( - new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.post.id, info)) + new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.siteId, data.post.id, info)) ); // Find tags with "default" category and update it @@ -59,7 +59,7 @@ async function uploadPost(data: PostUploadCommandData) { total: unsetCategoryTags.length, }; browser.runtime.sendMessage( - new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.post.id, info)) + new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.siteId, data.post.id, info)) ); // unsetCategoryTags is of type MicroTag[] and we need a Tag resource to update it, so let's get those @@ -71,7 +71,7 @@ async function uploadPost(data: PostUploadCommandData) { for (const i in tags) { info.updateTagsState.current = parseInt(i); browser.runtime.sendMessage( - new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.post.id, info)) + new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.siteId, data.post.id, info)) ); const wantedCategory = tagsWithCategory.find((x) => tags[i].names.includes(x.name))?.category; @@ -91,7 +91,7 @@ async function uploadPost(data: PostUploadCommandData) { if (categoriesChangedCount > 0) { info.updateTagsState.totalChanged = categoriesChangedCount; browser.runtime.sendMessage( - new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.post.id, info)) + new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.siteId, data.post.id, info)) ); } } @@ -99,19 +99,19 @@ async function uploadPost(data: PostUploadCommandData) { if (ex.name && ex.name == "PostAlreadyUploadedError") { const otherPostId = (ex as PostAlreadyUploadedError).otherPostId; browser.runtime.sendMessage( - new BrowserCommand("set_exact_post_id", new SetExactPostId(data.post.id, otherPostId)) + new BrowserCommand("set_exact_post_id", new SetExactPostId(data.siteId, data.post.id, otherPostId)) ); // No error message, because we have a different message for posts that are already uploaded. info.state = "error"; browser.runtime.sendMessage( - new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.post.id, info)) + new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.siteId, data.post.id, info)) ); } else { info.state = "error"; info.error = getErrorMessage(ex); browser.runtime.sendMessage( - new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.post.id, info)) + new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.siteId, data.post.id, info)) ); } } diff --git a/src/models/index.ts b/src/models/index.ts index 016d6d1..d7ae0c7 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -25,6 +25,12 @@ export class TagDetails { } } +export class InstanceSpecificData { + genericError?: string; + reverseSearchResult?: SimpleImageSearchResult; + uploadState?: PostUploadInfo; +} + export class ScrapedPostDetails { id = window.crypto.randomUUID(); name = ""; @@ -38,8 +44,7 @@ export class ScrapedPostDetails { source = ""; referrer?: string; resolution?: [number, number]; - reverseSearchResult?: SimpleImageSearchResult; - uploadState?: PostUploadInfo; + instanceSpecificData = new Map(); constructor(post: ScrapedPost) { this.contentUrl = post.contentUrl; @@ -98,9 +103,17 @@ export class PostUploadCommandData { } export class SetPostUploadInfoData { - constructor(public readonly postId: string, public readonly info: PostUploadInfo) {} + constructor( + public readonly instanceId: string, + public readonly postId: string, + public readonly info: PostUploadInfo + ) {} } export class SetExactPostId { - constructor(public readonly postId: string, public readonly exactPostId: number) {} + constructor( + public readonly instanceId: string, + public readonly postId: string, + public readonly exactPostId: number + ) {} } diff --git a/src/popup/App.vue b/src/popup/App.vue index f8a40d8..18f087b 100644 --- a/src/popup/App.vue +++ b/src/popup/App.vue @@ -19,6 +19,7 @@ import { ImageSearchResult } from "~/api/models"; import { isChrome, isMobile } from "~/env"; import { Runtime, WebRequest } from "webextension-polyfill"; import { Config } from "~/config"; +import { DeepReadonly } from "vue"; let config = new Config(); let posts = reactive([] as ScrapedPostDetails[]); @@ -29,10 +30,9 @@ let autocompleteTags = ref([]); let cancelSource = ref(undefined); let autocompleteIndex = ref(-1); let selectedSiteId = ref(undefined); -let isSearchingForSimilarPosts = ref(false); -let genericError = ref(undefined); +let isSearchingForSimilarPosts = ref(0); -const activeSite = computed(() => { +const selectedInstance = computed(() => { if (selectedSiteId.value) { return config.sites.find((x) => x.id == selectedSiteId.value); } @@ -45,18 +45,38 @@ const selectedPost = computed(() => { }); const szuru = computed(() => { - return activeSite.value ? SzuruWrapper.createFromConfig(activeSite.value) : undefined; + return selectedInstance.value ? SzuruWrapper.createFromConfig(selectedInstance.value) : undefined; }); +// (Reactive) shorthand for `selectedPost.value.instanceSpecificData.get(selectedSiteId.value)`. +// This should only be used as if it were readonly, because the instanceId might change. +const instanceSpecificData = readonly( + computed(() => { + if (selectedPost.value && selectedSiteId.value) { + return selectedPost.value.instanceSpecificData.get(selectedSiteId.value); + } + }) +); + watch(selectedPostId, () => { - let selectedPost = posts.find((x) => x.id == selectedPostId.value); - if (selectedPost) findSimilar(selectedPost); + // Call findSimilar if wanted. + if (config.autoSearchSimilar) { + let selectedPost = posts.find((x) => x.id == selectedPostId.value); + if (selectedPost) findSimilar(selectedPost); + } }); watch(selectedSiteId, async (x) => { + // If changed if (config.selectedSiteId != x) { + // Update selectedSiteId in config config.selectedSiteId = x; await config.save(); + + // Call findSimilar if wanted. + if (config.autoSearchSimilar && selectedPost.value) { + findSimilar(selectedPost.value); + } } }); @@ -102,6 +122,11 @@ async function grabPost() { vm.source += vm.pageUrl; } + // Initialize instanceSpecificData with an empty object. + for (const site of config.sites) { + vm.instanceSpecificData.set(site.id, {}); + } + posts.push(vm); } } @@ -120,7 +145,7 @@ async function upload() { if (!selectedSiteId.value) return; // Don't try to upload if an exact copy is already on the server. - if (selectedPost.value?.reverseSearchResult?.exactPostId) return; + if (instanceSpecificData.value?.reverseSearchResult?.exactPostId) return; // TODO: Terrible code. const post = JSON.parse(JSON.stringify(selectedPost.value)); @@ -164,11 +189,11 @@ function removeTag(tag: TagDetails) { } function getActiveSitePostUrl(postId: number): string { - if (!activeSite.value) return ""; - return getUrl(activeSite.value.domain, "post", postId.toString()); + if (!selectedInstance.value) return ""; + return getUrl(selectedInstance.value.domain, "post", postId.toString()); } -function getSimilarPosts(data?: SimpleImageSearchResult): SimilarPostInfo[] { +function getSimilarPosts(data?: DeepReadonly): SimilarPostInfo[] { if (!data) return []; const lst: SimilarPostInfo[] = []; @@ -202,36 +227,44 @@ async function clickFindSimilar() { } async function findSimilar(post: ScrapedPostDetails | undefined) { - if (!post || !szuru.value) return; + if (!post || !szuru.value || !selectedSiteId.value) return; + + const selectedInstance = szuru.value; + let instanceSpecificData = post.instanceSpecificData.get(selectedSiteId.value); + + if (!instanceSpecificData) { + console.error("instanceSpecificData is undefined. This should never happen!"); + return; + } // Don't load this again if it is already loaded. - if (post.reverseSearchResult) return; + if (instanceSpecificData?.reverseSearchResult) return; - isSearchingForSimilarPosts.value = true; + isSearchingForSimilarPosts.value++; try { let res: ImageSearchResult | undefined; if (config.useContentTokens) { - let tmpRes = await szuru.value.uploadTempFile(post.contentUrl); + 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. post.contentToken = tmpRes.token; - res = await szuru.value.reverseSearchToken(tmpRes.token); + res = await selectedInstance.reverseSearchToken(tmpRes.token); } else { - res = await szuru.value.reverseSearch(post.contentUrl); + res = await selectedInstance.reverseSearch(post.contentUrl); } - post.reverseSearchResult = { + instanceSpecificData.reverseSearchResult = { exactPostId: res.exactPost?.id, similarPosts: res.similarPosts.map((x) => { postId: x.post.id, distance: x.distance }), }; } catch (ex: any) { - genericError.value = "Couldn't reverse search. " + getErrorMessage(ex); + instanceSpecificData.genericError = "Couldn't reverse search. " + getErrorMessage(ex); } - isSearchingForSimilarPosts.value = false; + isSearchingForSimilarPosts.value--; } function getPostForUrl(contentUrl: string): ScrapedPostDetails | undefined { @@ -362,20 +395,29 @@ onMounted(async () => { switch (cmd.name) { case "set_post_upload_info": { - const data = cmd.data; - let post = posts.find((x) => x.id == data.postId); - if (post) post.uploadState = data.info; + const { postId, instanceId, info } = cmd.data; + let post = posts.find((x) => x.id == postId); + if (post) { + let instanceSpecificData = post.instanceSpecificData.get(instanceId); + if (instanceSpecificData) { + instanceSpecificData.uploadState = info; + } + } } break; case "set_exact_post_id": { - const { postId, exactPostId } = cmd.data; + const { postId, instanceId, exactPostId } = cmd.data; let post = posts.find((x) => x.id == postId); - if (post) { - if (!post.reverseSearchResult) { - post.reverseSearchResult = { exactPostId, similarPosts: [] }; + if (post && selectedPostId.value) { + let instanceSpecificData = post.instanceSpecificData.get(instanceId); + if (instanceSpecificData) { + if (instanceSpecificData?.reverseSearchResult) { + instanceSpecificData.reverseSearchResult.exactPostId = exactPostId; + } else { + instanceSpecificData.reverseSearchResult = { exactPostId, similarPosts: [] }; + } } - post.reverseSearchResult.exactPostId = exactPostId; } } break; @@ -428,20 +470,20 @@ useDark();
  • No szurubooru server configured!
  • -
  • No target server selected!
  • +
  • No target server selected!
  • -
  • - Post already uploaded ({{ selectedPost.reverseSearchResult.exactPostId }}) + Post already uploaded ({{ instanceSpecificData.reverseSearchResult.exactPostId }})
  • -
  • - {{ selectedPost.uploadState.error }} +
  • + Couldn't upload post. {{ instanceSpecificData.uploadState.error }}
  • -
  • {{ genericError }}
  • +
  • {{ instanceSpecificData.genericError }}
  • - Uploaded post {{ selectedPost.uploadState.instancePostId }}Uploaded post {{ instanceSpecificData.uploadState.instancePostId }}
  • -
  • - {{ getUpdatedTagsText(selectedPost.uploadState?.updateTagsState?.totalChanged) }} +
  • + {{ getUpdatedTagsText(instanceSpecificData.uploadState?.updateTagsState?.totalChanged) }}
  • -
  • Uploading...
  • +
  • Uploading...
  • - {{ selectedPost.uploadState?.updateTagsState?.total }} tags need a different category + {{ instanceSpecificData.uploadState?.updateTagsState?.total }} tags need a different category
  • - Updating tag {{ selectedPost.uploadState?.updateTagsState?.current }}/{{ - selectedPost.uploadState?.updateTagsState?.total + Updating tag {{ instanceSpecificData.uploadState?.updateTagsState?.current }}/{{ + instanceSpecificData.uploadState?.updateTagsState?.total }}
  • -
  • +
  • Searching for similar posts...
  • -
  • +
  • No similar posts found
  • -
  • +
  • Post {{ similarPost.id }} looks {{ similarPost.percentage }}% similar