Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 83f314e6c08654c42c03b180cabc49ee286185be Author: Gary Kim <gary@garykim.dev> Date: Mon Oct 19 17:16:34 2020 +0000 Add support for downloading files (nctalk) (#1249) Signed-off-by: Gary Kim <gary@garykim.dev> diff --git a/bridge/nctalk/nctalk.go b/bridge/nctalk/nctalk.go index 23b5913..6af819a 100644 --- a/bridge/nctalk/nctalk.go +++ b/bridge/nctalk/nctalk.go @@ -97 +96 @@ import (   "github.com/42wim/matterbridge/bridge"   "github.com/42wim/matterbridge/bridge/config"   - talk "gomod.garykim.dev/nc-talk"   "gomod.garykim.dev/nc-talk/ocs"   "gomod.garykim.dev/nc-talk/room"   "gomod.garykim.dev/nc-talk/user" @@ -618 +6012 @@ func (b *Btalk) Disconnect() error {  }    func (b *Btalk) JoinChannel(channel config.ChannelInfo) error { + tr, err := room.NewTalkRoom(b.user, channel.Name) + if err != nil { + return err + }   newRoom := Broom{ - room: talk.NewRoom(b.user, channel.Name), + room: tr,   }   newRoom.ctx, newRoom.ctxCancel = context.WithCancel(context.Background())   c, err := newRoom.room.ReceiveMessages(newRoom.ctx) @@ -796 +827 @@ func (b *Btalk) JoinChannel(channel config.ChannelInfo) error {     go func() {   for msg := range c { + msg := msg   // ignore messages that are one of the following   // * not a message from a user   // * from ourselves @@ -976 +10115 @@ func (b *Btalk) JoinChannel(channel config.ChannelInfo) error {   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) + + continue + } +   b.Log.Debugf("<= Message is %#v", remoteMessage)   b.Remote <- remoteMessage   } @@ -1326 +14531 @@ func (b *Btalk) getRoom(token string) *Broom {   return nil  }   +func (b *Btalk) handleFiles(mmsg *config.Message, message *ocs.TalkRoomMessageData) error { + for _, parameter := range message.MessageParameters { + if parameter.Type == ocs.ROSTypeFile { + // Get the file + file, err := b.user.DownloadFile(parameter.Path) + if err != nil { + return err + } + + if mmsg.Extra == nil { + mmsg.Extra = make(map[string][]interface{}) + } + + mmsg.Extra["file"] = append(mmsg.Extra["file"], config.FileInfo{ + Name: parameter.Name, + Data: file, + Size: int64(len(*file)), + Avatar: false, + }) + } + } + + return nil +} +  // 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 { @@ -1427 +1807 @@ func formatRichObjectString(message string, parameters map[string]ocs.RichObject   text = "@" + text   case ocs.ROSTypeFile:   if parameter.Link != "" { - text = parameter.Link + text = parameter.Name   }   }   diff --git a/go.mod b/go.mod index 35d9138..8cf37bb 100644 --- a/go.mod +++ b/go.mod @@ -507 +507 @@ require (   github.com/zfjagann/golang-ring v0.0.0-20190304061218-d34796e0a6c2   golang.org/x/image v0.0.0-20200801110659-972c09e46d76   golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43 - gomod.garykim.dev/nc-talk v0.1.4 + gomod.garykim.dev/nc-talk v0.1.5   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 66535e9..e016983 100644 --- a/go.sum +++ b/go.sum @@ -9936 +9938 @@ 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.1.4 h1:U9viudEgq/biocorgWvZRVR+27IPEczYl/yszSvzN+8=  gomod.garykim.dev/nc-talk v0.1.4/go.mod h1:zKg8yxCk2KaTy6aPDEfRac0Jik72czX+nRsG8CZuhtc= +gomod.garykim.dev/nc-talk v0.1.5 h1:zZ/FviVpwJuhD/YrKiAvs6Z3Oew/DL/w6RKbKaanhFA= +gomod.garykim.dev/nc-talk v0.1.5/go.mod h1:zKg8yxCk2KaTy6aPDEfRac0Jik72czX+nRsG8CZuhtc=  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=