Class: Chatrix::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/chatrix/message.rb

Overview

Describes a message sent in a room.

Constant Summary collapse

TYPES =

Supported message types.

:html is a special message type parsed in #parse_body!.

{
  'm.text' => :text,
  'm.emote' => :emote,
  'm.notice' => :notice
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sender, timestamp, content) ⇒ Message

Initializes a new Message instance.

Parameters:

  • sender (User)

    The user who sent the message.

  • timestamp (Integer)

    The timestamp of the message.

  • content (Hash)

    The message content.



40
41
42
43
44
45
46
47
48
49
# File 'lib/chatrix/message.rb', line 40

def initialize(sender, timestamp, content)
  @raw = content

  @type = TYPES[@raw['msgtype']]
  @body = @raw['body']
  @sender = sender
  @timestamp = timestamp

  parse_body!
end

Instance Attribute Details

#bodyString (readonly)

Returns The text content of the message. If the message is of :html type, this will contain HTML format. To get the raw message text, use the 'body' field of the #raw hash.

Returns:

  • (String)

    The text content of the message. If the message is of :html type, this will contain HTML format. To get the raw message text, use the 'body' field of the #raw hash.



29
30
31
# File 'lib/chatrix/message.rb', line 29

def body
  @body
end

#rawHash (readonly)

Returns The raw message data (the content field).

Returns:

  • (Hash)

    The raw message data (the content field).



17
18
19
# File 'lib/chatrix/message.rb', line 17

def raw
  @raw
end

#senderUser (readonly)

Returns The user who sent this message.

Returns:

  • (User)

    The user who sent this message.



24
25
26
# File 'lib/chatrix/message.rb', line 24

def sender
  @sender
end

#timestampInteger (readonly)

Returns The timestamp of the message, indicating when it was sent, according to the origin server.

Returns:

  • (Integer)

    The timestamp of the message, indicating when it was sent, according to the origin server.



33
34
35
# File 'lib/chatrix/message.rb', line 33

def timestamp
  @timestamp
end

#typeSymbol? (readonly)

Returns The type of message. Will be nil if the type failed to parse.

Returns:

  • (Symbol, nil)

    The type of message. Will be nil if the type failed to parse.



21
22
23
# File 'lib/chatrix/message.rb', line 21

def type
  @type
end

Instance Method Details

#parse_body!Object (private)

Parses the message content to see if there's any special formatting available.



55
56
57
58
59
60
61
62
# File 'lib/chatrix/message.rb', line 55

def parse_body!
  return unless @raw.key? 'format'
  case @raw['format']
  when 'org.matrix.custom.html'
    @type = :html
    @formatted = @raw['formatted_body']
  end
end