-
Notifications
You must be signed in to change notification settings - Fork 18
/
message_send.go
87 lines (68 loc) · 2.08 KB
/
message_send.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
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
package proton
import (
"context"
"fmt"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/go-resty/resty/v2"
)
func (c *Client) CreateDraft(ctx context.Context, addrKR *crypto.KeyRing, req CreateDraftReq) (Message, error) {
var res struct {
Message Message
}
kr, err := addrKR.FirstKey()
if err != nil {
return Message{}, fmt.Errorf("failed to get first key: %w", err)
}
enc, err := kr.Encrypt(crypto.NewPlainMessageFromString(req.Message.Body), nil)
if err != nil {
return Message{}, fmt.Errorf("failed to encrypt draft: %w", err)
}
arm, err := enc.GetArmored()
if err != nil {
return Message{}, fmt.Errorf("failed to armor draft: %w", err)
}
req.Message.Body = arm
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(req).SetResult(&res).Post("/mail/v4/messages")
}); err != nil {
return Message{}, err
}
return res.Message, nil
}
func (c *Client) UpdateDraft(ctx context.Context, draftID string, addrKR *crypto.KeyRing, req UpdateDraftReq) (Message, error) {
var res struct {
Message Message
}
if req.Message.Body != "" {
kr, err := addrKR.FirstKey()
if err != nil {
return Message{}, fmt.Errorf("failed to get first key: %w", err)
}
enc, err := kr.Encrypt(crypto.NewPlainMessageFromString(req.Message.Body), nil)
if err != nil {
return Message{}, fmt.Errorf("failed to encrypt draft: %w", err)
}
arm, err := enc.GetArmored()
if err != nil {
return Message{}, fmt.Errorf("failed to armor draft: %w", err)
}
req.Message.Body = arm
}
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(req).SetResult(&res).Put("/mail/v4/messages/" + draftID)
}); err != nil {
return Message{}, err
}
return res.Message, nil
}
func (c *Client) SendDraft(ctx context.Context, draftID string, req SendDraftReq) (Message, error) {
var res struct {
Sent Message
}
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(req).SetResult(&res).Post("/mail/v4/messages/" + draftID)
}); err != nil {
return Message{}, err
}
return res.Sent, nil
}