File: labels.go

package info (click to toggle)
golang-github-henrybear327-go-proton-api 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: sh: 55; makefile: 26
file content (100 lines) | stat: -rw-r--r-- 2,306 bytes parent folder | download
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
package server

import (
	"net/http"
	"strconv"

	"github.com/bradenaw/juniper/xslices"
	"github.com/gin-gonic/gin"
	"github.com/henrybear327/go-proton-api"
)

func (s *Server) handleGetMailLabels() gin.HandlerFunc {
	return func(c *gin.Context) {
		types := xslices.Map(c.QueryArray("Type"), func(val string) proton.LabelType {
			labelType, err := strconv.Atoi(val)
			if err != nil {
				panic(err)
			}

			return proton.LabelType(labelType)
		})

		labels, err := s.b.GetLabels(c.GetString("UserID"), types...)
		if err != nil {
			c.AbortWithStatus(http.StatusInternalServerError)
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"Labels": labels,
		})
	}
}

func (s *Server) handlePostMailLabels() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req proton.CreateLabelReq

		if err := c.BindJSON(&req); err != nil {
			c.AbortWithStatus(http.StatusBadRequest)
			return
		}

		if _, has, err := s.b.HasLabel(c.GetString("UserID"), req.Name); err != nil {
			c.AbortWithStatus(http.StatusInternalServerError)
			return
		} else if has {
			c.AbortWithStatus(http.StatusConflict)
			return
		}

		label, err := s.b.CreateLabel(c.GetString("UserID"), req.Name, req.ParentID, req.Type)
		if err != nil {
			c.AbortWithStatus(http.StatusInternalServerError)
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"Label": label,
		})
	}
}

func (s *Server) handlePutMailLabel() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req proton.UpdateLabelReq

		if err := c.BindJSON(&req); err != nil {
			c.AbortWithStatus(http.StatusBadRequest)
			return
		}

		if labelID, has, err := s.b.HasLabel(c.GetString("UserID"), req.Name); err != nil {
			c.AbortWithStatus(http.StatusInternalServerError)
			return
		} else if has && labelID != c.Param("labelID") {
			c.AbortWithStatus(http.StatusConflict)
			return
		}

		label, err := s.b.UpdateLabel(c.GetString("UserID"), c.Param("labelID"), req.Name, req.ParentID)
		if err != nil {
			c.AbortWithStatus(http.StatusInternalServerError)
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"Label": label,
		})
	}
}

func (s *Server) handleDeleteMailLabel() gin.HandlerFunc {
	return func(c *gin.Context) {
		if err := s.b.DeleteLabel(c.GetString("UserID"), c.Param("labelID")); err != nil {
			c.AbortWithStatus(http.StatusBadRequest)
			return
		}
	}
}