diff options
Diffstat (limited to 'vendor/gomod.garykim.dev/nc-talk/room/room.go')
-rw-r--r-- | vendor/gomod.garykim.dev/nc-talk/room/room.go | 61 |
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 |