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
+27 -27
+27 -27
server/main.go
··· 25 25 const ListenTypeFollows = "follows" 26 26 27 27 type SubscriberData struct { 28 - SubscribedTo syntax.DID 29 - Conn *websocket.Conn 30 - ListenType string 31 - ListenTo Set[syntax.DID] 28 + ForActor syntax.DID 29 + Conn *websocket.Conn 30 + ListenType string 31 + ListenTo Set[syntax.DID] 32 32 } 33 33 34 - type UserData struct { 34 + type ActorData struct { 35 35 targets *hashmap.Map[string, *SubscriberData] 36 36 likes map[syntax.RecordKey]bsky.FeedLike 37 37 follows *hashmap.Map[syntax.RecordKey, bsky.GraphFollow] ··· 58 58 // storing the subscriber data in both Should Be Fine 59 59 // we dont modify subscriber data at the same time in two places 60 60 subscribers = hashmap.New[string, *SubscriberData]() 61 - userData = hashmap.New[syntax.DID, *UserData]() 61 + actorData = hashmap.New[syntax.DID, *ActorData]() 62 62 63 63 likeStream *client.Client 64 64 followStream *client.Client ··· 75 75 func getSubscriberDids() []string { 76 76 _dids := make(Set[string], subscribers.Len()) 77 77 subscribers.Range(func(s string, sd *SubscriberData) bool { 78 - _dids[string(sd.SubscribedTo)] = struct{}{} 78 + _dids[string(sd.ForActor)] = struct{}{} 79 79 return true 80 80 }) 81 81 dids := make([]string, 0, len(_dids)) ··· 85 85 return dids 86 86 } 87 87 88 - func getUserData(did syntax.DID) *UserData { 89 - ud, _ := userData.GetOrInsert(did, &UserData{ 88 + func getActorData(did syntax.DID) *ActorData { 89 + ud, _ := actorData.GetOrInsert(did, &ActorData{ 90 90 targets: hashmap.New[string, *SubscriberData](), 91 91 likes: make(map[syntax.RecordKey]bsky.FeedLike), 92 92 follows: hashmap.New[syntax.RecordKey, bsky.GraphFollow](), ··· 94 94 return ud 95 95 } 96 96 97 - func startListeningTo(sid string, sd *SubscriberData, did syntax.DID) { 98 - ud := getUserData(did) 97 + func markActorForLikes(sid string, sd *SubscriberData, did syntax.DID) { 98 + ud := getActorData(did) 99 99 ud.targets.Insert(sid, sd) 100 100 } 101 101 102 - func stopListeningTo(sid string, did syntax.DID) { 103 - if ud, exists := userData.Get(did); exists { 102 + func unmarkActorForLikes(sid string, did syntax.DID) { 103 + if ud, exists := actorData.Get(did); exists { 104 104 ud.targets.Del(sid) 105 105 } 106 106 } ··· 157 157 Host: pdsURI, 158 158 } 159 159 160 - ud := getUserData(did) 160 + ud := getActorData(did) 161 161 sd := &SubscriberData{ 162 - SubscribedTo: did, 163 - Conn: conn, 164 - ListenType: listenType, 162 + ForActor: did, 163 + Conn: conn, 164 + ListenType: listenType, 165 165 } 166 166 167 167 switch listenType { ··· 195 195 196 196 subscribers.Set(sid, sd) 197 197 for listenDid := range sd.ListenTo { 198 - startListeningTo(sid, sd, listenDid) 198 + markActorForLikes(sid, sd, listenDid) 199 199 } 200 200 updateFollowStreamOpts() 201 201 // delete subscriber after we are done 202 202 defer func() { 203 203 for listenDid := range sd.ListenTo { 204 - stopListeningTo(sid, listenDid) 204 + unmarkActorForLikes(sid, listenDid) 205 205 } 206 206 subscribers.Del(sid) 207 207 updateFollowStreamOpts() ··· 230 230 } 231 231 // remove all current listens and add the ones the user requested 232 232 for listenDid := range sd.ListenTo { 233 - stopListeningTo(sid, listenDid) 233 + unmarkActorForLikes(sid, listenDid) 234 234 delete(sd.ListenTo, listenDid) 235 235 } 236 236 for _, listenDid := range innerMsg.ListenTo { 237 237 sd.ListenTo[listenDid] = struct{}{} 238 - startListeningTo(sid, sd, listenDid) 238 + markActorForLikes(sid, sd, listenDid) 239 239 } 240 240 } 241 241 } ··· 271 271 272 272 byDid := syntax.DID(event.Did) 273 273 // skip handling event if its not from a source we are listening to 274 - ud, exists := userData.Get(byDid) 274 + ud, exists := actorData.Get(byDid) 275 275 if !exists || ud.targets.Len() == 0 { 276 276 return nil 277 277 } ··· 315 315 return err 316 316 } 317 317 ud.targets.Range(func(sid string, sd *SubscriberData) bool { 318 - if sd.SubscribedTo != reposterDID { 318 + if sd.ForActor != reposterDID { 319 319 return true 320 320 } 321 321 ··· 327 327 } 328 328 329 329 if err := sd.Conn.WriteJSON(notification); err != nil { 330 - logger.Error("failed to send notification", "subscriber", sd.SubscribedTo, "error", err) 330 + logger.Error("failed to send notification", "subscriber", sd.ForActor, "error", err) 331 331 } 332 332 return true 333 333 }) ··· 341 341 } 342 342 343 343 byDid := syntax.DID(event.Did) 344 - ud, exists := userData.Get(byDid) 344 + ud, exists := actorData.Get(byDid) 345 345 if !exists { 346 346 return nil 347 347 } ··· 374 374 } 375 375 subjectDid := syntax.DID(r.Subject) 376 376 if deleted { 377 - stopListeningTo(sid, subjectDid) 377 + unmarkActorForLikes(sid, subjectDid) 378 378 delete(sd.ListenTo, subjectDid) 379 379 } else { 380 380 sd.ListenTo[subjectDid] = struct{}{} 381 - startListeningTo(sid, sd, subjectDid) 381 + markActorForLikes(sid, sd, subjectDid) 382 382 } 383 383 return true 384 384 })