File size: 4,788 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package entities

import (
	"net/url"
	"strings"
	"time"

	"github.com/NdoleStudio/stacktrace"
	"github.com/google/uuid"
)

// Phone represents an android phone which has installed the http sms app
type Phone struct {
	ID                    uuid.UUID  `json:"id" gorm:"primaryKey;type:uuid;" example:"32343a19-da5e-4b1b-a767-3298a73703cb"`
	UserID                UserID     `json:"user_id" example:"WB7DRDWrJZRGbYrv2CKGkqbzvqdC"`
	FcmToken              *string    `json:"fcm_token" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzd....." validate:"optional"`
	PhoneNumber           string     `json:"phone_number" example:"+18005550199"`
	MessagesPerMinute     uint       `json:"messages_per_minute" example:"1"`
	SIM                   SIM        `json:"sim" gorm:"default:SIM1"`
	MessageSendScheduleID *uuid.UUID `json:"message_send_schedule_id" gorm:"type:uuid" example:"32343a19-da5e-4b1b-a767-3298a73703cb" validate:"optional"`

	// MaxSendAttempts determines how many times to retry sending an SMS message
	MaxSendAttempts uint `json:"max_send_attempts" example:"2"`

	// MessageExpirationSeconds is the duration in seconds after sending a message when it is considered to be expired.
	MessageExpirationSeconds uint `json:"message_expiration_seconds"`

	MissedCallAutoReply *string `json:"missed_call_auto_reply" example:"This phone cannot receive calls. Please send an SMS instead." validate:"optional"`

	// UnarchiveThread moves an archived message thread back to the inbox when a new message is received on this phone.
	UnarchiveThread bool `json:"unarchive_thread" gorm:"default:false" example:"false"`

	// ArchiveNewOutgoingMessageThreads archives new conversations started by an outgoing message on this phone.
	ArchiveNewOutgoingMessageThreads bool `json:"archive_new_outgoing_message_threads" gorm:"default:false" example:"false"`

	CreatedAt time.Time `json:"created_at" example:"2022-06-05T14:26:02.302718+03:00"`
	UpdatedAt time.Time `json:"updated_at" example:"2022-06-05T14:26:10.303278+03:00"`
}

// NotificationTransport identifies how a phone receives wake-up notifications.
type NotificationTransport string

const (
	// NotificationTransportFCM sends notifications through Firebase.
	NotificationTransportFCM NotificationTransport = "fcm"
	// NotificationTransportHTTP sends notifications to an HTTPS endpoint.
	NotificationTransportHTTP NotificationTransport = "http"
)

func isNotificationURLCandidate(token string) bool {
	lower := strings.ToLower(token)

	return strings.Contains(token, "://") ||
		strings.HasPrefix(lower, "http:") ||
		strings.HasPrefix(lower, "https:") ||
		strings.HasPrefix(lower, "ftp:")
}

// MessageExpirationDuration returns the message expiration as time.Duration
func (phone *Phone) MessageExpirationDuration() time.Duration {
	return time.Duration(int(phone.MessageExpirationSecondsSanitized())) * time.Second
}

// MessageExpirationSecondsSanitized returns the message expiration seconds with default of 1 hour
func (phone *Phone) MessageExpirationSecondsSanitized() uint {
	if phone.MessageExpirationSeconds == 0 {
		return 10 * 60 // 10 minutes
	}
	return phone.MessageExpirationSeconds
}

// MaxSendAttemptsSanitized returns the max send attempts replacing 0 with 2
func (phone *Phone) MaxSendAttemptsSanitized() uint {
	if phone.MaxSendAttempts == 0 {
		return 2
	}
	return phone.MaxSendAttempts
}

// NotificationTransport returns the transport encoded by FcmToken.
func (phone *Phone) NotificationTransport() (NotificationTransport, error) {
	if phone == nil || phone.FcmToken == nil {
		return "", stacktrace.NewErrorf("phone has no notification token")
	}

	token := strings.TrimSpace(*phone.FcmToken)
	if token == "" {
		return "", stacktrace.NewErrorf("phone has no notification token")
	}

	if !isNotificationURLCandidate(token) {
		return NotificationTransportFCM, nil
	}

	endpoint, err := url.Parse(token)
	if err != nil {
		return "", stacktrace.NewError("invalid notification URL")
	}

	if !strings.EqualFold(endpoint.Scheme, "https") {
		return "", stacktrace.NewErrorf("notification URL must use https")
	}
	if endpoint.Hostname() == "" {
		return "", stacktrace.NewErrorf("notification URL must include a hostname")
	}

	return NotificationTransportHTTP, nil
}

// NotificationURL returns the parsed endpoint for an HTTP notification token.
func (phone *Phone) NotificationURL() (*url.URL, error) {
	transport, err := phone.NotificationTransport()
	if err != nil {
		return nil, err
	}

	if transport != NotificationTransportHTTP {
		return nil, stacktrace.NewErrorf("phone notification transport is [%s], not HTTP", transport)
	}

	endpoint, err := url.Parse(strings.TrimSpace(*phone.FcmToken))
	if err != nil {
		return nil, stacktrace.NewError("cannot parse notification URL")
	}

	return endpoint, nil
}