diff options
| author | Hunternif <hunternif@gmail.com> | 2021-09-01 23:46:13 +0100 |
|---|---|---|
| committer | Hunternif <hunternif@gmail.com> | 2021-09-01 23:51:16 +0100 |
| commit | 125c43177506dae11677ac3a9f2f5703881d1d1e (patch) | |
| tree | 13235f4110c9afaa9bc1676392e7b9d80cf187ca /server/szurubooru | |
| parent | 91f707a83b96c3ed834fe28ebeb91801345ff15a (diff) | |
server, client: honor safety when fetching similar posts
Diffstat (limited to 'server/szurubooru')
| -rw-r--r-- | server/szurubooru/api/post_api.py | 4 | ||||
| -rw-r--r-- | server/szurubooru/func/similar.py | 6 |
2 files changed, 7 insertions, 3 deletions
diff --git a/server/szurubooru/api/post_api.py b/server/szurubooru/api/post_api.py index edb78fb..ad779ae 100644 --- a/server/szurubooru/api/post_api.py +++ b/server/szurubooru/api/post_api.py @@ -346,11 +346,13 @@ def get_posts_similar( ) -> rest.Response: auth.verify_privilege(ctx.user, "posts:view:similar") _search_executor_config.user = ctx.user + query_text = ctx.get_param_as_string("query", default="") post_id = _get_post_id(params) post = posts.get_post_by_id(post_id) limit = ctx.get_param_as_int("limit", default=10, min=1, max=100) - results = similar.find_similar_posts(post, limit) + results = similar.find_similar_posts(post, limit, query_text) return { + "query": query_text, "limit": limit, "results": list([ posts.serialize_micro_post(result, ctx.user) for result in results diff --git a/server/szurubooru/func/similar.py b/server/szurubooru/func/similar.py index 37f3f0d..2e100ab 100644 --- a/server/szurubooru/func/similar.py +++ b/server/szurubooru/func/similar.py @@ -8,7 +8,9 @@ _search_executor_config = search.configs.PostSearchConfig() _search_executor = search.Executor(_search_executor_config) -def find_similar_posts(source_post: model.Post, limit: int) -> List[model.Post]: +def find_similar_posts( + source_post: model.Post, limit: int, query_text: str = '' +) -> List[model.Post]: results = [] # Sort tags in order of increasing post count, i.e. least to most popular # This will help yield results quicker @@ -19,7 +21,7 @@ def find_similar_posts(source_post: model.Post, limit: int) -> List[model.Post]: tags = source_tags for x in range(max_removals + 1): # prepare the current search, remove known results - query = ' '.join([t.first_name for t in tags]) + query = query_text + ' ' + ' '.join([t.first_name for t in tags]) query += ' -id:%d' % source_post.post_id for r in results: query += ' -id:%d' % r.post_id |