1
0
mirror of synced 2024-11-25 14:26:09 +03:00

Consolidate data structures

This commit is contained in:
Harrison Shoebridge 2016-04-13 19:36:38 +10:00
parent ce641cb233
commit ed71354256
4 changed files with 26 additions and 37 deletions

View File

@ -35,7 +35,7 @@ func main() {
}) })
m.HandleDelivery(func(d messenger.Delivery, r *messenger.Response) { m.HandleDelivery(func(d messenger.Delivery, r *messenger.Response) {
fmt.Println(d.Watermark.Format(time.UnixDate)) fmt.Println(d.Watermark().Format(time.UnixDate))
}) })
fmt.Println("Serving messenger bot on localhost:8080") fmt.Println("Serving messenger bot on localhost:8080")

View File

@ -3,15 +3,20 @@ package messenger
import "time" import "time"
type Message struct { type Message struct {
Sender Sender Sender Sender `json:"-"`
Recipient Recipient Recipient Recipient `json:"-"`
Time time.Time Time time.Time `json:"-"`
Text string Mid string `json:"mid"`
Seq int Text string `json:"text"`
Seq int `json:"seq"`
} }
type Delivery struct { type Delivery struct {
Mids []string Mids []string `json:"mids"`
Watermark time.Time RawWatermark int64 `json:"watermark"`
Seq int Seq int `json:"seq"`
}
func (d Delivery) Watermark() time.Time {
return time.Unix(d.RawWatermark, 0)
} }

View File

@ -91,20 +91,16 @@ func (m *Messenger) dispatch(r Receive) {
switch a { switch a {
case TextAction: case TextAction:
for _, f := range m.messageHandlers { for _, f := range m.messageHandlers {
f(Message{ message := *info.Message
Sender: info.Sender, message.Sender = info.Sender
Recipient: info.Recipient, message.Recipient = info.Recipient
Time: time.Unix(info.Timestamp, 0), message.Time = time.Unix(info.Timestamp, 0)
Text: info.Message.Text,
}, resp) f(message, resp)
} }
case DeliveryAction: case DeliveryAction:
for _, f := range m.deliveryHandlers { for _, f := range m.deliveryHandlers {
f(Delivery{ f(*info.Delivery, resp)
Mids: info.Delivery.Mids,
Seq: info.Delivery.Seq,
Watermark: time.Unix(info.Delivery.Watermark, 0),
}, resp)
} }
} }
} }

View File

@ -15,8 +15,8 @@ type MessageInfo struct {
Sender Sender `json:"sender"` Sender Sender `json:"sender"`
Recipient Recipient `json:"recipient"` Recipient Recipient `json:"recipient"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
Message *MessageCallback `json:"message"` Message *Message `json:"message"`
Delivery *DeliveryCallback `json:"delivery"` Delivery *Delivery `json:"delivery"`
} }
type Sender struct { type Sender struct {
@ -26,15 +26,3 @@ type Sender struct {
type Recipient struct { type Recipient struct {
ID int64 `json:"id"` ID int64 `json:"id"`
} }
type MessageCallback struct {
Mid string `json:"mid"`
Seq int `json:"seq"`
Text string `json:"text"`
}
type DeliveryCallback struct {
Mids []string `json:"mids"`
Watermark int64 `json:"watermark"`
Seq int `json:"seq"`
}