Use visitor instead of UserID in topicSubscription

This commit is contained in:
Karmanyaah Malhotra 2023-02-14 13:07:32 -06:00
parent cc309e87e9
commit d686e1ee77
2 changed files with 9 additions and 8 deletions

View file

@ -1023,7 +1023,7 @@ func (s *Server) handleSubscribeHTTP(w http.ResponseWriter, r *http.Request, v *
defer cancel()
subscriberIDs := make([]int, 0)
for _, t := range topics {
subscriberIDs = append(subscriberIDs, t.Subscribe(sub, v.MaybeUserID(), cancel))
subscriberIDs = append(subscriberIDs, t.Subscribe(sub, v, cancel))
}
defer func() {
for i, subscriberID := range subscriberIDs {
@ -1155,7 +1155,7 @@ func (s *Server) handleSubscribeWS(w http.ResponseWriter, r *http.Request, v *vi
}
subscriberIDs := make([]int, 0)
for _, t := range topics {
subscriberIDs = append(subscriberIDs, t.Subscribe(sub, v.MaybeUserID(), cancel))
subscriberIDs = append(subscriberIDs, t.Subscribe(sub, v, cancel))
}
defer func() {
for i, subscriberID := range subscriberIDs {

View file

@ -15,8 +15,8 @@ type topic struct {
}
type topicSubscriber struct {
userID string // User ID associated with this subscription, may be empty
subscriber subscriber
visitor *visitor // User ID associated with this subscription, may be empty
cancel func()
}
@ -32,12 +32,12 @@ func newTopic(id string) *topic {
}
// Subscribe subscribes to this topic
func (t *topic) Subscribe(s subscriber, userID string, cancel func()) int {
func (t *topic) Subscribe(s subscriber, visitor *visitor, cancel func()) int {
t.mu.Lock()
defer t.mu.Unlock()
subscriberID := rand.Int()
t.subscribers[subscriberID] = &topicSubscriber{
userID: userID, // May be empty
visitor: visitor, // May be empty
subscriber: s,
cancel: cancel,
}
@ -87,8 +87,9 @@ func (t *topic) CancelSubscribers(exceptUserID string) {
t.mu.Lock()
defer t.mu.Unlock()
for _, s := range t.subscribers {
if s.userID != exceptUserID {
log.Tag(tagSubscribe).Field("topic", t.ID).Debug("Canceling subscriber %s", s.userID)
if s.visitor.MaybeUserID() != exceptUserID {
// TODO: Shouldn't this log the IP for anonymous visitors? It was s.userID before my change.
log.Tag(tagSubscribe).Field("topic", t.ID).Debug("Canceling subscriber %s", s.visitor.MaybeUserID())
s.cancel()
}
}
@ -101,7 +102,7 @@ func (t *topic) subscribersCopy() map[int]*topicSubscriber {
subscribers := make(map[int]*topicSubscriber)
for k, sub := range t.subscribers {
subscribers[k] = &topicSubscriber{
userID: sub.userID,
visitor: sub.visitor,
subscriber: sub.subscriber,
cancel: sub.cancel,
}