<commit_before>require 'xmpp4r'
require 'xmpp4r/client'
require 'forwardable'
require 'singleton'
require 'ap'

class Bot
    include Jabber
    include Singleton

    attr_reader :client

    class << self
        extend Forwardable
        def_delegators :instance, :deliver, :ping
    end

    def ping
        # pong
    end

    def jid_user fulljid
        jid = Jabber::JID.new(fulljid)
        # TODO: check if multiple users have the same jid
        XmppNotificationsUserSetting.where(jid: jid.strip.to_s).first.user
    end

    ###################################
    # COMMENT
    ###################################
    def add_comment original_message, issue, comment
        # jid = Jabber::JID.new(original_message.from)
        # # TODO: check if multiple users have the same jid
        # user = XmppNotificationsUserSetting.where(jid: jid.strip.to_s).first.user
        user = jid_user original_message.from
        iss = Issue.find(issue)


        if user.allowed_to?(:edit_issues, iss.project)
	    journal = iss.init_journal(user, comment)
            journal.save
            deliver original_message.from, "Added new comment to issue: " + iss.subject
        else
            deliver original_message.from, "Sorry! You've no rights to comment that issue"
        end
    rescue ActiveRecord::RecordNotFound => e
        deliver original_message.from, "ERROR: unknown issue"
    end

    ###################################
    # SET STATE
    ###################################
    def set_state original_message, issue, state
        iss = Issue.find(issue)

        deliver original_message.from, "ERROR: Not implemented yet"
    rescue ActiveRecord::RecordNotFound => e
        deliver original_message.from, "ERROR: unknown issue"
    end

    def select_matching klass, value, match_fields, equal_fields
        t = klass.arel_table
        preql = t[:id].eq(0)
        match_fields.each do |field|
            preql = preql.or(t[field].matches("%#{value}%"))
        end
        equal_fields.each do |field|
            preql = preql.or(t[field].eq(value))
        end
        klass.where(preql)
    end
    ###################################
    # CREATE NEW ISSUE
    ###################################
    def create_new_issue original_message, message
        # iss = Issue.find(issue)
        rmentions = %r{(?'project'\+(?:\w+|"[\w\s]+"))|(?'assigned'\!\w+)|(?'watcher'\@\w+)}
        user = jid_user original_message.from

        mentions = Hash.new([].freeze)
        match_all(message, rmentions).each do |match|
            match.names.each do |name|
                mentions[name] += [match[name]] unless match[name].nil?
            end
        end

        subject = message.gsub(rmentions, '').strip

        ##### GETTING PROJECT
        if mentions["project"].length.zero?
            deliver original_message.from, "ERROR: No project specified!"
            return
        end

        project = mentions["project"][0].gsub(/(^[\+"]*|["]*$)/, '')<commit_msg>expanded symbol list used for project id (added "-" and "_")<commit_after>require 'xmpp4r'
require 'xmpp4r/client'
require 'forwardable'
require 'singleton'
require 'ap'

class Bot
    include Jabber
    include Singleton

    attr_reader :client

    class << self
        extend Forwardable
        def_delegators :instance, :deliver, :ping
    end

    def ping
        # pong
    end

    def jid_user fulljid
        jid = Jabber::JID.new(fulljid)
        # TODO: check if multiple users have the same jid
        XmppNotificationsUserSetting.where(jid: jid.strip.to_s).first.user
    end

    ###################################
    # COMMENT
    ###################################
    def add_comment original_message, issue, comment
        # jid = Jabber::JID.new(original_message.from)
        # # TODO: check if multiple users have the same jid
        # user = XmppNotificationsUserSetting.where(jid: jid.strip.to_s).first.user
        user = jid_user original_message.from
        iss = Issue.find(issue)


        if user.allowed_to?(:edit_issues, iss.project)
	    journal = iss.init_journal(user, comment)
            journal.save
            deliver original_message.from, "Added new comment to issue: " + iss.subject
        else
            deliver original_message.from, "Sorry! You've no rights to comment that issue"
        end
    rescue ActiveRecord::RecordNotFound => e
        deliver original_message.from, "ERROR: unknown issue"
    end

    ###################################
    # SET STATE
    ###################################
    def set_state original_message, issue, state
        iss = Issue.find(issue)

        deliver original_message.from, "ERROR: Not implemented yet"
    rescue ActiveRecord::RecordNotFound => e
        deliver original_message.from, "ERROR: unknown issue"
    end

    def select_matching klass, value, match_fields, equal_fields
        t = klass.arel_table
        preql = t[:id].eq(0)
        match_fields.each do |field|
            preql = preql.or(t[field].matches("%#{value}%"))
        end
        equal_fields.each do |field|
            preql = preql.or(t[field].eq(value))
        end
        klass.where(preql)
    end
    ###################################
    # CREATE NEW ISSUE
    ###################################
    def create_new_issue original_message, message
        # iss = Issue.find(issue)
        rmentions = %r{(?'project'\+(?:[\w-_]+|"[\w\-_\s]+"))|(?'assigned'\!\w+)|(?'watcher'\@\w+)}
        user = jid_user original_message.from

        mentions = Hash.new([].freeze)
        match_all(message, rmentions).each do |match|
            match.names.each do |name|
                mentions[name] += [match[name]] unless match[name].nil?
            end
        end

        subject = message.gsub(rmentions, '').strip

        ##### GETTING PROJECT
        if mentions["project"].length.zero?
            deliver original_message.from, "ERROR: No project specified!"
            return
        end

        project = mentions["project"][0].gsub(/(^[\+"]*|["]*$)/, '')
{'fraction_non_alphanumeric': 0.17314022383146807, 'fraction_numerical': 0.0013166556945358788, 'mean_word_length': 2.568408690546095, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6669251', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1746, 'n_words': 415}