mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-24 01:17:13 +00:00
check for duplicates before storing
This commit is contained in:
parent
a7ade9a40d
commit
353510b74f
25
server/db/mongo/checkDuplicate.go
Normal file
25
server/db/mongo/checkDuplicate.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package mongo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
nostr "grain/server/types" // Adjust import path as needed
|
||||||
|
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CheckDuplicateEvent checks if an event with the same ID already exists in the collection.
|
||||||
|
func CheckDuplicateEvent(ctx context.Context, evt nostr.Event) (bool, error) {
|
||||||
|
collection := GetCollection(evt.Kind)
|
||||||
|
filter := bson.M{"id": evt.ID}
|
||||||
|
|
||||||
|
var existingEvent nostr.Event
|
||||||
|
err := collection.FindOne(ctx, filter).Decode(&existingEvent)
|
||||||
|
if err != nil {
|
||||||
|
if err.Error() == "mongo: no documents in result" {
|
||||||
|
return false, nil // No duplicate found
|
||||||
|
}
|
||||||
|
return false, fmt.Errorf("error checking for duplicate event: %v", err)
|
||||||
|
}
|
||||||
|
return true, nil // Duplicate found
|
||||||
|
}
|
@ -66,6 +66,19 @@ func HandleEvent(ws *websocket.Conn, message []interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for duplicate event
|
||||||
|
isDuplicate, err := mongo.CheckDuplicateEvent(context.TODO(), evt)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error checking for duplicate event: %v\n", err)
|
||||||
|
response.SendOK(ws, evt.ID, false, "error: internal server error during duplicate check")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if isDuplicate {
|
||||||
|
response.SendOK(ws, evt.ID, false, "blocked: the database already contains this event")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Store the event in MongoDB or other storage
|
// Store the event in MongoDB or other storage
|
||||||
mongo.StoreMongoEvent(context.TODO(), evt, ws)
|
mongo.StoreMongoEvent(context.TODO(), evt, ws)
|
||||||
fmt.Println("Event processed:", evt.ID)
|
fmt.Println("Event processed:", evt.ID)
|
||||||
@ -106,20 +119,20 @@ func validateEventTimestamp(evt nostr.Event) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool {
|
func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool {
|
||||||
// Use the updated CheckBlacklist function
|
// Use the updated CheckBlacklist function
|
||||||
if blacklisted, msg := config.CheckBlacklist(evt.PubKey, evt.Content); blacklisted {
|
if blacklisted, msg := config.CheckBlacklist(evt.PubKey, evt.Content); blacklisted {
|
||||||
response.SendOK(ws, evt.ID, false, msg)
|
response.SendOK(ws, evt.ID, false, msg)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the whitelist using CheckWhitelist function
|
// Check the whitelist using CheckWhitelist function
|
||||||
isWhitelisted, msg := config.CheckWhitelist(evt)
|
isWhitelisted, msg := config.CheckWhitelist(evt)
|
||||||
if !isWhitelisted {
|
if !isWhitelisted {
|
||||||
response.SendOK(ws, evt.ID, false, msg)
|
response.SendOK(ws, evt.ID, false, msg)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleRateAndSizeLimits(ws *websocket.Conn, evt nostr.Event, eventSize int) bool {
|
func handleRateAndSizeLimits(ws *websocket.Conn, evt nostr.Event, eventSize int) bool {
|
||||||
|
Loading…
Reference in New Issue
Block a user