server store invitation answer by lookupkey + refactor background check
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
4b412ae0f3
commit
ead810e666
@ -1,11 +1,5 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"C"
|
||||
|
||||
"forge.redroom.link/yves/meowlib"
|
||||
"forge.redroom.link/yves/meowlib/client"
|
||||
)
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@ -15,6 +9,8 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"forge.redroom.link/yves/meowlib"
|
||||
"forge.redroom.link/yves/meowlib/client"
|
||||
"github.com/google/uuid"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
@ -31,39 +27,27 @@ type ReceivedMessage struct {
|
||||
Location meowlib.Location
|
||||
}
|
||||
|
||||
// CheckForMessages
|
||||
func CheckForMessages(message *C.char) (int, string, error) {
|
||||
var jsonjob map[string]interface{}
|
||||
// CheckForMessages checks for messages on a single server
|
||||
func CheckForMessages(storage_path string, job *client.RequestsJob) (int, string, error) {
|
||||
|
||||
count := 0
|
||||
err := json.Unmarshal([]byte(C.GoString(message)), &jsonjob)
|
||||
if err != nil {
|
||||
return -1, "CheckMessages: json.Unmarshal", err
|
||||
}
|
||||
|
||||
//fmt.Println(jsonjob)
|
||||
// if folder does not exist, create it
|
||||
if _, err := os.Stat(filepath.Join(jsonjob["storage_path"].(string), "inbox")); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(filepath.Join(jsonjob["storage_path"].(string), "inbox"), 0700)
|
||||
if _, err := os.Stat(filepath.Join(storage_path, "inbox")); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(filepath.Join(storage_path, "inbox"), 0700)
|
||||
if err != nil {
|
||||
return -1, "CheckMessages: MkdirAll", err
|
||||
}
|
||||
}
|
||||
//convert server to a server object
|
||||
var server client.Server
|
||||
jsonServer, err := json.Marshal(jsonjob["server"])
|
||||
if err != nil {
|
||||
return -1, "CheckMessages: json.Marshal server", err
|
||||
}
|
||||
err = json.Unmarshal(jsonServer, &server)
|
||||
if err != nil {
|
||||
return -1, "CheckMessages: json.Unmarshal server", err
|
||||
}
|
||||
|
||||
var crl []*meowlib.ConversationRequest
|
||||
// build conversation requests
|
||||
if jsonjob["lookup_keys"] != nil {
|
||||
for _, key := range jsonjob["lookup_keys"].([]interface{}) {
|
||||
keymap := key.(map[string]interface{})
|
||||
if job.LookupKeys != nil {
|
||||
for _, key := range job.LookupKeys {
|
||||
var cr meowlib.ConversationRequest
|
||||
cr.LookupKey = keymap["public"].(string)
|
||||
cr.LookupKey = key.Public
|
||||
cr.SendTimestamp = time.Now().UTC().Unix()
|
||||
// todo sign it
|
||||
//cr.LookupSignature =
|
||||
@ -73,38 +57,31 @@ func CheckForMessages(message *C.char) (int, string, error) {
|
||||
|
||||
var toSrv meowlib.ToServerMessage
|
||||
toSrv.PullRequest = crl
|
||||
toSrv.From = server.UserKp.Public
|
||||
data, err := server.ProcessOutboundMessage(&toSrv)
|
||||
toSrv.From = job.Server.UserKp.Public
|
||||
data, err := job.Server.ProcessOutboundMessage(&toSrv)
|
||||
if err != nil {
|
||||
return -1, "CheckMessages: ProcessOutboundMessage", err
|
||||
}
|
||||
|
||||
response, err := meowlib.HttpPostMessage(server.Url, data)
|
||||
response, err := meowlib.HttpPostMessage(job.Server.Url, data)
|
||||
if err != nil {
|
||||
return -1, "CheckMessages: httpPostMessage", err
|
||||
}
|
||||
fs_msg, err := server.ProcessInboundServerResponse(response)
|
||||
fs_msg, err := job.Server.ProcessInboundServerResponse(response)
|
||||
if err != nil {
|
||||
return -1, "CheckMessages: ProcessInboundServerResponse", err
|
||||
}
|
||||
if len(fs_msg.Chat) == 0 {
|
||||
if len(fs_msg.Chat) == 0 && fs_msg.Invitation == nil {
|
||||
|
||||
// todo: manage non usermessage, like serverlists
|
||||
} else {
|
||||
// for _, msg := range fs_msg.Chat {
|
||||
// // Store messages
|
||||
// out, err := proto.Marshal(msg)
|
||||
// if err != nil {
|
||||
// C.CString(errorToJson(err, "CheckMessages: protobuf marshal"))
|
||||
// }
|
||||
// if err := os.WriteFile(filepath.Join(jsonjob["storage_path"].(string), "inbox", strconv.FormatInt(time.Now().UTC().UnixNano(), 10)), out, 0644); err != nil {
|
||||
// C.CString(errorToJson(err, "CheckMessages: WriteFile"))
|
||||
// }
|
||||
// }
|
||||
|
||||
} else { // chat or invitation answer => save the server message
|
||||
|
||||
out, err := proto.Marshal(fs_msg)
|
||||
if err != nil {
|
||||
return -1, "CheckMessages: protobuf marshal", err
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(jsonjob["storage_path"].(string), "inbox", strconv.FormatInt(time.Now().UTC().UnixNano(), 10)), out, 0644); err != nil {
|
||||
if err := os.WriteFile(filepath.Join(storage_path, "inbox", strconv.FormatInt(time.Now().UTC().UnixNano(), 10)), out, 0644); err != nil {
|
||||
return -1, "CheckMessages: WriteFile", err
|
||||
}
|
||||
}
|
||||
@ -132,13 +109,17 @@ func ReadMessage(messageFilename string, storagePath string) (string, string, er
|
||||
// read message file
|
||||
msg, err := os.ReadFile(messageFilename)
|
||||
if err != nil {
|
||||
return "", "REadMessage: ReadFile", err
|
||||
return "", "ReadMessage: ReadFile", err
|
||||
}
|
||||
// protobuf unmarshal message
|
||||
var fromServerMessage meowlib.FromServerMessage
|
||||
err = proto.Unmarshal(msg, &fromServerMessage)
|
||||
if err != nil {
|
||||
return "", "ReadMessage: Unmarshal FromServerMessage", err
|
||||
}
|
||||
// check if invitation answer
|
||||
if fromServerMessage.Invitation != nil {
|
||||
|
||||
}
|
||||
// Chat messages
|
||||
if len(fromServerMessage.Chat) > 0 {
|
||||
@ -154,6 +135,7 @@ func ReadMessage(messageFilename string, storagePath string) (string, string, er
|
||||
if err != nil {
|
||||
return "", "ReadMessage: ProcessInboundUserMessage", err
|
||||
}
|
||||
|
||||
fmt.Println("From:", usermsg.From)
|
||||
jsonUserMessage, _ := json.Marshal(usermsg)
|
||||
fmt.Println(string(jsonUserMessage))
|
||||
|
@ -262,6 +262,7 @@ func (id *Identity) GetRequestJobs() []*RequestsJob {
|
||||
}
|
||||
// add ids to the map
|
||||
for _, peer := range id.Peers {
|
||||
// check if peer inviation is accepted
|
||||
for _, server := range peer.MyPullServers {
|
||||
srvs[server].LookupKeys = append(srvs[server].LookupKeys, peer.MyLookupKp)
|
||||
}
|
||||
|
@ -68,6 +68,7 @@ func (r *RedisRouter) Route(msg *meowlib.ToServerMessage) (*meowlib.FromServerMe
|
||||
panic(err)
|
||||
}
|
||||
for _, rq := range msg.PullRequest {
|
||||
// get messages from redis
|
||||
msgcnt, err := r.Client.ZCount(rq.LookupKey, "-inf", "+inf").Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -76,6 +77,7 @@ func (r *RedisRouter) Route(msg *meowlib.ToServerMessage) (*meowlib.FromServerMe
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// iterate over messages
|
||||
for _, redismsg := range res {
|
||||
//println(redismsg.Score)
|
||||
val := redismsg.Member
|
||||
@ -90,6 +92,20 @@ func (r *RedisRouter) Route(msg *meowlib.ToServerMessage) (*meowlib.FromServerMe
|
||||
|
||||
from_server.Chat = append(from_server.Chat, &usrmsg)
|
||||
}
|
||||
// if no messages check for invitationanswer payload
|
||||
if msgcnt == 0 {
|
||||
// get invitation answer
|
||||
var answer meowlib.Invitation
|
||||
storedAanswer, err := r.GetAnswerToInvitation(rq.LookupKey)
|
||||
if err != nil {
|
||||
err := proto.Unmarshal(storedAanswer, &answer)
|
||||
if err != nil {
|
||||
from_server.Invitation.Payload = []byte("invitation answer corrupted")
|
||||
}
|
||||
from_server.Invitation = &answer
|
||||
}
|
||||
// add invitation answer to the response
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -145,29 +161,35 @@ func (r *RedisRouter) Route(msg *meowlib.ToServerMessage) (*meowlib.FromServerMe
|
||||
|
||||
// accept invitation => store accepted invitation for initiator
|
||||
case 3:
|
||||
var usermsg meowlib.PackedUserMessage
|
||||
err := proto.Unmarshal(msg.Invitation.Payload, &usermsg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := proto.Marshal(msg.Invitation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
expiry := r.StoreAnswerToInvitation(msg.Invitation.Uuid, int(msg.Invitation.Timeout), data, r.InvitationTimeout)
|
||||
expiry := r.StoreAnswerToInvitation(usermsg.Destination, int(msg.Invitation.Timeout), data, r.InvitationTimeout)
|
||||
from_server.Invitation = &meowlib.Invitation{}
|
||||
from_server.Invitation.Expiry = expiry.UTC().Unix()
|
||||
|
||||
// get accepted invitation => send accepted invitation to initiator
|
||||
case 4:
|
||||
from_server.Invitation = &meowlib.Invitation{}
|
||||
var answer meowlib.Invitation
|
||||
storedAanswer, err := r.GetAnswerToInvitation(msg.Invitation.Uuid)
|
||||
if err != nil {
|
||||
from_server.Invitation.Payload = []byte("invitation answer not found")
|
||||
} else {
|
||||
err := proto.Unmarshal(storedAanswer, &answer)
|
||||
if err != nil {
|
||||
from_server.Invitation.Payload = []byte("invitation answer corrupted")
|
||||
}
|
||||
from_server.Invitation = &answer
|
||||
}
|
||||
|
||||
// DONE IN NORMAL MESSAGE FLOW
|
||||
// get accepted invitation => send accepted invitation to initiator
|
||||
/* case 4:
|
||||
from_server.Invitation = &meowlib.Invitation{}
|
||||
var answer meowlib.Invitation
|
||||
storedAanswer, err := r.GetAnswerToInvitation(msg.Invitation.Uuid)
|
||||
if err != nil {
|
||||
from_server.Invitation.Payload = []byte("invitation answer not found")
|
||||
} else {
|
||||
err := proto.Unmarshal(storedAanswer, &answer)
|
||||
if err != nil {
|
||||
from_server.Invitation.Payload = []byte("invitation answer corrupted")
|
||||
}
|
||||
from_server.Invitation = &answer
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
/*
|
||||
|
Loading…
Reference in New Issue
Block a user