diff --git a/freak/__init__.py b/freak/__init__.py index 5ff898b..df9cf6e 100644 --- a/freak/__init__.py +++ b/freak/__init__.py @@ -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__)) diff --git a/freak/algorithms.py b/freak/algorithms.py index 911b905..a5f3e3f 100644 --- a/freak/algorithms.py +++ b/freak/algorithms.py @@ -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. diff --git a/freak/models.py b/freak/models.py index ef44748..0d8432c 100644 --- a/freak/models.py +++ b/freak/models.py @@ -502,6 +502,14 @@ class Guild(Base): if typed: 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) diff --git a/freak/rest/__init__.py b/freak/rest/__init__.py index ad4650f..233f4b6 100644 --- a/freak/rest/__init__.py +++ b/freak/rest/__init__.py @@ -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) + + + diff --git a/freak/website/frontpage.py b/freak/website/frontpage.py index 48dcf3d..2cafeed 100644 --- a/freak/website/frontpage.py +++ b/freak/website/frontpage.py @@ -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():