change impl of last fix
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
ycc
2026-02-28 21:22:15 +01:00
parent cd9ee54f6d
commit b722a916a9
2 changed files with 32 additions and 25 deletions

View File

@@ -1,6 +1,7 @@
package helpers
import (
"errors"
"os"
"path/filepath"
"time"
@@ -26,29 +27,25 @@ func PackMessageForServer(packedMsg *meowlib.PackedUserMessage, srvuid string) (
}
func CreateStorePackUserMessageForServer(message string, srvuid string, peer_uid string, replyToUid string, filelist []string) ([]byte, string, error) {
usermessage, _, _, errtxt, err := CreateAndStoreUserMessage(message, peer_uid, replyToUid, filelist)
usermessage, errtxt, err := CreateAndStoreUserMessage(message, peer_uid, replyToUid, filelist)
if err != nil {
return nil, errtxt, err
}
return PackMessageForServer(usermessage, srvuid)
}
// CreateAndStoreUserMessage creates, stores and packs a user message.
// It returns the packed message, the message DB file UUID, the SQLite row ID,
// an error location string, and the error. The caller should set MessageDbFile
// and MessageDbId on the SendJob from the returned dbFile and dbId values.
func CreateAndStoreUserMessage(message string, peer_uid string, replyToUid string, filelist []string) (packedMsg *meowlib.PackedUserMessage, dbFile string, dbId int64, errTxt string, err error) {
func CreateAndStoreUserMessage(message string, peer_uid string, replyToUid string, filelist []string) (*meowlib.PackedUserMessage, string, error) {
peer := client.GetConfig().GetIdentity().Peers.GetFromUid(peer_uid)
// Creating User message
usermessage, err := peer.BuildSimpleUserMessage([]byte(message))
if err != nil {
return nil, "", 0, "PrepareServerMessage : BuildSimpleUserMessage", err
return nil, "PrepareServerMessage : BuildSimpleUserMessage", err
}
for _, file := range filelist {
err = usermessage.AddFile(file, client.GetConfig().Chunksize)
if err != nil {
return nil, "", 0, "PrepareServerMessage : AddFile", err
return nil, "PrepareServerMessage : AddFile", err
}
}
usermessage.Status.Sent = uint64(time.Now().UTC().Unix())
@@ -57,20 +54,16 @@ func CreateAndStoreUserMessage(message string, peer_uid string, replyToUid strin
// Store message
err = peer.StoreMessage(usermessage, nil)
if err != nil {
return nil, "", 0, "messageBuildPostprocess : StoreMessage", err
return nil, "messageBuildPostprocess : StoreMessage", err
}
// Prepare cyphered + packed user message
packedMsg, err = peer.ProcessOutboundUserMessage(usermessage)
packedMsg, err := peer.ProcessOutboundUserMessage(usermessage)
if err != nil {
return nil, "", 0, "messageBuildPostprocess : ProcessOutboundUserMessage", err
return nil, "messageBuildPostprocess : ProcessOutboundUserMessage", err
}
if peer.LastMessage != nil {
dbFile = peer.LastMessage.Dbfile
dbId = peer.LastMessage.Dbid
}
return packedMsg, dbFile, dbId, "", nil
return packedMsg, "", nil
}
func BuildAckMessage(messageUid string, srvuid string, peer_uid string, received int64, processed int64) ([]byte, string, error) {
@@ -107,6 +100,20 @@ func ReadAckMessageResponse() {
//! update the status in message store
}
// GetPeerLastMessageDbInfo returns the DB location of the most recently stored
// message for the given peer. Call this immediately after CreateAndStoreUserMessage
// to get the values needed for SendJob.MessageDbFile and SendJob.MessageDbId.
func GetPeerLastMessageDbInfo(peer_uid string) (dbFile string, dbId int64, errTxt string, err error) {
peer := client.GetConfig().GetIdentity().Peers.GetFromUid(peer_uid)
if peer == nil {
return "", 0, "GetPeerLastMessageDbInfo: peer not found", errors.New("peer not found")
}
if peer.LastMessage == nil {
return "", 0, "GetPeerLastMessageDbInfo: no message stored yet", errors.New("no message stored yet for this peer")
}
return peer.LastMessage.Dbfile, peer.LastMessage.Dbid, "", nil
}
// ProcessSentMessages scans every send queue under storagePath/queues/, updates
// the message storage entry with server delivery info for each sent job, then
// removes the job from the queue. Returns the number of messages updated.

View File

@@ -88,15 +88,13 @@ func storeTestMessage(t *testing.T, peer *client.Peer, text string) {
// pushAndMarkSent pushes a send job for the given peer and marks it as delivered
// by the given server. Returns the job after the status update.
// It mirrors the correct app usage: read MessageDbFile/MessageDbId from
// peer.LastMessage right after storing (i.e. from CreateAndStoreUserMessage's
// dbFile/dbId return values).
// It mirrors the correct app usage: call GetPeerLastMessageDbInfo immediately
// after storing to populate MessageDbFile and MessageDbId on the job.
func pushAndMarkSent(t *testing.T, dir string, peer *client.Peer, srv client.Server) *client.SendJob {
t.Helper()
require.NotNil(t, peer.LastMessage, "pushAndMarkSent: call storeTestMessage first")
dbFile := peer.LastMessage.Dbfile
dbId := peer.LastMessage.Dbid
dbFile, dbId, errTxt, err := GetPeerLastMessageDbInfo(peer.Uid)
require.NoError(t, err, errTxt)
msgFile := filepath.Join(dir, fmt.Sprintf("msg-%d.bin", dbId))
require.NoError(t, os.WriteFile(msgFile, []byte("packed-server-message"), 0600))
@@ -178,7 +176,9 @@ func TestProcessSentMessages_SkipsJobWithoutDeliveryInfo(t *testing.T) {
require.NoError(t, id.Peers.StorePeer(peer))
storeTestMessage(t, peer, "incomplete job")
require.NotNil(t, peer.LastMessage)
dbFile, dbId, errTxt, err := GetPeerLastMessageDbInfo(peer.Uid)
require.NoError(t, err, errTxt)
msgFile := filepath.Join(dir, "msg.bin")
require.NoError(t, os.WriteFile(msgFile, []byte("packed"), 0600))
@@ -186,8 +186,8 @@ func TestProcessSentMessages_SkipsJobWithoutDeliveryInfo(t *testing.T) {
require.NoError(t, client.PushSendJob(dir, &client.SendJob{
Queue: peer.Uid,
File: msgFile,
MessageDbFile: peer.LastMessage.Dbfile,
MessageDbId: peer.LastMessage.Dbid,
MessageDbFile: dbFile,
MessageDbId: dbId,
Servers: []client.Server{{Url: "http://test-server.example"}},
Timeout: 60,
}))