mirror of
https://mau.dev/mautrix/go.git
synced 2026-03-14 14:25:53 +01:00
Fix incorrect context.Backgrounds
This commit is contained in:
parent
c5f5135c96
commit
48bfc596f0
12 changed files with 28 additions and 36 deletions
|
|
@ -134,6 +134,7 @@ func (as *AppService) PutTransaction(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
log := as.Log.With().Str("transaction_id", txnID).Logger()
|
||||
// Don't use request context, handling shouldn't be stopped even if the request times out
|
||||
ctx := context.Background()
|
||||
ctx = log.WithContext(ctx)
|
||||
if as.txnIDC.IsProcessed(txnID) {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
|
@ -58,7 +57,7 @@ func fnSetPowerLevel(ce *Event) {
|
|||
ce.Reply("**Usage:** `set-pl [user] <level>`")
|
||||
return
|
||||
}
|
||||
_, err = ce.Portal.MainIntent().SetPowerLevel(context.Background(), ce.RoomID, userID, level)
|
||||
_, err = ce.Portal.MainIntent().SetPowerLevel(ce.Ctx, ce.RoomID, userID, level)
|
||||
if err != nil {
|
||||
ce.Reply("Failed to set power levels: %v", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@
|
|||
|
||||
package commands
|
||||
|
||||
import "context"
|
||||
|
||||
var CommandLoginMatrix = &FullHandler{
|
||||
Func: fnLoginMatrix,
|
||||
Name: "login-matrix",
|
||||
|
|
@ -56,7 +54,7 @@ func fnPingMatrix(ce *Event) {
|
|||
ce.Reply("You are not logged in with your Matrix account.")
|
||||
return
|
||||
}
|
||||
resp, err := puppet.CustomIntent().Whoami(context.Background())
|
||||
resp, err := puppet.CustomIntent().Whoami(ce.Ctx)
|
||||
if err != nil {
|
||||
ce.Reply("Failed to validate Matrix login: %v", err)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ func (ce *Event) Reply(msg string, args ...interface{}) {
|
|||
func (ce *Event) ReplyAdvanced(msg string, allowMarkdown, allowHTML bool) {
|
||||
content := format.RenderMarkdown(msg, allowMarkdown, allowHTML)
|
||||
content.MsgType = event.MsgNotice
|
||||
_, err := ce.MainIntent().SendMessageEvent(context.Background(), ce.RoomID, event.EventMessage, content)
|
||||
_, err := ce.MainIntent().SendMessageEvent(ce.Ctx, ce.RoomID, event.EventMessage, content)
|
||||
if err != nil {
|
||||
ce.ZLog.Error().Err(err).Msgf("Failed to reply to command")
|
||||
}
|
||||
|
|
@ -75,7 +75,7 @@ func (ce *Event) ReplyAdvanced(msg string, allowMarkdown, allowHTML bool) {
|
|||
|
||||
// React sends a reaction to the command.
|
||||
func (ce *Event) React(key string) {
|
||||
_, err := ce.MainIntent().SendReaction(context.Background(), ce.RoomID, ce.EventID, key)
|
||||
_, err := ce.MainIntent().SendReaction(ce.Ctx, ce.RoomID, ce.EventID, key)
|
||||
if err != nil {
|
||||
ce.ZLog.Error().Err(err).Msgf("Failed to react to command")
|
||||
}
|
||||
|
|
@ -83,7 +83,7 @@ func (ce *Event) React(key string) {
|
|||
|
||||
// Redact redacts the command.
|
||||
func (ce *Event) Redact(req ...mautrix.ReqRedact) {
|
||||
_, err := ce.MainIntent().RedactEvent(context.Background(), ce.RoomID, ce.EventID, req...)
|
||||
_, err := ce.MainIntent().RedactEvent(ce.Ctx, ce.RoomID, ce.EventID, req...)
|
||||
if err != nil {
|
||||
ce.ZLog.Error().Err(err).Msgf("Failed to redact command")
|
||||
}
|
||||
|
|
@ -91,7 +91,7 @@ func (ce *Event) Redact(req ...mautrix.ReqRedact) {
|
|||
|
||||
// MarkRead marks the command event as read.
|
||||
func (ce *Event) MarkRead() {
|
||||
err := ce.MainIntent().SendReceipt(context.Background(), ce.RoomID, ce.EventID, event.ReceiptTypeRead, nil)
|
||||
err := ce.MainIntent().SendReceipt(ce.Ctx, ce.RoomID, ce.EventID, event.ReceiptTypeRead, nil)
|
||||
if err != nil {
|
||||
ce.ZLog.Error().Err(err).Msgf("Failed to mark command as read")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,8 +7,6 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"maunium.net/go/mautrix/bridge"
|
||||
"maunium.net/go/mautrix/bridge/bridgeconfig"
|
||||
"maunium.net/go/mautrix/event"
|
||||
|
|
@ -78,7 +76,7 @@ func (fh *FullHandler) ShowInHelp(ce *Event) bool {
|
|||
}
|
||||
|
||||
func (fh *FullHandler) userHasRoomPermission(ce *Event) bool {
|
||||
levels, err := ce.MainIntent().PowerLevels(context.Background(), ce.RoomID)
|
||||
levels, err := ce.MainIntent().PowerLevels(ce.Ctx, ce.RoomID)
|
||||
if err != nil {
|
||||
ce.ZLog.Warn().Err(err).Msg("Failed to check room power levels")
|
||||
ce.Reply("Failed to get room power levels to see if you're allowed to use that command")
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ func (helper *CryptoHelper) Init() error {
|
|||
}
|
||||
|
||||
var isExistingDevice bool
|
||||
helper.client, isExistingDevice, err = helper.loginBot()
|
||||
helper.client, isExistingDevice, err = helper.loginBot(context.TODO())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -128,16 +128,15 @@ func (helper *CryptoHelper) Init() error {
|
|||
return err
|
||||
}
|
||||
if isExistingDevice {
|
||||
helper.verifyKeysAreOnServer()
|
||||
helper.verifyKeysAreOnServer(context.TODO())
|
||||
}
|
||||
|
||||
go helper.resyncEncryptionInfo()
|
||||
go helper.resyncEncryptionInfo(context.TODO())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (helper *CryptoHelper) resyncEncryptionInfo() {
|
||||
ctx := context.Background()
|
||||
func (helper *CryptoHelper) resyncEncryptionInfo(ctx context.Context) {
|
||||
log := helper.log.With().Str("action", "resync encryption event").Logger()
|
||||
rows, err := helper.bridge.DB.QueryContext(ctx, `SELECT room_id FROM mx_room_state WHERE encryption='{"resync":true}'`)
|
||||
if err != nil {
|
||||
|
|
@ -223,8 +222,7 @@ func (helper *CryptoHelper) allowKeyShare(ctx context.Context, device *id.Device
|
|||
}
|
||||
}
|
||||
|
||||
func (helper *CryptoHelper) loginBot() (*mautrix.Client, bool, error) {
|
||||
ctx := context.Background()
|
||||
func (helper *CryptoHelper) loginBot(ctx context.Context) (*mautrix.Client, bool, error) {
|
||||
deviceID := helper.store.FindDeviceID()
|
||||
if len(deviceID) > 0 {
|
||||
helper.log.Debug().Str("device_id", deviceID.String()).Msg("Found existing device ID for bot in database")
|
||||
|
|
@ -256,8 +254,7 @@ func (helper *CryptoHelper) loginBot() (*mautrix.Client, bool, error) {
|
|||
return client, deviceID != "", nil
|
||||
}
|
||||
|
||||
func (helper *CryptoHelper) verifyKeysAreOnServer() {
|
||||
ctx := context.Background()
|
||||
func (helper *CryptoHelper) verifyKeysAreOnServer(ctx context.Context) {
|
||||
helper.log.Debug().Msg("Making sure keys are still on server")
|
||||
resp, err := helper.client.QueryKeys(ctx, &mautrix.ReqQueryKeys{
|
||||
DeviceKeys: map[id.UserID]mautrix.DeviceIDList{
|
||||
|
|
@ -336,7 +333,7 @@ func (helper *CryptoHelper) Reset(startAfterReset bool) {
|
|||
helper.log.Debug().Msg("Crypto syncer stopped, clearing database")
|
||||
helper.clearDatabase()
|
||||
helper.log.Debug().Msg("Crypto database cleared, logging out of all sessions")
|
||||
_, err := helper.client.LogoutAll(context.Background())
|
||||
_, err := helper.client.LogoutAll(context.TODO())
|
||||
if err != nil {
|
||||
helper.log.Warn().Err(err).Msg("Failed to log out all devices")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -118,12 +118,11 @@ var (
|
|||
const useConfigASToken = "appservice-config"
|
||||
const asTokenModePrefix = "as_token:"
|
||||
|
||||
func (dp *doublePuppetUtil) Setup(mxid id.UserID, savedAccessToken string, reloginOnFail bool) (intent *appservice.IntentAPI, newAccessToken string, err error) {
|
||||
func (dp *doublePuppetUtil) Setup(ctx context.Context, mxid id.UserID, savedAccessToken string, reloginOnFail bool) (intent *appservice.IntentAPI, newAccessToken string, err error) {
|
||||
if len(mxid) == 0 {
|
||||
err = ErrNoMXID
|
||||
return
|
||||
}
|
||||
ctx := context.Background()
|
||||
_, homeserver, _ := mxid.Parse()
|
||||
loginSecret, hasSecret := dp.br.Config.Bridge.GetDoublePuppetConfig().SharedSecretMap[homeserver]
|
||||
// Special case appservice: prefix to not login and use it as an as_token directly.
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ func (mx *MatrixHandler) HandleEncryption(evt *event.Event) {
|
|||
Msg("Encryption was enabled in room")
|
||||
portal.MarkEncrypted()
|
||||
if portal.IsPrivateChat() {
|
||||
err := mx.as.BotIntent().EnsureJoined(context.Background(), evt.RoomID, appservice.EnsureJoinedParams{BotOverride: portal.MainIntent().Client})
|
||||
err := mx.as.BotIntent().EnsureJoined(context.TODO(), evt.RoomID, appservice.EnsureJoinedParams{BotOverride: portal.MainIntent().Client})
|
||||
if err != nil {
|
||||
mx.log.Err(err).
|
||||
Str("room_id", evt.RoomID.String()).
|
||||
|
|
@ -237,7 +237,7 @@ func (mx *MatrixHandler) HandleMembership(evt *event.Event) {
|
|||
return
|
||||
}
|
||||
defer mx.TrackEventDuration(evt.Type)()
|
||||
ctx := context.Background()
|
||||
ctx := context.TODO()
|
||||
|
||||
if mx.bridge.Crypto != nil {
|
||||
mx.bridge.Crypto.HandleMemberEvent(evt)
|
||||
|
|
@ -481,7 +481,7 @@ func (mx *MatrixHandler) HandleEncrypted(evt *event.Event) {
|
|||
return
|
||||
}
|
||||
content := evt.Content.AsEncrypted()
|
||||
ctx := context.Background()
|
||||
ctx := context.TODO()
|
||||
log := mx.log.With().
|
||||
Str("event_id", evt.ID.String()).
|
||||
Str("session_id", content.SessionID.String()).
|
||||
|
|
@ -526,7 +526,7 @@ func (mx *MatrixHandler) waitLongerForSession(ctx context.Context, evt *event.Ev
|
|||
Int("wait_seconds", int(extendedSessionWaitTimeout.Seconds())).
|
||||
Msg("Couldn't find session, requesting keys and waiting longer...")
|
||||
|
||||
go mx.bridge.Crypto.RequestSession(context.Background(), evt.RoomID, content.SenderKey, content.SessionID, evt.Sender, content.DeviceID)
|
||||
go mx.bridge.Crypto.RequestSession(ctx, evt.RoomID, content.SenderKey, content.SessionID, evt.Sender, content.DeviceID)
|
||||
errorEventID := mx.sendCryptoStatusError(ctx, evt, "", fmt.Errorf("%w. The bridge will retry for %d seconds", errNoDecryptionKeys, int(extendedSessionWaitTimeout.Seconds())), 1, false)
|
||||
|
||||
if !mx.bridge.Crypto.WaitForSession(evt.RoomID, content.SenderKey, content.SessionID, extendedSessionWaitTimeout) {
|
||||
|
|
@ -553,7 +553,7 @@ func (mx *MatrixHandler) HandleMessage(evt *event.Event) {
|
|||
Str("room_id", evt.RoomID.String()).
|
||||
Str("sender", evt.Sender.String()).
|
||||
Logger()
|
||||
ctx := log.WithContext(context.Background())
|
||||
ctx := log.WithContext(context.TODO())
|
||||
if mx.shouldIgnoreEvent(evt) {
|
||||
return
|
||||
} else if !evt.Mautrix.WasEncrypted && mx.bridge.Config.Bridge.GetEncryptionConfig().Require {
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ func (helper *CryptoHelper) Init() error {
|
|||
} else {
|
||||
stateStore = helper.client.StateStore.(crypto.StateStore)
|
||||
}
|
||||
ctx := context.Background()
|
||||
ctx := context.TODO()
|
||||
var cryptoStore crypto.Store
|
||||
if helper.unmanagedCryptoStore == nil {
|
||||
managedCryptoStore := crypto.NewSQLCryptoStore(helper.dbForManagedStores, dbutil.ZeroLogger(helper.log.With().Str("db_section", "crypto").Logger()), helper.DBAccountID, helper.client.DeviceID, helper.pickleKey)
|
||||
|
|
@ -310,7 +310,7 @@ func (helper *CryptoHelper) waitLongerForSession(log zerolog.Logger, src mautrix
|
|||
content := evt.Content.AsEncrypted()
|
||||
log.Debug().Int("wait_seconds", int(extendedSessionWaitTimeout.Seconds())).Msg("Couldn't find session, requesting keys and waiting longer...")
|
||||
|
||||
go helper.RequestSession(context.Background(), evt.RoomID, content.SenderKey, content.SessionID, evt.Sender, content.DeviceID)
|
||||
go helper.RequestSession(context.TODO(), evt.RoomID, content.SenderKey, content.SessionID, evt.Sender, content.DeviceID)
|
||||
|
||||
if !helper.mach.WaitForSession(evt.RoomID, content.SenderKey, content.SessionID, extendedSessionWaitTimeout) {
|
||||
log.Debug().Msg("Didn't get session, giving up")
|
||||
|
|
|
|||
|
|
@ -228,7 +228,7 @@ const MinUnwedgeInterval = 1 * time.Hour
|
|||
|
||||
func (mach *OlmMachine) unwedgeDevice(log zerolog.Logger, sender id.UserID, senderKey id.SenderKey) {
|
||||
log = log.With().Str("action", "unwedge olm session").Logger()
|
||||
ctx := log.WithContext(context.Background())
|
||||
ctx := log.WithContext(context.TODO())
|
||||
mach.recentlyUnwedgedLock.Lock()
|
||||
prevUnwedge, ok := mach.recentlyUnwedged[senderKey]
|
||||
delta := time.Now().Sub(prevUnwedge)
|
||||
|
|
|
|||
|
|
@ -243,7 +243,7 @@ func (mach *OlmMachine) HandleOTKCounts(otkCount *mautrix.OTKCount) {
|
|||
if otkCount.SignedCurve25519 < int(minCount) {
|
||||
traceID := time.Now().Format("15:04:05.000000")
|
||||
log := mach.Log.With().Str("trace_id", traceID).Logger()
|
||||
ctx := log.WithContext(context.Background())
|
||||
ctx := log.WithContext(context.TODO())
|
||||
log.Debug().
|
||||
Int("keys_left", otkCount.Curve25519).
|
||||
Msg("Sync response said we have less than 50 signed curve25519 keys left, sharing new ones...")
|
||||
|
|
@ -334,7 +334,7 @@ func (mach *OlmMachine) HandleToDeviceEvent(evt *event.Event) {
|
|||
Str("sender", evt.Sender.String()).
|
||||
Str("type", evt.Type.Type).
|
||||
Logger()
|
||||
ctx := log.WithContext(context.Background())
|
||||
ctx := log.WithContext(context.TODO())
|
||||
if evt.Type != event.ToDeviceEncrypted {
|
||||
log.Debug().Msg("Starting handling to-device event")
|
||||
}
|
||||
|
|
@ -344,7 +344,7 @@ func (mach *OlmMachine) HandleToDeviceEvent(evt *event.Event) {
|
|||
Str("sender_key", content.SenderKey.String()).
|
||||
Logger()
|
||||
log.Debug().Msg("Handling encrypted to-device event")
|
||||
ctx = log.WithContext(context.Background())
|
||||
ctx = log.WithContext(ctx)
|
||||
decryptedEvt, err := mach.decryptOlmEvent(ctx, evt)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to decrypt to-device event")
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ func (mach *OlmMachine) ProcessInRoomVerification(evt *event.Event) error {
|
|||
return ErrNoRelatesTo
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
ctx := context.TODO()
|
||||
switch content := evt.Content.Parsed.(type) {
|
||||
case *event.MessageEventContent:
|
||||
if content.MsgType == event.MsgVerificationRequest {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue