1# frozen_string_literal: true
2
3require "jennifer"
4require_relative "../rantly_extensions/data_extensions"
5
6class Message
7 module SegmentLimit
8 GSM_7 = 160
9 UCS_2 = 70
10 end
11 CHANNELS = ["sms", "mms", "rbm"]
12
13 # @param redis [RedisClient]
14 def initialize(redis)
15 @redis = redis
16 end
17
18 include Jennifer.rant(self) { |registered, jid, dir, top_level_to|
19 from {
20 case dir
21 when "in"
22 choose(nanpa_phone, shortcode)
23 when "out"
24 nanpa_phone
25 end
26 }
27 to derived_from(:from) {
28 case dir
29 when "in"
30 array(integer(4)) { nanpa_phone }
31 when "out"
32 array(integer(4)) { nanpa_phone } +
33 [top_level_to] +
34 array(integer(4)) { nanpa_phone }
35 end
36 }
37 owner derived_from(:to, :from) { |to, from|
38 case dir
39 when "in"
40 choose(*to)
41 when "out"
42 from
43 end
44 }
45 direction { dir }
46 time { iso8601 }
47 text {
48 # From bandwidth docs
49 # ```
50 # message.to: Empty text/string. Message text content will not be sent in callback.
51 # ```
52 case dir
53 when "in"
54 string
55 when "out"
56 ""
57 end
58 }
59 media {
60 choose(nil, array(range(0, 10)) { media_url })
61 }
62 stanza_id(transient: true) { string(:alnum) }
63 resourcepart(transient: true) { string }
64 tag derived_from(:stanza_id, :resourcepart) { |stanza_id, resourcepart|
65 [stanza_id, resourcepart].join(" ")
66 }
67 id { string }
68 application_id { string }
69 channel { choose(*CHANNELS) }
70 segment_count derived_from(:channel, :text) { |channel, text|
71 next 0 unless text
72
73 case channel
74 when "mms"
75 1
76 when "sms", "rbm"
77 segment_limit = choose(SegmentLimit::GSM_7, SegmentLimit::UCS_2)
78 (text.size / segment_limit).floor +
79 (text.size % segment_limit == 0 ? 0 : 1)
80 end
81 }
82 description { string }
83 errorCode {
84 case dir
85 when "out" then range(4000, 5999).to_s
86 when "in" then nil
87 end
88 }
89 redis_state derived_from(:owner), transient: true do |owner|
90 @redis.reset!
91 @redis.set("catapult_jid-", "HERE")
92 next unless registered
93
94 @redis.set("catapult_jid-#{owner}", jid)
95 end
96 }
97end