server: poolpost nearby implementation

This commit is contained in:
noirscape
2023-01-04 17:16:40 +01:00
committed by Eva
parent 376f687c38
commit c0504692e1
3 changed files with 97 additions and 3 deletions

View File

@ -975,6 +975,43 @@ data.
Retrieves information about posts that are before or after an existing post.
## Getting pools around post
- **Request**
`GET /post/<id>/pools-nearby`
- **Output**
```json5
[
{
"pool": <pool>,
"firstPost": <first-post>,
"lastPost": <last-post>,
"nextPost": <next-post>,
"previousPost": <previous-post>
},
...
]
```
- **Field meaning**
- `<pool>`: The associated [micro pool resource](#micro-pool).
- `firstPost`: A [micro post resource](#micro-post) that displays the first post in the pool.
- `lastPost`: A [micro post resource](#micro-post) that displays the last post in the pool.
- `nextPost`: A [micro post resource](#micro-post) that displays the next post in the pool.
- `prevPost`: A [micro post resource](#micro-post) that displays the previous post in the pool.
- **Errors**
- the post does not exist
- privileges are too low
- **Description**
Retrieves extra information about any pools that the post is in.
## Deleting post
- **Request**

View File

@ -5,12 +5,10 @@ from szurubooru import db, errors, model, rest, search
from szurubooru.func import (
auth,
favorites,
mime,
posts,
scores,
serialization,
snapshots,
tags,
versions,
)
@ -283,6 +281,19 @@ def get_posts_around(
ctx, post_id, lambda post: _serialize_post(ctx, post)
)
@rest.routes.get("/post/(?P<post_id>[^/]+)/pools-nearby/?")
def get_pools_around(
ctx: rest.Context, params: Dict[str, str]
) -> rest.Response:
auth.verify_privilege(ctx.user, "posts:list")
auth.verify_privilege(ctx.user, "posts:view")
auth.verify_privilege(ctx.user, "pools:list")
_search_executor_config.user = ctx.user
post = _get_post(params)
results = posts.get_pools_nearby(post)
return posts.serialize_pool_posts_nearby(results)
@rest.routes.post("/posts/reverse-search/?")
def get_posts_by_image(

View File

@ -1,5 +1,6 @@
import hmac
import logging
from collections import namedtuple
from datetime import datetime
from typing import Any, Callable, Dict, List, Optional, Tuple
@ -15,7 +16,6 @@ from szurubooru.func import (
pools,
scores,
serialization,
snapshots,
tags,
users,
util,
@ -968,3 +968,49 @@ def search_by_image(image_content: bytes) -> List[Tuple[float, model.Post]]:
]
else:
return []
PoolPostsNearby = namedtuple('PoolPostsNearby', 'pool first_post prev_post next_post last_post')
def get_pools_nearby(
post: model.Post
) -> List[PoolPostsNearby]:
response = []
pools = post.pools
for pool in pools:
prev_post_id = None
next_post_id = None
break_loop = False
for pool_post in pools.posts:
next_post_id = pool_post.post_id
if break_loop == True:
break
if post.id == pool_post.post_id:
break_loop = True
prev_post_id = pool_post.post_id
resp_entry = PoolPostsNearby(
pool=pool,
first_post=pool.posts[0].post_id,
last_post=pool.posts[-1].post_id,
prev_post=next_post_id,
next_post=prev_post_id,
)
response.append(resp_entry)
return response
def serialize_pool_posts_nearby(
nearby: List[PoolPostsNearby]
) -> Optional[rest.Response]:
return [
{
"pool": pools.serialize_pool(entry.pool),
"firstPost": serialize_micro_post(try_get_post_by_id(entry.first_post)),
"lastPost": serialize_micro_post(try_get_post_by_id(entry.last_post)),
"prevPost": serialize_micro_post(try_get_post_by_id(entry.prev_post)),
"nextPost": serialize_micro_post(try_get_post_by_id(entry.first_post)),
} for entry in nearby
]