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) {
fmt.Println(d.Watermark.Format(time.UnixDate))
fmt.Println(d.Watermark().Format(time.UnixDate))
})
fmt.Println("Serving messenger bot on localhost:8080")

View File

@ -3,15 +3,20 @@ package messenger
import "time"
type Message struct {
Sender Sender
Recipient Recipient
Time time.Time
Text string
Seq int
Sender Sender `json:"-"`
Recipient Recipient `json:"-"`
Time time.Time `json:"-"`
Mid string `json:"mid"`
Text string `json:"text"`
Seq int `json:"seq"`
}
type Delivery struct {
Mids []string
Watermark time.Time
Seq int
Mids []string `json:"mids"`
RawWatermark int64 `json:"watermark"`
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 {
case TextAction:
for _, f := range m.messageHandlers {
f(Message{
Sender: info.Sender,
Recipient: info.Recipient,
Time: time.Unix(info.Timestamp, 0),
Text: info.Message.Text,
}, resp)
message := *info.Message
message.Sender = info.Sender
message.Recipient = info.Recipient
message.Time = time.Unix(info.Timestamp, 0)
f(message, resp)
}
case DeliveryAction:
for _, f := range m.deliveryHandlers {
f(Delivery{
Mids: info.Delivery.Mids,
Seq: info.Delivery.Seq,
Watermark: time.Unix(info.Delivery.Watermark, 0),
}, resp)
f(*info.Delivery, resp)
}
}
}

View File

@ -15,8 +15,8 @@ type MessageInfo struct {
Sender Sender `json:"sender"`
Recipient Recipient `json:"recipient"`
Timestamp int64 `json:"timestamp"`
Message *MessageCallback `json:"message"`
Delivery *DeliveryCallback `json:"delivery"`
Message *Message `json:"message"`
Delivery *Delivery `json:"delivery"`
}
type Sender struct {
@ -26,15 +26,3 @@ type Sender struct {
type Recipient struct {
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"`
}