mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-10-30 01:26:32 +00:00
only upserting kind0 event if created at is newer
This commit is contained in:
parent
4389be4009
commit
8ad674401b
@ -1,6 +1,6 @@
|
|||||||
# GRAIN 🌾
|
# GRAIN 🌾
|
||||||
|
|
||||||
**Go Relay Archetecture for Implementing Nostr**
|
## Go Relay Archetecture for Implementing Nostr
|
||||||
|
|
||||||
GRAIN is an open-source Nostr relay implementation written in Go. This project aims to provide a robust and efficient Nostr relay that currently supports the NIP-01 of the nostr protocol.
|
GRAIN is an open-source Nostr relay implementation written in Go. This project aims to provide a robust and efficient Nostr relay that currently supports the NIP-01 of the nostr protocol.
|
||||||
|
|
||||||
@ -29,8 +29,7 @@ cp config.example.yml config.yml
|
|||||||
|
|
||||||
### TODO
|
### TODO
|
||||||
|
|
||||||
- Handle more kinds
|
- Explicitely Handle more kinds
|
||||||
- Fix Request Query to handle and serve unhandled events by kind
|
|
||||||
- create whitelist/blacklist functionality
|
- create whitelist/blacklist functionality
|
||||||
for:
|
for:
|
||||||
- valid nip05 domain
|
- valid nip05 domain
|
||||||
|
@ -54,7 +54,7 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) {
|
|||||||
var err error
|
var err error
|
||||||
switch evt.Kind {
|
switch evt.Kind {
|
||||||
case 0:
|
case 0:
|
||||||
err = kinds.HandleKind0(ctx, evt, collection)
|
err = kinds.HandleKind0(ctx, evt, collection, ws)
|
||||||
case 1:
|
case 1:
|
||||||
err = kinds.HandleKind1(ctx, evt, collection)
|
err = kinds.HandleKind1(ctx, evt, collection)
|
||||||
default:
|
default:
|
||||||
|
@ -2,18 +2,35 @@ package kinds
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
relay "grain/relay/types"
|
relay "grain/relay/types"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
"golang.org/x/net/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collection) error {
|
func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error {
|
||||||
// Replace the existing event if it has the same pubkey
|
// Find the existing event with the same pubkey
|
||||||
filter := bson.M{"pubkey": evt.PubKey}
|
filter := bson.M{"pubkey": evt.PubKey}
|
||||||
|
var existingEvent relay.Event
|
||||||
|
err := collection.FindOne(ctx, filter).Decode(&existingEvent)
|
||||||
|
if err != nil && err != mongo.ErrNoDocuments {
|
||||||
|
return fmt.Errorf("Error finding existing event: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If an existing event is found, compare the created_at times
|
||||||
|
if err != mongo.ErrNoDocuments {
|
||||||
|
if existingEvent.CreatedAt >= evt.CreatedAt {
|
||||||
|
// If the existing event is newer or the same, respond with a NOTICE
|
||||||
|
sendNotice(ws, evt.PubKey, "relay already has a newer kind 0 event for this pubkey")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace the existing event if it has the same pubkey
|
||||||
update := bson.M{
|
update := bson.M{
|
||||||
"$set": bson.M{
|
"$set": bson.M{
|
||||||
"id": evt.ID,
|
"id": evt.ID,
|
||||||
@ -26,7 +43,7 @@ func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collect
|
|||||||
}
|
}
|
||||||
|
|
||||||
opts := options.Update().SetUpsert(true) // Insert if not found
|
opts := options.Update().SetUpsert(true) // Insert if not found
|
||||||
_, err := collection.UpdateOne(ctx, filter, update, opts)
|
_, err = collection.UpdateOne(ctx, filter, update, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error updating/inserting event kind 0 into MongoDB: %v", err)
|
return fmt.Errorf("Error updating/inserting event kind 0 into MongoDB: %v", err)
|
||||||
}
|
}
|
||||||
@ -34,3 +51,9 @@ func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collect
|
|||||||
fmt.Println("Upserted event kind 0 into MongoDB:", evt.ID)
|
fmt.Println("Upserted event kind 0 into MongoDB:", evt.ID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sendNotice(ws *websocket.Conn, pubKey, message string) {
|
||||||
|
notice := []interface{}{"NOTICE", pubKey, message}
|
||||||
|
noticeBytes, _ := json.Marshal(notice)
|
||||||
|
websocket.Message.Send(ws, string(noticeBytes))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user