mirror of
https://github.com/rr-/szurubooru.git
synced 2025-07-17 08:26:24 +00:00
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:
@ -17,11 +17,6 @@ class PostMainController extends BasePostController {
|
|||||||
constructor(ctx, editMode) {
|
constructor(ctx, editMode) {
|
||||||
super(ctx);
|
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;
|
let parameters = ctx.parameters;
|
||||||
Promise.all([
|
Promise.all([
|
||||||
Post.get(ctx.parameters.id),
|
Post.get(ctx.parameters.id),
|
||||||
@ -29,10 +24,9 @@ class PostMainController extends BasePostController {
|
|||||||
ctx.parameters.id,
|
ctx.parameters.id,
|
||||||
parameters ? parameters.query : null
|
parameters ? parameters.query : null
|
||||||
),
|
),
|
||||||
poolPostsNearby
|
|
||||||
]).then(
|
]).then(
|
||||||
(responses) => {
|
(responses) => {
|
||||||
const [post, aroundResponse, poolPostsNearby] = responses;
|
const [post, aroundResponse] = responses;
|
||||||
let aroundPool = null;
|
let aroundPool = null;
|
||||||
|
|
||||||
// remove junk from query, but save it into history so that it can
|
// 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);
|
const found = item.match(/^pool:([0-9]+)/i);
|
||||||
if (found) {
|
if (found) {
|
||||||
const activePool = parseInt(found[1]);
|
const activePool = parseInt(found[1]);
|
||||||
poolPostsNearby.forEach((nearbyPosts) => {
|
post.pools.map((pool) => {
|
||||||
if (nearbyPosts.pool.id == activePool) {
|
if (pool.id == activePool) {
|
||||||
aroundPool = nearbyPosts
|
aroundPool = pool;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -63,7 +57,6 @@ class PostMainController extends BasePostController {
|
|||||||
this._post = post;
|
this._post = post;
|
||||||
this._view = new PostMainView({
|
this._view = new PostMainView({
|
||||||
post: post,
|
post: post,
|
||||||
poolPostsNearby: poolPostsNearby,
|
|
||||||
editMode: editMode,
|
editMode: editMode,
|
||||||
prevPostId: aroundPool
|
prevPostId: aroundPool
|
||||||
? (aroundPool.previousPost ? aroundPool.previousPost.id : null)
|
? (aroundPool.previousPost ? aroundPool.previousPost.id : null)
|
||||||
|
@ -16,9 +16,9 @@ class PoolNavigatorControl extends events.EventTarget {
|
|||||||
views.replaceContent(
|
views.replaceContent(
|
||||||
this._hostNode,
|
this._hostNode,
|
||||||
template({
|
template({
|
||||||
pool: poolPostNearby.pool,
|
pool: poolPostNearby,
|
||||||
parameters: { query: `pool:${poolPostNearby.pool.id}` },
|
parameters: { query: `pool:${poolPostNearby.id}` },
|
||||||
linkClass: misc.makeCssName(poolPostNearby.pool.category, "pool"),
|
linkClass: misc.makeCssName(poolPostNearby.category, "pool"),
|
||||||
canViewPosts: api.hasPrivilege("posts:view"),
|
canViewPosts: api.hasPrivilege("posts:view"),
|
||||||
canViewPools: api.hasPrivilege("pools:view"),
|
canViewPools: api.hasPrivilege("pools:view"),
|
||||||
firstPost: poolPostNearby.firstPost,
|
firstPost: poolPostNearby.firstPost,
|
||||||
|
@ -13,8 +13,8 @@ class PoolNavigatorListControl extends events.EventTarget {
|
|||||||
this._poolPostNearby = poolPostNearby;
|
this._poolPostNearby = poolPostNearby;
|
||||||
this._indexToNode = {};
|
this._indexToNode = {};
|
||||||
|
|
||||||
for (let [i, entry] of this._poolPostNearby.entries()) {
|
for (const entry of this._poolPostNearby) {
|
||||||
this._installPoolNavigatorNode(entry, i);
|
this._installPoolNavigatorNode(entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ class PoolNavigatorListControl extends events.EventTarget {
|
|||||||
return this._hostNode;
|
return this._hostNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
_installPoolNavigatorNode(poolPostNearby, i) {
|
_installPoolNavigatorNode(poolPostNearby) {
|
||||||
const poolListItemNode = document.createElement("div");
|
const poolListItemNode = document.createElement("div");
|
||||||
const poolControl = new PoolNavigatorControl(
|
const poolControl = new PoolNavigatorControl(
|
||||||
poolListItemNode,
|
poolListItemNode,
|
||||||
|
@ -51,6 +51,22 @@ class Pool extends events.EventTarget {
|
|||||||
return this._lastEditTime;
|
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) {
|
set names(value) {
|
||||||
this._names = value;
|
this._names = value;
|
||||||
}
|
}
|
||||||
@ -169,6 +185,10 @@ class Pool extends events.EventTarget {
|
|||||||
_creationTime: response.creationTime,
|
_creationTime: response.creationTime,
|
||||||
_lastEditTime: response.lastEditTime,
|
_lastEditTime: response.lastEditTime,
|
||||||
_postCount: response.postCount || 0,
|
_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]) {
|
for (let obj of [this, this._orig]) {
|
||||||
|
@ -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) {
|
static search(text, offset, limit, fields) {
|
||||||
return api
|
return api
|
||||||
.get(
|
.get(
|
||||||
|
@ -58,7 +58,7 @@ class PostMainView {
|
|||||||
this._installSidebar(ctx);
|
this._installSidebar(ctx);
|
||||||
this._installCommentForm();
|
this._installCommentForm();
|
||||||
this._installComments(ctx.post.comments);
|
this._installComments(ctx.post.comments);
|
||||||
this._installPoolNavigators(ctx.poolPostsNearby);
|
this._installPoolNavigators(ctx);
|
||||||
|
|
||||||
const showPreviousImage = () => {
|
const showPreviousImage = () => {
|
||||||
if (ctx.prevPostId) {
|
if (ctx.prevPostId) {
|
||||||
@ -139,7 +139,7 @@ class PostMainView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_installPoolNavigators(poolPostsNearby) {
|
_installPoolNavigators(ctx) {
|
||||||
const poolNavigatorsContainerNode = document.querySelector(
|
const poolNavigatorsContainerNode = document.querySelector(
|
||||||
"#content-holder .pool-navigators-container"
|
"#content-holder .pool-navigators-container"
|
||||||
);
|
);
|
||||||
@ -149,7 +149,7 @@ class PostMainView {
|
|||||||
|
|
||||||
this.poolNavigatorsControl = new PoolNavigatorListControl(
|
this.poolNavigatorsControl = new PoolNavigatorListControl(
|
||||||
poolNavigatorsContainerNode,
|
poolNavigatorsContainerNode,
|
||||||
poolPostsNearby,
|
ctx.post.pools,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -281,19 +281,6 @@ def get_posts_around(
|
|||||||
ctx, post_id, lambda post: _serialize_post(ctx, post)
|
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/?")
|
@rest.routes.post("/posts/reverse-search/?")
|
||||||
def get_posts_by_image(
|
def get_posts_by_image(
|
||||||
|
@ -9,6 +9,7 @@ import sqlalchemy as sa
|
|||||||
|
|
||||||
from szurubooru import config, db, errors, model, rest
|
from szurubooru import config, db, errors, model, rest
|
||||||
from szurubooru.func import (
|
from szurubooru.func import (
|
||||||
|
auth,
|
||||||
comments,
|
comments,
|
||||||
files,
|
files,
|
||||||
image_hash,
|
image_hash,
|
||||||
@ -345,8 +346,10 @@ class PostSerializer(serialization.BaseSerializer):
|
|||||||
]
|
]
|
||||||
|
|
||||||
def serialize_pools(self) -> List[Any]:
|
def serialize_pools(self) -> List[Any]:
|
||||||
|
if not auth.has_privilege(self.auth_user, "pools:list"):
|
||||||
|
return []
|
||||||
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(
|
for pool in sorted(
|
||||||
self.post.pools, key=lambda pool: pool.creation_time
|
self.post.pools, key=lambda pool: pool.creation_time
|
||||||
)
|
)
|
||||||
@ -977,46 +980,37 @@ def search_by_image(image_content: bytes) -> List[Tuple[float, model.Post]]:
|
|||||||
else:
|
else:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
PoolPostsNearby = namedtuple('PoolPostsNearby', 'pool first_post prev_post next_post last_post')
|
def serialize_safe_post(
|
||||||
def get_pools_nearby(
|
post: Optional[model.Post]
|
||||||
post: model.Post
|
) -> rest.Response:
|
||||||
) -> List[PoolPostsNearby]:
|
return {"id": getattr(post, "post_id", None)} if post else None
|
||||||
response = []
|
|
||||||
pools = post.pools
|
|
||||||
|
|
||||||
for pool in pools:
|
|
||||||
prev_post_id = None
|
|
||||||
next_post_id = None
|
|
||||||
first_post_id = pool.posts[0].post_id,
|
|
||||||
last_post_id = pool.posts[-1].post_id,
|
|
||||||
|
|
||||||
for previous_item, current_item, next_item in _get_nearby_iter(pool.posts):
|
def serialize_id_post(
|
||||||
if post.post_id == current_item.post_id:
|
post_id: Optional[int]
|
||||||
if previous_item != None:
|
) -> rest.Response:
|
||||||
prev_post_id = previous_item.post_id
|
return serialize_safe_post(try_get_post_by_id(post_id)) if post_id else None
|
||||||
if next_item != None:
|
|
||||||
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(
|
def get_pool_posts_nearby(
|
||||||
nearby: List[PoolPostsNearby]
|
post: model.Post, pool: model.Pool
|
||||||
) -> Optional[rest.Response]:
|
) -> rest.Response:
|
||||||
return [
|
prev_post_id = None
|
||||||
{
|
next_post_id = None
|
||||||
"pool": pools.serialize_micro_pool(entry.pool),
|
first_post_id = pool.posts[0].post_id,
|
||||||
"firstPost": {"id": getattr(try_get_post_by_id(entry.first_post), "post_id", None)} if entry.first_post else None,
|
last_post_id = pool.posts[-1].post_id,
|
||||||
"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,
|
for previous_item, current_item, next_item in _get_nearby_iter(pool.posts):
|
||||||
"nextPost": {"id": getattr(try_get_post_by_id(entry.next_post), "post_id", None)} if entry.next_post else None,
|
if post.post_id == current_item.post_id:
|
||||||
} for entry in nearby
|
if previous_item != None:
|
||||||
]
|
prev_post_id = previous_item.post_id
|
||||||
|
if next_item != None:
|
||||||
|
next_post_id = next_item.post_id
|
||||||
|
break
|
||||||
|
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user