Class: Teams::Activity

Inherits:
Object
  • Object
show all
Defined in:
lib/teams/activity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw = {}) ⇒ Activity

Returns a new instance of Activity.



7
8
9
# File 'lib/teams/activity.rb', line 7

def initialize(raw = {})
  @raw = normalize_hash(raw)
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



5
6
7
# File 'lib/teams/activity.rb', line 5

def raw
  @raw
end

Instance Method Details

#channel_dataObject



47
48
49
# File 'lib/teams/activity.rb', line 47

def channel_data
  Api::ChannelData.new(raw["channelData"] || raw["channel_data"] || {})
end

#channel_idObject



35
36
37
# File 'lib/teams/activity.rb', line 35

def channel_id
  raw["channelId"] || raw["channel_id"]
end

#conversationObject



59
60
61
# File 'lib/teams/activity.rb', line 59

def conversation
  Api::ConversationAccount.new(raw["conversation"] || {})
end

#entitiesObject



68
69
70
# File 'lib/teams/activity.rb', line 68

def entities
  Array(raw["entities"])
end

#fromObject



51
52
53
# File 'lib/teams/activity.rb', line 51

def from
  Api::Account.new(raw["from"] || {})
end

#get_account_mention(account_id) ⇒ Object



82
83
84
# File 'lib/teams/activity.rb', line 82

def ()
  get_mentions.find { |mention| mention.dig("mentioned", "id") ==  }
end

#get_mentionsObject



78
79
80
# File 'lib/teams/activity.rb', line 78

def get_mentions
  entities.select { |entity| entity.is_a?(Hash) && entity["type"] == "mention" }
end

#get_quoted_messagesObject



72
73
74
75
76
# File 'lib/teams/activity.rb', line 72

def get_quoted_messages
  entities
    .select { |entity| entity.is_a?(Hash) && entity["type"] == "quotedReply" }
    .map { |entity| Api::QuotedReplyEntity.new(entity) }
end

#idObject



23
24
25
# File 'lib/teams/activity.rb', line 23

def id
  raw["id"]
end

#install_update?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/teams/activity.rb', line 131

def install_update?
  type == "installationUpdate"
end

#invoke?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/teams/activity.rb', line 127

def invoke?
  type == "invoke"
end

#local_timestampObject



43
44
45
# File 'lib/teams/activity.rb', line 43

def local_timestamp
  raw["localTimestamp"] || raw["local_timestamp"]
end

#localeObject



39
40
41
# File 'lib/teams/activity.rb', line 39

def locale
  raw["locale"]
end

#message?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/teams/activity.rb', line 115

def message?
  type == "message"
end

#message_update?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/teams/activity.rb', line 119

def message_update?
  type == "messageUpdate"
end

#nameObject



15
16
17
# File 'lib/teams/activity.rb', line 15

def name
  raw["name"]
end

#recipientObject



55
56
57
# File 'lib/teams/activity.rb', line 55

def recipient
  Api::Account.new(raw["recipient"] || {})
end

#recipient_mentioned?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/teams/activity.rb', line 86

def recipient_mentioned?
  !(recipient.id).nil?
end

#reply_to_idObject



27
28
29
# File 'lib/teams/activity.rb', line 27

def reply_to_id
  raw["replyToId"] || raw["reply_to_id"]
end

#service_urlObject



31
32
33
# File 'lib/teams/activity.rb', line 31

def service_url
  raw["serviceUrl"] || raw["service_url"]
end

#strip_mentions_text(account_id: nil, tag_only: false) ⇒ Object

Returns the text with "..." mention tokens removed, without mutating the raw activity. With tag_only, only the tags are removed and the mention names are kept.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/teams/activity.rb', line 93

def strip_mentions_text(account_id: nil, tag_only: false)
  return nil unless text

  get_mentions.reduce(text) do |result, mention|
    next result if  && mention.dig("mentioned", "id") != 

    mention_text = mention["text"].to_s
    if !mention_text.empty?
      without_tags = mention_text.gsub("<at>", "").gsub("</at>", "")
      result.sub(mention_text, tag_only ? without_tags : "")
    elsif (name = mention.dig("mentioned", "name"))
      result.sub("<at>#{name}</at>", tag_only ? name : "")
    else
      result
    end
  end.strip
end

#suggested_action_submit?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/teams/activity.rb', line 135

def suggested_action_submit?
  type == "invoke" && name == "suggestedActions/submit"
end

#textObject



19
20
21
# File 'lib/teams/activity.rb', line 19

def text
  raw["text"]
end

#to_hObject



111
112
113
# File 'lib/teams/activity.rb', line 111

def to_h
  raw.dup
end

#typeObject



11
12
13
# File 'lib/teams/activity.rb', line 11

def type
  raw["type"]
end

#typing?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/teams/activity.rb', line 123

def typing?
  type == "typing"
end

#valueObject



63
64
65
66
# File 'lib/teams/activity.rb', line 63

def value
  value = raw["value"]
  value.is_a?(Hash) ? Api::ActivityValue.new(value) : value
end