Don't share errors, uploadState and reverseSearchResult between szurubooru instances

next
neobooru 2023-02-19 16:23:21 +01:00
parent 7aa65edd91
commit 8a849a3ca4
3 changed files with 124 additions and 62 deletions

View File

@ -35,7 +35,7 @@ async function uploadPost(data: PostUploadCommandData) {
// Create and upload post // Create and upload post
browser.runtime.sendMessage( 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); const createdPost = await szuru.createPost(data.post);
@ -44,7 +44,7 @@ async function uploadPost(data: PostUploadCommandData) {
info.instancePostId = createdPost.id; info.instancePostId = createdPost.id;
browser.runtime.sendMessage( 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 // Find tags with "default" category and update it
@ -59,7 +59,7 @@ async function uploadPost(data: PostUploadCommandData) {
total: unsetCategoryTags.length, total: unsetCategoryTags.length,
}; };
browser.runtime.sendMessage( 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 // 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) { for (const i in tags) {
info.updateTagsState.current = parseInt(i); info.updateTagsState.current = parseInt(i);
browser.runtime.sendMessage( 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; const wantedCategory = tagsWithCategory.find((x) => tags[i].names.includes(x.name))?.category;
@ -91,7 +91,7 @@ async function uploadPost(data: PostUploadCommandData) {
if (categoriesChangedCount > 0) { if (categoriesChangedCount > 0) {
info.updateTagsState.totalChanged = categoriesChangedCount; info.updateTagsState.totalChanged = categoriesChangedCount;
browser.runtime.sendMessage( 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") { if (ex.name && ex.name == "PostAlreadyUploadedError") {
const otherPostId = (ex as PostAlreadyUploadedError).otherPostId; const otherPostId = (ex as PostAlreadyUploadedError).otherPostId;
browser.runtime.sendMessage( 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. // No error message, because we have a different message for posts that are already uploaded.
info.state = "error"; info.state = "error";
browser.runtime.sendMessage( 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 { } else {
info.state = "error"; info.state = "error";
info.error = getErrorMessage(ex); info.error = getErrorMessage(ex);
browser.runtime.sendMessage( 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))
); );
} }
} }

View File

@ -25,6 +25,12 @@ export class TagDetails {
} }
} }
export class InstanceSpecificData {
genericError?: string;
reverseSearchResult?: SimpleImageSearchResult;
uploadState?: PostUploadInfo;
}
export class ScrapedPostDetails { export class ScrapedPostDetails {
id = window.crypto.randomUUID(); id = window.crypto.randomUUID();
name = ""; name = "";
@ -38,8 +44,7 @@ export class ScrapedPostDetails {
source = ""; source = "";
referrer?: string; referrer?: string;
resolution?: [number, number]; resolution?: [number, number];
reverseSearchResult?: SimpleImageSearchResult; instanceSpecificData = new Map<string, InstanceSpecificData>();
uploadState?: PostUploadInfo;
constructor(post: ScrapedPost) { constructor(post: ScrapedPost) {
this.contentUrl = post.contentUrl; this.contentUrl = post.contentUrl;
@ -98,9 +103,17 @@ export class PostUploadCommandData {
} }
export class SetPostUploadInfoData { 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 { 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
) {}
} }

View File

@ -19,6 +19,7 @@ import { ImageSearchResult } from "~/api/models";
import { isChrome, isMobile } from "~/env"; import { isChrome, isMobile } from "~/env";
import { Runtime, WebRequest } from "webextension-polyfill"; import { Runtime, WebRequest } from "webextension-polyfill";
import { Config } from "~/config"; import { Config } from "~/config";
import { DeepReadonly } from "vue";
let config = new Config(); let config = new Config();
let posts = reactive([] as ScrapedPostDetails[]); let posts = reactive([] as ScrapedPostDetails[]);
@ -29,10 +30,9 @@ let autocompleteTags = ref<TagDetails[]>([]);
let cancelSource = ref<CancelTokenSource | undefined>(undefined); let cancelSource = ref<CancelTokenSource | undefined>(undefined);
let autocompleteIndex = ref(-1); let autocompleteIndex = ref(-1);
let selectedSiteId = ref<string | undefined>(undefined); let selectedSiteId = ref<string | undefined>(undefined);
let isSearchingForSimilarPosts = ref<boolean>(false); let isSearchingForSimilarPosts = ref<number>(0);
let genericError = ref<string | undefined>(undefined);
const activeSite = computed(() => { const selectedInstance = computed(() => {
if (selectedSiteId.value) { if (selectedSiteId.value) {
return config.sites.find((x) => x.id == selectedSiteId.value); return config.sites.find((x) => x.id == selectedSiteId.value);
} }
@ -45,18 +45,38 @@ const selectedPost = computed(() => {
}); });
const szuru = 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, () => { watch(selectedPostId, () => {
// Call findSimilar if wanted.
if (config.autoSearchSimilar) {
let selectedPost = posts.find((x) => x.id == selectedPostId.value); let selectedPost = posts.find((x) => x.id == selectedPostId.value);
if (selectedPost) findSimilar(selectedPost); if (selectedPost) findSimilar(selectedPost);
}
}); });
watch(selectedSiteId, async (x) => { watch(selectedSiteId, async (x) => {
// If changed
if (config.selectedSiteId != x) { if (config.selectedSiteId != x) {
// Update selectedSiteId in config
config.selectedSiteId = x; config.selectedSiteId = x;
await config.save(); 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; vm.source += vm.pageUrl;
} }
// Initialize instanceSpecificData with an empty object.
for (const site of config.sites) {
vm.instanceSpecificData.set(site.id, {});
}
posts.push(vm); posts.push(vm);
} }
} }
@ -120,7 +145,7 @@ async function upload() {
if (!selectedSiteId.value) return; if (!selectedSiteId.value) return;
// Don't try to upload if an exact copy is already on the server. // 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. // TODO: Terrible code.
const post = JSON.parse(JSON.stringify(selectedPost.value)); const post = JSON.parse(JSON.stringify(selectedPost.value));
@ -164,11 +189,11 @@ function removeTag(tag: TagDetails) {
} }
function getActiveSitePostUrl(postId: number): string { function getActiveSitePostUrl(postId: number): string {
if (!activeSite.value) return ""; if (!selectedInstance.value) return "";
return getUrl(activeSite.value.domain, "post", postId.toString()); return getUrl(selectedInstance.value.domain, "post", postId.toString());
} }
function getSimilarPosts(data?: SimpleImageSearchResult): SimilarPostInfo[] { function getSimilarPosts(data?: DeepReadonly<SimpleImageSearchResult>): SimilarPostInfo[] {
if (!data) return []; if (!data) return [];
const lst: SimilarPostInfo[] = []; const lst: SimilarPostInfo[] = [];
@ -202,36 +227,44 @@ async function clickFindSimilar() {
} }
async function findSimilar(post: ScrapedPostDetails | undefined) { 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. // Don't load this again if it is already loaded.
if (post.reverseSearchResult) return; if (instanceSpecificData?.reverseSearchResult) return;
isSearchingForSimilarPosts.value = true; isSearchingForSimilarPosts.value++;
try { try {
let res: ImageSearchResult | undefined; let res: ImageSearchResult | undefined;
if (config.useContentTokens) { if (config.useContentTokens) {
let tmpRes = await szuru.value.uploadTempFile(post.contentUrl); let tmpRes = await selectedInstance.uploadTempFile(post.contentUrl);
// TODO: Error handling? // TODO: Error handling?
// Save contentToken in PostViewModel so that we can reuse it when creating/uploading the post. // Save contentToken in PostViewModel so that we can reuse it when creating/uploading the post.
post.contentToken = tmpRes.token; post.contentToken = tmpRes.token;
res = await szuru.value.reverseSearchToken(tmpRes.token); res = await selectedInstance.reverseSearchToken(tmpRes.token);
} else { } else {
res = await szuru.value.reverseSearch(post.contentUrl); res = await selectedInstance.reverseSearch(post.contentUrl);
} }
post.reverseSearchResult = { instanceSpecificData.reverseSearchResult = {
exactPostId: res.exactPost?.id, exactPostId: res.exactPost?.id,
similarPosts: res.similarPosts.map((x) => <SimpleSimilarPost>{ postId: x.post.id, distance: x.distance }), similarPosts: res.similarPosts.map((x) => <SimpleSimilarPost>{ postId: x.post.id, distance: x.distance }),
}; };
} catch (ex: any) { } 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 { function getPostForUrl(contentUrl: string): ScrapedPostDetails | undefined {
@ -362,20 +395,29 @@ onMounted(async () => {
switch (cmd.name) { switch (cmd.name) {
case "set_post_upload_info": case "set_post_upload_info":
{ {
const data = <SetPostUploadInfoData>cmd.data; const { postId, instanceId, info } = <SetPostUploadInfoData>cmd.data;
let post = posts.find((x) => x.id == data.postId); let post = posts.find((x) => x.id == postId);
if (post) post.uploadState = data.info; if (post) {
let instanceSpecificData = post.instanceSpecificData.get(instanceId);
if (instanceSpecificData) {
instanceSpecificData.uploadState = info;
}
}
} }
break; break;
case "set_exact_post_id": case "set_exact_post_id":
{ {
const { postId, exactPostId } = <SetExactPostId>cmd.data; const { postId, instanceId, exactPostId } = <SetExactPostId>cmd.data;
let post = posts.find((x) => x.id == postId); let post = posts.find((x) => x.id == postId);
if (post) { if (post && selectedPostId.value) {
if (!post.reverseSearchResult) { let instanceSpecificData = post.instanceSpecificData.get(instanceId);
post.reverseSearchResult = { exactPostId, similarPosts: [] }; if (instanceSpecificData) {
if (instanceSpecificData?.reverseSearchResult) {
instanceSpecificData.reverseSearchResult.exactPostId = exactPostId;
} else {
instanceSpecificData.reverseSearchResult = { exactPostId, similarPosts: [] };
}
} }
post.reverseSearchResult.exactPostId = exactPostId;
} }
} }
break; break;
@ -428,20 +470,20 @@ useDark();
<li v-if="config.sites.length == 0" class="bg-danger">No szurubooru server configured!</li> <li v-if="config.sites.length == 0" class="bg-danger">No szurubooru server configured!</li>
<li v-if="!activeSite" class="bg-danger">No target server selected!</li> <li v-if="!selectedInstance" class="bg-danger">No target server selected!</li>
<li v-if="selectedPost?.reverseSearchResult?.exactPostId" class="bg-danger"> <li v-if="instanceSpecificData?.reverseSearchResult?.exactPostId" class="bg-danger">
<a :href="getActiveSitePostUrl(selectedPost.reverseSearchResult?.exactPostId)" <a :href="getActiveSitePostUrl(instanceSpecificData.reverseSearchResult?.exactPostId)"
>Post already uploaded ({{ selectedPost.reverseSearchResult.exactPostId }})</a >Post already uploaded ({{ instanceSpecificData.reverseSearchResult.exactPostId }})</a
> >
</li> </li>
<!-- Only show error when it's not undefined and not empty. --> <!-- Only show error when it's not undefined and not empty. -->
<li v-if="selectedPost?.uploadState?.error?.length" class="bg-danger"> <li v-if="instanceSpecificData?.uploadState?.error?.length" class="bg-danger">
{{ selectedPost.uploadState.error }} Couldn't upload post. {{ instanceSpecificData.uploadState.error }}
</li> </li>
<li v-if="genericError" class="bg-danger">{{ genericError }}</li> <li v-if="instanceSpecificData?.genericError" class="bg-danger">{{ instanceSpecificData.genericError }}</li>
<!-- <!--
Success messages Success messages
@ -449,53 +491,60 @@ useDark();
<!-- TODO: Clicking a link doesn't actually open it in a new tab, see https://stackoverflow.com/questions/8915845 --> <!-- TODO: Clicking a link doesn't actually open it in a new tab, see https://stackoverflow.com/questions/8915845 -->
<li <li
v-if="selectedPost?.uploadState?.state == 'uploaded' && selectedPost?.uploadState.instancePostId" v-if="
instanceSpecificData?.uploadState?.state == 'uploaded' && instanceSpecificData?.uploadState.instancePostId
"
class="bg-success" class="bg-success"
> >
<a :href="getActiveSitePostUrl(selectedPost.uploadState.instancePostId)" <a :href="getActiveSitePostUrl(instanceSpecificData.uploadState.instancePostId)"
>Uploaded post {{ selectedPost.uploadState.instancePostId }}</a >Uploaded post {{ instanceSpecificData.uploadState.instancePostId }}</a
> >
</li> </li>
<li v-if="selectedPost?.uploadState?.updateTagsState?.totalChanged" class="bg-success"> <li v-if="instanceSpecificData?.uploadState?.updateTagsState?.totalChanged" class="bg-success">
{{ getUpdatedTagsText(selectedPost.uploadState?.updateTagsState?.totalChanged) }} {{ getUpdatedTagsText(instanceSpecificData.uploadState?.updateTagsState?.totalChanged) }}
</li> </li>
<!-- <!--
Info messages Info messages
--> -->
<li v-if="selectedPost?.uploadState?.state == 'uploading'">Uploading...</li> <li v-if="instanceSpecificData?.uploadState?.state == 'uploading'">Uploading...</li>
<li <li
v-if=" v-if="
selectedPost?.uploadState?.updateTagsState?.total && instanceSpecificData?.uploadState?.updateTagsState?.total &&
selectedPost.uploadState?.updateTagsState?.current == undefined instanceSpecificData.uploadState?.updateTagsState?.current == undefined
" "
> >
{{ selectedPost.uploadState?.updateTagsState?.total }} tags need a different category {{ instanceSpecificData.uploadState?.updateTagsState?.total }} tags need a different category
</li> </li>
<li <li
v-if=" v-if="
selectedPost?.uploadState?.updateTagsState?.current && instanceSpecificData?.uploadState?.updateTagsState?.current &&
selectedPost?.uploadState?.updateTagsState?.totalChanged == undefined instanceSpecificData?.uploadState?.updateTagsState?.totalChanged == undefined
" "
> >
Updating tag {{ selectedPost.uploadState?.updateTagsState?.current }}/{{ Updating tag {{ instanceSpecificData.uploadState?.updateTagsState?.current }}/{{
selectedPost.uploadState?.updateTagsState?.total instanceSpecificData.uploadState?.updateTagsState?.total
}} }}
</li> </li>
<li v-if="isSearchingForSimilarPosts && selectedPost?.uploadState == undefined"> <li v-if="isSearchingForSimilarPosts > 0 && instanceSpecificData?.uploadState == undefined">
Searching for similar posts... Searching for similar posts...
</li> </li>
<li v-if="selectedPost?.reverseSearchResult?.similarPosts.length == 0 && selectedPost.uploadState == undefined"> <li
v-if="
instanceSpecificData?.reverseSearchResult?.similarPosts.length == 0 &&
instanceSpecificData.uploadState == undefined
"
>
No similar posts found No similar posts found
</li> </li>
<li v-for="similarPost in getSimilarPosts(selectedPost?.reverseSearchResult)" :key="similarPost.id"> <li v-for="similarPost in getSimilarPosts(instanceSpecificData?.reverseSearchResult)" :key="similarPost.id">
<a :href="getActiveSitePostUrl(similarPost.id)" <a :href="getActiveSitePostUrl(similarPost.id)"
>Post {{ similarPost.id }} looks {{ similarPost.percentage }}% similar</a >Post {{ similarPost.id }} looks {{ similarPost.percentage }}% similar</a
> >