Broadcast new orders to allowed rooms

This commit is contained in:
Luca 2022-07-24 23:19:52 +02:00
parent b2a097c6ae
commit 8e608f0f17
3 changed files with 98 additions and 2 deletions

View File

@ -120,6 +120,52 @@ func (c *Client) Sync(ctx context.Context, wg *sync.WaitGroup) {
}
}
func (c *Client) Broadcast(message *event.MessageEventContent) {
content := event.Content{Parsed: message}
for _, roomID := range c.store.FindAllSharedRooms(c.userID) {
allowed := false
for _, room := range c.config.AllowedRooms {
if room == roomID.String() {
allowed = true
break
}
}
if !allowed {
continue
}
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)
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)
}
}
}
}
func (c *Client) handleMemberEvent(source mautrix.EventSource, evt *event.Event) {
if c.olmMachine != nil {
c.olmMachine.HandleMemberEvent(evt)

View File

@ -156,6 +156,50 @@ func (s sqlStore) FindSharedRooms(userID id.UserID) (sharedRooms []id.RoomID) {
return sharedRooms
}
func (s sqlStore) FindAllSharedRooms(userID id.UserID) (sharedRooms []id.RoomID) {
rows, err := s.db.Query("SELECT room_id FROM room_members GROUP BY room_id HAVING COUNT(*) > 1;")
if err != nil {
return nil
}
defer rows.Close()
for rows.Next() {
var roomID string
if err := rows.Scan(&roomID); err != nil {
continue
}
sharedRooms = append(sharedRooms, id.RoomID(roomID))
}
if rows.Err() != nil {
return nil
}
return sharedRooms
}
func (s sqlStore) GetRoomMembers(roomID id.RoomID) (roomMembers []id.UserID) {
rows, err := s.db.Query("SELECT user_id FROM room_members WHERE room_id = ?;", roomID)
if err != nil {
return nil
}
defer rows.Close()
for rows.Next() {
var userID string
if err := rows.Scan(&userID); err != nil {
continue
}
roomMembers = append(roomMembers, id.UserID(userID))
}
if rows.Err() != nil {
return nil
}
return roomMembers
}
func (s sqlStore) SetMembership(roomID id.RoomID, userID string, membership event.Membership) {
if membership.IsInviteOrJoin() {
_, err := s.db.Exec("INSERT INTO room_members VALUES(?, ?);", roomID, userID)

View File

@ -3,9 +3,10 @@ package pretix
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"maunium.net/go/mautrix/event"
)
const (
@ -93,6 +94,11 @@ func (s Server) orderPlaced(w http.ResponseWriter, r *http.Request) {
}
if rowsAffected > 0 && order.Datetime.Add(72 * time.Hour).After(time.Now()) { // pretix retries failed webhooks for up to three days
log.Print(order)
message := event.MessageEventContent{
MsgType: event.MsgText,
Body: fmt.Sprintf("%+v", order),
}
s.matrix.Broadcast(&message)
}
}