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 package helpers
import ( import (
"errors"
"os" "os"
"path/filepath" "path/filepath"
"time" "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) { 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 { if err != nil {
return nil, errtxt, err return nil, errtxt, err
} }
return PackMessageForServer(usermessage, srvuid) return PackMessageForServer(usermessage, srvuid)
} }
// CreateAndStoreUserMessage creates, stores and packs a user message. func CreateAndStoreUserMessage(message string, peer_uid string, replyToUid string, filelist []string) (*meowlib.PackedUserMessage, string, error) {
// 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) {
peer := client.GetConfig().GetIdentity().Peers.GetFromUid(peer_uid) peer := client.GetConfig().GetIdentity().Peers.GetFromUid(peer_uid)
// Creating User message // Creating User message
usermessage, err := peer.BuildSimpleUserMessage([]byte(message)) usermessage, err := peer.BuildSimpleUserMessage([]byte(message))
if err != nil { if err != nil {
return nil, "", 0, "PrepareServerMessage : BuildSimpleUserMessage", err return nil, "PrepareServerMessage : BuildSimpleUserMessage", err
} }
for _, file := range filelist { for _, file := range filelist {
err = usermessage.AddFile(file, client.GetConfig().Chunksize) err = usermessage.AddFile(file, client.GetConfig().Chunksize)
if err != nil { if err != nil {
return nil, "", 0, "PrepareServerMessage : AddFile", err return nil, "PrepareServerMessage : AddFile", err
} }
} }
usermessage.Status.Sent = uint64(time.Now().UTC().Unix()) usermessage.Status.Sent = uint64(time.Now().UTC().Unix())
@@ -57,20 +54,16 @@ func CreateAndStoreUserMessage(message string, peer_uid string, replyToUid strin
// Store message // Store message
err = peer.StoreMessage(usermessage, nil) err = peer.StoreMessage(usermessage, nil)
if err != nil { if err != nil {
return nil, "", 0, "messageBuildPostprocess : StoreMessage", err return nil, "messageBuildPostprocess : StoreMessage", err
} }
// Prepare cyphered + packed user message // Prepare cyphered + packed user message
packedMsg, err = peer.ProcessOutboundUserMessage(usermessage) packedMsg, err := peer.ProcessOutboundUserMessage(usermessage)
if err != nil { if err != nil {
return nil, "", 0, "messageBuildPostprocess : ProcessOutboundUserMessage", err return nil, "messageBuildPostprocess : ProcessOutboundUserMessage", err
} }
if peer.LastMessage != nil { return packedMsg, "", nil
dbFile = peer.LastMessage.Dbfile
dbId = peer.LastMessage.Dbid
}
return packedMsg, dbFile, dbId, "", nil
} }
func BuildAckMessage(messageUid string, srvuid string, peer_uid string, received int64, processed int64) ([]byte, string, error) { 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 //! 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 // ProcessSentMessages scans every send queue under storagePath/queues/, updates
// the message storage entry with server delivery info for each sent job, then // 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. // 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 // 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. // by the given server. Returns the job after the status update.
// It mirrors the correct app usage: read MessageDbFile/MessageDbId from // It mirrors the correct app usage: call GetPeerLastMessageDbInfo immediately
// peer.LastMessage right after storing (i.e. from CreateAndStoreUserMessage's // after storing to populate MessageDbFile and MessageDbId on the job.
// dbFile/dbId return values).
func pushAndMarkSent(t *testing.T, dir string, peer *client.Peer, srv client.Server) *client.SendJob { func pushAndMarkSent(t *testing.T, dir string, peer *client.Peer, srv client.Server) *client.SendJob {
t.Helper() t.Helper()
require.NotNil(t, peer.LastMessage, "pushAndMarkSent: call storeTestMessage first") dbFile, dbId, errTxt, err := GetPeerLastMessageDbInfo(peer.Uid)
dbFile := peer.LastMessage.Dbfile require.NoError(t, err, errTxt)
dbId := peer.LastMessage.Dbid
msgFile := filepath.Join(dir, fmt.Sprintf("msg-%d.bin", dbId)) msgFile := filepath.Join(dir, fmt.Sprintf("msg-%d.bin", dbId))
require.NoError(t, os.WriteFile(msgFile, []byte("packed-server-message"), 0600)) 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)) require.NoError(t, id.Peers.StorePeer(peer))
storeTestMessage(t, peer, "incomplete job") 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") msgFile := filepath.Join(dir, "msg.bin")
require.NoError(t, os.WriteFile(msgFile, []byte("packed"), 0600)) 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{ require.NoError(t, client.PushSendJob(dir, &client.SendJob{
Queue: peer.Uid, Queue: peer.Uid,
File: msgFile, File: msgFile,
MessageDbFile: peer.LastMessage.Dbfile, MessageDbFile: dbFile,
MessageDbId: peer.LastMessage.Dbid, MessageDbId: dbId,
Servers: []client.Server{{Url: "http://test-server.example"}}, Servers: []client.Server{{Url: "http://test-server.example"}},
Timeout: 60, Timeout: 60,
})) }))