forked from textileio/go-textile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_blocks.go
108 lines (91 loc) · 2.07 KB
/
api_blocks.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
100
101
102
103
104
105
106
107
108
package core
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
func (a *api) lsBlocks(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 == "" {
g.String(http.StatusBadRequest, "missing thread id")
return
}
thrd := a.node.Thread(threadId)
if thrd == nil {
g.String(http.StatusNotFound, ErrThreadNotFound.Error())
return
}
limit := 5
if opts["limit"] != "" {
limit, err = strconv.Atoi(opts["limit"])
if err != nil {
g.String(http.StatusBadRequest, err.Error())
return
}
}
infos := make([]BlockInfo, 0)
query := fmt.Sprintf("threadId='%s'", thrd.Id)
for _, block := range a.node.datastore.Blocks().List(opts["offset"], limit, query) {
infos = append(infos, BlockInfo{
Id: block.Id,
ThreadId: block.ThreadId,
AuthorId: block.AuthorId,
Username: a.node.ContactUsername(block.AuthorId),
Type: block.Type.Description(),
Date: block.Date,
Parents: block.Parents,
Target: block.Target,
Body: block.Body,
})
}
g.JSON(http.StatusOK, infos)
}
func (a *api) getBlocks(g *gin.Context) {
id := g.Param("id")
info, err := a.node.BlockInfo(id)
if err != nil {
g.String(http.StatusNotFound, "block not found")
return
}
g.JSON(http.StatusOK, info)
}
func (a *api) rmBlocks(g *gin.Context) {
id := g.Param("id")
thrd := a.getBlockThread(g, id)
if thrd == nil {
return
}
hash, err := thrd.AddIgnore(id)
if err != nil {
a.abort500(g, err)
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) getBlockThread(g *gin.Context, id string) *Thread {
block, err := a.node.Block(id)
if err != nil {
g.String(http.StatusNotFound, "block not found")
return nil
}
thrd := a.node.Thread(block.ThreadId)
if thrd == nil {
g.String(http.StatusNotFound, "thread not found")
return nil
}
return thrd
}