Set name for status broadcast room when creating and reformat example config

Fixes #27
This commit is contained in:
Tulir Asokan 2019-03-14 00:38:11 +02:00
parent 67a041c06d
commit c88e299348
5 changed files with 113 additions and 82 deletions

View file

@ -274,7 +274,7 @@ func (handler *CommandHandler) CommandPM(ce *CommandEvent) {
} }
return return
} }
err := portal.CreateMatrixRoom([]string{user.MXID}) err := portal.CreateMatrixRoom(user)
if err != nil { if err != nil {
ce.Reply(fmt.Sprintf("Failed to create portal room: %v", err)) ce.Reply(fmt.Sprintf("Failed to create portal room: %v", err))
return return

View file

@ -17,10 +17,11 @@ appservice:
# Database config. # Database config.
database: database:
# The database type. Only "sqlite3" is supported. # The database type. "sqlite3" and "postgres" are supported.
type: sqlite3 type: sqlite3
# The database URI. Usually file name. https://github.com/mattn/go-sqlite3#connection-string # The database URI.
# postres example: postgres://synapse:changeme@db/whatsapp?sslmode=disable # SQLite: File name is enough. https://github.com/mattn/go-sqlite3#connection-string
# Postgres: Connection string. For example, postgres://user:password@host/database
uri: mautrix-whatsapp.db uri: mautrix-whatsapp.db
# Path to the Matrix room state store. # Path to the Matrix room state store.
state_store_path: ./mx-state.json state_store_path: ./mx-state.json

1
go.mod
View file

@ -4,6 +4,7 @@ require (
github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f // indirect github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f // indirect
github.com/Rhymen/go-whatsapp v0.0.0-20190208090600-c1173899de99 github.com/Rhymen/go-whatsapp v0.0.0-20190208090600-c1173899de99
github.com/gorilla/mux v1.7.0 // indirect github.com/gorilla/mux v1.7.0 // indirect
github.com/lib/pq v1.0.0
github.com/mattn/go-colorable v0.1.1 // indirect github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-sqlite3 v1.10.0 github.com/mattn/go-sqlite3 v1.10.0
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect

2
go.sum
View file

@ -10,6 +10,8 @@ github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U=
github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg= github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=

View file

@ -300,7 +300,10 @@ func (portal *Portal) UpdateAvatar(user *User, avatar *whatsappExt.ProfilePicInf
func (portal *Portal) UpdateName(name string, setBy types.WhatsAppID) bool { func (portal *Portal) UpdateName(name string, setBy types.WhatsAppID) bool {
if portal.Name != name { if portal.Name != name {
intent := portal.bridge.GetPuppetByJID(setBy).Intent() intent := portal.MainIntent()
if len(setBy) > 0 {
intent = portal.bridge.GetPuppetByJID(setBy).Intent()
}
_, err := intent.SetRoomName(portal.MXID, name) _, err := intent.SetRoomName(portal.MXID, name)
if err == nil { if err == nil {
portal.Name = name portal.Name = name
@ -313,7 +316,10 @@ func (portal *Portal) UpdateName(name string, setBy types.WhatsAppID) bool {
func (portal *Portal) UpdateTopic(topic string, setBy types.WhatsAppID) bool { func (portal *Portal) UpdateTopic(topic string, setBy types.WhatsAppID) bool {
if portal.Topic != topic { if portal.Topic != topic {
intent := portal.bridge.GetPuppetByJID(setBy).Intent() intent := portal.MainIntent()
if len(setBy) > 0 {
intent = portal.bridge.GetPuppetByJID(setBy).Intent()
}
_, err := intent.SetRoomTopic(portal.MXID, topic) _, err := intent.SetRoomTopic(portal.MXID, topic)
if err == nil { if err == nil {
portal.Topic = topic portal.Topic = topic
@ -325,6 +331,14 @@ func (portal *Portal) UpdateTopic(topic string, setBy types.WhatsAppID) bool {
} }
func (portal *Portal) UpdateMetadata(user *User) bool { func (portal *Portal) UpdateMetadata(user *User) bool {
if portal.IsPrivateChat() {
return false
} else if portal.IsStatusBroadcastRoom() {
update := false
update = portal.UpdateName("WhatsApp Status Broadcast", "") || update
update = portal.UpdateTopic("WhatsApp status updates from your contacts", "") || update
return update
}
metadata, err := user.Conn.GetGroupMetaData(portal.Key.JID) metadata, err := user.Conn.GetGroupMetaData(portal.Key.JID)
if err != nil { if err != nil {
portal.log.Errorln(err) portal.log.Errorln(err)
@ -354,7 +368,7 @@ func (portal *Portal) Sync(user *User, contact whatsapp.Contact) {
if len(portal.MXID) == 0 { if len(portal.MXID) == 0 {
portal.Name = contact.Name portal.Name = contact.Name
err := portal.CreateMatrixRoom([]string{user.MXID}) err := portal.CreateMatrixRoom(user)
if err != nil { if err != nil {
portal.log.Errorln("Failed to create portal room:", err) portal.log.Errorln("Failed to create portal room:", err)
return return
@ -368,7 +382,9 @@ func (portal *Portal) Sync(user *User, contact whatsapp.Contact) {
update := false update := false
update = portal.UpdateMetadata(user) || update update = portal.UpdateMetadata(user) || update
if !portal.IsStatusBroadcastRoom() {
update = portal.UpdateAvatar(user, nil) || update update = portal.UpdateAvatar(user, nil) || update
}
if update { if update {
portal.Update() portal.Update()
} }
@ -459,7 +475,7 @@ func (portal *Portal) RestrictMetadataChanges(restrict bool) {
} }
} }
func (portal *Portal) CreateMatrixRoom(invite []string) error { func (portal *Portal) CreateMatrixRoom(user *User) error {
portal.roomCreateLock.Lock() portal.roomCreateLock.Lock()
defer portal.roomCreateLock.Unlock() defer portal.roomCreateLock.Unlock()
if len(portal.MXID) > 0 { if len(portal.MXID) > 0 {
@ -471,20 +487,27 @@ func (portal *Portal) CreateMatrixRoom(invite []string) error {
return err return err
} }
name := portal.Name
topic := portal.Topic
isPrivateChat := false isPrivateChat := false
if portal.IsPrivateChat() { if portal.IsPrivateChat() {
name = "" portal.Name = ""
topic = "WhatsApp private chat" portal.Topic = "WhatsApp private chat"
isPrivateChat = true isPrivateChat = true
} else if portal.IsStatusBroadcastRoom() {
portal.Name = "WhatsApp Status Broadcast"
portal.Topic = "WhatsApp status updates from your contacts"
} else {
metadata, err := user.Conn.GetGroupMetaData(portal.Key.JID)
if err == nil && metadata.Status == 0 {
portal.Name = metadata.Name
portal.Topic = metadata.Topic
}
} }
resp, err := intent.CreateRoom(&mautrix.ReqCreateRoom{ resp, err := intent.CreateRoom(&mautrix.ReqCreateRoom{
Visibility: "private", Visibility: "private",
Name: name, Name: portal.Name,
Topic: topic, Topic: portal.Topic,
Invite: invite, Invite: []string{user.MXID},
Preset: "private_chat", Preset: "private_chat",
IsDirect: isPrivateChat, IsDirect: isPrivateChat,
InitialState: []*mautrix.Event{{ InitialState: []*mautrix.Event{{
@ -510,6 +533,10 @@ func (portal *Portal) IsPrivateChat() bool {
return *portal.isPrivate return *portal.isPrivate
} }
func (portal *Portal) IsStatusBroadcastRoom() bool {
return portal.Key.JID == "status@broadcast"
}
func (portal *Portal) MainIntent() *appservice.IntentAPI { func (portal *Portal) MainIntent() *appservice.IntentAPI {
if portal.IsPrivateChat() { if portal.IsPrivateChat() {
return portal.bridge.GetPuppetByJID(portal.Key.JID).Intent() return portal.bridge.GetPuppetByJID(portal.Key.JID).Intent()
@ -560,7 +587,7 @@ func (portal *Portal) HandleTextMessage(source *User, message whatsapp.TextMessa
} }
defer lock.Unlock() defer lock.Unlock()
err := portal.CreateMatrixRoom([]string{source.MXID}) err := portal.CreateMatrixRoom(source)
if err != nil { if err != nil {
portal.log.Errorln("Failed to create portal room:", err) portal.log.Errorln("Failed to create portal room:", err)
return return
@ -595,7 +622,7 @@ func (portal *Portal) HandleMediaMessage(source *User, download func() ([]byte,
} }
defer lock.Unlock() defer lock.Unlock()
err := portal.CreateMatrixRoom([]string{source.MXID}) err := portal.CreateMatrixRoom(source)
if err != nil { if err != nil {
portal.log.Errorln("Failed to create portal room:", err) portal.log.Errorln("Failed to create portal room:", err)
return return