add /top/guilds

This commit is contained in:
Yusur 2025-09-18 12:27:34 +02:00
parent f51f488567
commit 79b1b574a8
5 changed files with 37 additions and 13 deletions

View file

@ -26,7 +26,7 @@ from suou import twocolon_list, WantsContentType
from .colors import color_themes, theme_classes
__version__ = '0.5.0-dev36'
__version__ = '0.5.0-dev37'
APP_BASE_DIR = os.path.dirname(os.path.dirname(__file__))

View file

@ -2,6 +2,7 @@
from flask_login import current_user
from sqlalchemy import and_, distinct, func, select
from suou import not_implemented
from .models import Comment, Member, Post, Guild, User
@ -29,10 +30,21 @@ def user_timeline(user: User):
).order_by(Post.created_at.desc())
def new_comments(p: Post):
return select(Comment).join(Post, Post.id == Comment.parent_post_id).join(User, User.id == Comment.author_id).where(Comment.parent_post_id == p.id, Comment.parent_comment_id == None,
Comment.not_removed(), User.has_not_blocked(Comment.author_id, cuser_id())).order_by(Comment.created_at.desc())
return select(Comment).join(Post, Post.id == Comment.parent_post_id).join(User, User.id == Comment.author_id
).where(Comment.parent_post_id == p.id, Comment.parent_comment_id == None, Comment.not_removed(), User.has_not_blocked(Comment.author_id, cuser_id())
).order_by(Comment.created_at.desc())
def top_guilds_query():
q_post_count = func.count(distinct(Post.id)).label('post_count')
q_sub_count = func.count(distinct(Member.id)).label('sub_count')
qr = select(Guild, q_post_count, q_sub_count)\
.join(Post, Post.topic_id == Guild.id, isouter=True)\
.join(Member, and_(Member.guild_id == Guild.id, Member.is_subscribed == True), isouter=True)\
.group_by(Guild).having(q_post_count > 5).order_by(q_post_count.desc(), q_sub_count.desc())
return qr
@not_implemented()
class Algorithms:
"""
Return SQL queries for algorithms.

View file

@ -503,6 +503,14 @@ class Guild(Base):
gg['type'] = 'guild'
return gg
async def sub_info(self):
"""
Guild info including subscriber count.
"""
gg = self.simple_info()
gg['subscriber_count'] = await self.subscriber_count()
gg['post_count'] = await self.post_count()
Topic = deprecated('renamed to Guild')(Guild)

View file

@ -13,7 +13,7 @@ from werkzeug.security import check_password_hash
from suou.quart import add_rest
from freak.accounts import LoginStatus, check_login
from freak.algorithms import public_timeline, topic_timeline, user_timeline
from freak.algorithms import public_timeline, top_guilds_query, topic_timeline, user_timeline
from ..models import Guild, Post, User, db
from .. import UserLoader, app, app_config, __version__ as freak_version, csrf
@ -237,3 +237,14 @@ async def home_feed():
return dict(feed=feed)
@bp.get('/top/guilds')
async def top_guilds():
async with db as session:
top_g = [await x.sub_info() for x in
(await session.execute(top_guilds_query().limit(10))).scalars()]
return dict(has=top_g)

View file

@ -11,20 +11,13 @@ from freak.utils import get_request_form
from ..search import SearchQuery
from ..models import Guild, Member, Post, User, db
from ..algorithms import public_timeline, topic_timeline
from ..algorithms import public_timeline, top_guilds_query, topic_timeline
current_user: UserLoader
bp = Blueprint('frontpage', __name__)
def top_guilds_query():
q_post_count = func.count(distinct(Post.id)).label('post_count')
q_sub_count = func.count(distinct(Member.id)).label('sub_count')
qr = select(Guild.name, q_post_count, q_sub_count)\
.join(Post, Post.topic_id == Guild.id, isouter=True)\
.join(Member, and_(Member.guild_id == Guild.id, Member.is_subscribed == True), isouter=True)\
.group_by(Guild).having(q_post_count > 5).order_by(q_post_count.desc(), q_sub_count.desc())
return qr
@bp.route('/')
async def homepage():