From a79e632cdc26f3870b0d8b37f9a94bdcf6f47711 Mon Sep 17 00:00:00 2001 From: Gary Kim Date: Sat, 19 Jun 2021 15:45:19 -0400 Subject: [PATCH] Add support for separate display name (nctalk) (#1506) Signed-off-by: Gary Kim --- bridge/nctalk/nctalk.go | 19 ++++++++++--- go.mod | 2 +- go.sum | 2 ++ matterbridge.toml.sample | 5 +++- vendor/gomod.garykim.dev/nc-talk/room/room.go | 27 +++++++++++++++---- vendor/modules.txt | 2 +- 6 files changed, 45 insertions(+), 12 deletions(-) diff --git a/bridge/nctalk/nctalk.go b/bridge/nctalk/nctalk.go index 9d0d4517..82acba4b 100644 --- a/bridge/nctalk/nctalk.go +++ b/bridge/nctalk/nctalk.go @@ -85,7 +85,7 @@ func (b *Btalk) JoinChannel(channel config.ChannelInfo) error { } // Ignore messages that are from the bot user - if msg.ActorID == b.user.User { + if msg.ActorID == b.user.User || msg.ActorType == "bridged" { continue } @@ -123,7 +123,7 @@ func (b *Btalk) Send(msg config.Message) (string, error) { return "", nil } - sentMessage, err := r.room.SendMessage(msg.Username + msg.Text) + sentMessage, err := b.sendText(r, &msg, msg.Text) if err != nil { b.Log.Errorf("Could not send message to room %v from %v: %v", msg.Channel, msg.Username, err) @@ -158,6 +158,17 @@ func (b *Btalk) getRoom(token string) *Broom { return nil } +func (b *Btalk) sendText(r *Broom, msg *config.Message, text string) (*ocs.TalkRoomMessageData, error) { + messageToSend := &room.Message{Message: msg.Username + text} + + if b.GetBool("SeparateDisplayName") { + messageToSend.Message = text + messageToSend.ActorDisplayName = msg.Username + } + + return r.room.SendComplexMessage(messageToSend) +} + func (b *Btalk) handleFiles(mmsg *config.Message, message *ocs.TalkRoomMessageData) error { for _, parameter := range message.MessageParameters { if parameter.Type == ocs.ROSTypeFile { @@ -190,12 +201,12 @@ func (b *Btalk) handleSendingFile(msg *config.Message, r *Broom) error { continue } - message := msg.Username + message := "" if fi.Comment != "" { message += fi.Comment + " " } message += fi.URL - _, err := r.room.SendMessage(message) + _, err := b.sendText(r, msg, message) if err != nil { return err } diff --git a/go.mod b/go.mod index 0799bed2..69914eee 100644 --- a/go.mod +++ b/go.mod @@ -52,7 +52,7 @@ require ( github.com/zfjagann/golang-ring v0.0.0-20210116075443-7c86fdb43134 golang.org/x/image v0.0.0-20210607152325-775e3b0c77b9 golang.org/x/oauth2 v0.0.0-20210615190721-d04028783cf1 - gomod.garykim.dev/nc-talk v0.2.2 + gomod.garykim.dev/nc-talk v0.3.0 gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376 layeh.com/gumble v0.0.0-20200818122324-146f9205029b ) diff --git a/go.sum b/go.sum index c6ff71e7..63f18f9c 100644 --- a/go.sum +++ b/go.sum @@ -1319,6 +1319,8 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1N golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 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= +gomod.garykim.dev/nc-talk v0.3.0 h1:MZxLc/gX2/+bdOw4xt6pi+qQFUQld1woGfw1hEJ0fbM= +gomod.garykim.dev/nc-talk v0.3.0/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= diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index c48ed835..9dadd520 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -1469,6 +1469,9 @@ Password = "talkuserpass" # Suffix for Guest Users GuestSuffix = " (Guest)" +# Separate display name (Note: needs to be configured from Nextcloud Talk to work) +SeparateDisplayName=false + ################################################################### # Mumble ################################################################### @@ -1883,7 +1886,7 @@ enable=true # ------------------------------------------------------------------------------------------------------------------------------------- # telegram | chatid | -123456789 | A large negative number. see https://www.linkedin.com/pulse/telegram-bots-beginners-marco-frau # ------------------------------------------------------------------------------------------------------------------------------------- - # vk | peerid | 2000000002 | A number that starts form 2000000000. Use --debug and send any message in chat to get PeerID in the logs + # vk | peerid | 2000000002 | A number that starts form 2000000000. Use --debug and send any message in chat to get PeerID in the logs # ------------------------------------------------------------------------------------------------------------------------------------- # whatsapp | group JID | 48111222333-123455678999@g.us | A unique group JID. If you specify an empty string, bridge will list all the possibilities # | "Group Name" | "Family Chat" | if you specify a group name, the bridge will find hint the JID to specify. Names can change over time and are not stable. diff --git a/vendor/gomod.garykim.dev/nc-talk/room/room.go b/vendor/gomod.garykim.dev/nc-talk/room/room.go index eb72c2c0..9e94f9d7 100644 --- a/vendor/gomod.garykim.dev/nc-talk/room/room.go +++ b/vendor/gomod.garykim.dev/nc-talk/room/room.go @@ -56,6 +56,21 @@ type TalkRoom struct { Token string } +// Message represents a message to be sent +type Message struct { + Message string + ActorDisplayName string + ReplyTo int +} + +func (t *Message) toParameters() map[string]string { + return map[string]string{ + "message": t.Message, + "actorDisplayName": t.ActorDisplayName, + "replyTo": strconv.Itoa(t.ReplyTo), + } +} + // NewTalkRoom returns a new TalkRoom instance // Token should be the Nextcloud Room Token (e.g. "d6zoa2zs" if the room URL is https://cloud.mydomain.me/call/d6zoa2zs) func NewTalkRoom(tuser *user.TalkUser, token string) (*TalkRoom, error) { @@ -71,17 +86,19 @@ func NewTalkRoom(tuser *user.TalkUser, token string) (*TalkRoom, error) { }, nil } -// SendMessage sends a message in the Talk room +// SendMessage sends a string message in the Talk room func (t *TalkRoom) SendMessage(msg string) (*ocs.TalkRoomMessageData, error) { + return t.SendComplexMessage(&Message{Message: msg}) +} + +// SendComplexMessage sends a Message type message in the talk room +func (t *TalkRoom) SendComplexMessage(msg *Message) (*ocs.TalkRoomMessageData, error) { url := t.User.NextcloudURL + constants.BaseEndpoint + "chat/" + t.Token - requestParams := map[string]string{ - "message": msg, - } client := t.User.RequestClient(request.Client{ URL: url, Method: "POST", - Params: requestParams, + Params: msg.toParameters(), }) res, err := client.Do() if err != nil { diff --git a/vendor/modules.txt b/vendor/modules.txt index 9e990f84..4a06f74f 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -386,7 +386,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.2.2 +# gomod.garykim.dev/nc-talk v0.3.0 ## explicit gomod.garykim.dev/nc-talk/constants gomod.garykim.dev/nc-talk/ocs