forked from evan-buss/openbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.go
150 lines (127 loc) · 3.69 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package server
import (
"encoding/json"
"fmt"
"math"
"path"
"github.com/evan-buss/openbooks/core"
)
//go:generate stringer -type=MessageType
type MessageType int
// Available commands. These are sent via integers starting at 1
const (
STATUS MessageType = iota
CONNECT
SEARCH
DOWNLOAD
RATELIMIT
)
type NotificationType int
const (
NOTIFY NotificationType = iota
SUCCESS
WARNING
DANGER
)
type StatusResponse struct {
MessageType MessageType `json:"type"`
NotificationType NotificationType `json:"appearance"`
Title string `json:"title"`
Detail string `json:"detail"`
}
// Request in a generic structure for all requests from the websocket client
type Request struct {
MessageType MessageType `json:"type"`
Payload json.RawMessage `json:"payload"`
}
// ConnectionRequest is a request to start the IRC server
type ConnectionRequest struct{}
// SearchRequest is a request that sends a search request to the IRC server for a specific query
type SearchRequest struct {
Query string `json:"query"`
}
// DownloadRequest is a request to download a specific book from the IRC server
type DownloadRequest struct {
Book string `json:"book"`
}
// ConnectionResponse
type ConnectionResponse struct {
StatusResponse
Name string `json:"name"`
}
// SearchResponse is a response that is sent containing BookDetails objects that matched the query
type SearchResponse struct {
StatusResponse
Books []core.BookDetail `json:"books"`
Errors []core.ParseError `json:"errors"`
}
// DownloadResponse is a response that sends the requested book to the client
type DownloadResponse struct {
StatusResponse
Name string `json:"name"`
DownloadPath string `json:"downloadPath"`
}
func newRateLimitResponse(remainingSeconds float64) StatusResponse {
wait := math.Round(remainingSeconds)
units := "seconds"
if wait == 1 {
units = "second"
}
return StatusResponse{
MessageType: RATELIMIT,
NotificationType: WARNING,
Title: "You are searching too frequently!",
Detail: fmt.Sprintf("Please wait %v %s to submit another search.", wait, units),
}
}
func newSearchResponse(results []core.BookDetail, errors []core.ParseError) SearchResponse {
detail := fmt.Sprintf("There were %v parsing errors.", len(errors))
if len(errors) == 1 {
detail = "There was 1 parsing error."
}
return SearchResponse{
StatusResponse: StatusResponse{
MessageType: SEARCH,
NotificationType: SUCCESS,
Title: fmt.Sprintf("%v Search Results Received", len(results)),
Detail: detail,
},
Books: results,
Errors: errors,
}
}
func newDownloadResponse(filePath string, disableBrowserDownloads bool) DownloadResponse {
// If we don't want to autodownload the file, show the user the path to the file
// otherwise just show file name.
if !disableBrowserDownloads {
filePath = path.Base(filePath)
}
response := DownloadResponse{
StatusResponse: StatusResponse{
MessageType: DOWNLOAD,
NotificationType: SUCCESS,
Title: "Book file received.",
Detail: filePath,
},
}
// If we want to autodownload the file, add the path to the response
// client will not attempt autodownload if the path is empty
if !disableBrowserDownloads {
response.DownloadPath = path.Join("library", filePath)
}
return response
}
func newStatusResponse(notificationType NotificationType, title string) StatusResponse {
return StatusResponse{
MessageType: STATUS,
NotificationType: notificationType,
Title: title,
}
}
func newErrorResponse(title string) StatusResponse {
return StatusResponse{
MessageType: STATUS,
NotificationType: DANGER,
Title: title,
}
}