Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Analyzer/FavoritesAnalyzerResult.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import { cloneDeep, debounce } from "lodash";
import * as Comlink from "comlink";
import { useRoute } from "vue-router";
import ProgressMessage from "@/Suggester/ProgressMessage.vue";
import { useUrlStore } from "@/services";
import { useUrlStore, useAccountStore } from "@/services";
const account = useAccountStore();
import { useHead } from "@unhead/vue";
import TagLabel from "@/Tag/TagLabel.vue";

Expand All @@ -65,12 +66,18 @@ const args = computed<IAnalyzeTagsArgs>(() => {
const result = ref<IAnalyzeTagsResult>();

const analyze = debounce(async (a) => {
if (!account.auth) {
result.value = undefined;
progress.value = { message: "You must be logged in with an API key to analyze favorites.", progress: 1 };
return;
}
const service = await getAnalyzeService();
result.value = await service.analyzeTags(
a,
Comlink.proxy((progressEvent) => {
progress.value = progressEvent;
}),
account.auth,
);
}, 500);

Expand Down
15 changes: 8 additions & 7 deletions src/Suggester/SuggesterResult.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ const result = ref<FavoriteTagsResult | null>(null);

const analyze = async (username: string) => {
const service = await getAnalyzeService();
result.value = await service.getFavoriteTags(
username,
urlStore.e621Url,
Comlink.proxy((progressEvent) => {
progress.value = progressEvent;
}),
);
result.value = await service.getFavoriteTags(
username,
urlStore.e621Url,
Comlink.proxy((progressEvent) => {
progress.value = progressEvent;
}),
toRaw(account.auth),
);
await nextTick();
await loadNextPage();
};
Expand Down
139 changes: 53 additions & 86 deletions src/worker/AnalyzeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,52 +90,19 @@ export class AnalyzeService {

cache: { [key: string]: Post[] | undefined } = {};

private async fetchPostsCached(
tags: string[],
postLimit: number,
baseUrl: string,
onProgress: (event: IProgressEvent) => void,
) {
const service = new ApiService();
const posts: Post[] = [];
let page = 1;
const key = [tags, postLimit].join("");
log("start fetch");
if (key && this.cache[key]) {
posts.push(...this.cache[key]!);
} else {
while (posts.length < postLimit) {
const newPosts: Post[] = await service.getPosts({
blacklistMode: BlacklistMode.blur,
limit: 320,
tags,
baseUrl,
page,
});
page += 1;
posts.push(...newPosts);
onProgress({
message: `got ${posts.length} of ${postLimit} posts`,
progress: Math.min(1, posts.length / postLimit),
});
if (newPosts.length !== 320) {
break;
}
}
this.cache[key] = posts;
}
return posts;
}
// ...removed duplicate fetchPostsCached...

async analyzeTags(
args: IAnalyzeTagsArgs,
onProgress: (event: IProgressEvent) => void,
auth?: { login: string; api_key: string },
): Promise<IAnalyzeTagsResult> {
const posts = await this.fetchPostsCached(
args.tags,
args.postLimit,
args.baseUrl,
onProgress,
auth,
);
onProgress({
message: "got posts, sorting tags",
Expand Down Expand Up @@ -164,12 +131,14 @@ export class AnalyzeService {
username: string,
baseUrl: string,
onProgress: (event: IProgressEvent) => void,
auth?: { login: string; api_key: string },
): Promise<FavoriteTagsResult> {
const posts = await this.fetchPostsCached(
[`fav:${username}`],
320 * 6,
baseUrl,
onProgress,
auth,
);

const counts = getCounts(posts);
Expand All @@ -190,64 +159,62 @@ export class AnalyzeService {
| "lore"
| "invalid"]: number;
},

limit: number,
args: {
direction: "next" | "previous",
page: number,
},
auth:
| {
login: string;
api_key: string;
}
| undefined,
): Promise<Post[]> {
// This implementation fetches posts and scores them for suggestions.
const posts = await this.fetchPostsCached(
[""],
limit,
"https://e621.net/", // or use a configurable baseUrl if needed
() => {},
);
// If posts is not an array, return an empty array to avoid errors
if (!Array.isArray(posts)) return [];
// Score posts (dummy scoring for now)
return posts;
}

private async fetchPostsCached(
tags: string[],
postLimit: number,
baseUrl: string,
onProgress: (event: IProgressEvent) => void,
blacklist: string[][],
blacklistMode: BlacklistMode,
) {
// fetch posts, sort them by score and display the top `limit` ones
const toFetch = limit * 40;
auth?: { login: string; api_key: string },
): Promise<Post[]> {
const service = new ApiService();
const posts: ScoredPost[] = [];
let page = args.page;
while (posts.length < toFetch && page >= 1) {
onProgress({
progress: Math.min(1, posts.length / toFetch),
message: `got ${posts.length} of ${toFetch} posts`,
});
const newPosts = await service.getPosts({
blacklistMode,
blacklist,
limit: 320,
tags: [],
page,
auth,
baseUrl,
});

const scoredNewPosts = scorePosts(tags, weights, newPosts);
if (args.direction === "previous") {
page -= 1;
posts.unshift(...scoredNewPosts);
} else {
const posts: Post[] = [];
let page = 1;
const key = [tags, postLimit].join("");
log("start fetch");
if (key && this.cache[key]) {
posts.push(...this.cache[key]!);
} else {
while (posts.length < postLimit) {
const newPosts: Post[] = await service.getPosts({
blacklistMode: BlacklistMode.blur,
limit: 320,
tags,
baseUrl,
page,
auth,
});
page += 1;
posts.push(...scoredNewPosts);
}

if (scoredNewPosts.length < 320) {
break;
posts.push(...newPosts);
onProgress({
message: `got ${posts.length} of ${postLimit} posts`,
progress: Math.min(1, posts.length / postLimit),
});
if (newPosts.length !== 320) {
break;
}
}
this.cache[key] = posts;
}
const bestPostIds = [...posts]
.sort((a, b) => b.__score - a.__score)
.slice(0, limit)
.map((p) => p.id);
const result = posts.filter((p) => bestPostIds.includes(p.id)); // keep original order
onProgress({ indeterminate: true, message: "done", progress: 1 });

return result;
return posts;
}

// ...removed stray code after fetchPostsCached...
}

const scorePosts = (
Expand Down