its for when you want to get like notifications for your reposts

refactor: remove unused func, reorganize a bit

ptr.pet bbcf2989 75bb7d9b

verified
Changed files
+13 -24
+13 -24
main.go
···
type Set[T comparable] map[T]struct{}
-
// Data structures
type SubscriberData struct {
DID string
Conn *websocket.Conn
···
RepostURI string `json:"repost_uri"`
}
-
// Global state
var (
+
// storing the subscriber data in both Should Be Fine
+
// we dont modify subscriber data at the same time in two places
subscribers = hashmap.New[string, *SubscriberData]()
listeningTo = hashmap.New[string, *hashmap.Map[string, *SubscriberData]]()
···
logger *slog.Logger
)
-
func getFollowsDids() []string {
-
var dids []string
-
subscribers.Range(func(s string, sd *SubscriberData) bool {
-
for follow, _ := range sd.ListenTo {
-
dids = append(dids, follow)
-
}
-
return true
-
})
-
return dids
-
}
-
func getSubscriberDids() []string {
dids := make([]string, 0, subscribers.Len())
subscribers.Range(func(s string, sd *SubscriberData) bool {
···
return true
})
return dids
+
}
+
+
func listenTo(sd *SubscriberData, did string) {
+
targetDids, _ := listeningTo.GetOrInsert(did, hashmap.New[string, *SubscriberData]())
+
targetDids.Insert(sd.DID, sd)
+
}
+
+
func stopListeningTo(subscriberDid, did string) {
+
if targetDids, exists := listeningTo.Get(did); exists {
+
targetDids.Del(subscriberDid)
+
}
}
func main() {
···
logger.Info("WebSocket connection closed", "error", err)
break
}
-
}
-
}
-
-
func listenTo(sd *SubscriberData, did string) {
-
targetDids, _ := listeningTo.GetOrInsert(did, hashmap.New[string, *SubscriberData]())
-
targetDids.Insert(sd.DID, sd)
-
}
-
-
func stopListeningTo(subscriberDid, did string) {
-
if targetDids, exists := listeningTo.Get(did); exists {
-
targetDids.Del(subscriberDid)
}
}