This commit is contained in:
Alex 2020-02-23 20:24:50 +01:00
parent 0f6be9663e
commit ec9bc9b752
10 changed files with 95 additions and 96 deletions

View file

@ -286,24 +286,24 @@ func (a *Account) eventInternal(event *Event) error {
if err != nil {
return err
}
content := map[string]interface{} {
"body": mxfile.Filename(),
content := map[string]interface{}{
"body": mxfile.Filename(),
"filename": mxfile.Filename(),
"url": fmt.Sprintf("mxc://%s/%s", mxfile.MxcServer, mxfile.MxcMediaId),
"url": fmt.Sprintf("mxc://%s/%s", mxfile.MxcServer, mxfile.MxcMediaId),
}
if sz := mxfile.ImageSize(); sz != nil {
content["msgtype"] = "m.image"
content["info"] = map[string]interface{} {
content["info"] = map[string]interface{}{
"mimetype": mxfile.Mimetype(),
"size": mxfile.Size(),
"w": sz.Width,
"h": sz.Height,
"size": mxfile.Size(),
"w": sz.Width,
"h": sz.Height,
}
} else {
content["msgtype"] = "m.file"
content["info"] = map[string]interface{} {
content["info"] = map[string]interface{}{
"mimetype": mxfile.Mimetype(),
"size": mxfile.Size(),
"size": mxfile.Size(),
}
}
err = mx.SendAs(mx_room_id, "m.room.message", content, mx_user_id)

View file

@ -41,7 +41,7 @@ func InitDb() error {
type DbCache struct {
gorm.Model
Key string `gorm:"unique_index"`
Key string `gorm:"unique_index"`
Value string
}

View file

@ -136,12 +136,12 @@ type UserInfo struct {
// If non-empty, the Filename of the avatar object will be used by Easybridge
// to deduplicate the update events and prevent needless reuploads.
// Example strategy that works for the mattermost backend: use the update timestamp as fictious file name
Avatar MediaObject
Avatar MediaObject
}
type RoomInfo struct {
Name string
Topic string
Name string
Topic string
// Same deduplication comment as for UserInfo.Avatar
Picture MediaObject
@ -164,6 +164,6 @@ type MediaObject interface {
}
type ImageSize struct {
Width int
Width int
Height int
}

View file

@ -1,16 +1,16 @@
package mattermost
import (
"net/http"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
_ "os"
"strings"
"time"
"io/ioutil"
"encoding/json"
"github.com/mattermost/mattermost-server/model"
"github.com/42wim/matterbridge/matterclient"
"github.com/mattermost/mattermost-server/model"
log "github.com/sirupsen/logrus"
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
@ -22,19 +22,18 @@ import (
type Mattermost struct {
handler Handler
server string
server string
username string
teams map[string]bool
teams map[string]bool
conn *matterclient.MMClient
conn *matterclient.MMClient
handlerStopChan chan bool
usermap map[string]string // map username to mm user id
sentjoinedmap map[string]bool // map username/room name to bool
userdisplaynamemap map[UserID]string // map username to last displayname
usermap map[string]string // map username to mm user id
sentjoinedmap map[string]bool // map username/room name to bool
userdisplaynamemap map[UserID]string // map username to last displayname
}
func (mm *Mattermost) SetHandler(h Handler) {
mm.handler = h
}
@ -318,7 +317,7 @@ func (mm *Mattermost) handleConnected() {
// Update room info
room_info := &RoomInfo{
Name: ch.DisplayName,
Name: ch.DisplayName,
Topic: ch.Header,
}
for _, t := range mm.conn.OtherTeams {
@ -446,8 +445,8 @@ func (mm *Mattermost) ensureJoined(user *model.User, roomId RoomID) {
if _, ok := mm.sentjoinedmap[cache_key]; !ok {
mm.handler.Event(&Event{
Author: userId,
Room: roomId,
Type: EVENT_JOIN,
Room: roomId,
Type: EVENT_JOIN,
})
mm.sentjoinedmap[cache_key] = true
}
@ -484,10 +483,10 @@ func (mm *Mattermost) handlePost(channel_name string, post *model.Post, only_mes
// Build message event
msg_ev := &Event{
Id: post.Id,
Id: post.Id,
Author: userId,
Text: post.Message,
Type: EVENT_MESSAGE,
Text: post.Message,
Type: EVENT_MESSAGE,
}
if post.Type == "me" {
msg_ev.Type = EVENT_ACTION
@ -511,7 +510,7 @@ func (mm *Mattermost) handlePost(channel_name string, post *model.Post, only_mes
}
if file.Width > 0 {
media_object.ObjectImageSize = &ImageSize{
Width: file.Width,
Width: file.Width,
Height: file.Height,
}
}

View file

@ -58,10 +58,10 @@ func (m *FileMediaObject) URL() string {
// ----
type UrlMediaObject struct {
ObjectFilename string
ObjectSize int64
ObjectMimetype string
ObjectURL string
ObjectFilename string
ObjectSize int64
ObjectMimetype string
ObjectURL string
ObjectImageSize *ImageSize
}
@ -96,10 +96,10 @@ func (m *UrlMediaObject) URL() string {
// ----
type BlobMediaObject struct {
ObjectFilename string
ObjectMimetype string
ObjectFilename string
ObjectMimetype string
ObjectImageSize *ImageSize
ObjectData []byte
ObjectData []byte
}
func (m *BlobMediaObject) Filename() string {
@ -129,6 +129,7 @@ func (m *BlobMediaObject) URL() string {
type nullCloseReader struct {
io.Reader
}
func (ncr nullCloseReader) Close() error {
return nil
}
@ -136,10 +137,10 @@ func (ncr nullCloseReader) Close() error {
// ----
type LazyBlobMediaObject struct {
ObjectFilename string
ObjectMimetype string
ObjectFilename string
ObjectMimetype string
ObjectImageSize *ImageSize
ObjectData []byte
ObjectData []byte
GetFn func(o *LazyBlobMediaObject) error
}

View file

@ -3,12 +3,12 @@ package xmpp
import (
"time"
//"os"
"strings"
"fmt"
"crypto/tls"
"fmt"
"strings"
log "github.com/sirupsen/logrus"
gxmpp "github.com/mattn/go-xmpp"
log "github.com/sirupsen/logrus"
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
)
@ -22,16 +22,16 @@ type XMPP struct {
handler Handler
connectorLoopNum int
connected bool
timeout int
connected bool
timeout int
server string
port int
ssl bool
jid string
server string
port int
ssl bool
jid string
jid_localpart string
password string
nickname string
password string
nickname string
conn *gxmpp.Client
@ -42,7 +42,7 @@ func (xm *XMPP) SetHandler(h Handler) {
xm.handler = h
}
func(xm *XMPP) Protocol() string {
func (xm *XMPP) Protocol() string {
return "xmpp"
}
@ -97,7 +97,7 @@ func (xm *XMPP) Configure(c Configuration) error {
go xm.connectLoop(xm.connectorLoopNum)
for i := 0; i < 42; i++ {
time.Sleep(time.Duration(1)*time.Second)
time.Sleep(time.Duration(1) * time.Second)
if xm.connected {
return nil
}
@ -116,12 +116,12 @@ func (xm *XMPP) connectLoop(num int) {
InsecureSkipVerify: true,
}
options := gxmpp.Options{
Host: xm.server,
User: xm.jid,
Password: xm.password,
NoTLS: true,
StartTLS: xm.ssl,
Session: true,
Host: xm.server,
User: xm.jid,
Password: xm.password,
NoTLS: true,
StartTLS: xm.ssl,
Session: true,
TLSConfig: tc,
}
var err error
@ -237,8 +237,8 @@ func (xm *XMPP) handleXMPP() error {
user := UserID(remote[1] + "@" + remote[0])
event := &Event{
Type: EVENT_JOIN,
Room: RoomID(remote[0]),
Type: EVENT_JOIN,
Room: RoomID(remote[0]),
Author: user,
}
if v.Type == "unavailable" {
@ -264,8 +264,8 @@ func (xm *XMPP) SetUserInfo(info *UserInfo) error {
func (xm *XMPP) SetRoomInfo(roomId RoomID, info *RoomInfo) error {
if info.Topic != "" {
_, err := xm.conn.Send(gxmpp.Chat{
Type: "groupchat",
Remote: string(roomId),
Type: "groupchat",
Remote: string(roomId),
Subject: info.Topic,
})
if err != nil {
@ -324,16 +324,16 @@ func (xm *XMPP) Send(event *Event) error {
fmt.Printf("xm *XMPP Send %#v\n", event)
if len(event.Recipient) > 0 {
_, err := xm.conn.Send(gxmpp.Chat{
Type: "chat",
Type: "chat",
Remote: string(event.Recipient),
Text: event.Text,
Text: event.Text,
})
return err
} else if len(event.Room) > 0 {
_, err := xm.conn.Send(gxmpp.Chat{
Type: "groupchat",
Type: "groupchat",
Remote: string(event.Room),
Text: event.Text,
Text: event.Text,
})
return err
} else {
@ -346,4 +346,3 @@ func (xm *XMPP) Close() {
xm.conn = nil
xm.connectorLoopNum += 1
}

View file

@ -18,8 +18,8 @@ import (
"git.deuxfleurs.fr/Deuxfleurs/easybridge/appservice"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/irc"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/xmpp"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/mattermost"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/xmpp"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
)
@ -102,7 +102,7 @@ func readRegistration(file string) mxlib.Registration {
AsToken: hex.EncodeToString(rnd[:32]),
HsToken: hex.EncodeToString(rnd[32:]),
SenderLocalpart: "_ezbr_",
RateLimited: false,
RateLimited: false,
Namespaces: mxlib.RegistrationNamespaceSet{
Users: []mxlib.RegistrationNamespace{
mxlib.RegistrationNamespace{

View file

@ -1,12 +1,12 @@
package mxlib
import (
"strings"
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
log "github.com/sirupsen/logrus"
@ -280,7 +280,7 @@ func (mx *Client) RoomAvatarAs(room string, pic connector.MediaObject, as_user s
"url": mo.MxcUri(),
"info": map[string]interface{}{
"mimetype": mo.Mimetype(),
"size": mo.Size(),
"size": mo.Size(),
},
}
return mx.PutStateAs(room, "m.room.avatar", "", content, as_user)
@ -309,7 +309,7 @@ func (mx *Client) UploadMedia(m connector.MediaObject) (*MediaObject, error) {
mx.Server+"/_matrix/media/r0/upload?filename="+url.QueryEscape(m.Filename()),
reader)
req.Header.Add("Content-Type", m.Mimetype())
req.ContentLength = m.Size() // TODO: this wasn't specified as mandatory in the matrix client/server spec, do a PR to fix this
req.ContentLength = m.Size() // TODO: this wasn't specified as mandatory in the matrix client/server spec, do a PR to fix this
var resp UploadResponse
err = mx.DoAndParse(req, &resp)
@ -323,12 +323,12 @@ func (mx *Client) UploadMedia(m connector.MediaObject) (*MediaObject, error) {
}
media := &MediaObject{
mxClient: mx,
filename: m.Filename(),
size: m.Size(),
mimetype: m.Mimetype(),
imageSize: m.ImageSize(),
MxcServer: mxc[0],
mxClient: mx,
filename: m.Filename(),
size: m.Size(),
mimetype: m.Mimetype(),
imageSize: m.ImageSize(),
MxcServer: mxc[0],
MxcMediaId: mxc[1],
}
return media, nil
@ -339,16 +339,16 @@ func (mx *Client) ParseMediaInfo(content map[string]interface{}) *MediaObject {
info := content["info"].(map[string]interface{})
mxc := strings.Split(strings.Replace(content["url"].(string), "mxc://", "", 1), "/")
media := &MediaObject{
mxClient: mx,
filename: content["body"].(string),
size: int64(info["size"].(float64)),
mimetype: info["mimetype"].(string),
MxcServer: mxc[0],
mxClient: mx,
filename: content["body"].(string),
size: int64(info["size"].(float64)),
mimetype: info["mimetype"].(string),
MxcServer: mxc[0],
MxcMediaId: mxc[1],
}
if content["msgtype"].(string) == "m.image" {
media.imageSize = &connector.ImageSize{
Width: int(info["w"].(float64)),
Width: int(info["w"].(float64)),
Height: int(info["h"].(float64)),
}
}

View file

@ -1,21 +1,21 @@
package mxlib
import (
"io"
"fmt"
"net/url"
"io"
"net/http"
"net/url"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
)
type MediaObject struct {
mxClient *Client
filename string
size int64
mimetype string
imageSize *connector.ImageSize
MxcServer string
mxClient *Client
filename string
size int64
mimetype string
imageSize *connector.ImageSize
MxcServer string
MxcMediaId string
}

View file

@ -10,7 +10,7 @@ type Registration struct {
AsToken string `yaml:"as_token"`
HsToken string `yaml:"hs_token"`
SenderLocalpart string `yaml:"sender_localpart"`
RateLimited bool `yaml:"rate_limited"`
RateLimited bool `yaml:"rate_limited"`
Namespaces RegistrationNamespaceSet `yaml:"namespaces"`
}