This repository has been archived by the owner on May 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostPublications.go
195 lines (167 loc) · 4.71 KB
/
postPublications.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"encoding/json"
"fmt"
"gopkg.in/aabizri/goil.v0"
"io"
"io/ioutil"
"net/http"
"time"
)
// A PublishRequest sent to POST /publications
type postPublicationsRequest struct {
Message string `json:"message"`
Category uint8 `json:"category"`
Group uint8 `json:"group"`
Official bool `json:"official,omitempty"`
Private bool `json:"private"`
Dislike bool `json:"dislike"`
// Attachments directly in the request(string instead of []byte because encoding/json is retarded, see https://stackoverflow.com/questions/31449610/illegal-base64-data-error-message)
PicturesBase64 []string `json:"picturesBase64,omitempty"`
VideosBase64 []string `json:"videosBase64,omitempty"`
AudioBase64 []string `json:"audioBase64,omitempty"`
DocumentsBase64 []string `json:"documentsBase64,omitempty"`
// Attachments by URL
PicturesURI []string `json:"picturesURI"`
VideosURI []string `json:"videosURI"`
AudioURI []string `json:"audioURI"`
DocumentsURI []string `json:"documentsURI"`
// Event & Survey
Event EventReq
Survey SurveyReq
}
type EventReq struct {
Title string `json:"title"`
Starts time.Time `json:"starts"`
Ends time.Time `json:"ends"`
}
func (e EventReq) Populated() bool {
return (e.Title != "")
}
type SurveyReq struct {
Question string `json:"question"`
Ends time.Time `json:"ends"`
Answers []string `json:"answers"`
Multiple bool `json:"multiple,omitempty"`
}
func (s SurveyReq) Populated() bool {
return (s.Question != "")
}
// Publish
func postPublications(w http.ResponseWriter, req *http.Request) {
var pubreq postPublicationsRequest
// Read the body, limiting
body, err := ioutil.ReadAll(io.LimitReader(req.Body, 1024*1024*1024*3)) // 3 GiB Limit
if err != nil {
err := NewError(InternalErrorReadingRequestFailed, "", fmt.Sprintf("Error: %s", err)).JSONWrite(w)
if err != nil {
panic(err)
}
return
}
// Close the body
if err := req.Body.Close(); err != nil {
err := NewError(InternalError, "Closing request body failed", fmt.Sprintf("Error: %s", err.Error())).JSONWrite(w)
if err != nil {
panic(err)
}
return
}
// Unmarshal
err = json.Unmarshal(body, &pubreq)
if err != nil {
err := NewError(InternalErrorJSONUnmarshallingFailed, "", fmt.Sprintf("Error: %s", err.Error())).JSONWrite(w)
if err != nil {
panic(err)
}
return
}
//fmt.Printf("%#v\n",pubreq)
// Retrieve goil session
raw := req.Context().Value("session")
gs, ok := raw.(*goil.Session)
if !ok {
err := NewError(InternalErrorTypeAssertionFailed, "", "Type assertion failure while casting req.Context().Value(\"session\") to (*goil.Session)").JSONWrite(w)
if err != nil {
panic(err)
}
return
}
// Publish
err = pubreq.publish(gs)
if err != nil {
err := NewError(InternalError, "While calling pubreq.publish()", fmt.Sprintf("Error: %s", err.Error())).JSONWrite(w)
if err != nil {
panic(err)
}
return
}
}
// Publish a publication request
func (pubreq *postPublicationsRequest) publish(s *goil.Session) error {
// Prepare the post
post := goil.NewPost(pubreq.Message, goil.Category(pubreq.Category), pubreq.Private, pubreq.Dislike)
if pubreq.Group != 0 {
post.PostAs(goil.Group(pubreq.Group), pubreq.Official)
}
// Attach attachments
err := pubreq.attach(post)
if err != nil {
return err
}
// Publish
err = s.PublishPost(post)
return err
}
func (pubreq *postPublicationsRequest) attach(post *goil.Post) error {
// Prepare slices holding paths to files
var (
pics []string
vids []string
audio []string
docs []string
)
var err error
// Download base64 attachments
if len(pubreq.PicturesBase64)+len(pubreq.VideosBase64)+len(pubreq.AudioBase64)+len(pubreq.DocumentsBase64) != 0 {
// Download attachments to the temp directory
pics, vids, audio, docs, err = pubreq.decodeBase64Attachments()
if err != nil {
return err
}
}
// Download URI attachments
if len(pubreq.PicturesURI)+len(pubreq.VideosURI)+len(pubreq.AudioURI)+len(pubreq.DocumentsURI) != 0 {
// Create variables for storage of the paths coming from URI attachments
var (
picsURI []string
vidsURI []string
audioURI []string
docsURI []string
)
// Download attachments to the temp directory
picsURI, vidsURI, audioURI, docsURI, err = pubreq.downloadURIAttachments()
if err != nil {
return err
}
// Append to the main slice
pics = append(pics, picsURI...)
vids = append(vids, vidsURI...)
audio = append(audio, audioURI...)
docs = append(docs, docsURI...)
}
// Add them to request
for _, path := range pics {
post.AttachPhoto(path)
}
for _, path := range vids {
post.AttachVideo(path)
}
for _, path := range audio {
post.AttachAudio(path)
}
for _, path := range docs {
post.AttachDocument(path)
}
return nil
}