client, server: merge nearby pool posts into regular post serialization

Can still be cleaned up some more.
Need to compare speed of the get_around query vs nearby pool posts.
This commit is contained in:
Eva
2025-04-03 01:39:09 +02:00
parent 7708b4e5a3
commit 3875ec173f
8 changed files with 67 additions and 81 deletions

View File

@ -17,11 +17,6 @@ class PostMainController extends BasePostController {
constructor(ctx, editMode) {
super(ctx);
let poolPostsNearby = Promise.resolve({results: []});
if (api.hasPrivilege("pools:list") && api.hasPrivilege("pools:view")) {
poolPostsNearby = PostList.getNearbyPoolPosts(ctx.parameters.id);
}
let parameters = ctx.parameters;
Promise.all([
Post.get(ctx.parameters.id),
@ -29,10 +24,9 @@ class PostMainController extends BasePostController {
ctx.parameters.id,
parameters ? parameters.query : null
),
poolPostsNearby
]).then(
(responses) => {
const [post, aroundResponse, poolPostsNearby] = responses;
const [post, aroundResponse] = responses;
let aroundPool = null;
// remove junk from query, but save it into history so that it can
@ -51,9 +45,9 @@ class PostMainController extends BasePostController {
const found = item.match(/^pool:([0-9]+)/i);
if (found) {
const activePool = parseInt(found[1]);
poolPostsNearby.forEach((nearbyPosts) => {
if (nearbyPosts.pool.id == activePool) {
aroundPool = nearbyPosts
post.pools.map((pool) => {
if (pool.id == activePool) {
aroundPool = pool;
}
});
}
@ -63,7 +57,6 @@ class PostMainController extends BasePostController {
this._post = post;
this._view = new PostMainView({
post: post,
poolPostsNearby: poolPostsNearby,
editMode: editMode,
prevPostId: aroundPool
? (aroundPool.previousPost ? aroundPool.previousPost.id : null)

View File

@ -16,9 +16,9 @@ class PoolNavigatorControl extends events.EventTarget {
views.replaceContent(
this._hostNode,
template({
pool: poolPostNearby.pool,
parameters: { query: `pool:${poolPostNearby.pool.id}` },
linkClass: misc.makeCssName(poolPostNearby.pool.category, "pool"),
pool: poolPostNearby,
parameters: { query: `pool:${poolPostNearby.id}` },
linkClass: misc.makeCssName(poolPostNearby.category, "pool"),
canViewPosts: api.hasPrivilege("posts:view"),
canViewPools: api.hasPrivilege("pools:view"),
firstPost: poolPostNearby.firstPost,

View File

@ -13,8 +13,8 @@ class PoolNavigatorListControl extends events.EventTarget {
this._poolPostNearby = poolPostNearby;
this._indexToNode = {};
for (let [i, entry] of this._poolPostNearby.entries()) {
this._installPoolNavigatorNode(entry, i);
for (const entry of this._poolPostNearby) {
this._installPoolNavigatorNode(entry);
}
}
@ -22,7 +22,7 @@ class PoolNavigatorListControl extends events.EventTarget {
return this._hostNode;
}
_installPoolNavigatorNode(poolPostNearby, i) {
_installPoolNavigatorNode(poolPostNearby) {
const poolListItemNode = document.createElement("div");
const poolControl = new PoolNavigatorControl(
poolListItemNode,

View File

@ -51,6 +51,22 @@ class Pool extends events.EventTarget {
return this._lastEditTime;
}
get firstPost() {
return this._firstPost;
}
get lastPost() {
return this._lastPost;
}
get previousPost() {
return this._previousPost;
}
get nextPost() {
return this._nextPost;
}
set names(value) {
this._names = value;
}
@ -169,6 +185,10 @@ class Pool extends events.EventTarget {
_creationTime: response.creationTime,
_lastEditTime: response.lastEditTime,
_postCount: response.postCount || 0,
_firstPost: response.firstPost || null,
_lastPost: response.lastPost || null,
_previousPost: response.previousPost || null,
_nextPost: response.nextPost || null,
};
for (let obj of [this, this._orig]) {

View File

@ -16,14 +16,6 @@ class PostList extends AbstractList {
);
}
static getNearbyPoolPosts(id) {
return api.get(
uri.formatApiLink("post", id, "pools-nearby", {
fields: "id",
})
);
}
static search(text, offset, limit, fields) {
return api
.get(

View File

@ -58,7 +58,7 @@ class PostMainView {
this._installSidebar(ctx);
this._installCommentForm();
this._installComments(ctx.post.comments);
this._installPoolNavigators(ctx.poolPostsNearby);
this._installPoolNavigators(ctx);
const showPreviousImage = () => {
if (ctx.prevPostId) {
@ -139,7 +139,7 @@ class PostMainView {
}
}
_installPoolNavigators(poolPostsNearby) {
_installPoolNavigators(ctx) {
const poolNavigatorsContainerNode = document.querySelector(
"#content-holder .pool-navigators-container"
);
@ -149,7 +149,7 @@ class PostMainView {
this.poolNavigatorsControl = new PoolNavigatorListControl(
poolNavigatorsContainerNode,
poolPostsNearby,
ctx.post.pools,
);
}

View File

@ -281,19 +281,6 @@ 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

@ -9,6 +9,7 @@ import sqlalchemy as sa
from szurubooru import config, db, errors, model, rest
from szurubooru.func import (
auth,
comments,
files,
image_hash,
@ -345,8 +346,10 @@ class PostSerializer(serialization.BaseSerializer):
]
def serialize_pools(self) -> List[Any]:
if not auth.has_privilege(self.auth_user, "pools:list"):
return []
return [
pools.serialize_micro_pool(pool)
{**pools.serialize_micro_pool(pool), **get_pool_posts_nearby(self.post, pool)} if auth.has_privilege(self.auth_user, "pools:view") else pools.serialize_micro_pool(pool)
for pool in sorted(
self.post.pools, key=lambda pool: pool.creation_time
)
@ -977,14 +980,21 @@ 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
def serialize_safe_post(
post: Optional[model.Post]
) -> rest.Response:
return {"id": getattr(post, "post_id", None)} if post else None
for pool in pools:
def serialize_id_post(
post_id: Optional[int]
) -> rest.Response:
return serialize_safe_post(try_get_post_by_id(post_id)) if post_id else None
def get_pool_posts_nearby(
post: model.Post, pool: model.Pool
) -> rest.Response:
prev_post_id = None
next_post_id = None
first_post_id = pool.posts[0].post_id,
@ -998,25 +1008,9 @@ def get_pools_nearby(
next_post_id = next_item.post_id
break
resp_entry = PoolPostsNearby(
pool=pool,
first_post=first_post_id,
last_post=last_post_id,
prev_post=prev_post_id,
next_post=next_post_id,
)
response.append(resp_entry)
return response
def serialize_pool_posts_nearby(
nearby: List[PoolPostsNearby]
) -> Optional[rest.Response]:
return [
{
"pool": pools.serialize_micro_pool(entry.pool),
"firstPost": {"id": getattr(try_get_post_by_id(entry.first_post), "post_id", None)} if entry.first_post else None,
"lastPost": {"id": getattr(try_get_post_by_id(entry.last_post), "post_id", None)} if entry.last_post else None,
"previousPost": {"id": getattr(try_get_post_by_id(entry.prev_post), "post_id", None)} if entry.prev_post else None,
"nextPost": {"id": getattr(try_get_post_by_id(entry.next_post), "post_id", None)} if entry.next_post else None,
} for entry in nearby
]
return {
"firstPost": serialize_id_post(first_post_id),
"lastPost": serialize_id_post(last_post_id),
"previousPost": serialize_id_post(prev_post_id),
"nextPost": serialize_id_post(next_post_id),
}