mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-22 16:47:13 +00:00
52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package kinds
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"grain/relay/handlers/response"
|
|
relay "grain/relay/types"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"golang.org/x/net/websocket"
|
|
)
|
|
|
|
func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error {
|
|
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 err != mongo.ErrNoDocuments {
|
|
if existingEvent.CreatedAt >= evt.CreatedAt {
|
|
response.SendNotice(ws, evt.PubKey, "blocked: a newer kind 0 event already exists for this pubkey")
|
|
response.SendOK(ws, evt.ID, false, "blocked: a newer kind 0 event already exists for this pubkey")
|
|
return nil
|
|
}
|
|
}
|
|
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
"id": evt.ID,
|
|
"created_at": evt.CreatedAt,
|
|
"kind": evt.Kind,
|
|
"tags": evt.Tags,
|
|
"content": evt.Content,
|
|
"sig": evt.Sig,
|
|
},
|
|
}
|
|
|
|
opts := options.Update().SetUpsert(true)
|
|
_, err = collection.UpdateOne(ctx, filter, update, opts)
|
|
if err != nil {
|
|
response.SendOK(ws, evt.ID, false, "error: could not connect to the database")
|
|
return fmt.Errorf("error updating/inserting event kind 0 into MongoDB: %v", err)
|
|
}
|
|
response.SendOK(ws, evt.ID, true, "")
|
|
fmt.Println("Upserted event kind 0 into MongoDB:", evt.ID)
|
|
return nil
|
|
}
|