<commit_before>require 'chatrix/event_processor'
require 'chatrix/permissions'
require 'chatrix/message'

require 'logger'
require 'set'
require 'wisper'

module Chatrix
  # Provides functionality for interacting with a room.
  class Room < EventProcessor
    include Wisper::Publisher

    # @!attribute [r] id
    #   @return [String] The ID of this room.
    # @!attribute [r] alias
    #   @return [String,nil] This room's canonical alias, or `nil` if none
    #     has been set.
    # @!attribute [r] name
    #   @return [String,nil] The name of this room, or `nil` if none has
    #     been set.
    # @!attribute [r] topic
    #   @return [String,nil] The topic of this room, or `nil` if none has
    #     been set.
    # @!attribute [r] creator
    #   @return [User] The user who created this room.
    # @!attribute [r] guest_access
    #   @return [Boolean] `true` if guests are allowed in this room,
    #     otherwise `false`.
    # @!attribute [r] history_visibility
    #   @return [String] This room's history visibility.
    # @!attribute [r] join_rule
    #   @return [String] Join rules for this room.
    attr_reader :id, :alias, :name, :topic, :creator, :guest_access,
                :history_visibility, :join_rule

    # Initializes a new Room instance.
    #
    # @param id [String] The room ID.
    # @param users [Users] The User manager.
    # @param matrix [Matrix] The Matrix API instance.
    def initialize(id, users, matrix)
      super()

      @id = id
      @aliases = []
      @users = users
      @matrix = matrix
      @members = Set.new

      @permissions = Permissions.new self
    end

    # Sends a message to this channel.
    #
    # @param message [String] The message to send.
    # @return [String] Event ID for the send action.
    def send_message(message)
      @matrix.send_message @id, message
    end

    # Sends a notice to this channel.
    #
    # @param message [String] The notice to send.
    # @return (see #send_message)
    def send_notice(message)
      @matrix.send_notice @id, message
    end

    # Sends an emote to this channel.
    #
    # @param message [String] The emote text to send.
    # @return (see #send_message)
    def send_emote(message)
      @matrix.send_emote @id, message
    end

    def can?(user, action)
      @permissions.can? user, action
    end

    def can_set?(user, event)
      @permissions.can_set? user, event
    end

    def process_join(data)
      process_state data['state'] if data.key? 'state'
      process_timeline data['timeline'] if data.key? 'timeline'
    end

    def process_invite(data)
    end

    def process_leave(data)
    end

    def to_s
      return @name if @name
      return @alias if @alias
      @id
    end

    private

    def process_state(data)
      return unless data.key? 'events'
      data['events'].each { |e| process_state_event e }
    end

    def process_timeline(data)
      return unless data.key? 'events'
      data['events'].each { |e| process_timeline_event e }
    end

    def process_state_event(event)
      return if processed? event

      broadcast(:state, self, event)

      case event['type']
      when 'm.room.create'
        @creator = event['content']['creator']
        broadcast(:creator, self, @creator)
      when 'm.room.member'
        process_member_event event
      when 'm.room.canonical_alias'
        @alias = event['content']['alias']
        broadcast(:alias, self, @alias)
      when 'm.room.aliases'
        @aliases.replace event['content']['aliases']
        broadcast(:aliases, self, @aliases.dup)
      when 'm.room.name'
        @name = event['content']['name']
        broadcast(:name, self, @name)
      when 'm.room.topic'
        @topic = event['content']['topic']
        broadcast(:topic, self, @topic)
      when 'm.room.power_levels'
        process_power_levels_event event
      when 'm.room.guest_access'
        @guest_access = event['content']['guest_access'] == 'can_join'<commit_msg>Make User.creator an actual User instance<commit_after>require 'chatrix/event_processor'
require 'chatrix/permissions'
require 'chatrix/message'

require 'logger'
require 'set'
require 'wisper'

module Chatrix
  # Provides functionality for interacting with a room.
  class Room < EventProcessor
    include Wisper::Publisher

    # @!attribute [r] id
    #   @return [String] The ID of this room.
    # @!attribute [r] alias
    #   @return [String,nil] This room's canonical alias, or `nil` if none
    #     has been set.
    # @!attribute [r] name
    #   @return [String,nil] The name of this room, or `nil` if none has
    #     been set.
    # @!attribute [r] topic
    #   @return [String,nil] The topic of this room, or `nil` if none has
    #     been set.
    # @!attribute [r] creator
    #   @return [User] The user who created this room.
    # @!attribute [r] guest_access
    #   @return [Boolean] `true` if guests are allowed in this room,
    #     otherwise `false`.
    # @!attribute [r] history_visibility
    #   @return [String] This room's history visibility.
    # @!attribute [r] join_rule
    #   @return [String] Join rules for this room.
    attr_reader :id, :alias, :name, :topic, :creator, :guest_access,
                :history_visibility, :join_rule

    # Initializes a new Room instance.
    #
    # @param id [String] The room ID.
    # @param users [Users] The User manager.
    # @param matrix [Matrix] The Matrix API instance.
    def initialize(id, users, matrix)
      super()

      @id = id
      @aliases = []
      @users = users
      @matrix = matrix
      @members = Set.new

      @permissions = Permissions.new self
    end

    # Sends a message to this channel.
    #
    # @param message [String] The message to send.
    # @return [String] Event ID for the send action.
    def send_message(message)
      @matrix.send_message @id, message
    end

    # Sends a notice to this channel.
    #
    # @param message [String] The notice to send.
    # @return (see #send_message)
    def send_notice(message)
      @matrix.send_notice @id, message
    end

    # Sends an emote to this channel.
    #
    # @param message [String] The emote text to send.
    # @return (see #send_message)
    def send_emote(message)
      @matrix.send_emote @id, message
    end

    def can?(user, action)
      @permissions.can? user, action
    end

    def can_set?(user, event)
      @permissions.can_set? user, event
    end

    def process_join(data)
      process_state data['state'] if data.key? 'state'
      process_timeline data['timeline'] if data.key? 'timeline'
    end

    def process_invite(data)
    end

    def process_leave(data)
    end

    def to_s
      return @name if @name
      return @alias if @alias
      @id
    end

    private

    def process_state(data)
      return unless data.key? 'events'
      data['events'].each { |e| process_state_event e }
    end

    def process_timeline(data)
      return unless data.key? 'events'
      data['events'].each { |e| process_timeline_event e }
    end

    def process_state_event(event)
      return if processed? event

      broadcast(:state, self, event)

      case event['type']
      when 'm.room.create'
        @creator = @users.send(:get_user, event['content']['creator'])
        broadcast(:creator, self, @creator)
      when 'm.room.member'
        process_member_event event
      when 'm.room.canonical_alias'
        @alias = event['content']['alias']
        broadcast(:alias, self, @alias)
      when 'm.room.aliases'
        @aliases.replace event['content']['aliases']
        broadcast(:aliases, self, @aliases.dup)
      when 'm.room.name'
        @name = event['content']['name']
        broadcast(:name, self, @name)
      when 'm.room.topic'
        @topic = event['content']['topic']
        broadcast(:topic, self, @topic)
      when 'm.room.power_levels'
        process_power_levels_event event
      when 'm.room.guest_access'
        @guest_access = event['content']['guest_access'] == 'can_join'
{'fraction_non_alphanumeric': 0.1128670980587357, 'fraction_numerical': 0.0, 'mean_word_length': 2.576769025367156, 'pattern_counts': {'":': 0, '<': 5, '<?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': '467550', 'n_tokens_mistral': 2536, 'n_tokens_neox': 2491, 'n_words': 767}