document and implement limit/offset behavior.

pull/2/head
fiatjaf 2020-12-06 16:42:47 -03:00
rodzic 3aa187e8fe
commit 7f8c28de0f
2 zmienionych plików z 36 dodań i 11 usunięć

Wyświetl plik

@ -32,13 +32,15 @@ Basic protocol flow description
Upon receiving this, the relay SHOULD begin watching for new events being saved coming from the given pubkeys and return them in the SSE stream identified by the given _session id_. Events triggered by this call MUST have their SSE type set to `n`.
- `POST /request_unwatch?session=<session id>` with body `{"keys": [<32-byte hex-encoded pubkey>, ...]}`
Upon receiving this, the relay SHOULD stop notifying the given SSE stream for updates from the given pubkeys.
- `POST /request_feed?session=<session id>` with optional body `{"limit": 100}`
- `POST /request_feed?session=<session id>` with optional body `{"limit": 50, "offset": 0}`
Upon receiving this, the relay MUST return in the same SSE stream previously opened identified by the given _session id_ recent past events from all the pubkeys it may be watching on behalf of the that SSE stream. Events triggered by this call MUST have their SSE type set to `p`.
- `POST /request_user?session=<session id>` with body `{"pubkey": ...}`
- `POST /request_user?session=<session id>` with body `{"pubkey": ..., "limit": 50, "offset": 0}`
Upon receiving this, the relay MUST return in the same SSE stream previously opened identified by the given _session id_ recent past events from the specified user, including a `set_metadata` event if the relay has it. Events triggered by this call MUST have their SSE type set to `r`.
- `POST /request_note?session=<session id>` with body `{"id": ...}`
- `POST /request_note?session=<session id>` with body `{"id": ..., "limit": 10}`
Same as above, but instead the relay returns the specified `text_note` and/or related notes it has (notes that reference it or notes that it references). Events triggered by this call MUST have their SSE type set to `r`.
The `limit` and `offset` parameters are optional. When they're not specified the relay is free to use any defaults it want. If `limit` is specified the relay MUST use any number equal or smaller than `limit`, never greater. If `offset` is specified the relay must use it too, and if `offset` is too big for the relay it can just do as if no events were found at that range.
### Format of the SSE event (example)
```

Wyświetl plik

@ -93,10 +93,20 @@ func requestFeed(w http.ResponseWriter, r *http.Request) {
}
var data struct {
Limit int `json:"limit"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
json.NewDecoder(r.Body).Decode(&data)
if data.Limit <= 0 || data.Limit > 100 {
data.Limit = 50
}
if data.Offset < 0 {
data.Offset = 0
} else if data.Offset > 500 {
return
}
keys, ok := backwatchers[es]
if !ok {
return
@ -117,8 +127,9 @@ func requestFeed(w http.ResponseWriter, r *http.Request) {
FROM event
WHERE pubkey IN (`+strings.Join(inkeys, ",")+`)
ORDER BY created_at DESC
LIMIT 50
`)
LIMIT $1
OFFSET $2
`, data.Limit, data.Offset)
if err != nil && err != sql.ErrNoRows {
w.WriteHeader(500)
log.Warn().Err(err).Interface("keys", keys).Msg("failed to fetch updates")
@ -171,12 +182,21 @@ func requestUser(w http.ResponseWriter, r *http.Request) {
var data struct {
PubKey string `json:"pubkey"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
json.NewDecoder(r.Body).Decode(&data)
if data.PubKey == "" {
w.WriteHeader(400)
return
}
if data.Limit <= 0 || data.Limit > 100 {
data.Limit = 30
}
if data.Offset < 0 {
data.Offset = 0
} else if data.Offset > 300 {
return
}
go func() {
var metadata Event
@ -198,8 +218,9 @@ func requestUser(w http.ResponseWriter, r *http.Request) {
if err := db.Select(&lastUpdates, `
SELECT * FROM event
WHERE pubkey = $1 AND kind != 0
ORDER BY created_at DESC LIMIT 30
`, data.PubKey); err == nil {
ORDER BY created_at DESC
LIMIT $1 OFFSET $2
`, data.PubKey, data.Limit, data.Offset); err == nil {
for _, evt := range lastUpdates {
jevent, _ := json.Marshal(evt)
(*es).SendEventMessage(string(jevent), "r", "")
@ -228,6 +249,9 @@ func requestNote(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
return
}
if data.Limit > 100 || data.Limit <= 0 {
data.Limit = 50
}
go func() {
var evt Event
@ -255,9 +279,8 @@ func requestNote(w http.ResponseWriter, r *http.Request) {
var related []Event
if err := db.Select(`
SELECT * FROM event WHERE ref = $1
-- UNION ALL
-- SELECT * FROM event WHERE ref IN (SELECT ref FROM event WHERE ref = $1)
`, data.Id); err == nil {
LIMIT $2
`, data.Id, data.Limit); err == nil {
for _, evt := range related {
jevent, _ := json.Marshal(evt)
(*es).SendEventMessage(string(jevent), "r", "")