Skip to content

Commit

Permalink
Merge pull request #832 from tinode/online
Browse files Browse the repository at this point in the history
Fix online status of unchanged online connections
  • Loading branch information
or-else committed Feb 20, 2023
2 parents 311db51 + 5811d12 commit e995606
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
13 changes: 13 additions & 0 deletions server/store/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,9 @@ type Subscription struct {

// Topic's or user's state.
state ObjState

// This is not a fully initialized subscription object
dummy bool
}

// SetPublic assigns a value to `public`, otherwise not accessible from outside the package.
Expand Down Expand Up @@ -1033,6 +1036,16 @@ func (s *Subscription) SetState(state ObjState) {
s.state = state
}

// SetDummy marks this subscription object as only partially intialized.
func (s *Subscription) SetDummy(dummy bool) {
s.dummy = dummy
}

// IsDummy is true if this subscription object as only partially intialized.
func (s *Subscription) IsDummy() bool {
return s.dummy
}

// Contact is a result of a search for connections
type Contact struct {
Id string
Expand Down
36 changes: 31 additions & 5 deletions server/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2367,6 +2367,30 @@ func (t *Topic) replyGetSub(sess *Session, asUid types.Uid, authLevel auth.Level
} else {
// User manages cache. Include deleted subscriptions too.
subs, err = store.Users.GetTopicsAny(asUid, msgOpts2storeOpts(req))

// Returned subscriptions do not contain topics which are online now but otherwise unchanged.
// We need to add these topic to the list otherwise the user would see them as offline.
selected := map[string]struct{}{}
for i := range subs {
sub := &subs[i]
with := sub.GetWith()
if with != "" {
selected[with] = struct{}{}
} else {
selected[sub.Topic] = struct{}{}
}
}

// Add dummy subscriptions for missing online topics.
for topic, psd := range t.perSubs {
_, present := selected[topic]
if !present && psd.online {
sub := types.Subscription{Topic: topic}
sub.SetWith(topic)
sub.SetDummy(true)
subs = append(subs, sub)
}
}
}
case types.TopicCatFnd:
// Select public or private query. Public has priority.
Expand Down Expand Up @@ -2499,15 +2523,15 @@ func (t *Topic) replyGetSub(sess *Session, asUid types.Uid, authLevel auth.Level

if !deleted && !banned {
if isReader {
if sub.GetTouchedAt().IsZero() {
touchedAt := sub.GetTouchedAt()
if touchedAt.IsZero() {
mts.TouchedAt = nil
} else {
touchedAt := sub.GetTouchedAt()
mts.TouchedAt = &touchedAt
}
mts.SeqId = sub.GetSeqId()
mts.DelId = sub.DelId
} else {
} else if !sub.UpdatedAt.IsZero() {
mts.TouchedAt = &sub.UpdatedAt
}

Expand Down Expand Up @@ -2545,15 +2569,17 @@ func (t *Topic) replyGetSub(sess *Session, asUid types.Uid, authLevel auth.Level
}

if !deleted {
mts.UpdatedAt = &sub.UpdatedAt
if !sub.UpdatedAt.IsZero() {
mts.UpdatedAt = &sub.UpdatedAt
}
if isReader && !banned {
mts.ReadSeqId = sub.ReadSeqId
mts.RecvSeqId = sub.RecvSeqId
}

if t.cat != types.TopicCatFnd {
// p2p and grp
if sharer || uid == asUid || subMode.IsAdmin() {
if !sub.IsDummy() && (sharer || uid == asUid || subMode.IsAdmin()) {
// If user is not a sharer, the access mode of other ordinary users if not accessible.
// Own and admin permissions only are visible to non-sharers.
mts.Acs.Mode = subMode.String()
Expand Down

0 comments on commit e995606

Please sign in to comment.