~thrrgilag/woodstock

ref: 3b9e0ef927990392fbad7ada8fc087ef8d05fc75 woodstock/channels.go -rw-r--r-- 8.6 KiB
3b9e0ef9 — Morgan McMillian Added go.mod 1 year, 5 months ago
                                                                                
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
196
197
198
199
200
201
package woodstock

import (
	"encoding/json"
	"net/url"
	"strings"
)

// ChannelResult is a response containing a channel object
type ChannelResult struct {
	*CommonResponse
	Data Channel `json:"data"`
}

// ChannelsResult is a response containing multiple channel objects
type ChannelsResult struct {
	*CommonResponse
	Data []Channel `json:"data"`
}

type createChannel struct {
	Type string `json:"type"`
	ACL  ACL    `json:"acl"`
}

type createMessage struct {
	Text string `json:"text"`
}

// GetChannel retrieves the channel object from the channel ID
// https://pnut.io/docs/resources/channels/lookup#get-channels-id
func (c *Client) GetChannel(id string) (result ChannelResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + id, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetChannels retrieves a list of channels from the provided channel IDs
// https://pnut.io/docs/resources/channels/lookup#get-channels
func (c *Client) GetChannels(ids []string) (result UsersResult, err error) {
	v := url.Values{}
	v.Set("ids", strings.Join(ids, ","))

	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "?" + v.Encode(), data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// CreateChannel creates a new channel
// https://pnut.io/docs/resources/channels/lifecycle#post-channels
func (c *Client) CreateChannel(typeStr string, acl ACL) (result ChannelResult, err error) {
	json, err := json.Marshal(&createChannel{Type: typeStr, ACL: acl})
	if err != nil {
		return
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI, data: &result, method: "POST", responseCh: responseCh, json: string(json)}
	return result, (<-responseCh).err
}

// UpdateChannel updates an existing channel
// https://pnut.io/docs/resources/channels/lifecycle#put-channels-id
func (c *Client) UpdateChannel(id string, acl ACL) (result ChannelResult, err error) {
	json, err := json.Marshal(&createChannel{ACL: acl})
	if err != nil {
		return
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + id, data: &result, method: "PUT", responseCh: responseCh, json: string(json)}
	return result, (<-responseCh).err
}

// DeleteChannel deletes an existing channel
// https://pnut.io/docs/resources/channels/lifecycle#delete-channels-id
func (c *Client) DeleteChannel(id string) (result ChannelResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + id, data: &result, method: "DELETE", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetSubscribersOfChannel retrives a list of subscribers from a channel ID
// https://pnut.io/docs/resources/channels/subscribing#get-channels-id-subscribers
func (c *Client) GetSubscribersOfChannel(id string) (result UsersResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + id + "/subscribers", data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// SubscribeChannel subscribes the authenticated user to a channel
// https://pnut.io/docs/resources/channels/subscribing#put-channels-id-subscribe
func (c *Client) SubscribeChannel(id string) (result ChannelResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + id + "/subscribe", data: &result, method: "PUT", responseCh: responseCh}
	return result, (<-responseCh).err
}

// UnSubscribeChannel unsubscribes the authenticated user from a channel
// https://pnut.io/docs/resources/channels/subscribing#delete-channels-id-subscribe
func (c *Client) UnSubscribeChannel(id string) (result ChannelResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + id + "/subscribe", data: &result, method: "DELETE", responseCh: responseCh}
	return result, (<-responseCh).err
}

// MuteChannel mutes notifications for a channel
// https://pnut.io/docs/resources/channels/muting#put-channels-id-mute
func (c *Client) MuteChannel(id string) (result ChannelResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + id + "/mute", data: &result, method: "PUT", responseCh: responseCh}
	return result, (<-responseCh).err
}

// UnMuteChannel unmutes notifications for a channel
// https://pnut.io/docs/resources/channels/muting#delete-channels-id-mute
func (c *Client) UnMuteChannel(id string) (result ChannelResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + id + "/mute", data: &result, method: "DELETE", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetMessage retrieves a specific message from a channel
// https://pnut.io/docs/resources/messages/lookup#get-channels-id-messages-id
func (c *Client) GetMessage(channelID string, messageID string) (result PostResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + channelID + "/messages/" + messageID, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetMessagesInThread retrieves messages in a thread from a channel
// https://pnut.io/docs/resources/messages/lookup#get-channels-id-messages-id-thread
func (c *Client) GetMessagesInThread(channelID string, messageID string) (result PostsResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + channelID + "/messages/" + messageID + "/thread", data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetMessages retrieves multiple messages
// https://pnut.io/docs/resources/messages/lookup#get-messages
func (c *Client) GetMessages(ids []string) (result PostsResult, err error) {
	v := url.Values{}
	v.Set("ids", strings.Join(ids, ","))

	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/messages?" + v.Encode(), data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetMessagesOfChannel retrieves messages bashed on channel ID
// https://pnut.io/docs/resources/messages/lookup#get-channels-id-messages
func (c *Client) GetMessagesOfChannel(id string) (result PostsResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + id + "/messages", data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// CreateMessage creates a new message
// https://pnut.io/docs/resources/messages/lifecycle#post-channels-id-messages
func (c *Client) CreateMessage(id string, text string) (result PostResult, err error) {
	var message createMessage
	message.Text = text
	json, err := json.Marshal(message)
	if err != nil {
		return
	}

	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + id + "/messages", data: &result, method: "POST", responseCh: responseCh, json: string(json)}
	return result, (<-responseCh).err
}

// DeleteMessage deletes a specific message
// https://pnut.io/docs/resources/messages/lifecycle#delete-channels-id-messages-id
func (c *Client) DeleteMessage(channelID string, messageID string) (result PostResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + channelID + "/messages/" + messageID, data: &result, method: "DELETE", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetStickyMessages retrives sticky messages
// https://pnut.io/docs/resources/messages/sticky#get-channels-id-sticky_messages
func (c *Client) GetStickyMessages(id string) (result PostsResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + id + "/sticky_messages", data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// Sticky sets a message as sticky
// https://pnut.io/docs/resources/messages/sticky#put-channels-id-messages-id-sticky
func (c *Client) Sticky(channelID string, messageID string) (result PostResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + channelID + "/messages/" + messageID + "/sticky", data: &result, method: "PUT", responseCh: responseCh}
	return result, (<-responseCh).err
}

// UnSticky unsets a message as sticky
// https://pnut.io/docs/resources/messages/sticky#delete-channels-id-messages-id-sticky
func (c *Client) UnSticky(channelID string, messageID string) (result PostResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: ChannelAPI + "/" + channelID + "/messages/" + messageID + "/sticky", data: &result, method: "DELETE", responseCh: responseCh}
	return result, (<-responseCh).err
}