<commit_before>"""
Contains the various formatting functions to convert reddit and GitHub objects into
Discord-friendly messages that can be relayed to the appropriate channels.
"""


import re
import logging

from images_of import settings

LOG = logging.getLogger(__name__)

# message type mapping
TYPES = {
    't1':'comment',
    't2':'account',
    't3':'link',
    't4':'message',
    't5':'subreddit',
    't6':'award',
    't8':'promocampaign'
}

ISSUE_ACTION_FILTER = ["opened", "closed", "reopened"]
PULL_REQUEST_ACTION_FILTER = ["opened", "edited", "closed", "reopened", "synchronize"]

#Regex pattern for identifying and stripping out markdown links
MD_LINK_PATTERN = r'(\[)([^\]()#\n]+)\]\(([^\]()#\n]+)\)'
MD_LINK_RE = re.compile(MD_LINK_PATTERN, flags=re.IGNORECASE)


def is_relayable_message(message):
    """
    Determines if an inbox message is a type of message that should or should not be relayed
    to Discord. Does not relay mod removal or remove replies, blacklist requests, or messages
    from AutoModerator/reddit itself.
    """
    #only matching remove exactly
    if (message.body == 'remove') or ('mod removal' in message.body):
        # Don't announce 'remove' or 'mod removal' replies
        message.mark_as_read()
        LOG.info('[Inbox] Not announcing message type: "remove"/"mod removal"')
        return False

    elif message.subject.lower() == 'please blacklist me':
        # Don't announce blacklist requests
        message.mark_as_read()
        LOG.info('[Inbox] Not announcing message type: "blacklist request"')
        return False

    elif (message.author.name == 'AutoModerator') or (message.author.name == 'reddit'):
        # Don't announce AutoModerator or reddit messages
        message.mark_as_read()
        LOG.info('[Inbox] Not announcing message type: "AutoMod Response"')
        return False

    else:
        return True

##------------------------------------

def format_inbox_message(message):
    """
    Formats a reddit Inboxable message to one that can be relayed to Discord
    """
    msg_body = message.body[:1500]
    if len(msg_body) == 1500:
        msg_body += '...'

    msg_body = re.sub('\n\n', '\n', msg_body)

    #Strip markdown hyperlinks, append to bottom
    msg_links = MD_LINK_RE.findall(msg_body)

    if msg_links:
        msg_body = MD_LINK_RE.sub(r'\g<2>', msg_body)
        msg_body += '\r\n __*Links:*__\n'
        i = 0
        for msg in msg_links:
            i += 1
            msg_body += '{}: {}\n'.format(i, msg[2])

    msg_type = TYPES[message.name[:2]]
    if msg_type == 'comment':
        if message.is_root:
            msg_type = 'post comment'
        else:
            msg_type = 'comment reply'

    notification = ("New __{}__ from **/u/{}**: \n```\n{}\n```".format(
        msg_type, message.author.name, msg_body))

    #Add permalink for comment replies
    if message.name[:2] == 't1':
        notification += ('\n**Permalink:** {}?context=10\r\n '.format(message.permalink))

    return notification

##------------------------------------

def format_github_issue_comment(event):
    """
    Formats a GitHub IssueComment event into a relayable Discord message
    """
    action = event.payload['action']
    if action == 'created':
        url = event.payload['issue'].html_url
        user = event.payload['comment'].user.login
        comment = event.payload['comment'].body[:1500]
        title = event.payload['issue'].title

        desc = 'GitHub Comment by **{}** on Issue `{}`:'.format(user, title)
        desc += '\n```\n{}\n```\n'.format(comment)
        desc += '**Link**: {}\r\n '.format(url)

        return desc

##------------------------------------

def format_github_push_event(event):
    """Takes a GitHub PushEvent and returns a markdown-formatted message
    that can be relayed to the Discord channel."""

    push_message = 'New Push to branch `{}` by **{}**:\r\n'.format(
        event.payload['ref'].replace('refs/heads/', ''), event.actor.login)

    for com in event.payload['commits']:
        desc = '\nCommit `{}` by `{}`:\n'.format(com['sha'][:7], com['author']['name']) \
             + '```\n{}```\n'.format(com['message']) \
             + 'https://github.com/amici-ursi/ImagesOfNetwork/commit/{}'.format(com['sha'])

        push_message += desc

    push_message += '\r\n---'
    return push_message




##------------------------------------

def format_github_issue_event(event):
    """Takes a GitHub IssuesEvent and returns a markdown-formatted message
    that can be relayed to the Discord channel."""

    action = event.payload['action']
    if action in ISSUE_ACTION_FILTER:

        title = event.payload['issue'].title
        user = event.actor.login
        url = event.payload['issue'].html_url

        if action in ["unlabeled", "labeled"]:
            label = event.payload['label']
            desc = '**{}** __{}__ **{}** on  issue `{}`'.format(user, action, label, title)

        elif action in ["unassigned", "assigned"]:
            assignee = event.payload['assignee']<commit_msg>discobot: More GitHubIssuesEvent tweaks<commit_after>"""
Contains the various formatting functions to convert reddit and GitHub objects into
Discord-friendly messages that can be relayed to the appropriate channels.
"""


import re
import logging

from images_of import settings

LOG = logging.getLogger(__name__)

# message type mapping
TYPES = {
    't1':'comment',
    't2':'account',
    't3':'link',
    't4':'message',
    't5':'subreddit',
    't6':'award',
    't8':'promocampaign'
}

ISSUE_ACTION_FILTER = ["opened", "closed", "reopened"]
PULL_REQUEST_ACTION_FILTER = ["opened", "edited", "closed", "reopened", "synchronize"]

#Regex pattern for identifying and stripping out markdown links
MD_LINK_PATTERN = r'(\[)([^\]()#\n]+)\]\(([^\]()#\n]+)\)'
MD_LINK_RE = re.compile(MD_LINK_PATTERN, flags=re.IGNORECASE)


def is_relayable_message(message):
    """
    Determines if an inbox message is a type of message that should or should not be relayed
    to Discord. Does not relay mod removal or remove replies, blacklist requests, or messages
    from AutoModerator/reddit itself.
    """
    #only matching remove exactly
    if (message.body == 'remove') or ('mod removal' in message.body):
        # Don't announce 'remove' or 'mod removal' replies
        message.mark_as_read()
        LOG.info('[Inbox] Not announcing message type: "remove"/"mod removal"')
        return False

    elif message.subject.lower() == 'please blacklist me':
        # Don't announce blacklist requests
        message.mark_as_read()
        LOG.info('[Inbox] Not announcing message type: "blacklist request"')
        return False

    elif (message.author.name == 'AutoModerator') or (message.author.name == 'reddit'):
        # Don't announce AutoModerator or reddit messages
        message.mark_as_read()
        LOG.info('[Inbox] Not announcing message type: "AutoMod Response"')
        return False

    else:
        return True

##------------------------------------

def format_inbox_message(message):
    """
    Formats a reddit Inboxable message to one that can be relayed to Discord
    """
    msg_body = message.body[:1500]
    if len(msg_body) == 1500:
        msg_body += '...'

    msg_body = re.sub('\n\n', '\n', msg_body)

    #Strip markdown hyperlinks, append to bottom
    msg_links = MD_LINK_RE.findall(msg_body)

    if msg_links:
        msg_body = MD_LINK_RE.sub(r'\g<2>', msg_body)
        msg_body += '\r\n __*Links:*__\n'
        i = 0
        for msg in msg_links:
            i += 1
            msg_body += '{}: {}\n'.format(i, msg[2])

    msg_type = TYPES[message.name[:2]]
    if msg_type == 'comment':
        if message.is_root:
            msg_type = 'post comment'
        else:
            msg_type = 'comment reply'

    notification = ("New __{}__ from **/u/{}**: \n```\n{}\n```".format(
        msg_type, message.author.name, msg_body))

    #Add permalink for comment replies
    if message.name[:2] == 't1':
        notification += ('\n**Permalink:** {}?context=10\r\n '.format(message.permalink))

    return notification

##------------------------------------

def format_github_issue_comment(event):
    """
    Formats a GitHub IssueComment event into a relayable Discord message
    """
    action = event.payload['action']
    if action == 'created':
        url = event.payload['issue'].html_url
        user = event.payload['comment'].user.login
        comment = event.payload['comment'].body[:1500]
        title = event.payload['issue'].title

        desc = 'GitHub Comment by **{}** on Issue `{}`:'.format(user, title)
        desc += '\n```\n{}\n```\n'.format(comment)
        desc += '**Link**: {}\r\n '.format(url)

        return desc

##------------------------------------

def format_github_push_event(event):
    """Takes a GitHub PushEvent and returns a markdown-formatted message
    that can be relayed to the Discord channel."""

    push_message = 'New Push to branch `{}` by **{}**:\r\n'.format(
        event.payload['ref'].replace('refs/heads/', ''), event.actor.login)

    for com in event.payload['commits']:
        desc = '\nCommit `{}` by `{}`:\n'.format(com['sha'][:7], com['author']['name']) \
             + '```\n{}```\n'.format(com['message']) \
             + 'https://github.com/amici-ursi/ImagesOfNetwork/commit/{}'.format(com['sha'])

        push_message += desc

    push_message += '\r\n---'
    return push_message




##------------------------------------

def format_github_issue_event(event):
    """Takes a GitHub IssuesEvent and returns a markdown-formatted message
    that can be relayed to the Discord channel."""

    action = event.payload['action']
    if action in ISSUE_ACTION_FILTER:

        title = event.payload['issue'].title
        user = event.actor.login
        url = event.payload['issue'].html_url

        if (action == "unlabeled") or (action == "labeled"):
            label = event.payload['label']
            desc = '**{}** __{}__ **{}** on  issue `{}`'.format(user, action, label, title)

        elif (action == "unassigned") or (action == "assigned"):
            assignee = event.payload['assignee']
{'fraction_non_alphanumeric': 0.17777995685428516, 'fraction_numerical': 0.005687389684251814, 'mean_word_length': 2.6425, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '3689817', 'n_tokens_mistral': 3211, 'n_tokens_neox': 3034, 'n_words': 810}