message storage and from removed from packedusermessage (weakness)
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
ycc
2024-02-18 13:46:11 +01:00
parent 05cc2ee218
commit 07dfae8f0e
6 changed files with 138 additions and 117 deletions

View File

@ -2,10 +2,38 @@ package client
import "forge.redroom.link/yves/meowlib"
const (
Inbound = 0
Outbound = 1
)
type InternalUserMessage struct {
Message *meowlib.UserMessage
Dbfile string
Dbid int64
Direction int // 0 = inbound, 1 = outbound
Messagetype string
Message string
ConversationStatus *meowlib.ConversationStatus
Contact *meowlib.ContactCard
//Group group
FilePaths []string
CurrentLocation meowlib.Location
appdata []byte
Dbfile string
Dbid int64
}
// InternalUserMessageFromUserMessage creates an InternalUserMessage from a UserMessage
func InternalUserMessageFromUserMessage(peer *Peer, msg *meowlib.UserMessage) *InternalUserMessage {
iu := new(InternalUserMessage)
if peer.ContactPublicKey == msg.From {
iu.Direction = Inbound
} else {
iu.Direction = Outbound
}
iu.Messagetype = msg.Type
iu.Message = string(msg.Data)
iu.ConversationStatus = msg.Status
iu.Contact = msg.Contact
return iu
}
func ProcessOutboundTextMessage(peer *Peer, text string, srv *Server) ([]byte, error) {

View File

@ -74,8 +74,8 @@ func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, password string)
}
// Get new messages from a peer
func GetNewMessages(peer *Peer, lastDbId int, password string) ([]InternalUserMessage, error) {
var messages []InternalUserMessage
func GetNewMessages(peer *Peer, lastDbId int, password string) ([]*InternalUserMessage, error) {
var messages []*InternalUserMessage
fileidx := len(peer.DbIds) - 1
// There fileidx should provide the db that we need (unless wantMore overlaps the next DB)
db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, peer.DbIds[fileidx]+GetConfig().DbSuffix)) // Open the created SQLite File
@ -96,7 +96,7 @@ func GetNewMessages(peer *Peer, lastDbId int, password string) ([]InternalUserMe
defer rows.Close()
for rows.Next() {
var ium InternalUserMessage
var ium *InternalUserMessage
var um meowlib.UserMessage
var id int64
var m []byte
@ -112,9 +112,10 @@ func GetNewMessages(peer *Peer, lastDbId int, password string) ([]InternalUserMe
if err != nil {
return nil, err
}
ium = InternalUserMessageFromUserMessage(peer, &um)
ium.Dbid = id
ium.Dbfile = peer.DbIds[fileidx]
ium.Message = &um
messages = append(messages, ium)
}
// TODO DB overlap
@ -122,8 +123,8 @@ func GetNewMessages(peer *Peer, lastDbId int, password string) ([]InternalUserMe
}
// Get old messages from a peer
func GetMessagesHistory(peer *Peer, inAppMsgCount int, lastDbId int, wantMore int, password string) ([]InternalUserMessage, error) {
var messages []InternalUserMessage
func GetMessagesHistory(peer *Peer, inAppMsgCount int, lastDbId int, wantMore int, password string) ([]*InternalUserMessage, error) {
var messages []*InternalUserMessage
fileidx := len(peer.DbIds) - 1
// initialize count with last db message count
countStack, err := getMessageCount(peer.DbIds[fileidx])
@ -177,9 +178,10 @@ func GetMessagesHistory(peer *Peer, inAppMsgCount int, lastDbId int, wantMore in
if err != nil {
return nil, err
}
ium = InternalUserMessageFromUserMessage(peer, &um)
ium.Dbid = id
ium.Dbfile = peer.DbIds[fileidx]
ium.Message = &um
messages = append(messages, ium)
}
// TODO DB overlap

View File

@ -24,7 +24,7 @@ func TestStoreMessage(t *testing.T) {
}
// Checks
assert.Equal(t, len(messages), 1, "not 1 message")
assert.Equal(t, messages[0].Message.Data, um.Data, "not 1 message")
assert.Equal(t, messages[0].Message, um.Data, "not 1 message")
// Cleanup
if exists("test.id") {
os.Remove("test.id")

View File

@ -35,17 +35,19 @@ type Peer struct {
InvitationUrl string `json:"invitation_url,omitempty"`
InvitationMessage string `json:"invitation_message,omitempty"`
InvitationExpiry time.Time `json:"invitation_expiry,omitempty"`
LastMessage string `json:"last_message,omitempty"`
LastMessageDate int64 `json:"last_message_date,omitempty"`
LastMessageStatus string `json:"last_message_status,omitempty"`
// Internal management attributes
Visible bool `json:"visible,omitempty"`
VisiblePassword string `json:"visible_password,omitempty"`
PasswordType string `json:"password_type,omitempty"`
Blocked bool `json:"blocked,omitempty"`
MessageNotification string `json:"message_notification,omitempty"`
MatriochkaMode bool `json:"matriochka_mode,omitempty"`
DirectMode bool `json:"direct_mode,omitempty"`
LastMessage time.Time `json:"last_message,omitempty"`
DbIds []string `json:"db_ids,omitempty"`
IsDevice bool `json:"is_device,omitempty"`
Visible bool `json:"visible,omitempty"`
VisiblePassword string `json:"visible_password,omitempty"`
PasswordType string `json:"password_type,omitempty"`
Blocked bool `json:"blocked,omitempty"`
MessageNotification string `json:"message_notification,omitempty"`
MatriochkaMode bool `json:"matriochka_mode,omitempty"`
DirectMode bool `json:"direct_mode,omitempty"`
DbIds []string `json:"db_ids,omitempty"`
IsDevice bool `json:"is_device,omitempty"`
dbPassword string
}
@ -213,7 +215,6 @@ func (p *Peer) AsymDecryptMessage(Message []byte, Signature []byte) (DecryptedMe
// PackUserMessage will package the previously encrypted message
func (p *Peer) PackUserMessage(message []byte, signature []byte) *meowlib.PackedUserMessage {
var msg meowlib.PackedUserMessage
msg.From = p.MyIdentity.Public
msg.Destination = p.ContactLookupKey
msg.Payload = message
msg.Signature = signature
@ -287,12 +288,12 @@ func (p *Peer) UpdateMessage(msg InternalUserMessage) error {
return nil
}
func (p *Peer) LoadLastMessages(alreadyLoadedCount int, oldestMessageId int, qty int) ([]InternalUserMessage, error) {
func (p *Peer) LoadLastMessages(alreadyLoadedCount int, oldestMessageId int, qty int) ([]*InternalUserMessage, error) {
return GetMessagesHistory(p, alreadyLoadedCount, oldestMessageId, 1, p.GetDbPassword())
}
func (p *Peer) LoadNewMessages(lastMessageId int) ([]InternalUserMessage, error) {
func (p *Peer) LoadNewMessages(lastMessageId int) ([]*InternalUserMessage, error) {
return GetNewMessages(p, lastMessageId, p.GetDbPassword())
}