From 1d50da4b1c43273ab2dc8c47829e03f5f6981f92 Mon Sep 17 00:00:00 2001 From: Gary Kim Date: Tue, 1 Jun 2021 17:17:07 -0400 Subject: Add support for message deletion (nctalk) (#1492) * nctalk: add message deletion support Signed-off-by: Gary Kim * nctalk: seperate out deletion and sending logic Signed-off-by: Gary Kim * nctalk: update library to v0.2.0 Signed-off-by: Gary Kim * Rename functions to be clearer Signed-off-by: Gary Kim * Update to go-nc-talk v0.2.1 Signed-off-by: Gary Kim * Update to go-nc-talk v0.2.2 Signed-off-by: Gary Kim * Make deletions easier to debug Signed-off-by: Gary Kim --- .../gomod.garykim.dev/nc-talk/ocs/capabilities.go | 6 +- vendor/gomod.garykim.dev/nc-talk/ocs/message.go | 11 ++ vendor/gomod.garykim.dev/nc-talk/room/room.go | 61 +++++++++-- vendor/gomod.garykim.dev/nc-talk/user/user.go | 117 +++++++++++++++------ 4 files changed, 152 insertions(+), 43 deletions(-) (limited to 'vendor/gomod.garykim.dev/nc-talk') diff --git a/vendor/gomod.garykim.dev/nc-talk/ocs/capabilities.go b/vendor/gomod.garykim.dev/nc-talk/ocs/capabilities.go index 4dbaa735..8b2919fc 100644 --- a/vendor/gomod.garykim.dev/nc-talk/ocs/capabilities.go +++ b/vendor/gomod.garykim.dev/nc-talk/ocs/capabilities.go @@ -33,10 +33,14 @@ type SpreedCapabilities struct { Folder string `json:"folder"` } `json:"attachments"` Chat struct { - MaxLength int `json:"max-length"` + MaxLength int `json:"max-length"` + ReadPrivacy int `json:"read-privacy"` } `json:"chat"` Conversations struct { CanCreate bool `json:"can-create"` } `json:"conversations"` + Previews struct { + MaxGifSize int `json:"max-gif-size"` + } `json:"previews"` } `json:"config"` } diff --git a/vendor/gomod.garykim.dev/nc-talk/ocs/message.go b/vendor/gomod.garykim.dev/nc-talk/ocs/message.go index d4766006..1a8f95dc 100644 --- a/vendor/gomod.garykim.dev/nc-talk/ocs/message.go +++ b/vendor/gomod.garykim.dev/nc-talk/ocs/message.go @@ -35,6 +35,15 @@ const ( // MessageCommand is a Nextcloud Talk message that is a command MessageCommand MessageType = "command" + // MessageDelete is a Nextcloud Talk message indicating a message that was deleted + // + // If a message has been deleted, a message of MessageType MessageSystem is + // sent through the channel for which the parent message's MessageType is MessageDelete. + // So, in order to check if a new message is a message deletion request, a check + // like this can be used: + // msg.MessageType == ocs.MessageSystem && msg.Parent != nil && msg.Parent.MessageType == ocs.MessageDelete + MessageDelete MessageType = "comment_deleted" + // ActorUser is a Nextcloud Talk message sent by a user ActorUser ActorType = "users" @@ -55,6 +64,8 @@ type TalkRoomMessageData struct { SystemMessage string `json:"systemMessage"` Timestamp int `json:"timestamp"` MessageType MessageType `json:"messageType"` + Deleted bool `json:"deleted"` + Parent *TalkRoomMessageData `json:"parent"` MessageParameters map[string]RichObjectString `json:"-"` } 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 diff --git a/vendor/gomod.garykim.dev/nc-talk/user/user.go b/vendor/gomod.garykim.dev/nc-talk/user/user.go index 2b42dacf..e557cffa 100644 --- a/vendor/gomod.garykim.dev/nc-talk/user/user.go +++ b/vendor/gomod.garykim.dev/nc-talk/user/user.go @@ -28,12 +28,16 @@ import ( const ( ocsCapabilitiesEndpoint = "/ocs/v2.php/cloud/capabilities" - ocsRoomsEndpoint = "/ocs/v2.php/apps/spreed/api/v2/room" + ocsRoomsv2Endpoint = "/ocs/v2.php/apps/spreed/api/v2/room" + ocsRoomsv4Endpoint = "/ocs/v2.php/apps/spreed/api/v4/room" ) var ( - // ErrUserIsNil is returned when a funciton is called with an nil user. + // ErrUserIsNil is returned when a function is called with an nil user. ErrUserIsNil = errors.New("user is nil") + + // ErrCannotDownloadFile is returned when a function cannot download the requested file + ErrCannotDownloadFile = errors.New("cannot download file") ) // TalkUser represents a user of Nextcloud Talk @@ -53,36 +57,52 @@ type TalkUserConfig struct { // Capabilities describes the capabilities that the Nextcloud Talk instance is capable of. Visit https://nextcloud-talk.readthedocs.io/en/latest/capabilities/ for more info. type Capabilities struct { AttachmentsFolder string `ocscapability:"config => attachments => folder"` - ChatMaxLength int - Audio bool `ocscapability:"audio"` - Video bool `ocscapability:"video"` - Chat bool `ocscapability:"chat"` - GuestSignaling bool `ocscapability:"guest-signaling"` - EmptyGroupRoom bool `ocscapability:"empty-group-room"` - GuestDisplayNames bool `ocscapability:"guest-display-names"` - MultiRoomUsers bool `ocscapability:"multi-room-users"` - ChatV2 bool `ocscapability:"chat-v2"` - Favorites bool `ocscapability:"favorites"` - LastRoomActivity bool `ocscapability:"last-room-activity"` - NoPing bool `ocscapability:"no-ping"` - SystemMessages bool `ocscapability:"system-messages"` - MentionFlag bool `ocscapability:"mention-flag"` - InCallFlags bool `ocscapability:"in-call-flags"` - InviteByMail bool `ocscapability:"invite-by-mail"` - NotificationLevels bool `ocscapability:"notification-levels"` - InviteGroupsAndMails bool `ocscapability:"invite-groups-and-mails"` - LockedOneToOneRooms bool `ocscapability:"locked-one-to-one-rooms"` - ReadOnlyRooms bool `ocscapability:"read-only-rooms"` - ChatReadMarker bool `ocscapability:"chat-read-marker"` - WebinaryLobby bool `ocscapability:"webinary-lobby"` - StartCallFlag bool `ocscapability:"start-call-flag"` - ChatReplies bool `ocscapability:"chat-replies"` - CirclesSupport bool `ocscapability:"circles-support"` - AttachmentsAllowed bool `ocscapability:"config => attachments => allowed"` - ConversationsCanCreate bool `ocscapability:"config => conversations => can-create"` - ForceMute bool `ocscapability:"force-mute"` - ConversationV2 bool `ocscapability:"conversation-v2"` - ChatReferenceID bool `ocscapability:"chat-reference-id"` + Audio bool `ocscapability:"audio"` + Video bool `ocscapability:"video"` + Chat bool `ocscapability:"chat"` + GuestSignaling bool `ocscapability:"guest-signaling"` + EmptyGroupRoom bool `ocscapability:"empty-group-room"` + GuestDisplayNames bool `ocscapability:"guest-display-names"` + MultiRoomUsers bool `ocscapability:"multi-room-users"` + ChatV2 bool `ocscapability:"chat-v2"` + Favorites bool `ocscapability:"favorites"` + LastRoomActivity bool `ocscapability:"last-room-activity"` + NoPing bool `ocscapability:"no-ping"` + SystemMessages bool `ocscapability:"system-messages"` + MentionFlag bool `ocscapability:"mention-flag"` + InCallFlags bool `ocscapability:"in-call-flags"` + InviteByMail bool `ocscapability:"invite-by-mail"` + NotificationLevels bool `ocscapability:"notification-levels"` + InviteGroupsAndMails bool `ocscapability:"invite-groups-and-mails"` + LockedOneToOneRooms bool `ocscapability:"locked-one-to-one-rooms"` + ReadOnlyRooms bool `ocscapability:"read-only-rooms"` + ChatReadMarker bool `ocscapability:"chat-read-marker"` + WebinaryLobby bool `ocscapability:"webinary-lobby"` + StartCallFlag bool `ocscapability:"start-call-flag"` + ChatReplies bool `ocscapability:"chat-replies"` + CirclesSupport bool `ocscapability:"circles-support"` + AttachmentsAllowed bool `ocscapability:"config => attachments => allowed"` + ConversationsCanCreate bool `ocscapability:"config => conversations => can-create"` + ForceMute bool `ocscapability:"force-mute"` + ConversationV2 bool `ocscapability:"conversation-v2"` + ChatReferenceID bool `ocscapability:"chat-reference-id"` + ConversationV3 bool `ocscapability:"conversation-v3"` + ConversationV4 bool `ocscapability:"conversation-v4"` + SIPSupport bool `ocscapability:"sip-support"` + ChatReadStatus bool `ocscapability:"chat-read-status"` + ListableRooms bool `ocscapability:"listable-rooms"` + PhonebookSearch bool `ocscapability:"phonebook-search"` + RaiseHand bool `ocscapability:"raise-hand"` + RoomDescription bool `ocscapability:"room-description"` + DeleteMessages bool `ocscapability:"delete-messages"` + RichObjectSharing bool `ocscapability:"rich-object-sharing"` + ConversationCallFlags bool `ocscapability:"conversation-call-flags"` + GeoLocationSharing bool `ocscapability:"geo-location-sharing"` + ReadPrivacyConfig bool `ocscapability:"config => chat => read-privacy"` + SignalingV3 bool `ocscapability:"signaling-v3"` + TempUserAvatarAPI bool `ocscapability:"temp-user-avatar-api"` + MaxGifSizeConfig int `ocscapability:"config => previews => max-gif-size"` + ChatMaxLength int `ocscapability:"config => chat => max-length"` } // RoomInfo contains information about a room @@ -160,8 +180,17 @@ func (t *TalkUser) RequestClient(client request.Client) *request.Client { // GetRooms returns a list of all rooms the user is in func (t *TalkUser) GetRooms() (*[]RoomInfo, error) { + endpoint := ocsRoomsv2Endpoint + capabilities, err := t.Capabilities() + if err != nil { + return nil, err + } + if capabilities.ConversationV4 { + endpoint = ocsRoomsv4Endpoint + } + client := t.RequestClient(request.Client{ - URL: ocsRoomsEndpoint, + URL: endpoint, }) res, err := client.Do() if err != nil { @@ -239,6 +268,22 @@ func (t *TalkUser) Capabilities() (*Capabilities, error) { ConversationV2: sliceContains(sc.Features, "conversation-v2"), ChatReferenceID: sliceContains(sc.Features, "chat-reference-id"), ChatMaxLength: sc.Config.Chat.MaxLength, + ConversationV3: sliceContains(sc.Features, "conversation-v3"), + ConversationV4: sliceContains(sc.Features, "conversation-v4"), + SIPSupport: sliceContains(sc.Features, "sip-support"), + ChatReadStatus: sliceContains(sc.Features, "chat-read-status"), + ListableRooms: sliceContains(sc.Features, "listable-rooms"), + PhonebookSearch: sliceContains(sc.Features, "phonebook-search"), + RaiseHand: sliceContains(sc.Features, "raise-hand"), + RoomDescription: sliceContains(sc.Features, "room-description"), + ReadPrivacyConfig: sc.Config.Chat.ReadPrivacy != 0, + MaxGifSizeConfig: sc.Config.Previews.MaxGifSize, + DeleteMessages: sliceContains(sc.Features, "delete-messages"), + RichObjectSharing: sliceContains(sc.Features, "rich-object-sharing"), + ConversationCallFlags: sliceContains(sc.Features, "conversation-call-flags"), + GeoLocationSharing: sliceContains(sc.Features, "geo-location-sharing"), + SignalingV3: sliceContains(sc.Features, "signaling-v3"), + TempUserAvatarAPI: sliceContains(sc.Features, "temp-user-avatar-api"), } t.capabilities = tr @@ -264,7 +309,11 @@ func (t *TalkUser) DownloadFile(path string) (data *[]byte, err error) { URL: url, }) res, err := c.Do() - if err != nil || res.StatusCode() != 200 { + if err != nil { + return + } + if res.StatusCode() != 200 { + err = ErrCannotDownloadFile return } data = &res.Data -- cgit v1.2.3