Assign topic id for point message

This commit is contained in:
Denis Zheleztsov 2018-11-12 15:06:39 +03:00
parent 05950c5a63
commit 2d3ae322e0
Signed by: Difrex
GPG Key ID: B047A0E62A285621
7 changed files with 47 additions and 9 deletions

View File

@ -5,7 +5,7 @@ import (
"os" "os"
"gitea.difrex.ru/Umbrella/lessmore/node" "gitea.difrex.ru/Umbrella/lessmore/node"
log "github.com/Sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var ( var (

View File

@ -7,8 +7,8 @@ import (
"strings" "strings"
"time" "time"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux" "github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
) )
// ListTXTHandler ... // ListTXTHandler ...

View File

@ -17,7 +17,7 @@ import (
"time" "time"
log "github.com/Sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
const ( const (

View File

@ -13,7 +13,7 @@ import (
"encoding/base64" "encoding/base64"
"gitea.difrex.ru/Umbrella/fetcher/i2es" "gitea.difrex.ru/Umbrella/fetcher/i2es"
log "github.com/Sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
const ( const (

View File

@ -7,7 +7,7 @@ import (
"strings" "strings"
log "github.com/Sirupsen/logrus" log "github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
log "github.com/Sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// LogRequest ... // LogRequest ...

View File

@ -5,11 +5,12 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings"
"bytes" "bytes"
log "github.com/Sirupsen/logrus"
idec "github.com/idec-net/go-idec" idec "github.com/idec-net/go-idec"
log "github.com/sirupsen/logrus"
) )
type ESDoc struct { type ESDoc struct {
@ -23,6 +24,7 @@ type ESDoc struct {
Tags string `json:"tags"` Tags string `json:"tags"`
Repto string `json:"repto"` Repto string `json:"repto"`
Address string `json:"address"` Address string `json:"address"`
TopicID string `json:"topicid"`
} }
// PointMessage add point message into DB // PointMessage add point message into DB
@ -41,8 +43,7 @@ func (es ESConf) PointMessage(req PointRequest, user User) error {
} }
// Make bundle ID // Make bundle ID
// Prevent collission via adding Timestamp id := idec.MakeMsgID(pmsg.String())
id := idec.MakeMsgID(fmt.Sprintf("%s\n%d", pmsg.String(), bmsg.Timestamp))
bmsg.ID = id bmsg.ID = id
bmsg.From = user.Name bmsg.From = user.Name
bmsg.Address = fmt.Sprintf("%s,%d", user.Address, user.UserID) bmsg.Address = fmt.Sprintf("%s,%d", user.Address, user.UserID)
@ -53,6 +54,42 @@ func (es ESConf) PointMessage(req PointRequest, user User) error {
return nil return nil
} }
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
}
func (es ESConf) IndexMessage(msg idec.Message) error { func (es ESConf) IndexMessage(msg idec.Message) error {
tags, _ := msg.Tags.CollectTags() tags, _ := msg.Tags.CollectTags()
doc := ESDoc{ doc := ESDoc{
@ -66,6 +103,7 @@ func (es ESConf) IndexMessage(msg idec.Message) error {
Repto: msg.Repto, Repto: msg.Repto,
Address: msg.Address, Address: msg.Address,
MsgID: msg.ID, MsgID: msg.ID,
TopicID: es.getTopicID(msg.Repto),
} }
reqURL := fmt.Sprintf("%s/%s/%s/%s", es.Host, es.Index, es.Type, msg.ID) reqURL := fmt.Sprintf("%s/%s/%s/%s", es.Host, es.Index, es.Type, msg.ID)
bdoc, err := json.Marshal(doc) bdoc, err := json.Marshal(doc)