Add first/last pool post to pool navigator

This commit is contained in:
Ruin0x11 2021-05-09 00:49:23 -07:00
parent e1c97049da
commit 28eaf53dfd
8 changed files with 80 additions and 14 deletions

View file

@ -18,7 +18,7 @@ $message-error-border-color = #FCC
$message-error-background-color = #FFF5F5
$message-success-border-color = #D3E3D3
$message-success-background-color = #F5FFF5
$pool-navigator-border-color = #888
$pool-navigator-border-color = #AAA
$pool-navigator-background-color = #EEE
$input-bad-border-color = #FCC
$input-bad-background-color = #FFF5F5

View file

@ -1,7 +1,7 @@
@import colors
.pool-navigator-container
padding: 0 0.50em 0 0.50em
padding: 0
margin: 0 auto
.pool-info-wrapper
@ -14,6 +14,8 @@
background: $pool-navigator-background-color
&.active
font-weight: bold
font-size: 1.10em;
padding: 0.58em 1em
.pool-name
flex: 1 1;
@ -23,7 +25,10 @@
-o-text-overflow: ellipsis;
text-overflow: ellipsis;
.prev, .next
.first, .last
flex-basis: 1em;
.first, .prev, .next, .last
flex: 0 1;
margin: 0 .25em;
white-space: nowrap;

View file

@ -40,11 +40,14 @@
width: 100%
.post-container
margin-bottom: 2em
margin-bottom: 1em
.post-content
margin: 0
.pool-navigators-container
margin-bottom: 2em
@media (max-width: 800px)
.post-view
flex-wrap: wrap

View file

@ -1,5 +1,14 @@
<div class='pool-navigator-container'>
<div class='pool-info-wrapper <%= ctx.isActivePool ? "active" : "" %>'>
<span class='first'>
<% if (ctx.canViewPosts && ctx.firstPost) { %>
<a class='<%- ctx.linkClass %>' href='<%= ctx.getPostUrl(ctx.firstPost.id, ctx.parameters) %>'>
<% } %>
«
<% if (ctx.canViewPosts && ctx.firstPost) { %>
</a>
<% } %>
</span>
<span class='prev'>
<% if (ctx.canViewPosts && ctx.prevPost) { %>
<a class='<%- ctx.linkClass %>' href='<%= ctx.getPostUrl(ctx.prevPost.id, ctx.parameters) %>'>
@ -27,5 +36,14 @@
</a>
<% } %>
</span>
<span class='last'>
<% if (ctx.canViewPosts && ctx.lastPost) { %>
<a class='<%- ctx.linkClass %>' href='<%= ctx.getPostUrl(ctx.lastPost.id, ctx.parameters) %>'>
<% } %>
»
<% if (ctx.canViewPosts && ctx.lastPost) { %>
</a>
<% } %>
</span>
</div>
</div>

View file

@ -22,8 +22,10 @@ class PoolNavigatorControl extends events.EventTarget {
linkClass: misc.makeCssName(poolPostAround.pool.category, "pool"),
canViewPosts: api.hasPrivilege("posts:view"),
canViewPools: api.hasPrivilege("pools:view"),
firstPost: poolPostAround.firstPost,
prevPost: poolPostAround.prevPost,
nextPost: poolPostAround.nextPost,
lastPost: poolPostAround.lastPost,
isActivePool: isActivePool
})
);

View file

@ -974,7 +974,7 @@ def search_by_image(image_content: bytes) -> List[Tuple[float, model.Post]]:
return []
PoolPostsAround = namedtuple('PoolPostsAround', 'pool prev_post next_post')
PoolPostsAround = namedtuple('PoolPostsAround', 'pool first_post prev_post next_post last_post')
def get_pool_posts_around(post: model.Post) -> List[PoolPostsAround]:
around = dict()
@ -990,11 +990,15 @@ def get_pool_posts_around(post: model.Post) -> List[PoolPostsAround]:
for order, pool_id, post_id, delta in db.session.execute(dbquery, {"post_id": post.post_id}):
if pool_id not in around:
around[pool_id] = [None, None]
if delta < 0:
around[pool_id] = [None, None, None, None]
if delta == -2:
around[pool_id][0] = post_id
elif delta > 0:
elif delta == -1:
around[pool_id][1] = post_id
elif delta == 1:
around[pool_id][2] = post_id
elif delta == 2:
around[pool_id][3] = post_id
pool_ids.add(pool_id)
post_ids.add(post_id)
@ -1011,13 +1015,19 @@ def get_pool_posts_around(post: model.Post) -> List[PoolPostsAround]:
results = []
for pool_id, entry in around.items():
first_post = None
prev_post = None
next_post = None
if entry[0] is not None:
prev_post = posts[entry[0]]
last_post = None
if entry[1] is not None:
next_post = posts[entry[1]]
results.append(PoolPostsAround(pools[pool_id], prev_post, next_post))
prev_post = posts[entry[1]]
if entry[0] is not None:
first_post = posts[entry[0]]
if entry[2] is not None:
next_post = posts[entry[2]]
if entry[3] is not None:
last_post = posts[entry[3]]
results.append(PoolPostsAround(pools[pool_id], first_post, prev_post, next_post, last_post))
return results
@ -1033,8 +1043,10 @@ def serialize_pool_posts_around(around: List[PoolPostsAround]) -> Optional[rest.
return [
{
"pool": pools.serialize_micro_pool(entry.pool),
"firstPost": entry.first_post,
"prevPost": entry.prev_post,
"nextPost": entry.next_post
"nextPost": entry.next_post,
"lastPost": entry.last_post
}
for entry in sort_pool_posts_around(around)
]

View file

@ -39,8 +39,30 @@ BEGIN
WHERE pool_post.ord < main.ord
AND pool_post.pool_id = main.pool_id
ORDER BY pool_post.ord DESC LIMIT 1)
UNION
(SELECT pool_post.ord,
pool_post.pool_id,
pool_post.post_id,
2 as delta,
main.ord AS target_ord,
main.pool_id AS target_pool_id
FROM pool_post, main
WHERE pool_post.ord = (SELECT MAX(pool_post.ord) FROM pool_post)
AND pool_post.pool_id = main.pool_id
ORDER BY pool_post.ord DESC LIMIT 1)
UNION
(SELECT pool_post.ord,
pool_post.pool_id,
pool_post.post_id,
-2 as delta,
main.ord AS target_ord,
main.pool_id AS target_pool_id
FROM pool_post, main
WHERE pool_post.ord = (SELECT MIN(pool_post.ord) FROM pool_post)
AND pool_post.pool_id = main.pool_id
ORDER BY pool_post.ord DESC LIMIT 1)
)
SELECT around.ord, around.pool_id, around.post_id, around.delta FROM around;
SELECT around.ord, around.pool_id, around.post_id, around.delta FROM around;
END
$$
""")

View file

@ -1166,7 +1166,11 @@ def test_get_pool_posts_around(post_factory, pool_factory, config_injector):
db.session.add_all([post1, post2, post3, post4, pool1, pool2])
db.session.flush()
around = posts.get_pool_posts_around(post2)
assert around[0].first_post["id"] == post1.post_id
assert around[0].prev_post["id"] == post1.post_id
assert around[0].next_post["id"] == post3.post_id
assert around[0].last_post["id"] == post4.post_id
assert around[1].first_post["id"] == post3.post_id
assert around[1].prev_post["id"] == post4.post_id
assert around[1].next_post == None
assert around[1].last_post == None