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