-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmessages.go
63 lines (57 loc) · 1.49 KB
/
messages.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
package mira
import (
"encoding/json"
"net/http"
"github.com/thecsw/mira/v4/models"
)
// Compose will send a message to next redditor.
func (c *Reddit) Compose(subject, text string) error {
name, _, err := c.checkType(redditorType)
if err != nil {
return err
}
target := RedditOauth + "/api/compose"
_, err = c.MiraRequest(http.MethodPost, target, map[string]string{
"subject": subject,
"text": text,
"to": name,
"api_type": JsonAPI,
})
return err
}
// ReadMessage marks the next comment/message as read.
func (c *Reddit) ReadMessage(messageID string) error {
_, _, err := c.checkType(meType)
if err != nil {
return err
}
target := RedditOauth + "/api/read_message"
_, err = c.MiraRequest(http.MethodPost, target, map[string]string{
"id": messageID,
})
return err
}
// ReadAllMessages uses ReadMessage on all unread messages.
func (c *Reddit) ReadAllMessages() error {
_, _, err := c.checkType(meType)
if err != nil {
return err
}
target := RedditOauth + "/api/read_all_messages"
_, err = c.MiraRequest(http.MethodPost, target, nil)
return err
}
// ListUnreadMessages returns a list of all unread messages.
func (c *Reddit) ListUnreadMessages() ([]models.Comment, error) {
_, _, err := c.checkType(meType)
if err != nil {
return nil, err
}
target := RedditOauth + "/message/unread"
ans, err := c.MiraRequest(http.MethodGet, target, map[string]string{
"mark": "false",
})
ret := &models.CommentListing{}
json.Unmarshal(ans, ret)
return ret.GetChildren(), err
}