Class: SM::Client

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::MHVSessionBasedClient
Defined in:
lib/sm/client.rb

Overview

Core class responsible for SM API interface operations

Constant Summary collapse

MHV_MAXIMUM_PER_PAGE =
250
CONTENT_DISPOSITION =
'attachment; filename='
STATSD_KEY_PREFIX =
if instance_of? SM::Client
  'api.sm'
else
  'mobile.sm'
end

Instance Attribute Summary

Attributes included from Common::Client::Concerns::MHVSessionBasedClient

#session

Preferences collapse

Folders collapse

Message Drafts collapse

Messages collapse

Triage Teams collapse

Instance Method Summary collapse

Methods included from Common::Client::Concerns::MHVSessionBasedClient

#authenticate, #get_session, #initialize

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger

Methods inherited from Common::Client::Base

configuration, #raise_backend_exception

Instance Method Details

#delete_folder(id) ⇒ Fixnum

Delete a Folder

Parameters:

  • id (Fixnum)

    id of the Folder

Returns:

  • (Fixnum)

    the status code of the response



132
133
134
135
# File 'lib/sm/client.rb', line 132

def delete_folder(id)
  response = perform(:delete, "folder/#{id}", nil, token_headers)
  response&.status
end

#delete_message(id) ⇒ Fixnum

Delete a message

Parameters:

  • id (Fixnum)

    id of message to be deleted

Returns:

  • (Fixnum)

    the response status code



399
400
401
402
403
404
# File 'lib/sm/client.rb', line 399

def delete_message(id)
  custom_headers = token_headers.merge('Content-Type' => 'application/json')
  response = perform(:post, "message/#{id}", nil, custom_headers)

  response&.status
end

#get_all_triage_teams(user_uuid, use_cache) ⇒ Common::Collection[AllTriageTeams]

Get a collection of all triage team recipients, including blocked with detailed attributes per each team including a total tally of associated and locked teams



446
447
448
449
450
451
452
453
454
# File 'lib/sm/client.rb', line 446

def get_all_triage_teams(user_uuid, use_cache)
  cache_key = "#{user_uuid}-all-triage-teams"
  get_cached_or_fetch_data(use_cache, cache_key, AllTriageTeams) do
    json = perform(:get, 'alltriageteams', nil, token_headers).body
    data = Common::Collection.new(AllTriageTeams, **json)
    AllTriageTeams.set_cached(cache_key, data)
    data
  end
end

#get_attachment(message_id, attachment_id) ⇒ Hash

Retrieve a message attachment

Parameters:

  • message_id (Fixnum)

    the message id

  • attachment_id (Fixnum)

    the attachment id

Returns:

  • (Hash)

    an object with attachment response details



413
414
415
416
417
418
419
# File 'lib/sm/client.rb', line 413

def get_attachment(message_id, attachment_id)
  path = "message/#{message_id}/attachment/#{attachment_id}"

  response = perform(:get, path, nil, token_headers)
  filename = response.response_headers['content-disposition'].gsub(CONTENT_DISPOSITION, '').gsub(/%22|"/, '')
  { body: response.body, filename: }
end

#get_cached_or_fetch_data(use_cache, cache_key, model) ⇒ Object



457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/sm/client.rb', line 457

def get_cached_or_fetch_data(use_cache, cache_key, model)
  data = nil
  data = model.get_cached(cache_key) if use_cache

  if data
    Rails.logger.info("secure messaging #{model} cache fetch", cache_key)
    statsd_cache_hit
    Common::Collection.new(model, data:)
  else
    Rails.logger.info("secure messaging #{model} service fetch", cache_key)
    statsd_cache_miss
    yield
  end
end

#get_categoriesCategory

Get message categories

Returns:



252
253
254
255
256
257
# File 'lib/sm/client.rb', line 252

def get_categories
  path = 'message/category'

  json = perform(:get, path, nil, token_headers).body
  Category.new(json)
end

#get_folder(id) ⇒ Folder

Get a single Folder

Returns:



98
99
100
101
# File 'lib/sm/client.rb', line 98

def get_folder(id)
  json = perform(:get, "folder/#{id}", nil, token_headers).body
  Folder.new(json)
end

#get_folder_messages(user_uuid, folder_id, use_cache) ⇒ Common::Collection

Get a collection of Messages

Returns:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/sm/client.rb', line 142

def get_folder_messages(user_uuid, folder_id, use_cache)
  cache_key = "#{user_uuid}-folder-messages-#{folder_id}"
  get_cached_or_fetch_data(use_cache, cache_key, Message) do
    page = 1
    json = { data: [], errors: {}, metadata: {} }

    loop do
      path = "folder/#{folder_id}/message/page/#{page}/pageSize/#{MHV_MAXIMUM_PER_PAGE}"
      page_data = perform(:get, path, nil, token_headers).body
      json[:data].concat(page_data[:data])
      json[:metadata].merge(page_data[:metadata])
      break unless page_data[:data].size == MHV_MAXIMUM_PER_PAGE

      page += 1
    end
    messages = Common::Collection.new(Message, **json)
    Message.set_cached(cache_key, messages)
    messages
  end
end

#get_folder_threads(folder_id, page_size, page_number, sort_field, sort_order) ⇒ Common::Collection

Get a collection of Threads

Parameters:

  • folder_id (Fixnum)

    id of the user’s folder (0 Inbox, -1 Sent, -2 Drafts, -3 Deleted, > 0 for custom folder)

  • page_start (Fixnum)

    Pagination start numbering

  • page_end (Fixnum)

    Pagination end numbering (max: 100)

  • sort_field (String)

    field to sort results by (SENDER_NAME or RECIPIENT_NAME or SENT_DATE or DRAFT_DATE)

  • sort_order (String)

    order to sort results by (ASC for Ascending or DESC for Descending)

Returns:



174
175
176
177
178
179
180
181
182
# File 'lib/sm/client.rb', line 174

def get_folder_threads(folder_id, page_size, page_number, sort_field, sort_order)
  path = "folder/threadlistview/#{folder_id}"

  params = "?pageSize=#{page_size}&pageNumber=#{page_number}&sortField=#{sort_field}&sortOrder=#{sort_order}"

  json = perform(:get, path + params, nil, token_headers).body

  Common::Collection.new(MessageThread, **json)
end

#get_folders(user_uuid, use_cache) ⇒ Common::Collection[Folder]

Get a collection of Folders



83
84
85
86
87
88
89
90
91
# File 'lib/sm/client.rb', line 83

def get_folders(user_uuid, use_cache)
  cache_key = "#{user_uuid}-folders"
  get_cached_or_fetch_data(use_cache, cache_key, Folder) do
    json = perform(:get, 'folder', nil, token_headers).body
    data = Common::Collection.new(Folder, **json)
    Folder.set_cached(cache_key, data)
    data
  end
end

#get_full_messages_for_thread(id) ⇒ Common::Collection[MessageThreadDetails]

Get a message thread with full body and attachments

Parameters:

  • id (Fixnum)

    message id

Returns:



301
302
303
304
305
# File 'lib/sm/client.rb', line 301

def get_full_messages_for_thread(id)
  path = "message/#{id}/allmessagesforthread/1"
  json = perform(:get, path, nil, token_headers).body
  Common::Collection.new(MessageThreadDetails, **json)
end

#get_message(id) ⇒ Message

Get a message

Parameters:

  • id (Fixnum)

    message id

Returns:



265
266
267
268
269
# File 'lib/sm/client.rb', line 265

def get_message(id)
  path = "message/#{id}/read"
  json = perform(:get, path, nil, token_headers).body
  Message.new(json)
end

#get_message_history(id) ⇒ Common::Collection[Message]

Get a message thread old api

Parameters:

  • id (Fixnum)

    message id

Returns:



277
278
279
280
281
# File 'lib/sm/client.rb', line 277

def get_message_history(id)
  path = "message/#{id}/history"
  json = perform(:get, path, nil, token_headers).body
  Common::Collection.new(Message, **json)
end

#get_messages_for_thread(id) ⇒ Common::Collection[MessageThread]

Get a message thread

Parameters:

  • id (Fixnum)

    message id

Returns:



289
290
291
292
293
# File 'lib/sm/client.rb', line 289

def get_messages_for_thread(id)
  path = "message/#{id}/messagesforthread"
  json = perform(:get, path, nil, token_headers).body
  Common::Collection.new(MessageThreadDetails, **json)
end

#get_preferencesMessagingPreference

Fetch the current email settings, including address and frequency

Returns:



42
43
44
45
46
47
# File 'lib/sm/client.rb', line 42

def get_preferences
  json = perform(:get, 'preferences/notification', nil, token_headers).body
  frequency = MessagingPreference::FREQUENCY_GET_MAP[json[:data][:notify_me]]
  MessagingPreference.new(email_address: json[:data][:email_address],
                          frequency:)
end

#get_preferences_frequency_listHash

Fetch the list of available constant values for email frequency

Returns:

  • (Hash)

    an object containing the body of the response



33
34
35
# File 'lib/sm/client.rb', line 33

def get_preferences_frequency_list
  perform(:get, 'preferences/notification/list', nil, token_headers).body
end

#get_signatureSting

Fetch current message signature

Returns:

  • (Sting)

    json response



71
72
73
# File 'lib/sm/client.rb', line 71

def get_signature
  perform(:get, 'preferences/signature', nil, token_headers).body
end

#get_triage_teams(user_uuid, use_cache) ⇒ Common::Collection[TriageTeam]

Get a collection of triage team recipients



429
430
431
432
433
434
435
436
437
# File 'lib/sm/client.rb', line 429

def get_triage_teams(user_uuid, use_cache)
  cache_key = "#{user_uuid}-triage-teams"
  get_cached_or_fetch_data(use_cache, cache_key, TriageTeam) do
    json = perform(:get, 'triageteam', nil, token_headers).body
    data = Common::Collection.new(TriageTeam, **json)
    TriageTeam.set_cached(cache_key, data)
    data
  end
end

#post_create_folder(name) ⇒ Folder

Create a Folder

Parameters:

  • name (String)

    name for the folder

Returns:



109
110
111
112
# File 'lib/sm/client.rb', line 109

def post_create_folder(name)
  json = perform(:post, 'folder', { 'name' => name }, token_headers).body
  Folder.new(json)
end

#post_create_message(args = {}) ⇒ Message

Create a message

Parameters:

  • args (Hash) (defaults to: {})

    a hash of message arguments

Returns:

Raises:



314
315
316
317
318
319
# File 'lib/sm/client.rb', line 314

def post_create_message(args = {})
  validate_create_context(args)

  json = perform(:post, 'message', args.to_h, token_headers).body
  Message.new(json)
end

#post_create_message_draft(args = {}) ⇒ MessageDraft

Create and update a new message draft

Parameters:

  • args (Hash) (defaults to: {})

    arguments for the message draft

Returns:

Raises:



214
215
216
217
218
219
220
221
222
# File 'lib/sm/client.rb', line 214

def post_create_message_draft(args = {})
  # Prevent call if this is a reply draft, otherwise reply-to message suject can change.
  validate_draft(args)

  json = perform(:post, 'message/draft', args, token_headers).body
  draft = MessageDraft.new(json)
  draft.body = draft.original_attributes[:body]
  draft
end

#post_create_message_draft_reply(id, args = {}) ⇒ MessageDraft

Create and update a new message draft reply

Parameters:

  • id (Fixnum)

    id of the message for which the reply is directed

  • args (Hash) (defaults to: {})

    arguments for the message draft reply

Returns:

Raises:



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/sm/client.rb', line 232

def post_create_message_draft_reply(id, args = {})
  # prevent call if this an existing draft with no association to a reply-to message
  validate_reply_draft(args)

  json = perform(:post, "message/#{id}/replydraft", args, token_headers).body
  json[:data][:has_message] = true

  draft = MessageDraft.new(json)
  draft.body = draft.original_attributes[:body]
  draft.as_reply
end

#post_create_message_reply(id, args = {}) ⇒ Message

Create a message reply

Parameters:

  • args (Hash) (defaults to: {})

    a hash of message arguments

Returns:

Raises:



358
359
360
361
362
363
# File 'lib/sm/client.rb', line 358

def post_create_message_reply(id, args = {})
  validate_reply_context(args)

  json = perform(:post, "message/#{id}/reply", args.to_h, token_headers).body
  Message.new(json)
end

#post_create_message_reply_with_attachment(id, args = {}) ⇒ Message

Create a message reply with an attachment

Parameters:

  • args (Hash) (defaults to: {})

    a hash of message arguments

Returns:

Raises:



343
344
345
346
347
348
349
# File 'lib/sm/client.rb', line 343

def post_create_message_reply_with_attachment(id, args = {})
  validate_reply_context(args)

  custom_headers = token_headers.merge('Content-Type' => 'multipart/form-data')
  json = perform(:post, "message/#{id}/reply/attach", args.to_h, custom_headers).body
  Message.new(json)
end

#post_create_message_with_attachment(args = {}) ⇒ Message

Create a message with an attachment

Parameters:

  • args (Hash) (defaults to: {})

    a hash of message arguments

Returns:

Raises:



328
329
330
331
332
333
334
# File 'lib/sm/client.rb', line 328

def post_create_message_with_attachment(args = {})
  validate_create_context(args)

  custom_headers = token_headers.merge('Content-Type' => 'multipart/form-data')
  json = perform(:post, 'message/attach', args.to_h, custom_headers).body
  Message.new(json)
end

#post_move_message(id, folder_id) ⇒ Fixnum

Move a message to a given folder

Parameters:

  • id (Fixnum)

    the Message id

  • folder_id (Fixnum)

    the Folder id

Returns:

  • (Fixnum)

    the response status code



372
373
374
375
376
377
# File 'lib/sm/client.rb', line 372

def post_move_message(id, folder_id)
  custom_headers = token_headers.merge('Content-Type' => 'application/json')
  response = perform(:post, "message/#{id}/move/tofolder/#{folder_id}", nil, custom_headers)

  response&.status
end

#post_move_thread(id, folder_id) ⇒ Fixnum

Move a thread to a given folder

Parameters:

  • id (Fixnum)

    the thread id

  • folder_id (Fixnum)

    the Folder id

Returns:

  • (Fixnum)

    the response status code



386
387
388
389
390
391
# File 'lib/sm/client.rb', line 386

def post_move_thread(id, folder_id)
  custom_headers = token_headers.merge('Content-Type' => 'application/json')
  response = perform(:post, "message/#{id}/movethreadmessages/tofolder/#{folder_id}", nil, custom_headers)

  response&.status
end

#post_preferences(params) ⇒ MessagingPreference

Set the email address and frequency for getting emails.

Examples:

client.post_preferences(email_address: '[email protected]', frequency: 'daily')

Parameters:

  • params (Hash)

    a hash of parameter objects

Returns:

Raises:



60
61
62
63
64
# File 'lib/sm/client.rb', line 60

def post_preferences(params)
  mhv_params = MessagingPreference.new(params).mhv_params
  perform(:post, 'preferences/notification', mhv_params, token_headers)
  get_preferences
end

#post_rename_folder(folder_id, name) ⇒ Folder

Rename a Folder

Parameters:

  • folder_id (Fixnum)

    id of the Folder

  • name (String)

    new name for the folder

Returns:



121
122
123
124
# File 'lib/sm/client.rb', line 121

def post_rename_folder(folder_id, name)
  json = perform(:post, "folder/#{folder_id}/rename", { 'folderName' => name }, token_headers).body
  Folder.new(json)
end

#post_search_folder(folder_id, page_num, page_size, args = {}) ⇒ Common::Collection

Run a search of messages in the given folder

Parameters:

  • folder_id (Fixnum)

    id of the folder to search

  • page_num (Fixnum)

    page number of results to return

  • page_size (Fixnum)

    number of messages per page

  • args (Hash) (defaults to: {})

    arguments for the message search

Returns:



193
194
195
196
197
198
199
200
201
202
# File 'lib/sm/client.rb', line 193

def post_search_folder(folder_id, page_num, page_size, args = {})
  page_num ||= 1
  page_size ||= MHV_MAXIMUM_PER_PAGE

  json_data = perform(:post,
                      "folder/#{folder_id}/searchMessage/page/#{page_num}/pageSize/#{page_size}",
                      args.to_h,
                      token_headers).body
  Common::Collection.new(Message, **json_data)
end