Class: Teems::Models::Chat
- Inherits:
-
Data
- Object
- Data
- Teems::Models::Chat
- Defined in:
- lib/teems/models/chat.rb
Overview
Represents a chat (1:1, group, or meeting chat)
Instance Attribute Summary collapse
-
#chat_type ⇒ Object
readonly
Returns the value of attribute chat_type.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#favorite ⇒ Object
readonly
Returns the value of attribute favorite.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#last_updated ⇒ Object
readonly
Returns the value of attribute last_updated.
-
#pinned ⇒ Object
readonly
Returns the value of attribute pinned.
-
#topic ⇒ Object
readonly
Returns the value of attribute topic.
-
#unread ⇒ Object
readonly
Returns the value of attribute unread.
Class Method Summary collapse
- .from_api(data) ⇒ Object
- .from_graph(data) ⇒ Object
- .from_ngmsg(data) ⇒ Object
- .ngmsg_status(props, last_message) ⇒ Object
-
.normalize_chat_type(type) ⇒ Object
Normalize ng.msg threadType to a consistent chat type.
- .parse_time(time_str) ⇒ Object
Instance Method Summary collapse
- #channel? ⇒ Boolean
- #chat_type_label ⇒ Object
- #display_name ⇒ Object
- #favorite? ⇒ Boolean
- #group? ⇒ Boolean
- #meeting? ⇒ Boolean
- #one_on_one? ⇒ Boolean
- #pinned? ⇒ Boolean
- #space? ⇒ Boolean
- #to_s ⇒ Object
- #unread? ⇒ Boolean
Instance Attribute Details
#chat_type ⇒ Object (readonly)
Returns the value of attribute chat_type
11 12 13 |
# File 'lib/teems/models/chat.rb', line 11 def chat_type @chat_type end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at
11 12 13 |
# File 'lib/teems/models/chat.rb', line 11 def created_at @created_at end |
#favorite ⇒ Object (readonly)
Returns the value of attribute favorite
11 12 13 |
# File 'lib/teems/models/chat.rb', line 11 def favorite @favorite end |
#id ⇒ Object (readonly)
Returns the value of attribute id
11 12 13 |
# File 'lib/teems/models/chat.rb', line 11 def id @id end |
#last_updated ⇒ Object (readonly)
Returns the value of attribute last_updated
11 12 13 |
# File 'lib/teems/models/chat.rb', line 11 def last_updated @last_updated end |
#pinned ⇒ Object (readonly)
Returns the value of attribute pinned
11 12 13 |
# File 'lib/teems/models/chat.rb', line 11 def pinned @pinned end |
#topic ⇒ Object (readonly)
Returns the value of attribute topic
11 12 13 |
# File 'lib/teems/models/chat.rb', line 11 def topic @topic end |
#unread ⇒ Object (readonly)
Returns the value of attribute unread
11 12 13 |
# File 'lib/teems/models/chat.rb', line 11 def unread @unread end |
Class Method Details
.from_api(data) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/teems/models/chat.rb', line 12 def self.from_api(data) # Handle both Graph API format and ng.msg format if data['threadProperties'] from_ngmsg(data) else from_graph(data) end end |
.from_graph(data) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/teems/models/chat.rb', line 21 def self.from_graph(data) new( id: data['id'], topic: data['topic'], chat_type: data['chatType'], created_at: parse_time(data['createdDateTime']), last_updated: parse_time(data['lastUpdatedDateTime']), unread: false, favorite: false, pinned: false ) end |
.from_ngmsg(data) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/teems/models/chat.rb', line 32 def self.from_ngmsg(data) thread_props = data['threadProperties'] || {} props = data['properties'] || {} new( id: data['id'], topic: thread_props['topic'] || thread_props['spaceThreadTopic'], chat_type: normalize_chat_type(thread_props['threadType']), created_at: parse_time(thread_props['createdat']), last_updated: parse_time(props['lastimreceivedtime']), **ngmsg_status(props, data['lastMessage']) ) end |
.ngmsg_status(props, last_message) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/teems/models/chat.rb', line 45 def self.ngmsg_status(props, ) horizon = props['consumptionhorizon'] last_msg_id = &.dig('id') { unread: horizon && last_msg_id ? horizon.split(';').first.to_i < last_msg_id.to_i : false, favorite: props['favorite'] == 'true', pinned: props['ispinned'] == 'true' } end |
.normalize_chat_type(type) ⇒ Object
Normalize ng.msg threadType to a consistent chat type. ng.msg uses: ‘chat’ (group), ‘meeting’, ‘topic’ (channel), ‘space’ Normalized to: ‘group’, ‘meeting’, ‘channel’, ‘space’
58 59 60 61 62 63 64 65 66 |
# File 'lib/teems/models/chat.rb', line 58 def self.normalize_chat_type(type) case type&.downcase when 'chat' then 'group' when 'meeting' then 'meeting' when 'topic' then 'channel' when 'space' then 'space' else type end end |
.parse_time(time_str) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/teems/models/chat.rb', line 68 def self.parse_time(time_str) return nil unless time_str Time.parse(time_str) rescue ArgumentError nil end |
Instance Method Details
#channel? ⇒ Boolean
98 99 100 |
# File 'lib/teems/models/chat.rb', line 98 def channel? chat_type == 'channel' end |
#chat_type_label ⇒ Object
80 |
# File 'lib/teems/models/chat.rb', line 80 def chat_type_label = CHAT_TYPE_LABELS.fetch(chat_type, chat_type) |
#display_name ⇒ Object
76 77 78 |
# File 'lib/teems/models/chat.rb', line 76 def display_name topic.to_s.empty? ? chat_type_label : topic end |
#favorite? ⇒ Boolean
83 |
# File 'lib/teems/models/chat.rb', line 83 def favorite? = favorite |
#group? ⇒ Boolean
90 91 92 |
# File 'lib/teems/models/chat.rb', line 90 def group? chat_type == 'group' end |
#meeting? ⇒ Boolean
94 95 96 |
# File 'lib/teems/models/chat.rb', line 94 def meeting? chat_type == 'meeting' end |
#one_on_one? ⇒ Boolean
86 87 88 |
# File 'lib/teems/models/chat.rb', line 86 def one_on_one? chat_type == 'oneOnOne' end |
#pinned? ⇒ Boolean
84 |
# File 'lib/teems/models/chat.rb', line 84 def pinned? = pinned |
#space? ⇒ Boolean
102 103 104 |
# File 'lib/teems/models/chat.rb', line 102 def space? chat_type == 'space' end |
#to_s ⇒ Object
106 107 108 |
# File 'lib/teems/models/chat.rb', line 106 def to_s display_name end |
#unread? ⇒ Boolean
82 |
# File 'lib/teems/models/chat.rb', line 82 def unread? = unread |