From e435962b50f2ff678aae4568344228ee76a03aa9 Mon Sep 17 00:00:00 2001 From: VagantemNumen Date: Sat, 14 May 2016 05:15:56 +0500 Subject: [PATCH 1/4] Added CHANNEL_MESSAGES_BULK_DELETE endpoint. --- endpoints.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/endpoints.go b/endpoints.go index f97c03f..f480150 100644 --- a/endpoints.go +++ b/endpoints.go @@ -73,14 +73,15 @@ var ( GUILD_ICON = func(gID, hash string) string { return GUILDS + gID + "/icons/" + hash + ".jpg" } GUILD_SPLASH = func(gID, hash string) string { return GUILDS + gID + "/splashes/" + hash + ".jpg" } - CHANNEL = func(cID string) string { return CHANNELS + cID } - CHANNEL_PERMISSIONS = func(cID string) string { return CHANNELS + cID + "/permissions" } - CHANNEL_PERMISSION = func(cID, tID string) string { return CHANNELS + cID + "/permissions/" + tID } - CHANNEL_INVITES = func(cID string) string { return CHANNELS + cID + "/invites" } - CHANNEL_TYPING = func(cID string) string { return CHANNELS + cID + "/typing" } - CHANNEL_MESSAGES = func(cID string) string { return CHANNELS + cID + "/messages" } - CHANNEL_MESSAGE = func(cID, mID string) string { return CHANNELS + cID + "/messages/" + mID } - CHANNEL_MESSAGE_ACK = func(cID, mID string) string { return CHANNELS + cID + "/messages/" + mID + "/ack" } + CHANNEL = func(cID string) string { return CHANNELS + cID } + CHANNEL_PERMISSIONS = func(cID string) string { return CHANNELS + cID + "/permissions" } + CHANNEL_PERMISSION = func(cID, tID string) string { return CHANNELS + cID + "/permissions/" + tID } + CHANNEL_INVITES = func(cID string) string { return CHANNELS + cID + "/invites" } + CHANNEL_TYPING = func(cID string) string { return CHANNELS + cID + "/typing" } + CHANNEL_MESSAGES = func(cID string) string { return CHANNELS + cID + "/messages" } + CHANNEL_MESSAGE = func(cID, mID string) string { return CHANNELS + cID + "/messages/" + mID } + CHANNEL_MESSAGE_ACK = func(cID, mID string) string { return CHANNELS + cID + "/messages/" + mID + "/ack" } + CHANNEL_MESSAGES_BULK_DELETE = func(cID string) string { return CHANNEL(cID) + "/messages/bulk_delete" } INVITE = func(iID string) string { return API + "invite/" + iID } From 35e6225f87fbbbeed4b856bfce220c0f794c7662 Mon Sep 17 00:00:00 2001 From: VagantemNumen Date: Sat, 14 May 2016 05:23:50 +0500 Subject: [PATCH 2/4] Added ChannelMessagesBulkDelete function. Requires a channelID and a slice of messageIDs from the channel. If only on ID is in the slice calls ChannelMessageDelete() internally. If the slice of IDs is empty do nothing. --- restapi.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/restapi.go b/restapi.go index 06c2485..aba74ef 100644 --- a/restapi.go +++ b/restapi.go @@ -1107,6 +1107,30 @@ func (s *Session) ChannelMessageDelete(channelID, messageID string) (err error) return } +// ChannelMessagesBulkDelete bulk deletes the messages from the channel for the provided messageIDs. +// If only one messageID is in the slice call channelMessageDelete funciton. +// If the slice is empty do nothing. +// channelID : The ID of the channel for the messages to delete. +// messages : The IDs of the messages to be deleted. A slice of string IDs. +func (s *Session) ChannelMessagesBulkDelete(channelID string, messages []string) (err error) { + + if len(messages) == 0 { + return + } + + if len(messages) == 1 { + err = s.ChannelMessageDelete(channelID, messages[0]) + return + } + + data := struct { + Messages []string `json:"messages"` + }{messages} + + _, err = s.Request("POST", CHANNEL_MESSAGES_BULK_DELETE(channelID), data) + return +} + // ChannelFileSend sends a file to the given channel. // channelID : The ID of a Channel. // io.Reader : A reader for the file contents. From c4f596a93e6504b741e9e6f9978254dc84a6f733 Mon Sep 17 00:00:00 2001 From: VagantemNumen Date: Sat, 14 May 2016 05:23:50 +0500 Subject: [PATCH 3/4] Added ChannelMessagesBulkDelete function. Requires a channelID and a slice of messageIDs from the channel. If only on ID is in the slice calls ChannelMessageDelete() internally. If the slice of IDs is empty do nothing. Noted maximum of 100 messageIDs in the comment. --- restapi.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/restapi.go b/restapi.go index 06c2485..1e5756b 100644 --- a/restapi.go +++ b/restapi.go @@ -1107,6 +1107,30 @@ func (s *Session) ChannelMessageDelete(channelID, messageID string) (err error) return } +// ChannelMessagesBulkDelete bulk deletes the messages from the channel for the provided messageIDs. +// If only one messageID is in the slice call channelMessageDelete funciton. +// If the slice is empty do nothing. +// channelID : The ID of the channel for the messages to delete. +// messages : The IDs of the messages to be deleted. A slice of string IDs. Maximum 100. +func (s *Session) ChannelMessagesBulkDelete(channelID string, messages []string) (err error) { + + if len(messages) == 0 { + return + } + + if len(messages) == 1 { + err = s.ChannelMessageDelete(channelID, messages[0]) + return + } + + data := struct { + Messages []string `json:"messages"` + }{messages} + + _, err = s.Request("POST", CHANNEL_MESSAGES_BULK_DELETE(channelID), data) + return +} + // ChannelFileSend sends a file to the given channel. // channelID : The ID of a Channel. // io.Reader : A reader for the file contents. From a2c826192ff3d13ee08fd141ad0a82ef8f61bbc3 Mon Sep 17 00:00:00 2001 From: VagantemNumen Date: Sat, 14 May 2016 06:15:50 +0500 Subject: [PATCH 4/4] Added check for maximum messages in the slice. If more than 100 is present send the first 100 in the request and ignore the rest. --- restapi.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/restapi.go b/restapi.go index aba74ef..6f2ace9 100644 --- a/restapi.go +++ b/restapi.go @@ -1111,7 +1111,7 @@ func (s *Session) ChannelMessageDelete(channelID, messageID string) (err error) // If only one messageID is in the slice call channelMessageDelete funciton. // If the slice is empty do nothing. // channelID : The ID of the channel for the messages to delete. -// messages : The IDs of the messages to be deleted. A slice of string IDs. +// messages : The IDs of the messages to be deleted. A slice of string IDs. A maximum of 100 messages. func (s *Session) ChannelMessagesBulkDelete(channelID string, messages []string) (err error) { if len(messages) == 0 { @@ -1123,6 +1123,10 @@ func (s *Session) ChannelMessagesBulkDelete(channelID string, messages []string) return } + if len(messages) > 100 { + messages = messages[:100] + } + data := struct { Messages []string `json:"messages"` }{messages}