2024-07-20 01:12:06 +00:00
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2024-07-20 14:03:52 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
2024-07-20 01:12:06 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
2024-07-20 14:03:52 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
2024-07-20 01:12:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func HandleEventKind0(ctx context.Context, evt Event, collection *mongo.Collection) error {
|
2024-07-20 14:03:52 +00:00
|
|
|
// Replace the existing event if it has the same pubkey
|
|
|
|
filter := bson.M{"pubkey": evt.PubKey}
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-07-22 17:50:43 +00:00
|
|
|
opts := options.Update().SetUpsert(true) // Insert if not found
|
|
|
|
_, err := collection.UpdateOne(ctx, filter, update, opts)
|
2024-07-20 01:12:06 +00:00
|
|
|
if err != nil {
|
2024-07-22 17:50:43 +00:00
|
|
|
return fmt.Errorf("Error updating/inserting event kind 0 into MongoDB: %v", err)
|
2024-07-20 01:12:06 +00:00
|
|
|
}
|
|
|
|
|
2024-07-20 14:03:52 +00:00
|
|
|
fmt.Println("Upserted event kind 0 into MongoDB:", evt.ID)
|
2024-07-20 01:12:06 +00:00
|
|
|
return nil
|
|
|
|
}
|