<commit_before>from html import escape
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, TelegramError
from telegram.ext import CallbackQueryHandler, CommandHandler, Filters

from main import database, TRUNCATE_ARGS_LENGTH, format_quote

dm_kwargs = {
    'filters': Filters.private,
    'pass_user_data': True
}


CHECK_MARK = '\u2705'
UP_ARROW = '\u2B06'
DOWN_ARROW = '\u2B07'


def create_vote_buttons(user_id, quote_id, direct=False):
    upvotes, score, downvotes = database.get_votes_by_id(quote_id)

    text_up = f'{UP_ARROW} ({upvotes})'
    text_zero = f'score {score}'
    text_down = f'{DOWN_ARROW} ({downvotes})'

    if direct:
        vote = database.get_user_vote(user_id, quote_id)

        if vote == 0:
            pass
        elif vote == 1:
            text_up = CHECK_MARK + text_up
        elif vote == -1:
            text_down = CHECK_MARK + text_down

    up = InlineKeyboardButton(text_up, callback_data='1')
    zero = InlineKeyboardButton(text_zero, callback_data='0')
    down = InlineKeyboardButton(text_down, callback_data='-1')

    options = [up, zero, down]
    keyboard = InlineKeyboardMarkup([options])

    return keyboard


def handle_vote(bot, update, user_data):
    query = update.callback_query

    user = query.from_user
    quote_message = query.message
    direction = int(query.data)

    if direction == 0:
        return query.answer('')

    current_chat_id = query.message.chat_id

    # Check if the query originated from a direct message:
    # callback query handlers don't accept filters
    direct = current_chat_id == user.id

    if direct:
        quote_chat_id = user_data['current']
    else:
        quote_chat_id = current_chat_id

    quote_id = database.get_quote_id_from_message(
        quote_chat_id, quote_message.message_id)

    if quote_id is None:
        return query.answer('')

    status = database.add_vote(user.id, quote_id, direction)

    if status == database.VOTE_ADDED:
        response = "vote added!"
    elif status == database.ALREADY_VOTED:
        database.add_vote(user.id, quote_id, 0)
        response = "vote removed!"
    elif status == database.QUOTE_DELETED:
        response = "vote added and quote deleted!"
        query.answer(response)

        for chat_id, message_id in database.get_quote_messages(quote_id):
            try:
                bot.edit_message_text(
                    chat_id=chat_id, message_id=message_id,
                    text="[quote was deleted]", reply_markup=[])
            except TelegramError as e:
                # The message is over 48 hours old and can't be edited
                pass

        return

    query.answer(response)

    keyboard = create_vote_buttons(user.id, quote_id, direct=direct)
<commit_msg>Display the user's vote when the score button is activated<commit_after>from html import escape
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, TelegramError
from telegram.ext import CallbackQueryHandler, CommandHandler, Filters

from main import database, TRUNCATE_ARGS_LENGTH, format_quote

dm_kwargs = {
    'filters': Filters.private,
    'pass_user_data': True
}


CHECK_MARK = '\u2705'
UP_ARROW = '\u2B06'
DOWN_ARROW = '\u2B07'


def create_vote_buttons(user_id, quote_id, direct=False):
    upvotes, score, downvotes = database.get_votes_by_id(quote_id)

    text_up = f'{UP_ARROW} ({upvotes})'
    text_zero = f'score {score}'
    text_down = f'{DOWN_ARROW} ({downvotes})'

    if direct:
        vote = database.get_user_vote(user_id, quote_id)

        if vote == 0:
            pass
        elif vote == 1:
            text_up = CHECK_MARK + text_up
        elif vote == -1:
            text_down = CHECK_MARK + text_down

    up = InlineKeyboardButton(text_up, callback_data='1')
    zero = InlineKeyboardButton(text_zero, callback_data='0')
    down = InlineKeyboardButton(text_down, callback_data='-1')

    options = [up, zero, down]
    keyboard = InlineKeyboardMarkup([options])

    return keyboard


def handle_vote(bot, update, user_data):
    query = update.callback_query

    user = query.from_user
    quote_message = query.message
    data = int(query.data)

    current_chat_id = query.message.chat_id

    # Check if the query originated from a direct message:
    # callback query handlers don't accept filters
    direct = current_chat_id == user.id

    if direct:
        quote_chat_id = user_data['current']
    else:
        quote_chat_id = current_chat_id

    quote_id = database.get_quote_id_from_message(
        quote_chat_id, quote_message.message_id)

    if quote_id is None:
        return query.answer('')

    if data == 0:
        vote = database.get_user_vote(user.id, quote_id)

        if vote == 0:
            return query.answer("you haven't voted on this quote!")
        elif vote == 1:
            return query.answer(f"{UP_ARROW} you upvoted this quote")
        elif vote == -1:
            return query.answer(f"{DOWN_ARROW} you downvoted this quote")

    status = database.add_vote(user.id, quote_id, data)

    if status == database.VOTE_ADDED:
        response = "vote added!"
    elif status == database.ALREADY_VOTED:
        database.add_vote(user.id, quote_id, 0)
        response = "vote removed!"
    elif status == database.QUOTE_DELETED:
        response = "vote added and quote deleted!"
        query.answer(response)

        for chat_id, message_id in database.get_quote_messages(quote_id):
            try:
                bot.edit_message_text(
                    chat_id=chat_id, message_id=message_id,
                    text="[quote was deleted]", reply_markup=[])
            except TelegramError as e:
                # The message is over 48 hours old and can't be edited
                pass

        return

    query.answer(response)

    keyboard = create_vote_buttons(user.id, quote_id, direct=direct)

{'fraction_non_alphanumeric': 0.09038297872340426, 'fraction_numerical': 0.007319148936170213, 'mean_word_length': 2.823031880286272, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7416904', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1804, 'n_words': 450}