Use visitor instead of UserID in topicSubscription

pull/609/head
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() defer cancel()
subscriberIDs := make([]int, 0) subscriberIDs := make([]int, 0)
for _, t := range topics { for _, t := range topics {
subscriberIDs = append(subscriberIDs, t.Subscribe(sub, v.MaybeUserID(), cancel)) subscriberIDs = append(subscriberIDs, t.Subscribe(sub, v, cancel))
} }
defer func() { defer func() {
for i, subscriberID := range subscriberIDs { 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) subscriberIDs := make([]int, 0)
for _, t := range topics { for _, t := range topics {
subscriberIDs = append(subscriberIDs, t.Subscribe(sub, v.MaybeUserID(), cancel)) subscriberIDs = append(subscriberIDs, t.Subscribe(sub, v, cancel))
} }
defer func() { defer func() {
for i, subscriberID := range subscriberIDs { for i, subscriberID := range subscriberIDs {

View File

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