From b388acb8c5b93c55a6d7a5586c24af586cef37be Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Sun, 4 Nov 2018 11:15:46 +0300 Subject: [PATCH] Fix /m/ID handler --- node/elastic.go | 107 ++++++++++++++++++++++++++---------------------- node/structs.go | 1 + 2 files changed, 58 insertions(+), 50 deletions(-) diff --git a/node/elastic.go b/node/elastic.go index 122e4a0..2ee85f6 100644 --- a/node/elastic.go +++ b/node/elastic.go @@ -10,7 +10,7 @@ import ( "fmt" - "github.com/Jeffail/gabs" + "gitea.difrex.ru/Umbrella/fetcher/i2es" log "github.com/Sirupsen/logrus" ) @@ -19,23 +19,34 @@ const ( ) // MakePlainTextMessage ... -func MakePlainTextMessage(hit interface{}) string { +func MakePlainTextMessage(hit i2es.ESDoc) []byte { + tags := "ii/ok" + if hit.Repto != "" { + tags += fmt.Sprintf("/repto/%s", hit.Repto) + } + m := []string{ + tags, + hit.Echo, + hit.Date, + hit.Author, + hit.Address, + hit.To, + hit.Subg, + hit.Message, + } - h := make(map[string]interface{}) - h = hit.(map[string]interface{}) - s := make(map[string]interface{}) - s = h["_source"].(map[string]interface{}) - - m := []string{"ii/ok", s["echo"].(string), s["date"].(string), s["author"].(string), "null", s["to"].(string), s["subg"].(string), "", s["message"].(string)} - - return strings.Join(m, "\n") + return []byte(strings.Join(m, "\n")) } // GetPlainTextMessage ... func (es ESConf) GetPlainTextMessage(msgid string) []byte { - var message []byte + var searchURI string + if es.Index != "" && es.Type != "" { + searchURI = strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/") + } else { + searchURI = strings.Join([]string{es.Host, "search"}, "/") + } - searchURI := strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/") searchQ := []byte(strings.Join([]string{ `{"query": {"match": {"_id": "`, msgid, `"}}}`}, "")) @@ -47,27 +58,30 @@ func (es ESConf) GetPlainTextMessage(msgid string) []byte { } defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) + var esr ESSearchResp + err = json.NewDecoder(resp.Body).Decode(&esr) if err != nil { - return message + log.Error(err.Error()) + return []byte("") } - esresp, err := gabs.ParseJSON(body) - if err != nil { - panic(err) + if len(esr.Hits.Hits) > 0 { + return MakePlainTextMessage(esr.Hits.Hits[0].Source) } - hits, _ := esresp.Path("hits.hits").Data().([]interface{}) - - return []byte(MakePlainTextMessage(hits[0])) + return []byte("") } // GetEchoMessageHashes ... func (es ESConf) GetEchoMessageHashes(echo string) []string { var hashes []string + var searchURI string + if es.Index != "" && es.Type != "" { + searchURI = strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/") + } else { + searchURI = strings.Join([]string{es.Host, "search"}, "/") + } - searchURI := strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/") searchQ := []byte(strings.Join([]string{ `{"sort": [ {"date":{ "order": "desc" }},{ "_score":{ "order": "desc" }}], @@ -78,27 +92,23 @@ func (es ESConf) GetEchoMessageHashes(echo string) []string { resp, err := client.Do(req) if err != nil { log.Error(err.Error()) + return hashes } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + var esr ESSearchResp + err = json.NewDecoder(resp.Body).Decode(&esr) if err != nil { + b, _ := ioutil.ReadAll(resp.Body) + log.Error(string(b)) + log.Error(err.Error()) + hashes = append(hashes, "error: Internal error") return hashes } - esresp, err := gabs.ParseJSON(body) - if err != nil { - panic(err) - } - - hits, _ := esresp.Path("hits.hits").Data().([]interface{}) - for _, hit := range hits { - h := make(map[string]interface{}) - h = hit.(map[string]interface{}) - source := make(map[string]interface{}) - source = h["_source"].(map[string]interface{}) - hashes = append(hashes, source["msgid"].(string)) + for _, hit := range esr.Hits.Hits { + hashes = append(hashes, hit.Source.MsgID) } return hashes @@ -107,6 +117,12 @@ func (es ESConf) GetEchoMessageHashes(echo string) []string { // GetLimitedEchoMessageHashes ... func (es ESConf) GetLimitedEchoMessageHashes(echo string, offset int, limit int) []string { var hashes []string + var searchURI string + if es.Index != "" && es.Type != "" { + searchURI = strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/") + } else { + searchURI = strings.Join([]string{es.Host, "search"}, "/") + } // Check offset var order string @@ -118,7 +134,6 @@ func (es ESConf) GetLimitedEchoMessageHashes(echo string, offset int, limit int) l := strconv.Itoa(limit) - searchURI := strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/") searchQ := []byte(strings.Join([]string{ `{"sort": [ {"date":{ "order": "`, order, `" }},{ "_score":{ "order": "`, order, `" }}], @@ -129,27 +144,19 @@ func (es ESConf) GetLimitedEchoMessageHashes(echo string, offset int, limit int) resp, err := client.Do(req) if err != nil { log.Error(err.Error()) + return hashes } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + var esr ESSearchResp + err = json.NewDecoder(resp.Body).Decode(&esr) if err != nil { + log.Error(err.Error()) return hashes } - - esresp, err := gabs.ParseJSON(body) - if err != nil { - panic(err) - } - - hits, _ := esresp.Path("hits.hits").Data().([]interface{}) - for _, hit := range hits { - h := make(map[string]interface{}) - h = hit.(map[string]interface{}) - source := make(map[string]interface{}) - source = h["_source"].(map[string]interface{}) - hashes = append(hashes, source["msgid"].(string)) + for _, hit := range esr.Hits.Hits { + hashes = append(hashes, hit.Source.MsgID) } return hashes diff --git a/node/structs.go b/node/structs.go index e99043c..6afef3b 100644 --- a/node/structs.go +++ b/node/structs.go @@ -16,6 +16,7 @@ type ESSearchResp struct { Took int64 `json:"took"` TimedOut bool `json:"timed_out"` ESSearchShards `json:"_shards"` + Hits `json:"hits"` } type Hits struct {