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

refactor: rename some types, functions etc to be more clear

ptr.pet 6f790069 7dba1ada

verified
Changed files
+27 -27
server
+27 -27
server/main.go
···
const ListenTypeFollows = "follows"
type SubscriberData struct {
-
SubscribedTo syntax.DID
-
Conn *websocket.Conn
-
ListenType string
-
ListenTo Set[syntax.DID]
+
ForActor syntax.DID
+
Conn *websocket.Conn
+
ListenType string
+
ListenTo Set[syntax.DID]
}
-
type UserData struct {
+
type ActorData struct {
targets *hashmap.Map[string, *SubscriberData]
likes map[syntax.RecordKey]bsky.FeedLike
follows *hashmap.Map[syntax.RecordKey, bsky.GraphFollow]
···
// 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]()
-
userData = hashmap.New[syntax.DID, *UserData]()
+
actorData = hashmap.New[syntax.DID, *ActorData]()
likeStream *client.Client
followStream *client.Client
···
func getSubscriberDids() []string {
_dids := make(Set[string], subscribers.Len())
subscribers.Range(func(s string, sd *SubscriberData) bool {
-
_dids[string(sd.SubscribedTo)] = struct{}{}
+
_dids[string(sd.ForActor)] = struct{}{}
return true
})
dids := make([]string, 0, len(_dids))
···
return dids
}
-
func getUserData(did syntax.DID) *UserData {
-
ud, _ := userData.GetOrInsert(did, &UserData{
+
func getActorData(did syntax.DID) *ActorData {
+
ud, _ := actorData.GetOrInsert(did, &ActorData{
targets: hashmap.New[string, *SubscriberData](),
likes: make(map[syntax.RecordKey]bsky.FeedLike),
follows: hashmap.New[syntax.RecordKey, bsky.GraphFollow](),
···
return ud
}
-
func startListeningTo(sid string, sd *SubscriberData, did syntax.DID) {
-
ud := getUserData(did)
+
func markActorForLikes(sid string, sd *SubscriberData, did syntax.DID) {
+
ud := getActorData(did)
ud.targets.Insert(sid, sd)
}
-
func stopListeningTo(sid string, did syntax.DID) {
-
if ud, exists := userData.Get(did); exists {
+
func unmarkActorForLikes(sid string, did syntax.DID) {
+
if ud, exists := actorData.Get(did); exists {
ud.targets.Del(sid)
}
}
···
Host: pdsURI,
}
-
ud := getUserData(did)
+
ud := getActorData(did)
sd := &SubscriberData{
-
SubscribedTo: did,
-
Conn: conn,
-
ListenType: listenType,
+
ForActor: did,
+
Conn: conn,
+
ListenType: listenType,
}
switch listenType {
···
subscribers.Set(sid, sd)
for listenDid := range sd.ListenTo {
-
startListeningTo(sid, sd, listenDid)
+
markActorForLikes(sid, sd, listenDid)
}
updateFollowStreamOpts()
// delete subscriber after we are done
defer func() {
for listenDid := range sd.ListenTo {
-
stopListeningTo(sid, listenDid)
+
unmarkActorForLikes(sid, listenDid)
}
subscribers.Del(sid)
updateFollowStreamOpts()
···
}
// remove all current listens and add the ones the user requested
for listenDid := range sd.ListenTo {
-
stopListeningTo(sid, listenDid)
+
unmarkActorForLikes(sid, listenDid)
delete(sd.ListenTo, listenDid)
}
for _, listenDid := range innerMsg.ListenTo {
sd.ListenTo[listenDid] = struct{}{}
-
startListeningTo(sid, sd, listenDid)
+
markActorForLikes(sid, sd, listenDid)
}
}
}
···
byDid := syntax.DID(event.Did)
// skip handling event if its not from a source we are listening to
-
ud, exists := userData.Get(byDid)
+
ud, exists := actorData.Get(byDid)
if !exists || ud.targets.Len() == 0 {
return nil
}
···
return err
}
ud.targets.Range(func(sid string, sd *SubscriberData) bool {
-
if sd.SubscribedTo != reposterDID {
+
if sd.ForActor != reposterDID {
return true
}
···
}
if err := sd.Conn.WriteJSON(notification); err != nil {
-
logger.Error("failed to send notification", "subscriber", sd.SubscribedTo, "error", err)
+
logger.Error("failed to send notification", "subscriber", sd.ForActor, "error", err)
}
return true
})
···
}
byDid := syntax.DID(event.Did)
-
ud, exists := userData.Get(byDid)
+
ud, exists := actorData.Get(byDid)
if !exists {
return nil
}
···
}
subjectDid := syntax.DID(r.Subject)
if deleted {
-
stopListeningTo(sid, subjectDid)
+
unmarkActorForLikes(sid, subjectDid)
delete(sd.ListenTo, subjectDid)
} else {
sd.ListenTo[subjectDid] = struct{}{}
-
startListeningTo(sid, sd, subjectDid)
+
markActorForLikes(sid, sd, subjectDid)
}
return true
})