Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connector/matrix: more logging for matrix message handling #68

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,14 @@ func (t *TelegramClient) getUserInfoFromTelegramUser(ctx context.Context, u tg.U
}

func (t *TelegramClient) IsLoggedIn() bool {
return t != nil && t.client != nil && t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey()
if t == nil {
return false
}
t.main.Bridge.Log.Debug().
Bool("has_client", t.client != nil).
Bool("has_auth_key", t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey()).
Msg("Checking if user is logged in")
return t.client != nil && t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey()
}

func (t *TelegramClient) LogoutRemote(ctx context.Context) {
Expand Down
22 changes: 18 additions & 4 deletions pkg/connector/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package connector
import (
"context"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"image"
Expand Down Expand Up @@ -184,6 +185,8 @@ func (t *TelegramClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.
if err != nil {
return nil, err
}
log := zerolog.Ctx(ctx).With().Stringer("portal_key", msg.Portal.PortalKey).Any("peer_id", peer).Logger()
ctx = log.WithContext(ctx)

var contentURI id.ContentURIString

Expand All @@ -195,12 +198,12 @@ func (t *TelegramClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.
if msg.ReplyTo != nil {
_, messageID, err := ids.ParseMessageID(msg.ReplyTo.ID)
if err != nil {
log.Warn().Msg("failed to parse replied-to message ID")
return nil, err
}
replyTo = &tg.InputReplyToMessage{ReplyToMsgID: messageID}
}

// TODO handle sticker
var updates tg.UpdatesClass
if msg.Event.Type == event.EventSticker {
var media tg.InputMediaClass
Expand Down Expand Up @@ -267,6 +270,7 @@ func (t *TelegramClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.
}
}
if err != nil {
log.Err(err).Msg("failed to send message to Telegram")
return nil, t.humanizeSendError(err)
}

Expand Down Expand Up @@ -297,15 +301,25 @@ func (t *TelegramClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.
return nil, fmt.Errorf("unknown update from message response %T", updates)
}

messageID := ids.MakeMessageID(msg.Portal.PortalKey, tgMessageID)
timestamp := time.Unix(int64(tgDate), 0)
hash := hasher.Sum(nil)
log.Info().
Int("tg_message_id", tgMessageID).
Str("message_id", string(messageID)).
Time("timestamp", timestamp).
Str("content_hash", base64.StdEncoding.EncodeToString(hash)).
Msg("sent message successfully")

resp = &bridgev2.MatrixMessageResponse{
DB: &database.Message{
ID: ids.MakeMessageID(msg.Portal.PortalKey, tgMessageID),
ID: messageID,
MXID: msg.Event.ID,
Room: msg.Portal.PortalKey,
SenderID: t.userID,
Timestamp: time.Unix(int64(tgDate), 0),
Timestamp: timestamp,
Metadata: &MessageMetadata{
ContentHash: hasher.Sum(nil),
ContentHash: hash,
ContentURI: contentURI,
},
},
Expand Down