Add greeting when joining rooms

This commit is contained in:
Luca 2022-07-25 01:07:28 +02:00
parent a18353b531
commit cecd903810
1 changed files with 48 additions and 29 deletions

View File

@ -20,6 +20,7 @@ type Client struct {
config *config.MatrixConfig config *config.MatrixConfig
db *sql.DB db *sql.DB
deviceID id.DeviceID deviceID id.DeviceID
greetedRooms []id.RoomID
olmMachine *crypto.OlmMachine olmMachine *crypto.OlmMachine
store *sqlStore store *sqlStore
syncer *mautrix.DefaultSyncer syncer *mautrix.DefaultSyncer
@ -50,7 +51,7 @@ func NewClient(config *config.MatrixConfig, db *sql.DB) (*Client, error) {
deviceID := loadDeviceID(db, userID) deviceID := loadDeviceID(db, userID)
return &Client{client, config, db, deviceID, nil, store, syncer, userID}, nil return &Client{client, config, db, deviceID, nil, nil, store, syncer, userID}, nil
} }
func (c *Client) Login() error { func (c *Client) Login() error {
@ -123,9 +124,39 @@ func (c *Client) Sync(ctx context.Context, wg *sync.WaitGroup) {
} }
} }
func (c *Client) Broadcast(message *event.MessageEventContent) { func (c *Client) Send(roomID id.RoomID, message *event.MessageEventContent) {
content := event.Content{Parsed: message} content := event.Content{Parsed: message}
if c.store.IsEncrypted(roomID) {
encrypted, err := c.olmMachine.EncryptMegolmEvent(roomID, event.EventMessage, &content)
if err == crypto.NoGroupSession || err == crypto.SessionExpired || err == crypto.SessionNotShared {
err = c.olmMachine.ShareGroupSession(roomID, c.store.GetRoomMembers(roomID))
if err != nil {
log.Print(err)
return
}
encrypted, err = c.olmMachine.EncryptMegolmEvent(roomID, event.EventMessage, &content)
}
if err != nil {
log.Print(err)
return
}
_, err = c.client.SendMessageEvent(roomID, event.EventEncrypted, encrypted)
if err != nil {
log.Print(err)
}
} else {
_, err := c.client.SendMessageEvent(roomID, event.EventMessage, &content)
if err != nil {
log.Print(err)
}
}
}
func (c *Client) Broadcast(message *event.MessageEventContent) {
for _, roomID := range c.store.FindAllSharedRooms(c.userID) { for _, roomID := range c.store.FindAllSharedRooms(c.userID) {
allowed := false allowed := false
for _, room := range c.config.AllowedRooms { for _, room := range c.config.AllowedRooms {
@ -139,33 +170,7 @@ func (c *Client) Broadcast(message *event.MessageEventContent) {
continue continue
} }
if c.store.IsEncrypted(roomID) { c.Send(roomID, message)
encrypted, err := c.olmMachine.EncryptMegolmEvent(roomID, event.EventMessage, &content)
if err == crypto.NoGroupSession || err == crypto.SessionExpired || err == crypto.SessionNotShared {
err = c.olmMachine.ShareGroupSession(roomID, c.store.GetRoomMembers(roomID))
if err != nil {
log.Print(err)
continue
}
encrypted, err = c.olmMachine.EncryptMegolmEvent(roomID, event.EventMessage, &content)
}
if err != nil {
log.Print(err)
continue
}
_, err = c.client.SendMessageEvent(roomID, event.EventEncrypted, encrypted)
if err != nil {
log.Print(err)
}
} else {
_, err := c.client.SendMessageEvent(roomID, event.EventMessage, &content)
if err != nil {
log.Print(err)
}
}
} }
} }
@ -190,6 +195,20 @@ func (c *Client) handleMemberEvent(source mautrix.EventSource, evt *event.Event)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
for _, room := range c.greetedRooms {
if room == evt.RoomID {
return
}
}
message := event.MessageEventContent{
MsgType: event.MsgText,
Body: "Ich heiße Marvin \U0001f41e und werde ab jetzt neue Ticketbestellungen verkünden.",
}
c.Send(evt.RoomID, &message)
c.greetedRooms = append(c.greetedRooms, evt.RoomID)
} else { } else {
_, err := c.client.LeaveRoom(evt.RoomID) _, err := c.client.LeaveRoom(evt.RoomID)
if err != nil { if err != nil {