Class: Teems::Models::Chat

Inherits:
Data
  • Object
show all
Defined in:
lib/teems/models/chat.rb

Overview

Represents a chat (1:1, group, or meeting chat)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#chat_typeObject (readonly)

Returns the value of attribute chat_type

Returns:

  • (Object)

    the current value of chat_type



11
12
13
# File 'lib/teems/models/chat.rb', line 11

def chat_type
  @chat_type
end

#created_atObject (readonly)

Returns the value of attribute created_at

Returns:

  • (Object)

    the current value of created_at



11
12
13
# File 'lib/teems/models/chat.rb', line 11

def created_at
  @created_at
end

#favoriteObject (readonly)

Returns the value of attribute favorite

Returns:

  • (Object)

    the current value of favorite



11
12
13
# File 'lib/teems/models/chat.rb', line 11

def favorite
  @favorite
end

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



11
12
13
# File 'lib/teems/models/chat.rb', line 11

def id
  @id
end

#last_updatedObject (readonly)

Returns the value of attribute last_updated

Returns:

  • (Object)

    the current value of last_updated



11
12
13
# File 'lib/teems/models/chat.rb', line 11

def last_updated
  @last_updated
end

#pinnedObject (readonly)

Returns the value of attribute pinned

Returns:

  • (Object)

    the current value of pinned



11
12
13
# File 'lib/teems/models/chat.rb', line 11

def pinned
  @pinned
end

#topicObject (readonly)

Returns the value of attribute topic

Returns:

  • (Object)

    the current value of topic



11
12
13
# File 'lib/teems/models/chat.rb', line 11

def topic
  @topic
end

#unreadObject (readonly)

Returns the value of attribute unread

Returns:

  • (Object)

    the current value of 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, last_message)
  horizon = props['consumptionhorizon']
  last_msg_id = last_message&.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

Returns:

  • (Boolean)


98
99
100
# File 'lib/teems/models/chat.rb', line 98

def channel?
  chat_type == 'channel'
end

#chat_type_labelObject



80
# File 'lib/teems/models/chat.rb', line 80

def chat_type_label = CHAT_TYPE_LABELS.fetch(chat_type, chat_type)

#display_nameObject



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

Returns:

  • (Boolean)


83
# File 'lib/teems/models/chat.rb', line 83

def favorite? = favorite

#group?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/teems/models/chat.rb', line 90

def group?
  chat_type == 'group'
end

#meeting?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/teems/models/chat.rb', line 94

def meeting?
  chat_type == 'meeting'
end

#one_on_one?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/teems/models/chat.rb', line 86

def one_on_one?
  chat_type == 'oneOnOne'
end

#pinned?Boolean

Returns:

  • (Boolean)


84
# File 'lib/teems/models/chat.rb', line 84

def pinned? = pinned

#space?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/teems/models/chat.rb', line 102

def space?
  chat_type == 'space'
end

#to_sObject



106
107
108
# File 'lib/teems/models/chat.rb', line 106

def to_s
  display_name
end

#unread?Boolean

Returns:

  • (Boolean)


82
# File 'lib/teems/models/chat.rb', line 82

def unread? = unread