From 1fc788acf0be43215dfaa36b6589e93696c3acad Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Fri, 9 Apr 2021 22:04:49 +0300 Subject: [PATCH] Ensure newline --- node/api.go | 7 +------ node/elastic.go | 18 ++++++++++++------ node/helpers.go | 10 ++++++++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/node/api.go b/node/api.go index dd9707f..1bb4f48 100644 --- a/node/api.go +++ b/node/api.go @@ -80,13 +80,8 @@ func (es ESConf) UEHandler(w http.ResponseWriter, r *http.Request) { LogRequest(r) - ch := make(chan []string) // Get echolist - go func() { - ch <- es.GetUEchoMessageHashes(e) - }() - - messages := <-ch + messages := es.GetUEchoMessageHashes(e) w.WriteHeader(200) w.Write([]byte(strings.Join(messages, "\n"))) diff --git a/node/elastic.go b/node/elastic.go index 7d92b7d..c4320a5 100644 --- a/node/elastic.go +++ b/node/elastic.go @@ -114,11 +114,16 @@ func (es ESConf) GetEchoMessageHashes(echo string) []string { } defer resp.Body.Close() - var esr ESSearchResp - err = json.NewDecoder(resp.Body).Decode(&esr) + content, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Error(err.Error()) + hashes = append(hashes, "error: Internal error") + return hashes + } + + var esr ESSearchResp + err = json.Unmarshal(content, &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 @@ -325,7 +330,7 @@ func (es ESConf) GetUEchoMessageHashes(echoes string) []string { // Some20SimbolsHash333 for k, v := range eh { echohashes = append(echohashes, k) - if k == "" { + if k == "" || k == "\n" { continue } for _, e := range v { @@ -336,7 +341,7 @@ func (es ESConf) GetUEchoMessageHashes(echoes string) []string { } } - return echohashes + return addNewLineToLastWord(echohashes) } // GetXC implements /x/c @@ -497,6 +502,7 @@ func (es ESConf) GetThreads(pageNum int, echoes ...string) (posts []i2es.ESDoc) from := 30 * pageNum rangeStr = fmt.Sprintf(`"from":"now-%dd","to":"now-%dd"`, from, to) } + log.Debug(rangeStr) query := `{"sort":[{"date":{"order":"desc"}}],"aggs":{"topics":{"terms":{"field":"topicid.keyword","size":100},"aggs":{"post":{"top_hits":{"size":1,"sort":[{"date":{"order":"desc"}}],"_source":{"include": ["subg","author","date","echo","topicid","address", "repto"]}}}}}},"query":{"bool":{"must":[{"range":{"date":{` + rangeStr + `}}},{"constant_score":{"filter":{"terms":{"echo.keyword": [` + diff --git a/node/helpers.go b/node/helpers.go index fc5424f..2524402 100644 --- a/node/helpers.go +++ b/node/helpers.go @@ -11,6 +11,16 @@ import ( "golang.org/x/crypto/bcrypt" ) +func addNewLineToLastWord(slice []string) []string { + l := len(slice) - 1 + word := slice[l] + if strings.Contains(word, "\n") { + return slice + } + slice[l] = word + "\n" + return slice +} + func hashAndSalt(authString []byte) string { hash, err := bcrypt.GenerateFromPassword(authString, bcrypt.MinCost) if err != nil {