v.0.0.4.7 Добавлено шифрование сообщения
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
f983a2f9d9
commit
82f784996a
39
server.go
39
server.go
@ -372,16 +372,16 @@ func (s *server) GetUserChats(ctx context.Context, req *proto.GetUserChatsReques
|
||||
|
||||
s.logger.Printf("Querying chats for user_id=%d", req.UserId)
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT c.id, c.user1_id, c.user2_id, c.created_at, c.updated_at,
|
||||
m.id, m.content, m.status, m.created_at
|
||||
FROM chats c
|
||||
LEFT JOIN messages m ON m.id = (
|
||||
SELECT id FROM messages WHERE chat_id = c.id
|
||||
ORDER BY created_at DESC LIMIT 1
|
||||
)
|
||||
WHERE c.user1_id = $1 OR c.user2_id = $1
|
||||
ORDER BY c.updated_at DESC
|
||||
`, req.UserId)
|
||||
SELECT c.id, c.user1_id, c.user2_id, c.created_at, c.updated_at,
|
||||
m.id, m.encrypted_content, m.encrypted_key, m.nonce, m.status, m.created_at
|
||||
FROM chats c
|
||||
LEFT JOIN messages m ON m.id = (
|
||||
SELECT id FROM messages WHERE chat_id = c.id
|
||||
ORDER BY created_at DESC LIMIT 1
|
||||
)
|
||||
WHERE c.user1_id = $1 OR c.user2_id = $1
|
||||
ORDER BY c.updated_at DESC
|
||||
`, req.UserId)
|
||||
if err != nil {
|
||||
s.logger.Printf("Failed to query user chats: %v", err)
|
||||
return nil, err
|
||||
@ -393,13 +393,14 @@ func (s *server) GetUserChats(ctx context.Context, req *proto.GetUserChatsReques
|
||||
var chat proto.Chat
|
||||
var createdAt, updatedAt time.Time
|
||||
var lastMessageID sql.NullInt32
|
||||
var lastMessageContent sql.NullString
|
||||
var lastMessageEncryptedContent, lastMessageEncryptedKey, lastMessageNonce []byte
|
||||
var lastMessageStatus sql.NullString
|
||||
var lastMessageCreatedAt sql.NullTime
|
||||
|
||||
err := rows.Scan(
|
||||
&chat.Id, &chat.User1Id, &chat.User2Id, &createdAt, &updatedAt,
|
||||
&lastMessageID, &lastMessageContent, &lastMessageStatus, &lastMessageCreatedAt,
|
||||
&lastMessageID, &lastMessageEncryptedContent, &lastMessageEncryptedKey,
|
||||
&lastMessageNonce, &lastMessageStatus, &lastMessageCreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
s.logger.Printf("Failed to scan chat row: %v", err)
|
||||
@ -410,14 +411,24 @@ func (s *server) GetUserChats(ctx context.Context, req *proto.GetUserChatsReques
|
||||
chat.UpdatedAt = timestamppb.New(updatedAt)
|
||||
|
||||
if lastMessageID.Valid {
|
||||
// Расшифровываем последнее сообщение
|
||||
decryptedContent, err := s.crypto.DecryptMessage(
|
||||
lastMessageEncryptedContent,
|
||||
lastMessageEncryptedKey,
|
||||
lastMessageNonce,
|
||||
)
|
||||
if err != nil {
|
||||
s.logger.Printf("Failed to decrypt last message: %v", err)
|
||||
decryptedContent = "[не удалось расшифровать]"
|
||||
}
|
||||
|
||||
chat.LastMessage = &proto.Message{
|
||||
Id: lastMessageID.Int32,
|
||||
ChatId: chat.Id,
|
||||
Content: lastMessageContent.String,
|
||||
Content: decryptedContent,
|
||||
Status: lastMessageStatus.String,
|
||||
CreatedAt: timestamppb.New(lastMessageCreatedAt.Time),
|
||||
}
|
||||
s.logger.Printf("Found last message for chat %d: message_id=%d", chat.Id, lastMessageID.Int32)
|
||||
}
|
||||
|
||||
chats = append(chats, &chat)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user