From 9da7f15e8f7eac024c0c7fc0377a0b581c2366ef Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 26 Aug 2018 17:02:32 +0300 Subject: [PATCH] Add Matrix->WhatsApp room name changes --- ROADMAP.md | 2 +- matrix.go | 41 +++++++++++++++++++++++++++++++++++++---- portal.go | 2 +- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index f77d52b..59e6de8 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -16,7 +16,7 @@ * [ ] Leave * [ ] Kick * [ ] Room metadata changes - * [ ] Name + * [x] Name * [ ] Avatar * [ ] Topic * [ ] Initial room metadata diff --git a/matrix.go b/matrix.go index 78beccc..aa63e57 100644 --- a/matrix.go +++ b/matrix.go @@ -17,11 +17,12 @@ package main import ( - "maunium.net/go/gomatrix" - "maunium.net/go/mautrix-whatsapp/types" - "maunium.net/go/mautrix-appservice" - "maunium.net/go/maulogger" "strings" + + "maunium.net/go/gomatrix" + "maunium.net/go/maulogger" + "maunium.net/go/mautrix-appservice" + "maunium.net/go/mautrix-whatsapp/types" ) type MatrixHandler struct { @@ -40,6 +41,9 @@ func NewMatrixHandler(bridge *Bridge) *MatrixHandler { } bridge.EventProcessor.On(gomatrix.EventMessage, handler.HandleMessage) bridge.EventProcessor.On(gomatrix.StateMember, handler.HandleMembership) + bridge.EventProcessor.On(gomatrix.StateRoomName, handler.HandleRoomMetadata) + bridge.EventProcessor.On(gomatrix.StateRoomAvatar, handler.HandleRoomMetadata) + bridge.EventProcessor.On(gomatrix.StateTopic, handler.HandleRoomMetadata) return handler } @@ -94,6 +98,35 @@ func (mx *MatrixHandler) HandleMembership(evt *gomatrix.Event) { } } +func (mx *MatrixHandler) HandleRoomMetadata(evt *gomatrix.Event) { + user := mx.bridge.GetUser(types.MatrixUserID(evt.Sender)) + if user == nil { + return + } + + portal := user.GetPortalByMXID(evt.RoomID) + if portal == nil || portal.IsPrivateChat() { + return + } + + var resp <-chan string + var err error + switch evt.Type { + case gomatrix.StateRoomName: + resp, err = user.Conn.UpdateGroupSubject(evt.Content.Name, portal.JID) + case gomatrix.StateRoomAvatar: + return + case gomatrix.StateTopic: + return + } + if err != nil { + mx.log.Errorln(err) + } else { + out := <-resp + mx.log.Infoln(out) + } +} + func (mx *MatrixHandler) HandleMessage(evt *gomatrix.Event) { roomID := types.MatrixRoomID(evt.RoomID) user := mx.bridge.GetUser(types.MatrixUserID(evt.Sender)) diff --git a/portal.go b/portal.go index 4adbb45..a350fad 100644 --- a/portal.go +++ b/portal.go @@ -347,7 +347,7 @@ func (portal *Portal) CreateMatrixRoom() error { InitialState: []*gomatrix.Event{{ Type: gomatrix.StatePowerLevels, Content: gomatrix.Content{ - PowerLevels: portal.GetBasePowerLevels(), + PowerLevels: *portal.GetBasePowerLevels(), }, }}, })