Debian Codegraph @

pkg/services/push/pushbullet/pushbullet_json.go

raw
1 package pushbullet
2
3 import (
4 "regexp"
5 )
6
7 type PushRequest struct {
8 Type string `json:"type"`
9 Title string `json:"title"`
10 Body string `json:"body"`
11
12 Email string `json:"email"`
13 ChannelTag string `json:"channel_tag"`
14 DeviceIden string `json:"device_iden"`
15 }
16
17 type PushResponse struct {
18 Active bool `json:"active"`
19 Body string `json:"body"`
20 Created float64 `json:"created"`
21 Direction string `json:"direction"`
22 Dismissed bool `json:"dismissed"`
23 Iden string `json:"iden"`
24 Modified float64 `json:"modified"`
25 ReceiverEmail string `json:"receiver_email"`
26 ReceiverEmailNormalized string `json:"receiver_email_normalized"`
27 ReceiverIden string `json:"receiver_iden"`
28 SenderEmail string `json:"sender_email"`
29 SenderEmailNormalized string `json:"sender_email_normalized"`
30 SenderIden string `json:"sender_iden"`
31 SenderName string `json:"sender_name"`
32 Title string `json:"title"`
33 Type string `json:"type"`
34 }
35
36 type ResponseError struct {
37 ErrorData struct {
38 Cat string `json:"cat"`
39 Message string `json:"message"`
40 Type string `json:"type"`
41 } `json:"error"`
42 }
43
44 var emailPattern = regexp.MustCompile(`.*@.*\..*`)
45
46 func (err *ResponseError) Error() string {
47 return err.ErrorData.Message
48 }
49
50 // NewNotePush creates a new push request.
51 //
52 //nolint:exhaustruct // PushRequest targeting fields (Email, ChannelTag, DeviceIden) are set by SetTarget method
53 func NewNotePush(message, title string) *PushRequest {
54 return &PushRequest{
55 Type: "note",
56 Title: title,
57 Body: message,
58 }
59 }
60
61 func (p *PushRequest) SetTarget(target string) {
62 if emailPattern.MatchString(target) {
63 p.Email = target
64
65 return
66 }
67
68 if target != "" && string(target[0]) == "#" {
69 p.ChannelTag = target[1:]
70
71 return
72 }
73
74 p.DeviceIden = target
75 }