Skip to content

Commit

Permalink
can now send messages using http route, simplified message format, bu…
Browse files Browse the repository at this point in the history
…mp version
  • Loading branch information
felipejfc committed Jul 13, 2016
1 parent 1775a8e commit d8dbb20
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 43 deletions.
24 changes: 9 additions & 15 deletions app/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ import (
"gopkg.in/topfreegames/elastic.v2"
)

// PayloadStruct contains the fields of a payload
type PayloadStruct struct {
From string `json:"from"`
Message string `json:"message"`
Timestamp int32 `json:"timestamp"`
Id string `json:"id"`
}

type Message struct {
Payload PayloadStruct `json:"payload"`
Topic string `json:"topic"`
Id string `json:"id"`
Timestamp int32 `json:"timstamp"`
Payload string `json:"payload"`
Topic string `json:"topic"`
}

// HistoryHandler is the handler responsible for sending the rooms history to the player
Expand Down Expand Up @@ -49,21 +43,21 @@ func HistoryHandler(app *App) func(c *iris.Context) {
if redisResults[0] != nil && redisResults[1] != nil {
termQuery := elastic.NewQueryStringQuery(fmt.Sprintf("topic:\"%s\"", topic))
searchResults, err := esclient.Search().Index("chat").Query(termQuery).
Sort("payload.timestamp", false).From(from).Size(limit).Do()
Sort("timestamp", false).From(from).Size(limit).Do()
if err != nil {
logger.Logger.Error(err.Error())
c.SetStatusCode(iris.StatusInternalServerError)
return
}
payloads := []PayloadStruct{}
messages := []Message{}
var ttyp Message
for _, item := range searchResults.Each(reflect.TypeOf(ttyp)) {
if t, ok := item.(Message); ok {
payloads = append(payloads, t.Payload)
messages = append(messages, t)
}
}
jsonPayloads, _ := json.Marshal(payloads)
c.Write(fmt.Sprintf("%s", jsonPayloads))
jsonMessages, _ := json.Marshal(messages)
c.Write(fmt.Sprintf("%s", jsonMessages))
c.SetStatusCode(iris.StatusOK)
} else {
c.SetStatusCode(iris.StatusForbidden)
Expand Down
18 changes: 10 additions & 8 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ import:
- package: gopkg.in/topfreegames/elastic.v2
- package: github.com/franela/goblin
- package: github.com/onsi/gomega
- package: github.com/satori/go.uuid
31 changes: 14 additions & 17 deletions modules/plugins_persistence_module.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package modules

import (
"encoding/json"
"fmt"
"reflect"
"time"

"github.com/layeh/gopher-luar"
"github.com/satori/go.uuid"
"github.com/topfreegames/mqttbot/es"
"github.com/topfreegames/mqttbot/logger"
"github.com/yuin/gopher-lua"
"gopkg.in/topfreegames/elastic.v2"
)

// PayloadStruct contains the fields of a payload
type PayloadStruct struct {
From string `json:"from"`
Message string `json:"message"`
Timestamp int32 `json:"timestamp"`
Id string `json:"id"`
}

type Message struct {
Payload PayloadStruct `json:"payload"`
Topic string `json:"topic"`
Id string `json:"id"`
Timestamp int32 `json:"timestamp"`
Payload string `json:"payload"`
Topic string `json:"topic"`
}

var esclient *elastic.Client
Expand All @@ -47,9 +42,11 @@ func IndexMessage(L *lua.LState) int {
topic := L.Get(-2)
payload := L.Get(-1)
L.Pop(2)
var message Message
json.Unmarshal([]byte(payload.String()), &message)
message := Message{}
message.Payload = payload.String()
message.Topic = topic.String()
message.Timestamp = int32(time.Now().Unix())
message.Id = uuid.NewV4().String()
if _, err := esclient.Index().Index("chat").Type("message").BodyJson(message).Do(); err != nil {
L.Push(lua.LString(fmt.Sprintf("%s", err)))
L.Push(L.ToNumber(1))
Expand All @@ -70,22 +67,22 @@ func QueryMessages(L *lua.LState) int {
int(lua.LVAsNumber(limit)), topic.String(), int(lua.LVAsNumber(start))))
termQuery := elastic.NewQueryStringQuery(fmt.Sprintf("topic:\"%s\"", topic.String()))
searchResults, err := esclient.Search().Index("chat").Query(termQuery).
Sort("payload.timestamp", false).From(int(lua.LVAsNumber(start))).
Sort("timestamp", false).From(int(lua.LVAsNumber(start))).
Size(int(lua.LVAsNumber(limit))).Do()
if err != nil {
L.Push(lua.LString(fmt.Sprintf("%s", err)))
L.Push(L.ToNumber(1))
return 2
}
payloads := []PayloadStruct{}
messages := []Message{}
var ttyp Message
for _, item := range searchResults.Each(reflect.TypeOf(ttyp)) {
if t, ok := item.(Message); ok {
logger.Logger.Debug(fmt.Sprintf("Message: %s\n", t))
payloads = append(payloads, t.Payload)
messages = append(messages, t)
}
}
L.Push(lua.LNil)
L.Push(luar.New(L, payloads))
L.Push(luar.New(L, messages))
return 2
}
2 changes: 1 addition & 1 deletion plugins/send_history.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function run_plugin(topic, payload)
end
local payload_table = {}
for i = 1, #payloads do
payload_table[i] = {from = payloads[i]["from"], message = payloads[i]["message"], timestamp = payloads[i]["timestamp"]..""}
payload_table[i] = {id = payloads[i]["id"], timestamp = payloads[i]["timestamp"].."", payload = payloads[i]["payload"]}
end
client.send_message(history_topic.."/history/"..user_requesting, 2, false, json.encode(payload_table))
return nil, 0
Expand Down
2 changes: 1 addition & 1 deletion vendor/github.com/onsi/gomega
Submodule gomega updated from fcb0f1 to 9574b2
1 change: 1 addition & 0 deletions vendor/github.com/satori/go.uuid
Submodule go.uuid added at 879c58
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.1
0.9.0

0 comments on commit d8dbb20

Please sign in to comment.