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
/
postPublications.go
165 lines (135 loc) · 3.74 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
package goil
import (
"bytes"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"time"
)
// TODO Check if when post with no group "official" field exists
func CreatePublication(message string, category Category) *Publication {
return &Publication{Message: message, Category: category, Group: NoGroup}
}
// Post as a group
func (p *Publication) PublishAs(group Group, official bool) {
p.Group = group
p.Official = official
}
// Add an event to a publication
func (p *Publication) AddEvent(event Event) {
p.Event = event
}
// CreateSurvey creates a survey structure
func CreateSurvey(question string, end time.Time, multiple bool, answers ...string) Survey {
return Survey{
Question: question,
End: end, Answers: answers,
Multiple: multiple,
}
}
// Adds a survey to a publication
func (p *Publication) AddSurvey(survey Survey) {
p.Survey = survey
}
// Converts bool to string
func bts(from bool) string {
var output string
if from {
output = "1"
} else {
output = "0"
}
return output
}
// Write the publication to the body of the request
func (publication *Publication) write(w io.Writer) (string, error) {
// Prepare it as a multipart/form-data
writer := multipart.NewWriter(w)
// Prepare the base parameters
params := map[string]string{
"message": publication.Message,
"category": publication.Category.format(),
"group": publication.Group.format(),
"private": bts(publication.Dislike),
"dislike": bts(publication.Dislike),
}
// If there is no group indicated, do not take into account publication.Official
if publication.Group != 0 {
params["official"] = bts(publication.Official)
}
// Is there an event ? Then add it
if publication.Event.populated() {
params["event_title"] = publication.Event.Name
params["event_start"] = publication.Event.Start.Format(timeLayout)
params["event_end"] = publication.Event.End.Format(timeLayout)
}
// Is there a survey ? Then add it
if publication.Survey.populated() {
params["survey_question"] = publication.Survey.Question
params["survey_end"] = publication.Survey.End.Format(timeLayout)
params["survey_multiple"] = bts(publication.Survey.Multiple)
// Answers will be added later, as there are multiple of them
}
// Add the key/value pairs to the multipart request
for key, val := range params {
err := writer.WriteField(key, val)
if err != nil {
return "", err
}
}
// Add the survey answers
if publication.Survey.populated() {
for _, answer := range params {
err := writer.WriteField("survey_answer[]", answer)
if err != nil {
return "", err
}
}
}
var err error
// Add the attachments
if publication.Attachments.Populated() {
err = publication.Attachments.writeToMultipart(writer)
}
// Get content type
contentType := writer.FormDataContentType()
// Close the writer
err = writer.Close()
return contentType, err
}
// The postPublicationURI is the URI for posting a publication
const postPublicationURI string = BaseURLString + "post/add"
// Publish publication
func (s *Session) PostPublication(publication *Publication) error {
// Check
if s == nil {
return fmt.Errorf("Given session pointer is nil")
}
// Prepare the body of the request
body := &bytes.Buffer{}
// Write to the body
contentType, err := publication.write(body)
if err != nil {
return err
}
// Create the request
req, err := http.NewRequest("POST", postPublicationURI, body)
if err != nil {
return err
}
// Add the right header
req.Header.Set("Content-Type", contentType)
// Execute it
resp, err := s.Client.Do(req)
if err != nil {
return err
}
// Check for good feedback
resp.Body.Close()
if resp.StatusCode != 200 {
return errors.New(fmt.Sprintf("Status Code of response isn't right: %d instead of 200", resp.StatusCode))
}
return nil
}