2018-11-04 16:11:37 +03:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2018-11-05 18:24:46 +03:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2018-11-12 15:06:39 +03:00
|
|
|
"strings"
|
2018-11-05 18:24:46 +03:00
|
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
idec "github.com/idec-net/go-idec"
|
2018-11-12 15:06:39 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-11-04 16:11:37 +03:00
|
|
|
)
|
|
|
|
|
2018-11-05 18:24:46 +03:00
|
|
|
type ESDoc struct {
|
|
|
|
Echo string `json:"echo"`
|
|
|
|
Subg string `json:"subg"`
|
|
|
|
To string `json:"to"`
|
|
|
|
Author string `json:"author"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Date string `json:"date"`
|
|
|
|
MsgID string `json:"msgid"`
|
|
|
|
Tags string `json:"tags"`
|
|
|
|
Repto string `json:"repto"`
|
|
|
|
Address string `json:"address"`
|
2018-11-12 15:06:39 +03:00
|
|
|
TopicID string `json:"topicid"`
|
2018-11-05 18:24:46 +03:00
|
|
|
}
|
|
|
|
|
2018-11-04 16:11:37 +03:00
|
|
|
// PointMessage add point message into DB
|
2018-11-05 18:24:46 +03:00
|
|
|
func (es ESConf) PointMessage(req PointRequest, user User) error {
|
|
|
|
pmsg, err := idec.ParsePointMessage(req.Tmsg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-06 12:38:38 +03:00
|
|
|
if err := pmsg.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-05 18:24:46 +03:00
|
|
|
|
|
|
|
bmsg, err := idec.MakeBundledMessage(pmsg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make bundle ID
|
2018-11-12 15:06:39 +03:00
|
|
|
id := idec.MakeMsgID(pmsg.String())
|
2018-11-05 18:24:46 +03:00
|
|
|
bmsg.ID = id
|
|
|
|
bmsg.From = user.Name
|
|
|
|
bmsg.Address = fmt.Sprintf("%s,%d", user.Address, user.UserID)
|
|
|
|
|
|
|
|
if err := es.IndexMessage(bmsg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-12 15:06:39 +03:00
|
|
|
func (es ESConf) getTopicID(msgid string) string {
|
|
|
|
var topicid string
|
|
|
|
if msgid == "" {
|
|
|
|
return topicid
|
|
|
|
}
|
|
|
|
reqURL := fmt.Sprintf("%s/%s/%s/%s", es.Host, es.Index, es.Type, msgid)
|
|
|
|
req, err := http.NewRequest("GET", reqURL, strings.NewReader(""))
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return topicid
|
|
|
|
}
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return topicid
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var hit Hit
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&hit)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return topicid
|
|
|
|
}
|
|
|
|
|
|
|
|
if hit.Source.TopicID != "" {
|
|
|
|
topicid = hit.Source.TopicID
|
|
|
|
} else if hit.Source.Repto != "" {
|
|
|
|
return es.getTopicID(hit.Source.Repto)
|
|
|
|
}
|
|
|
|
|
|
|
|
return topicid
|
|
|
|
}
|
|
|
|
|
2018-11-05 18:24:46 +03:00
|
|
|
func (es ESConf) IndexMessage(msg idec.Message) error {
|
|
|
|
tags, _ := msg.Tags.CollectTags()
|
|
|
|
doc := ESDoc{
|
|
|
|
Tags: tags,
|
|
|
|
Echo: msg.Echo,
|
|
|
|
Subg: msg.Subg,
|
|
|
|
To: msg.To,
|
|
|
|
Author: msg.From,
|
|
|
|
Message: msg.Body,
|
|
|
|
Date: fmt.Sprintf("%d", msg.Timestamp),
|
|
|
|
Repto: msg.Repto,
|
|
|
|
Address: msg.Address,
|
|
|
|
MsgID: msg.ID,
|
2018-11-12 15:06:39 +03:00
|
|
|
TopicID: es.getTopicID(msg.Repto),
|
2018-11-05 18:24:46 +03:00
|
|
|
}
|
|
|
|
reqURL := fmt.Sprintf("%s/%s/%s/%s", es.Host, es.Index, es.Type, msg.ID)
|
|
|
|
bdoc, err := json.Marshal(doc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", reqURL, bytes.NewReader(bdoc))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
2018-11-04 16:11:37 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-05 18:24:46 +03:00
|
|
|
defer resp.Body.Close()
|
|
|
|
content, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
log.Info("Message added, response: ", string(content))
|
2018-11-04 16:11:37 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|