Refactor code, respond with 500 status if message could not be broadcast, terminate on sync failure
This commit is contained in:
parent
4fb9fa2060
commit
f9c6f48ef7
|
@ -17,7 +17,8 @@ type DatabaseConfig struct {
|
||||||
|
|
||||||
type MatrixConfig struct {
|
type MatrixConfig struct {
|
||||||
AllowedRooms []string `json:"allowed_rooms"`
|
AllowedRooms []string `json:"allowed_rooms"`
|
||||||
DisplayName string `json:"display_name"`
|
DeviceName string `json:"device_name"`
|
||||||
|
Greeting string
|
||||||
LogLevel uint `json:"log_level"`
|
LogLevel uint `json:"log_level"`
|
||||||
HomeserverURL string `json:"homeserver_url"`
|
HomeserverURL string `json:"homeserver_url"`
|
||||||
UserIdentifier string `json:"user_identifier"`
|
UserIdentifier string `json:"user_identifier"`
|
||||||
|
|
|
@ -19,12 +19,10 @@ type Client struct {
|
||||||
client *mautrix.Client
|
client *mautrix.Client
|
||||||
config *config.MatrixConfig
|
config *config.MatrixConfig
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
deviceID id.DeviceID
|
|
||||||
greetedRooms []id.RoomID
|
greetedRooms []id.RoomID
|
||||||
olmMachine *crypto.OlmMachine
|
olmMachine *crypto.OlmMachine
|
||||||
store *sqlStore
|
store *sqlStore
|
||||||
syncer *mautrix.DefaultSyncer
|
syncer mautrix.Syncer
|
||||||
userID id.UserID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(config *config.MatrixConfig, db *sql.DB) (*Client, error) {
|
func NewClient(config *config.MatrixConfig, db *sql.DB) (*Client, error) {
|
||||||
|
@ -44,36 +42,43 @@ func NewClient(config *config.MatrixConfig, db *sql.DB) (*Client, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
userID, err := makeUserID(config.UserIdentifier, config.HomeserverURL)
|
c := &Client{
|
||||||
if err != nil {
|
client: client,
|
||||||
return nil, err
|
config: config,
|
||||||
|
db: db,
|
||||||
|
store: store,
|
||||||
|
syncer: syncer,
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceID := loadDeviceID(db, userID)
|
syncer.OnEventType(event.StateMember, c.handleMemberEvent)
|
||||||
|
|
||||||
return &Client{client, config, db, deviceID, nil, nil, store, syncer, userID}, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Login() error {
|
func (c *Client) Login() error {
|
||||||
c.syncer.OnEventType(event.StateMember, c.handleMemberEvent)
|
userID, err := makeUserID(c.config.UserIdentifier, c.config.HomeserverURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := c.client.Login(&mautrix.ReqLogin{
|
deviceID := c.loadDeviceID(userID)
|
||||||
|
|
||||||
|
_, err := c.client.Login(&mautrix.ReqLogin{
|
||||||
Type: mautrix.AuthTypePassword,
|
Type: mautrix.AuthTypePassword,
|
||||||
Identifier: mautrix.UserIdentifier{
|
Identifier: mautrix.UserIdentifier{
|
||||||
Type: mautrix.IdentifierTypeUser,
|
Type: mautrix.IdentifierTypeUser,
|
||||||
User: c.config.UserIdentifier,
|
User: c.config.UserIdentifier,
|
||||||
},
|
},
|
||||||
Password: c.config.Password,
|
Password: c.config.Password,
|
||||||
DeviceID: c.deviceID,
|
DeviceID: deviceID,
|
||||||
InitialDeviceDisplayName: c.config.DisplayName,
|
InitialDeviceDisplayName: c.config.DeviceName,
|
||||||
StoreCredentials: true,
|
StoreCredentials: true,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.deviceID = resp.DeviceID
|
log.Print("device ID: ", c.client.DeviceID)
|
||||||
log.Print("device ID: ", c.deviceID)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -82,8 +87,8 @@ func (c *Client) Encrypt() error {
|
||||||
sqlCryptoStore := crypto.NewSQLCryptoStore(
|
sqlCryptoStore := crypto.NewSQLCryptoStore(
|
||||||
c.db,
|
c.db,
|
||||||
database.DBDriverName,
|
database.DBDriverName,
|
||||||
c.userID.String(),
|
c.client.UserID.String(),
|
||||||
c.deviceID,
|
c.client.DeviceID,
|
||||||
[]byte(c.config.PickleKey),
|
[]byte(c.config.PickleKey),
|
||||||
logger{},
|
logger{},
|
||||||
)
|
)
|
||||||
|
@ -107,7 +112,7 @@ func (c *Client) Encrypt() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Sync(ctx context.Context, wg *sync.WaitGroup) {
|
func (c *Client) Sync(ctx context.Context, cancel context.CancelFunc, wg *sync.WaitGroup) {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
|
@ -119,12 +124,13 @@ func (c *Client) Sync(ctx context.Context, wg *sync.WaitGroup) {
|
||||||
err := c.client.SyncWithContext(ctx)
|
err := c.client.SyncWithContext(ctx)
|
||||||
if err != nil && err != ctx.Err() {
|
if err != nil && err != ctx.Err() {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
|
cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Send(roomID id.RoomID, message *event.MessageEventContent) {
|
func (c *Client) Send(roomID id.RoomID, message *event.MessageEventContent) error {
|
||||||
content := event.Content{Parsed: message}
|
content := event.Content{Parsed: message}
|
||||||
|
|
||||||
if c.store.IsEncrypted(roomID) {
|
if c.store.IsEncrypted(roomID) {
|
||||||
|
@ -132,32 +138,32 @@ func (c *Client) Send(roomID id.RoomID, message *event.MessageEventContent) {
|
||||||
if err == crypto.NoGroupSession || err == crypto.SessionExpired || err == crypto.SessionNotShared {
|
if err == crypto.NoGroupSession || err == crypto.SessionExpired || err == crypto.SessionNotShared {
|
||||||
err = c.olmMachine.ShareGroupSession(roomID, c.store.GetRoomMembers(roomID))
|
err = c.olmMachine.ShareGroupSession(roomID, c.store.GetRoomMembers(roomID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
encrypted, err = c.olmMachine.EncryptMegolmEvent(roomID, event.EventMessage, &content)
|
encrypted, err = c.olmMachine.EncryptMegolmEvent(roomID, event.EventMessage, &content)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = c.client.SendMessageEvent(roomID, event.EventEncrypted, encrypted)
|
_, err = c.client.SendMessageEvent(roomID, event.EventEncrypted, encrypted)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_, err := c.client.SendMessageEvent(roomID, event.EventMessage, &content)
|
_, err := c.client.SendMessageEvent(roomID, event.EventMessage, &content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Broadcast(message *event.MessageEventContent) {
|
return nil
|
||||||
for _, roomID := range c.store.FindAllSharedRooms(c.userID) {
|
}
|
||||||
|
|
||||||
|
func (c *Client) Broadcast(message *event.MessageEventContent) (success bool) {
|
||||||
|
for _, roomID := range c.store.FindAllSharedRooms(c.client.UserID) {
|
||||||
allowed := false
|
allowed := false
|
||||||
for _, room := range c.config.AllowedRooms {
|
for _, room := range c.config.AllowedRooms {
|
||||||
if room == roomID.String() {
|
if room == roomID.String() {
|
||||||
|
@ -170,7 +176,12 @@ func (c *Client) Broadcast(message *event.MessageEventContent) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Send(roomID, message)
|
err := c.Send(roomID, message)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
} else {
|
||||||
|
success = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,7 +192,7 @@ func (c *Client) handleMemberEvent(source mautrix.EventSource, evt *event.Event)
|
||||||
|
|
||||||
c.store.SetMembership(evt.RoomID, evt.GetStateKey(), evt.Content.AsMember().Membership)
|
c.store.SetMembership(evt.RoomID, evt.GetStateKey(), evt.Content.AsMember().Membership)
|
||||||
|
|
||||||
if evt.GetStateKey() == c.userID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
|
if evt.GetStateKey() == c.client.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
|
||||||
allowed := false
|
allowed := false
|
||||||
for _, room := range c.config.AllowedRooms {
|
for _, room := range c.config.AllowedRooms {
|
||||||
if room == evt.RoomID.String() {
|
if room == evt.RoomID.String() {
|
||||||
|
@ -204,9 +215,14 @@ func (c *Client) handleMemberEvent(source mautrix.EventSource, evt *event.Event)
|
||||||
|
|
||||||
message := event.MessageEventContent{
|
message := event.MessageEventContent{
|
||||||
MsgType: event.MsgText,
|
MsgType: event.MsgText,
|
||||||
Body: "Ich heiße Marvin \U0001f41e und werde ab jetzt neue Ticketbestellungen verkünden.",
|
Body: c.config.Greeting,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = c.Send(evt.RoomID, &message)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
c.Send(evt.RoomID, &message)
|
|
||||||
|
|
||||||
c.greetedRooms = append(c.greetedRooms, evt.RoomID)
|
c.greetedRooms = append(c.greetedRooms, evt.RoomID)
|
||||||
} else {
|
} else {
|
||||||
|
@ -222,8 +238,8 @@ func (c *Client) handleEncryptionEvent(source mautrix.EventSource, evt *event.Ev
|
||||||
c.store.SetEncryptionEvent(evt.RoomID, evt.Content.AsEncryption())
|
c.store.SetEncryptionEvent(evt.RoomID, evt.Content.AsEncryption())
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadDeviceID(db *sql.DB, accountID id.UserID) (deviceID id.DeviceID) {
|
func (c *Client) loadDeviceID(accountID id.UserID) (deviceID id.DeviceID) {
|
||||||
row := db.QueryRow("SELECT device_id FROM crypto_account WHERE account_id = ?;", accountID)
|
row := c.db.QueryRow("SELECT device_id FROM crypto_account WHERE account_id = ?;", accountID)
|
||||||
|
|
||||||
err := row.Scan(&deviceID)
|
err := row.Scan(&deviceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -152,7 +152,10 @@ func (s Server) orderPlaced(w http.ResponseWriter, r *http.Request) {
|
||||||
Body: fmt.Sprint("\U0001f389 Es ", verb, free, conjunction, paid, noun, total, "bestellt."),
|
Body: fmt.Sprint("\U0001f389 Es ", verb, free, conjunction, paid, noun, total, "bestellt."),
|
||||||
}
|
}
|
||||||
|
|
||||||
s.matrix.Broadcast(&message)
|
success := s.matrix.Broadcast(&message)
|
||||||
|
if !success {
|
||||||
|
writeStatus(w, http.StatusInternalServerError)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue