Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7692c8b

Browse files
committedMay 1, 2024
Implement get_history command (WIP)
1 parent cf3e621 commit 7692c8b

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed
 

Diff for: ‎README.md

+30-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ Creates a new incoming message from the client:
5252
}
5353
```
5454

55+
### `get_history`
56+
57+
Requests message history for the current contact:
58+
59+
```json
60+
{
61+
"type": "get_history"
62+
}
63+
```
64+
65+
```json
66+
{
67+
"type": "get_history",
68+
"before": "2024-05-01T17:15:30.123456Z"
69+
}
70+
```
71+
5572
### `set_email`
5673

5774
Updates the email address for the current contact:
@@ -105,6 +122,17 @@ A new outgoing message has been created and should be displayed in the client:
105122
"type": "msg_created",
106123
"text": "How can we help?",
107124
"origin": "chat",
108-
"origin": "bob@nyaruka.com"
125+
"user": "bob@nyaruka.com"
126+
}
127+
```
128+
129+
### `history_fetched`
130+
131+
A new outgoing message has been created and should be displayed in the client:
132+
133+
```json
134+
{
135+
"type": "history_fetched",
136+
"history": []
109137
}
110-
```
138+
```

Diff for: ‎web/client.go

+14
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ func (c *Client) onCommand(cmd commands.Command) error {
117117
return errors.Wrap(err, "error notifying courier")
118118
}
119119

120+
case *commands.GetHistory:
121+
if c.contact == nil {
122+
log.Debug("chat not started, command ignored")
123+
return nil
124+
}
125+
126+
msgs, err := models.LoadContactMessages(ctx, c.server.rt, c.contact.ID, typed.Before, 10)
127+
if err != nil {
128+
return errors.Wrap(err, "error loading contact messages")
129+
130+
}
131+
132+
c.Send(events.NewHistoryFetched(msgs))
133+
120134
case *commands.SetEmail:
121135
if c.contact == nil {
122136
log.Debug("chat not started, command ignored")

Diff for: ‎web/commands/get_history.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package commands
2+
3+
import "time"
4+
5+
func init() {
6+
registerType(TypeGetHistory, func() Command { return &GetHistory{} })
7+
}
8+
9+
const TypeGetHistory string = "get_history"
10+
11+
type GetHistory struct {
12+
baseCommand
13+
14+
Before *time.Time `json:"before"`
15+
}

Diff for: ‎web/events/history_fetched.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package events
2+
3+
import "github.com/nyaruka/tembachat/core/models"
4+
5+
const TypeHistoryFetched string = "history_fetched"
6+
7+
type HistoryFetched struct {
8+
baseEvent
9+
10+
History []any `json:"history"`
11+
}
12+
13+
func NewHistoryFetched(msgs []*models.Msg) *HistoryFetched {
14+
// TODO convert messages to something
15+
16+
return &HistoryFetched{baseEvent: baseEvent{Type_: TypeHistoryFetched}, History: []any{}}
17+
}

0 commit comments

Comments
 (0)