Debian Codegraph @

server/labels.go

raw
1 package server
2
3 import (
4 "net/http"
5 "strconv"
6
7 "github.com/bradenaw/juniper/xslices"
8 "github.com/gin-gonic/gin"
9 "github.com/henrybear327/go-proton-api"
10 )
11
12 func (s *Server) handleGetMailLabels() gin.HandlerFunc {
13 return func(c *gin.Context) {
14 types := xslices.Map(c.QueryArray("Type"), func(val string) proton.LabelType {
15 labelType, err := strconv.Atoi(val)
16 if err != nil {
17 panic(err)
18 }
19
20 return proton.LabelType(labelType)
21 })
22
23 labels, err := s.b.GetLabels(c.GetString("UserID"), types...)
24 if err != nil {
25 c.AbortWithStatus(http.StatusInternalServerError)
26 return
27 }
28
29 c.JSON(http.StatusOK, gin.H{
30 "Labels": labels,
31 })
32 }
33 }
34
35 func (s *Server) handlePostMailLabels() gin.HandlerFunc {
36 return func(c *gin.Context) {
37 var req proton.CreateLabelReq
38
39 if err := c.BindJSON(&req); err != nil {
40 c.AbortWithStatus(http.StatusBadRequest)
41 return
42 }
43
44 if _, has, err := s.b.HasLabel(c.GetString("UserID"), req.Name); err != nil {
45 c.AbortWithStatus(http.StatusInternalServerError)
46 return
47 } else if has {
48 c.AbortWithStatus(http.StatusConflict)
49 return
50 }
51
52 label, err := s.b.CreateLabel(c.GetString("UserID"), req.Name, req.ParentID, req.Type)
53 if err != nil {
54 c.AbortWithStatus(http.StatusInternalServerError)
55 return
56 }
57
58 c.JSON(http.StatusOK, gin.H{
59 "Label": label,
60 })
61 }
62 }
63
64 func (s *Server) handlePutMailLabel() gin.HandlerFunc {
65 return func(c *gin.Context) {
66 var req proton.UpdateLabelReq
67
68 if err := c.BindJSON(&req); err != nil {
69 c.AbortWithStatus(http.StatusBadRequest)
70 return
71 }
72
73 if labelID, has, err := s.b.HasLabel(c.GetString("UserID"), req.Name); err != nil {
74 c.AbortWithStatus(http.StatusInternalServerError)
75 return
76 } else if has && labelID != c.Param("labelID") {
77 c.AbortWithStatus(http.StatusConflict)
78 return
79 }
80
81 label, err := s.b.UpdateLabel(c.GetString("UserID"), c.Param("labelID"), req.Name, req.ParentID)
82 if err != nil {
83 c.AbortWithStatus(http.StatusInternalServerError)
84 return
85 }
86
87 c.JSON(http.StatusOK, gin.H{
88 "Label": label,
89 })
90 }
91 }
92
93 func (s *Server) handleDeleteMailLabel() gin.HandlerFunc {
94 return func(c *gin.Context) {
95 if err := s.b.DeleteLabel(c.GetString("UserID"), c.Param("labelID")); err != nil {
96 c.AbortWithStatus(http.StatusBadRequest)
97 return
98 }
99 }
100 }