<commit_before>import collections
import json

from bot.action.chatsettings import ChatSettings
from bot.action.core.action import Action
from bot.action.core.command import UnderscoredCommandBuilder
from bot.action.core.command.usagemessage import CommandUsageMessage
from bot.action.extra.messages import analyzer
from bot.action.userinfo import UserStorageHandler
from bot.action.util.format import UserFormatter, DateFormatter
from bot.action.util.textformat import FormattedText
from bot.api.domain import Message, ApiObject

MIN_MESSAGES_TO_KEEP = 5000
MAX_MESSAGES_TO_KEEP = 15000


def is_store_messages_enabled(event):
    return event.settings.get(ChatSettings.STORE_MESSAGES) == "on"


class SaveMessageAction(Action):
    def process(self, event):
        if is_store_messages_enabled(event):
            storage_handler = MessageStorageHandler(event)
            storage_handler.save_message(event.message)
            storage_handler.delete_old_messages()


class ListMessageAction(Action):
    def process(self, event):
        action, action_param, help_args = self.parse_args(event.command_args.split())
        if action in ("recent", "show", "ranking"):
            if not is_store_messages_enabled(event):
                response = self.get_response_disabled()
            else:
                messages = MessageStorageHandler(event).get_stored_messages()
                if messages.is_empty():
                    response = self.get_response_empty()
                elif action == "recent":
                    response = self.get_response_recent(event, messages, action_param)
                elif action == "show":
                    response = self.get_response_show(event, messages, action_param)
                else:
                    response = self.get_response_ranking(event, messages, action_param)
        elif action == "whereis":
            response = self.get_response_whereis(event, action_param)
        elif action == "opt-out":
            response = self.get_response_opt_out(event, action_param)
        else:
            response = self.get_response_help(event, help_args)
        if type(response) is not list:
            response = [response]
        last_message = event.message
        for message in response:
            message.to_chat(message=last_message)
            if message.reply_to_message_id is None:
                message.reply_to_message(last_message)
            last_message = self.api.send_message(message)

    @staticmethod
    def parse_args(args):
        action = "help"
        action_param = 10
        help_args = args[1:]
        if len(args) == 0:
            action = "recent"
        elif len(args) == 1:
            if args[0].isnumeric():
                action = "show"
                action_param = int(args[0])
            elif args[0] not in ("show", "whereis"):
                action = args[0]
        elif len(args) == 2:
            if args[1].isnumeric() or args[0] == "opt-out":
                action_param = int(args[1]) if args[0] != "opt-out" else args[1]
                action = args[0]
        return action, action_param, help_args

    @staticmethod
    def get_response_help(event, help_args):
        args = ["[recent number_of_messages]",
                "[show] message_id",
                "ranking [number_of_users]",
                "whereis message_id",
                "opt-out [action]"]
        description = "_Have you ever wondered that, like in WhatsApp, Telegram messages could not be deleted or" \
                      " edited once sent?_\n" \
                      "Then this is for you! It allows you to recover deleted messages content, or original text"\
                      " of edited messages.\n\n" \
                      "There is a way to opt-out to avoid your messages being recovered (see at the end of this"\
                      " message). Keep in mind that group admins can still configure the group to override opt-out"\
                      " on that specific group (by setting `override_messages_opt_out` to `off` in `/settings`).\n\n" \
                      "Group admins can also disable completely this feature on the group. That will disable" \
                      " message storing and retrieving via this command. To do it, set `store_messages`" \
                      " to `off` using the `/settings` command.\n\n" \
                      "By default, this command displays a list with information about last messages.\n" \
                      "You can use *recent* with a number to modify the number of messages to list" \
                      " (default is 10).\n\n" \
                      "Use *show* along with a `message_id` to view that particular message.\n\n" \
                      "Use *ranking* to display a ranking of the users who wrote most recent messages" \
                      " (approximately last 1000 messages are counted).\n" \
                      "You can add a number to modify the number of top users to display (default is 10).\n\n" \
                      "Use *whereis* followed by a `message_id` to locate a particular message.\n\n" \
                      "Use *opt-out* followed by *add-me* or *remove-me* to be added or removed from the opt-out list" \
                      " of this feature. Use *get-status* or nothing to query your status on the list.\n" \
                      "While you are in the opt-out list, nobody but you can show the content of your messages using" \
                      " this feature, in any group."
        return CommandUsageMessage.get_usage_message(event.command, args, description)

    @staticmethod
    def get_response_disabled():
        return FormattedText().bold("Sorry, this feature is disabled on this chat. 😕").newline().newline()\
            .normal("Group admins can enable it using the following command:").newline()\
            .code_inline("/settings set store_messages on")\
            .build_message()

    @staticmethod<commit_msg>Add documentation to from feature of messages<commit_after>import collections
import json

from bot.action.chatsettings import ChatSettings
from bot.action.core.action import Action
from bot.action.core.command import UnderscoredCommandBuilder
from bot.action.core.command.usagemessage import CommandUsageMessage
from bot.action.extra.messages import analyzer
from bot.action.userinfo import UserStorageHandler
from bot.action.util.format import UserFormatter, DateFormatter
from bot.action.util.textformat import FormattedText
from bot.api.domain import Message, ApiObject

MIN_MESSAGES_TO_KEEP = 5000
MAX_MESSAGES_TO_KEEP = 15000


def is_store_messages_enabled(event):
    return event.settings.get(ChatSettings.STORE_MESSAGES) == "on"


class SaveMessageAction(Action):
    def process(self, event):
        if is_store_messages_enabled(event):
            storage_handler = MessageStorageHandler(event)
            storage_handler.save_message(event.message)
            storage_handler.delete_old_messages()


class ListMessageAction(Action):
    def process(self, event):
        action, action_param, help_args = self.parse_args(event.command_args.split())
        if action in ("recent", "show", "ranking"):
            if not is_store_messages_enabled(event):
                response = self.get_response_disabled()
            else:
                messages = MessageStorageHandler(event).get_stored_messages()
                if messages.is_empty():
                    response = self.get_response_empty()
                elif action == "recent":
                    response = self.get_response_recent(event, messages, action_param)
                elif action == "show":
                    response = self.get_response_show(event, messages, action_param)
                else:
                    response = self.get_response_ranking(event, messages, action_param)
        elif action == "whereis":
            response = self.get_response_whereis(event, action_param)
        elif action == "opt-out":
            response = self.get_response_opt_out(event, action_param)
        else:
            response = self.get_response_help(event, help_args)
        if type(response) is not list:
            response = [response]
        last_message = event.message
        for message in response:
            message.to_chat(message=last_message)
            if message.reply_to_message_id is None:
                message.reply_to_message(last_message)
            last_message = self.api.send_message(message)

    @staticmethod
    def parse_args(args):
        action = "help"
        action_param = 10
        help_args = args[1:]
        if len(args) == 0:
            action = "recent"
        elif len(args) == 1:
            if args[0].isnumeric():
                action = "show"
                action_param = int(args[0])
            elif args[0] not in ("show", "whereis"):
                action = args[0]
        elif len(args) == 2:
            if args[1].isnumeric() or args[0] == "opt-out":
                action_param = int(args[1]) if args[0] != "opt-out" else args[1]
                action = args[0]
        return action, action_param, help_args

    @staticmethod
    def get_response_help(event, help_args):
        args = ["[recent number_of_messages]",
                "from [message_id] [number_of_messages]",
                "[show] message_id",
                "ranking [number_of_users]",
                "whereis message_id",
                "opt-out [action]"]
        description = "_Have you ever wondered that, like in WhatsApp, Telegram messages could not be deleted or" \
                      " edited once sent?_\n" \
                      "Then this is for you! It allows you to recover deleted messages content, or original text"\
                      " of edited messages.\n\n" \
                      "There is a way to opt-out to avoid your messages being recovered (see at the end of this"\
                      " message). Keep in mind that group admins can still configure the group to override opt-out"\
                      " on that specific group (by setting `override_messages_opt_out` to `off` in `/settings`).\n\n" \
                      "Group admins can also disable completely this feature on the group. That will disable" \
                      " message storing and retrieving via this command. To do it, set `store_messages`" \
                      " to `off` using the `/settings` command.\n\n" \
                      "By default, this command displays a list with information about last messages.\n" \
                      "You can use *recent* with a number to modify the number of messages to list" \
                      " (default is 10).\n\n" \
                      "Use *from* to get a list of messages sent after a specific one.\n" \
                      "The message you want to use as a reference can be specified by its `message_id`, or by sending" \
                      " this command as a reply to the message you want to use as reference.\n" \
                      "You can also add the `number_of_messages` you want to be listed. If using this command by "\
                      "replying a message, this is the only parameter accepted.\n\n" \
                      "Use *show* along with a `message_id` to view that particular message.\n\n" \
                      "Use *ranking* to display a ranking of the users who wrote most recent messages" \
                      " (approximately last 1000 messages are counted).\n" \
                      "You can add a number to modify the number of top users to display (default is 10).\n\n" \
                      "Use *whereis* followed by a `message_id` to locate a particular message.\n\n" \
                      "Use *opt-out* followed by *add-me* or *remove-me* to be added or removed from the opt-out list" \
                      " of this feature. Use *get-status* or nothing to query your status on the list.\n" \
                      "While you are in the opt-out list, nobody but you can show the content of your messages using" \
                      " this feature, in any group."
        return CommandUsageMessage.get_usage_message(event.command, args, description)

    @staticmethod
    def get_response_disabled():
        return FormattedText().bold("Sorry, this feature is disabled on this chat. 😕").newline().newline()\
            .normal("Group admins can enable it using the following command:").newline()\
            .code_inline("/settings set store_messages on")\
            .build_message()

    @staticmethod
{'fraction_non_alphanumeric': 0.08580962974733831, 'fraction_numerical': 0.005243921817892897, 'mean_word_length': 2.0722479863314622, 'pattern_counts': {'":': 10, '<': 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': '1441209', 'n_tokens_mistral': 3352, 'n_tokens_neox': 3159, 'n_words': 1078}