2024-03-23 20:09:14 +01:00
|
|
|
package helpers
|
|
|
|
|
2024-05-22 10:06:00 +02:00
|
|
|
import (
|
2024-05-27 18:38:38 +02:00
|
|
|
"forge.redroom.link/yves/meowlib"
|
2024-05-22 10:06:00 +02:00
|
|
|
"forge.redroom.link/yves/meowlib/client"
|
|
|
|
)
|
2024-03-23 20:09:14 +01:00
|
|
|
|
2024-05-27 18:38:38 +02:00
|
|
|
func messageBuildPostprocess(msg *meowlib.UserMessage, srvuid string, peer *client.Peer) ([]byte, string, error) {
|
|
|
|
// Get the message server
|
2024-03-23 20:09:14 +01:00
|
|
|
srv, err := client.GetConfig().GetIdentity().MessageServers.LoadServer(srvuid)
|
|
|
|
if err != nil {
|
2024-05-27 18:38:38 +02:00
|
|
|
return nil, "messageBuildPostprocess : LoadServer", err
|
2024-03-23 20:09:14 +01:00
|
|
|
}
|
|
|
|
// Prepare cyphered + packed user message
|
2024-05-27 18:38:38 +02:00
|
|
|
packedMsg, err := peer.ProcessOutboundUserMessage(msg)
|
2024-03-23 20:09:14 +01:00
|
|
|
if err != nil {
|
2024-05-27 18:38:38 +02:00
|
|
|
return nil, "messageBuildPostprocess : ProcessOutboundUserMessage", err
|
2024-03-23 20:09:14 +01:00
|
|
|
}
|
|
|
|
// Creating Server message for transporting the user message
|
|
|
|
toServerMessage := srv.BuildToServerMessageFromUserMessage(packedMsg)
|
|
|
|
data, err := srv.ProcessOutboundMessage(toServerMessage)
|
|
|
|
if err != nil {
|
2024-05-27 18:38:38 +02:00
|
|
|
return nil, "messageBuildPostprocess : ProcessOutboundMessage", err
|
2024-03-23 20:09:14 +01:00
|
|
|
}
|
2024-04-06 10:13:25 +02:00
|
|
|
// Store message
|
2024-05-27 18:38:38 +02:00
|
|
|
err = peer.StoreMessage(msg, nil)
|
2024-04-06 10:13:25 +02:00
|
|
|
if err != nil {
|
2024-05-27 18:38:38 +02:00
|
|
|
return nil, "messageBuildPostprocess : StoreMessage", err
|
2024-04-06 10:13:25 +02:00
|
|
|
}
|
2024-03-23 20:09:14 +01:00
|
|
|
return data, "", nil
|
|
|
|
}
|
2024-05-22 10:06:00 +02:00
|
|
|
|
2024-05-27 18:38:38 +02:00
|
|
|
func PrepareUserMessage(message string, srvuid string, peer_idx int, replyToUid string, filelist []string) ([]byte, string, error) {
|
|
|
|
|
|
|
|
peer := client.GetConfig().GetIdentity().Peers[peer_idx]
|
|
|
|
|
|
|
|
// Creating User message
|
|
|
|
usermessage, err := peer.BuildSimpleUserMessage([]byte(message))
|
|
|
|
if err != nil {
|
|
|
|
return nil, "PrepareServerMessage : BuildSimpleUserMessage", err
|
|
|
|
}
|
|
|
|
for _, file := range filelist {
|
|
|
|
err = usermessage.AddFile(file, client.GetConfig().Chunksize)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "PrepareServerMessage : AddFile", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usermessage.Status.AnswerToUuid = replyToUid
|
|
|
|
|
|
|
|
return messageBuildPostprocess(usermessage, srvuid, peer)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-05-22 10:06:00 +02:00
|
|
|
func SendAck(messageUid string, srvuid string, peer_idx int, received int64, processed int64) ([]byte, string, error) {
|
|
|
|
|
|
|
|
peer := client.GetConfig().GetIdentity().Peers[peer_idx]
|
|
|
|
srv, err := client.GetConfig().GetIdentity().MessageServers.LoadServer(srvuid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "PrepareServerMessage : LoadServer", err
|
|
|
|
}
|
|
|
|
// Creating User message
|
|
|
|
usermessage, err := peer.BuildSimpleUserMessage(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "PrepareServerMessage : BuildSimpleUserMessage", err
|
|
|
|
}
|
|
|
|
usermessage.Status.Uuid = messageUid
|
|
|
|
usermessage.Status.Received = uint64(received)
|
|
|
|
usermessage.Status.Processed = uint64(processed)
|
|
|
|
// Prepare cyphered + packed user message
|
|
|
|
packedMsg, err := peer.ProcessOutboundUserMessage(usermessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "PrepareServerMessage : ProcessOutboundUserMessage", err
|
|
|
|
}
|
|
|
|
// Creating Server message for transporting the user message
|
|
|
|
toServerMessage := srv.BuildToServerMessageFromUserMessage(packedMsg)
|
|
|
|
data, err := srv.ProcessOutboundMessage(toServerMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "PrepareServerMessage : ProcessOutboundMessage", err
|
|
|
|
}
|
|
|
|
// Store message
|
|
|
|
err = peer.StoreMessage(usermessage, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "PrepareServerMessage : StoreMessage", err
|
|
|
|
}
|
|
|
|
return data, "", nil
|
|
|
|
}
|