This repository has been archived by the owner on Aug 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscriber.go
62 lines (53 loc) · 1.77 KB
/
subscriber.go
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
package common
import "github.com/rs/xid"
// Subscriber incapsulates newsletter subscriber information
// stored in the DynamoDB table
type Subscriber struct {
Name string `json:"name,omitempty"`
Newsletter string `json:"newsletter"`
Email string `json:"email"`
CreatedAt JSONTime `json:"created_at"`
UnsubscribedAt JSONTime `json:"unsubscribed_at"`
ConfirmedAt JSONTime `json:"confirmed_at"`
UserID string `json:"user_id,omitempty"`
}
// Confirmed checks if subscriber has confirmed the email via link
func (s *Subscriber) Confirmed() bool {
return s.ConfirmedAt.Time().After(s.CreatedAt.Time())
}
// Unsubscribed checks if subscriber pressed "Unsubscribe" link
func (s *Subscriber) Unsubscribed() bool {
return s.UnsubscribedAt.Time().After(s.CreatedAt.Time())
}
func (s *Subscriber) Validate() {
if len(s.UserID) > 0 {
return
}
guid := xid.New()
s.UserID = guid.String()
}
// SubscriberKey is used for deletion of subscribers
type SubscriberKey struct {
Newsletter string `json:"newsletter"`
Email string `json:"email"`
}
type SubscriberEx struct {
Name string `json:"name" yaml:"name"`
Newsletter string `json:"newsletter" yaml:"newsletter"`
Email string `json:"email" yaml:"email"`
Token string `json:"token" yaml:"token"`
Confirmed bool `json:"confirmed" yaml:"confirmed"`
Unsubscribed bool `json:"unsubscribed" yaml:"unsubscribed"`
UserID string `json:"user_id" yaml:"user_id"`
}
func NewSubscriberEx(s *Subscriber, secret string) *SubscriberEx {
return &SubscriberEx{
Name: s.Name,
Newsletter: s.Newsletter,
Email: s.Email,
Confirmed: s.Confirmed(),
Unsubscribed: s.Unsubscribed(),
Token: Sign(secret, s.Email),
UserID: s.UserID,
}
}