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:
Gary Kim 2021-06-01 17:17:07 -04:00 committed by GitHub
parent c7897cca5d
commit 1d50da4b1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 248 additions and 96 deletions

View File

@ -74,12 +74,6 @@ func (b *Btalk) JoinChannel(channel config.ChannelInfo) error {
}
b.rooms = append(b.rooms, newRoom)
// Config
guestSuffix := " (Guest)"
if b.IsKeySet("GuestSuffix") {
guestSuffix = b.GetString("GuestSuffix")
}
go func() {
for msg := range c {
msg := msg
@ -90,35 +84,23 @@ func (b *Btalk) JoinChannel(channel config.ChannelInfo) error {
return
}
// ignore messages that are one of the following
// * not a message from a user
// * from ourselves
if msg.MessageType != ocs.MessageComment || msg.ActorID == b.user.User {
continue
}
remoteMessage := config.Message{
Text: formatRichObjectString(msg.Message, msg.MessageParameters),
Channel: newRoom.room.Token,
Username: DisplayName(msg, guestSuffix),
UserID: msg.ActorID,
Account: b.Account,
}
// It is possible for the ID to not be set on older versions of Talk so we only set it if
// the ID is not blank
if msg.ID != 0 {
remoteMessage.ID = strconv.Itoa(msg.ID)
}
// Handle Files
err = b.handleFiles(&remoteMessage, &msg)
if err != nil {
b.Log.Errorf("Error handling file: %#v", msg)
// Ignore messages that are from the bot user
if msg.ActorID == b.user.User {
continue
}
// Handle deleting messages
if msg.MessageType == ocs.MessageSystem && msg.Parent != nil && msg.Parent.MessageType == ocs.MessageDelete {
b.handleDeletingMessage(&msg, &newRoom)
continue
}
// Handle sending messages
if msg.MessageType == ocs.MessageComment {
b.handleSendingMessage(&msg, &newRoom)
continue
}
b.Log.Debugf("<= Message is %#v", remoteMessage)
b.Remote <- remoteMessage
}
}()
return nil
@ -131,11 +113,8 @@ func (b *Btalk) Send(msg config.Message) (string, error) {
return "", nil
}
// Talk currently only supports sending normal messages
if msg.Event != "" {
return "", nil
}
// Standard Message Send
if msg.Event == "" {
// Handle sending files if they are included
err := b.handleSendingFile(&msg, r)
if err != nil {
@ -153,6 +132,23 @@ func (b *Btalk) Send(msg config.Message) (string, error) {
return strconv.Itoa(sentMessage.ID), nil
}
// Message Deletion
if msg.Event == config.EventMsgDelete {
messageID, err := strconv.Atoi(msg.ID)
if err != nil {
return "", err
}
data, err := r.room.DeleteMessage(messageID)
if err != nil {
return "", err
}
return strconv.Itoa(data.ID), nil
}
// Message is not a type that is currently supported
return "", nil
}
func (b *Btalk) getRoom(token string) *Broom {
for _, r := range b.rooms {
if r.room.Token == token {
@ -208,6 +204,53 @@ func (b *Btalk) handleSendingFile(msg *config.Message, r *Broom) error {
return nil
}
func (b *Btalk) handleSendingMessage(msg *ocs.TalkRoomMessageData, r *Broom) {
remoteMessage := config.Message{
Text: formatRichObjectString(msg.Message, msg.MessageParameters),
Channel: r.room.Token,
Username: DisplayName(msg, b.guestSuffix()),
UserID: msg.ActorID,
Account: b.Account,
}
// It is possible for the ID to not be set on older versions of Talk so we only set it if
// the ID is not blank
if msg.ID != 0 {
remoteMessage.ID = strconv.Itoa(msg.ID)
}
// Handle Files
err := b.handleFiles(&remoteMessage, msg)
if err != nil {
b.Log.Errorf("Error handling file: %#v", msg)
return
}
b.Log.Debugf("<= Message is %#v", remoteMessage)
b.Remote <- remoteMessage
}
func (b *Btalk) handleDeletingMessage(msg *ocs.TalkRoomMessageData, r *Broom) {
remoteMessage := config.Message{
Event: config.EventMsgDelete,
Text: config.EventMsgDelete,
Channel: r.room.Token,
ID: strconv.Itoa(msg.Parent.ID),
Account: b.Account,
}
b.Log.Debugf("<= Message being deleted is %#v", remoteMessage)
b.Remote <- remoteMessage
}
func (b *Btalk) guestSuffix() string {
guestSuffix := " (Guest)"
if b.IsKeySet("GuestSuffix") {
guestSuffix = b.GetString("GuestSuffix")
}
return guestSuffix
}
// Spec: https://github.com/nextcloud/server/issues/1706#issue-182308785
func formatRichObjectString(message string, parameters map[string]ocs.RichObjectString) string {
for id, parameter := range parameters {
@ -228,7 +271,7 @@ func formatRichObjectString(message string, parameters map[string]ocs.RichObject
return message
}
func DisplayName(msg ocs.TalkRoomMessageData, suffix string) string {
func DisplayName(msg *ocs.TalkRoomMessageData, suffix string) string {
if msg.ActorType == ocs.ActorGuest {
if msg.ActorDisplayName == "" {
return "Guest"

2
go.mod
View File

@ -57,7 +57,7 @@ require (
github.com/zfjagann/golang-ring v0.0.0-20210116075443-7c86fdb43134
golang.org/x/image v0.0.0-20210504121937-7319ad40d33e
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
gomod.garykim.dev/nc-talk v0.1.7
gomod.garykim.dev/nc-talk v0.2.2
gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376
layeh.com/gumble v0.0.0-20200818122324-146f9205029b
)

4
go.sum
View File

@ -1278,8 +1278,8 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gomod.garykim.dev/nc-talk v0.1.7 h1:G2qsiRcyaj5FEADQlulsBAFJHs27tPmH9VtKK+at9SM=
gomod.garykim.dev/nc-talk v0.1.7/go.mod h1:DNucAJ6zeaumBEwV5NiYk+Eea8Ca+Q5f+plhz9F7d58=
gomod.garykim.dev/nc-talk v0.2.2 h1:+U+daJFPPuwM7yRXYazeMHZgIBSGP6SeQURO0O5a32I=
gomod.garykim.dev/nc-talk v0.2.2/go.mod h1:q/Adot/H7iqi+H4lANopV7/xcMf+sX3AZXUXqiITwok=
google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/api v0.0.0-20181220000619-583d854617af/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=

View File

@ -34,9 +34,13 @@ type SpreedCapabilities struct {
} `json:"attachments"`
Chat struct {
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"`
}

View File

@ -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:"-"`
}

View File

@ -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

View File

@ -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,7 +57,6 @@ 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"`
@ -83,6 +86,23 @@ type Capabilities struct {
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

2
vendor/modules.txt vendored
View File

@ -395,7 +395,7 @@ golang.org/x/text/unicode/norm
golang.org/x/text/width
# golang.org/x/time v0.0.0-20201208040808-7e3f01d25324
golang.org/x/time/rate
# gomod.garykim.dev/nc-talk v0.1.7
# gomod.garykim.dev/nc-talk v0.2.2
## explicit
gomod.garykim.dev/nc-talk/constants
gomod.garykim.dev/nc-talk/ocs