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) {
|
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")
|
||||||
|
21
message.go
21
message.go
@ -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)
|
||||||
}
|
}
|
||||||
|
18
messenger.go
18
messenger.go
@ -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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
16
receiving.go
16
receiving.go
@ -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"`
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user