Ejemplo n.º 1
0
def handle_tag_next(session, context):
    """Send the next sticker for tagging."""
    chat = context.chat
    current_sticker = chat.current_sticker
    handle_next(session, context.bot, chat, context.tg_chat, context.user)
    if chat.current_sticker is not None:
        keyboard = get_fix_sticker_tags_keyboard(current_sticker.id)
        context.query.message.edit_reply_markup(reply_markup=keyboard)
Ejemplo n.º 2
0
def handle_tag_next(session, context):
    """Send the next sticker for tagging."""
    chat = context.chat
    current_sticker = chat.current_sticker
    handle_next(session, context.bot, chat, context.tg_chat, context.user)
    if chat.current_sticker is not None:
        keyboard = get_fix_sticker_tags_keyboard(current_sticker.file_id)
        call_tg_func(context.query.message, 'edit_reply_markup', [],
                     {'reply_markup': keyboard})
Ejemplo n.º 3
0
def tag_sticker(
    session,
    text,
    sticker,
    user,
    tg_chat=None,
    chat=None,
    message_id=None,
    replace=False,
    single_sticker=False,
):
    """Tag a single sticker."""
    # Extract all texts from message and clean/filter them
    raw_tags = get_tags_from_text(text)

    # No tags, early return
    if len(raw_tags) == 0:
        return

    # Only use the first few tags. This should prevent abuse from tag spammers.
    if len(raw_tags) > 10:
        raw_tags = raw_tags[:10]
        call_tg_func(
            tg_chat,
            "send_message",
            [
                "Please don't send that many tags. Try to describe everything as brief as possible."
            ],
        )

    # Inform us if the user managed to hit a special count of changes
    if tg_chat and len(user.changes) in reward_messages:
        reward = reward_messages[len(user.changes)]
        call_tg_func(tg_chat, "send_message", [reward])

        sentry.captureMessage(
            f"User hit {len(user.changes)} changes!",
            level="info",
            extra={
                "user": user.username,
                "user_id": user.id,
                "changes": len(user.changes),
            },
        )

    # List of tags that are newly added to this sticker
    new_tags = []
    # List of all new tags (raw_tags, but with resolved entities)
    # We need this, if we want to replace all tags
    incoming_tags = []

    international = user.international or sticker.sticker_set.international
    # Initialize the new tags array with the tags don't have the current language setting.
    for raw_tag in raw_tags:
        incoming_tag = Tag.get_or_create(session, raw_tag, international,
                                         False)
        incoming_tags.append(incoming_tag)

        # Add the tag to the list of new tags, if it doesn't exist on this sticker yet
        if incoming_tag not in sticker.tags:
            new_tags.append(incoming_tag)

    # We got no new tags
    if len(new_tags) == 0 and replace is False:
        session.commit()
        return

    # List of removed tags. This is only used, if we actually replace the sticker's tags

    removed_tags = []
    # Remove replace old tags
    if replace:
        # Merge the original emojis, since they should always be present on a sticker
        incoming_tags = incoming_tags + sticker.original_emojis
        # Find out, which stickers have been removed
        removed_tags = [
            tag for tag in sticker.tags if tag not in incoming_tags
        ]
        sticker.tags = incoming_tags
    else:
        for new_tag in new_tags:
            sticker.tags.append(new_tag)

    # Create a change for logging
    change = Change(
        user,
        sticker,
        international,
        new_tags,
        removed_tags,
        chat=chat,
        message_id=message_id,
    )
    session.add(change)

    session.commit()

    # Change the inline keyboard to allow fast fixing of the sticker's tags
    if tg_chat and chat and not single_sticker and chat.last_sticker_message_id:
        keyboard = get_fix_sticker_tags_keyboard(chat.current_sticker.file_id)
        call_tg_func(
            tg_chat.bot,
            "edit_message_reply_markup",
            [tg_chat.id, chat.last_sticker_message_id],
            {"reply_markup": keyboard},
        )