Consolidate data structures
This commit is contained in:
parent
ce641cb233
commit
ed71354256
@ -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")
|
||||
|
21
message.go
21
message.go
@ -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)
|
||||
}
|
||||
|
18
messenger.go
18
messenger.go
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
22
receiving.go
22
receiving.go
@ -12,11 +12,11 @@ type Entry struct {
|
||||
}
|
||||
|
||||
type MessageInfo struct {
|
||||
Sender Sender `json:"sender"`
|
||||
Recipient Recipient `json:"recipient"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Message *MessageCallback `json:"message"`
|
||||
Delivery *DeliveryCallback `json:"delivery"`
|
||||
Sender Sender `json:"sender"`
|
||||
Recipient Recipient `json:"recipient"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
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"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user