forked from textileio/go-textile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_messages.go
99 lines (83 loc) · 1.83 KB
/
api_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
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
package core
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
func (a *api) addThreadMessages(g *gin.Context) {
args, err := a.readArgs(g)
if err != nil {
a.abort500(g, err)
return
}
if len(args) == 0 {
g.String(http.StatusBadRequest, "missing message body")
return
}
threadId := g.Param("id")
if threadId == "default" {
threadId = a.node.config.Threads.Defaults.ID
}
thrd := a.node.Thread(threadId)
if thrd == nil {
g.String(http.StatusNotFound, ErrThreadNotFound.Error())
return
}
hash, err := thrd.AddMessage(args[0])
if err != nil {
g.String(http.StatusBadRequest, err.Error())
return
}
info, err := a.node.BlockInfo(hash.B58String())
if err != nil {
g.String(http.StatusBadRequest, err.Error())
return
}
g.JSON(http.StatusCreated, info)
}
func (a *api) lsThreadMessages(g *gin.Context) {
opts, err := a.readOpts(g)
if err != nil {
a.abort500(g, err)
return
}
threadId := opts["thread"]
if threadId == "default" {
threadId = a.node.config.Threads.Defaults.ID
}
if threadId != "" {
thrd := a.node.Thread(threadId)
if thrd == nil {
g.String(http.StatusNotFound, ErrThreadNotFound.Error())
return
}
}
limit := 10
if opts["limit"] != "" {
limit, err = strconv.Atoi(opts["limit"])
if err != nil {
g.String(http.StatusBadRequest, err.Error())
return
}
}
list, err := a.node.ThreadMessages(opts["offset"], limit, threadId)
if err != nil {
g.String(http.StatusBadRequest, err.Error())
return
}
g.JSON(http.StatusOK, list)
}
func (a *api) getThreadMessages(g *gin.Context) {
id := g.Param("block")
block, err := a.node.Block(id)
if err != nil {
g.String(http.StatusNotFound, "block not found")
return
}
info, err := a.node.ThreadMessage(*block)
if err != nil {
g.String(http.StatusBadRequest, err.Error())
return
}
g.JSON(http.StatusOK, info)
}