summaryrefslogtreecommitdiffstats
path: root/vendor/gomod.garykim.dev/nc-talk/room
diff options
context:
space:
mode:
authorGary Kim <gary@garykim.dev>2021-06-01 17:17:07 -0400
committerGitHub <noreply@github.com>2021-06-01 23:17:07 +0200
commit1d50da4b1c43273ab2dc8c47829e03f5f6981f92 (patch)
treec99fbf0d82b335fd298703f33ace3cb7f0f37742 /vendor/gomod.garykim.dev/nc-talk/room
parentc7897cca5d61bbe3579403fbbef62c0d70779876 (diff)
downloadmatterbridge-msglm-1d50da4b1c43273ab2dc8c47829e03f5f6981f92.tar.gz
matterbridge-msglm-1d50da4b1c43273ab2dc8c47829e03f5f6981f92.tar.bz2
matterbridge-msglm-1d50da4b1c43273ab2dc8c47829e03f5f6981f92.zip
Add support for message deletion (nctalk) (#1492)
* nctalk: add message deletion support Signed-off-by: Gary Kim <gary@garykim.dev> * nctalk: seperate out deletion and sending logic Signed-off-by: Gary Kim <gary@garykim.dev> * nctalk: update library to v0.2.0 Signed-off-by: Gary Kim <gary@garykim.dev> * Rename functions to be clearer Signed-off-by: Gary Kim <gary@garykim.dev> * Update to go-nc-talk v0.2.1 Signed-off-by: Gary Kim <gary@garykim.dev> * Update to go-nc-talk v0.2.2 Signed-off-by: Gary Kim <gary@garykim.dev> * Make deletions easier to debug Signed-off-by: Gary Kim <gary@garykim.dev>
Diffstat (limited to 'vendor/gomod.garykim.dev/nc-talk/room')
-rw-r--r--vendor/gomod.garykim.dev/nc-talk/room/room.go61
1 files changed, 53 insertions, 8 deletions
diff --git a/vendor/gomod.garykim.dev/nc-talk/room/room.go b/vendor/gomod.garykim.dev/nc-talk/room/room.go
index 1ee73740..eb72c2c0 100644
--- a/vendor/gomod.garykim.dev/nc-talk/room/room.go
+++ b/vendor/gomod.garykim.dev/nc-talk/room/room.go
@@ -18,6 +18,7 @@ import (
"context"
"errors"
"io/ioutil"
+ "net/http"
"strconv"
"time"
@@ -41,6 +42,12 @@ var (
ErrUnexpectedReturnCode = errors.New("unexpected return code")
// ErrTooManyRequests is returned if the server returns a 429
ErrTooManyRequests = errors.New("too many requests")
+ // ErrLackingCapabilities is returned if the server lacks the required capability for the given function
+ ErrLackingCapabilities = errors.New("lacking required capabilities")
+ // ErrForbidden is returned if the user is forbidden from accessing the requested resource
+ ErrForbidden = errors.New("request forbidden")
+ // ErrUnexpectedResponse is returned if the response from the Nextcloud Talk server is not formatted as expected
+ ErrUnexpectedResponse = errors.New("unexpected response")
)
// TalkRoom represents a room in Nextcloud Talk
@@ -90,6 +97,39 @@ func (t *TalkRoom) SendMessage(msg string) (*ocs.TalkRoomMessageData, error) {
return &msgInfo.OCS.TalkRoomMessage, err
}
+// DeleteMessage deletes the message with the given messageID on the server.
+//
+// Requires "delete-messages" capability on the Nextcloud Talk server
+func (t *TalkRoom) DeleteMessage(messageID int) (*ocs.TalkRoomMessageData, error) {
+ // Check for required capability
+ capable, err := t.User.Capabilities()
+ if err != nil {
+ return nil, err
+ }
+ if !capable.DeleteMessages {
+ return nil, ErrLackingCapabilities
+ }
+
+ url := t.User.NextcloudURL + constants.BaseEndpoint + "/chat/" + t.Token + "/" + strconv.Itoa(messageID)
+
+ client := t.User.RequestClient(request.Client{
+ URL: url,
+ Method: "DELETE",
+ })
+ res, err := client.Do()
+ if err != nil {
+ return nil, err
+ }
+ if res.StatusCode() != http.StatusOK && res.StatusCode() != http.StatusAccepted {
+ return nil, ErrUnexpectedReturnCode
+ }
+ msgInfo, err := ocs.TalkRoomSentResponseUnmarshal(&res.Data)
+ if err != nil {
+ return nil, err
+ }
+ return &msgInfo.OCS.TalkRoomMessage, nil
+}
+
// ReceiveMessages starts watching for new messages
func (t *TalkRoom) ReceiveMessages(ctx context.Context) (chan ocs.TalkRoomMessageData, error) {
c := make(chan ocs.TalkRoomMessageData)
@@ -133,23 +173,28 @@ func (t *TalkRoom) ReceiveMessages(ctx context.Context) (chan ocs.TalkRoomMessag
}
// If it seems that we no longer have access to the chat for one reason or another, stop the goroutine and set error in the next return.
- if res.StatusCode == 404 {
+ if res.StatusCode == http.StatusNotFound {
_ = res.Body.Close()
c <- ocs.TalkRoomMessageData{Error: ErrRoomNotFound}
return
}
- if res.StatusCode == 401 {
+ if res.StatusCode == http.StatusUnauthorized {
_ = res.Body.Close()
c <- ocs.TalkRoomMessageData{Error: ErrUnauthorized}
return
}
- if res.StatusCode == 429 {
+ if res.StatusCode == http.StatusTooManyRequests {
_ = res.Body.Close()
c <- ocs.TalkRoomMessageData{Error: ErrTooManyRequests}
return
}
+ if res.StatusCode == http.StatusForbidden {
+ _ = res.Body.Close()
+ c <- ocs.TalkRoomMessageData{Error: ErrForbidden}
+ return
+ }
- if res.StatusCode == 200 {
+ if res.StatusCode == http.StatusOK {
lastKnown = res.Header.Get("X-Chat-Last-Given")
data, err := ioutil.ReadAll(res.Body)
_ = res.Body.Close()
@@ -192,13 +237,13 @@ func (t *TalkRoom) TestConnection() error {
return err
}
switch res.StatusCode() {
- case 200:
+ case http.StatusOK:
return nil
- case 304:
+ case http.StatusNotModified:
return nil
- case 404:
+ case http.StatusNotFound:
return ErrRoomNotFound
- case 412:
+ case http.StatusPreconditionFailed:
return ErrNotModeratorInLobby
}
return ErrUnexpectedReturnCode