Exemplo n.º 1
0
def generate_counters():
    logger.info("Generating user counters.")
    redis_chat = NewparpRedis(connection_pool=redis_chat_pool)

    user_ids_online = list(UserListStore.multi_user_ids_online(
        redis_chat,
        UserListStore.scan_active_chats(redis_chat),
    ))

    generate_counters.redis.set(
        "connected_users",
        len(set.union(*user_ids_online)) if user_ids_online else 0,
    )
Exemplo n.º 2
0
def generate_counters():
    logger.info("Generating user counters.")
    redis_chat = NewparpRedis(connection_pool=redis_chat_pool)

    user_ids_online = list(
        UserListStore.multi_user_ids_online(
            redis_chat,
            UserListStore.scan_active_chats(redis_chat),
        ))

    generate_counters.redis.set(
        "connected_users",
        len(set.union(*user_ids_online)) if user_ids_online else 0,
    )
Exemplo n.º 3
0
def chat_list(fmt=None, type=None, page=1):
    if type is None:
        type = "all"

    try:
        ChatClass = chat_classes[type]
    except KeyError:
        abort(404)

    chats = g.db.query(ChatUser, ChatClass).join(ChatClass).filter(and_(
        ChatUser.user_id == g.user.id,
        ChatUser.subscribed == True,
    ))
    if type == "unread":
        chats = chats.filter(ChatClass.last_message > ChatUser.last_online)

    chats = chats.order_by(
        ChatClass.last_message.desc(),
    ).offset((page - 1) * 50).limit(50).all()

    if len(chats) == 0 and page != 1:
        abort(404)

    chat_count = g.db.query(func.count('*')).select_from(ChatUser).filter(and_(
        ChatUser.user_id == g.user.id,
        ChatUser.subscribed == True,
    ))
    if type == "unread":
        chat_count = chat_count.join(ChatClass).filter(
            ChatClass.last_message > ChatUser.last_online,
        )
    elif type is not None:
        chat_count = chat_count.join(ChatClass)
    chat_count = chat_count.scalar()

    online_userlists = UserListStore.multi_user_ids_online(
        NewparpRedis(connection_pool=redis_chat_pool),
        (c[1].id for c in chats),
    )

    chat_dicts = []
    for (chat_user, chat), online_user_ids in zip(chats, online_userlists):

        if chat.type == "pm":
            pm_chat_user = g.db.query(ChatUser).filter(and_(
                ChatUser.chat_id == chat.id,
                ChatUser.user_id != g.user.id,
            )).options(joinedload(ChatUser.user)).first()
        else:
            pm_chat_user = None

        cd = chat.to_dict(pm_user=pm_chat_user.user if pm_chat_user is not None else None)

        cd["online"] = len(set(online_user_ids))
        if chat.type == "pm":
            cd["partner_online"] = pm_chat_user.user.id in (int(_) for _ in online_user_ids)

        cd["unread"] = chat.last_message > chat_user.last_online

        chat_dicts.append({
            "chat_user": chat_user.to_dict(include_title_and_notes=True),
            "chat": cd,
        })

    if fmt == "json":

        return jsonify({
            "total": chat_count,
            "chats": chat_dicts,
        })

    paginator = paginate.Page(
        [],
        page=page,
        items_per_page=50,
        item_count=chat_count,
        url_maker=lambda page: url_for("rp_chat_list", page=page, type=type),
    )

    return render_template(
        "chat_list.html",
        level_options=level_options,
        type=type,
        chats=chat_dicts,
        paginator=paginator,
        chat_classes=chat_classes,
    )
Exemplo n.º 4
0
def groups(fmt=None):

    style_filter = set()
    for style in GroupChat.style.type.enums:
        if style in request.args:
            style_filter.add(style)
    if not style_filter:
        if g.user is not None:
            style_filter = g.user.group_chat_styles
        else:
            style_filter.add("script")

    level_filter = set()
    for level in GroupChat.level.type.enums:
        if level in request.args:
            level_filter.add(level)
    if not level_filter:
        if g.user is not None:
            level_filter = g.user.group_chat_levels
        else:
            level_filter.add("sfw")

    if g.user is not None:
        g.user.group_chat_styles = style_filter
        g.user.group_chat_levels = level_filter

    groups_query = g.db.query(GroupChat).filter(
        and_(
            GroupChat.publicity.in_(("listed", "pinned")),
            GroupChat.style.in_(style_filter),
            GroupChat.level.in_(level_filter),
        )).all()

    online_userlists = UserListStore.multi_user_ids_online(
        NewparpRedis(connection_pool=redis_chat_pool),
        (group.id for group in groups_query),
    )

    groups = []
    for group, online_users in zip(groups_query, online_userlists):
        online_user_count = len(set(online_users))
        if online_user_count > 0:
            groups.append((group, online_user_count))
    groups.sort(key=lambda _: (_[0].publicity, _[1]), reverse=True)

    chat_dicts = []
    for chat, online in groups:
        cd = chat.to_dict()
        cd["online"] = online
        chat_dicts.append(cd)

    if fmt == "json":
        return jsonify({
            "chats": chat_dicts,
        })

    return render_template(
        "groups.html",
        level_options=level_options,
        groups=chat_dicts,
        style_filter=style_filter,
        level_filter=level_filter,
    )
Exemplo n.º 5
0
def groups(fmt=None):

    style_filter = set()
    for style in GroupChat.style.type.enums:
        if style in request.args:
            style_filter.add(style)
    if not style_filter:
        if g.user is not None:
            style_filter = g.user.group_chat_styles
        else:
            style_filter.add("script")

    allowed_levels = g.user.level_options if g.user else allowed_level_options[AgeGroup.unknown]

    level_filter = set()
    for level in allowed_levels:
        if level in request.args:
            level_filter.add(level)
    if not level_filter:
        if g.user is not None:
            level_filter = g.user.group_chat_levels
        else:
            level_filter.add("sfw")

    if g.user is not None:
        g.user.group_chat_styles = style_filter
        g.user.group_chat_levels = level_filter

    groups_query = g.db.query(GroupChat).filter(and_(
        GroupChat.publicity.in_(("listed", "pinned")),
        GroupChat.style.in_(style_filter),
        GroupChat.level.in_(level_filter),
    )).all()

    online_userlists = UserListStore.multi_user_ids_online(
        NewparpRedis(connection_pool=redis_chat_pool),
        (group.id for group in groups_query),
    )

    groups = []
    for group, online_users in zip(groups_query, online_userlists):
        online_user_count = len(set(online_users))
        if online_user_count > 0:
            groups.append((group, online_user_count))
    groups.sort(key=lambda _: (_[0].publicity, _[1]), reverse=True)

    chat_dicts = []
    for chat, online in groups:
        cd = chat.to_dict()
        cd["online"] = online
        chat_dicts.append(cd)

    if fmt == "json":
        return jsonify({
            "chats": chat_dicts,
        })

    return render_template(
        "groups.html",
        allowed_levels=allowed_levels,
        level_options=level_options,
        AgeGroup=AgeGroup,
        groups=chat_dicts,
        style_filter=style_filter,
        level_filter=level_filter,
    )
Exemplo n.º 6
0
def chat_list(fmt=None, type=None, page=1):
    if type is None:
        type = "all"

    try:
        ChatClass = chat_classes[type]
    except KeyError:
        abort(404)

    chats = g.db.query(ChatUser, ChatClass).join(ChatClass).filter(
        and_(
            ChatUser.user_id == g.user.id,
            ChatUser.subscribed == True,
        ))
    if type == "unread":
        chats = chats.filter(ChatClass.last_message > ChatUser.last_online)

    chats = chats.order_by(ChatClass.last_message.desc(), ).offset(
        (page - 1) * 50).limit(50).all()

    if len(chats) == 0 and page != 1:
        abort(404)

    chat_count = g.db.query(func.count('*')).select_from(ChatUser).filter(
        and_(
            ChatUser.user_id == g.user.id,
            ChatUser.subscribed == True,
        ))
    if type == "unread":
        chat_count = chat_count.join(ChatClass).filter(
            ChatClass.last_message > ChatUser.last_online, )
    elif type is not None:
        chat_count = chat_count.join(ChatClass)
    chat_count = chat_count.scalar()

    online_userlists = UserListStore.multi_user_ids_online(
        NewparpRedis(connection_pool=redis_chat_pool),
        (c[1].id for c in chats),
    )

    chat_dicts = []
    for (chat_user, chat), online_user_ids in zip(chats, online_userlists):

        if chat.type == "pm":
            pm_chat_user = g.db.query(ChatUser).filter(
                and_(
                    ChatUser.chat_id == chat.id,
                    ChatUser.user_id != g.user.id,
                )).options(joinedload(ChatUser.user)).first()
        else:
            pm_chat_user = None

        cd = chat.to_dict(
            pm_user=pm_chat_user.user if pm_chat_user is not None else None)

        cd["online"] = len(set(online_user_ids))
        if chat.type == "pm":
            cd["partner_online"] = pm_chat_user.user.id in (
                int(_) for _ in online_user_ids)

        cd["unread"] = chat.last_message > chat_user.last_online

        chat_dicts.append({
            "chat_user":
            chat_user.to_dict(include_title_and_notes=True),
            "chat":
            cd,
        })

    if fmt == "json":

        return jsonify({
            "total": chat_count,
            "chats": chat_dicts,
        })

    paginator = paginate.Page(
        [],
        page=page,
        items_per_page=50,
        item_count=chat_count,
        url_maker=lambda page: url_for("rp_chat_list", page=page, type=type),
    )

    return render_template(
        "chat_list.html",
        level_options=level_options,
        type=type,
        chats=chat_dicts,
        paginator=paginator,
        chat_classes=chat_classes,
    )