mirror of
https://github.com/cwinfo/matterbridge.git
synced 2025-06-26 13:09:24 +00:00
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>
This commit is contained in:
61
vendor/gomod.garykim.dev/nc-talk/room/room.go
vendored
61
vendor/gomod.garykim.dev/nc-talk/room/room.go
vendored
@ -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
|
||||
|
Reference in New Issue
Block a user