diff --git a/client/js/controllers/post_main_controller.js b/client/js/controllers/post_main_controller.js index 4df4a496..00db7a1c 100644 --- a/client/js/controllers/post_main_controller.js +++ b/client/js/controllers/post_main_controller.js @@ -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) diff --git a/client/js/controls/pool_navigator_control.js b/client/js/controls/pool_navigator_control.js index 6a52cd48..41b68d53 100644 --- a/client/js/controls/pool_navigator_control.js +++ b/client/js/controls/pool_navigator_control.js @@ -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, diff --git a/client/js/controls/pool_navigator_list_control.js b/client/js/controls/pool_navigator_list_control.js index 6aa5302f..37d052f2 100644 --- a/client/js/controls/pool_navigator_list_control.js +++ b/client/js/controls/pool_navigator_list_control.js @@ -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, diff --git a/client/js/models/pool.js b/client/js/models/pool.js index 51fa8a05..24b89fe3 100644 --- a/client/js/models/pool.js +++ b/client/js/models/pool.js @@ -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]) { diff --git a/client/js/models/post_list.js b/client/js/models/post_list.js index 16df6eb8..8c2c9d4e 100644 --- a/client/js/models/post_list.js +++ b/client/js/models/post_list.js @@ -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( diff --git a/client/js/views/post_main_view.js b/client/js/views/post_main_view.js index 805a9e73..6ddd00cd 100644 --- a/client/js/views/post_main_view.js +++ b/client/js/views/post_main_view.js @@ -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, ); } diff --git a/server/szurubooru/api/post_api.py b/server/szurubooru/api/post_api.py index 34a2136c..69239725 100644 --- a/server/szurubooru/api/post_api.py +++ b/server/szurubooru/api/post_api.py @@ -281,19 +281,6 @@ def get_posts_around( ctx, post_id, lambda post: _serialize_post(ctx, post) ) -@rest.routes.get("/post/(?P[^/]+)/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( diff --git a/server/szurubooru/func/posts.py b/server/szurubooru/func/posts.py index 60a5d703..db51df2a 100644 --- a/server/szurubooru/func/posts.py +++ b/server/szurubooru/func/posts.py @@ -345,8 +345,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,46 +979,37 @@ 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: - 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): - if post.post_id == current_item.post_id: - if previous_item != None: - prev_post_id = previous_item.post_id - if next_item != None: - next_post_id = next_item.post_id - break +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 - 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 - ] +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, + last_post_id = pool.posts[-1].post_id, + + for previous_item, current_item, next_item in _get_nearby_iter(pool.posts): + if post.post_id == current_item.post_id: + 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), + }