Skip to content

Commit

Permalink
partial fix for missing LastSeen in p2p topics
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Jul 27, 2021
1 parent 7e202e8 commit 344daed
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 42 deletions.
5 changes: 4 additions & 1 deletion server/datamodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ type MsgTopicDesc struct {
// If the group topic is online.
Online bool `json:"online,omitempty"`

// P2P other user's last online timestamp & user agent
LastSeen *MsgLastSeenInfo `json:"seen,omitempty"`

DefaultAcs *MsgDefaultAcsMode `json:"defacs,omitempty"`
// Actual access mode
Acs *MsgAccessMode `json:"acs,omitempty"`
Expand Down Expand Up @@ -484,7 +487,7 @@ type MsgTopicSub struct {
// Id of the latest Delete operation
DelId int `json:"clear,omitempty"`

// P2P topics only:
// P2P topics in 'me' {get subs} response:

// Other user's last online timestamp & user agent
LastSeen *MsgLastSeenInfo `json:"seen,omitempty"`
Expand Down
13 changes: 3 additions & 10 deletions server/db/mongodb/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1335,8 +1335,10 @@ func (a *adapter) TopicsForUser(uid t.Uid, keepDeleted bool, opts *t.QueryOpt) (
uid1, uid2, _ := t.ParseP2P(sub.Topic)
if uid1 == uid {
usrq = append(usrq, uid2.String())
sub.SetWith(uid2.UserId())
} else {
usrq = append(usrq, uid1.String())
sub.SetWith(uid1.UserId())
}
topq = append(topq, tname)
} else {
Expand Down Expand Up @@ -1410,16 +1412,7 @@ func (a *adapter) TopicsForUser(uid t.Uid, keepDeleted bool, opts *t.QueryOpt) (
filter["state"] = b.M{"$ne": t.StateDeleted}
}

if !ims.IsZero() {
// Use cache timestamp if provided: get newer entries only.
filter["updatedat"] = b.M{"$gt": ims}

findOpts = nil
if limit > 0 && limit < len(topq) {
// No point in fetching more than the requested limit.
findOpts = mdbopts.Find().SetLimit(int64(limit))
}
}
// Ignoring ims: we need all users to get LastSeen and UserAgent.

cur, err = a.db.Collection("users").Find(a.ctx, filter, findOpts)
if err != nil {
Expand Down
12 changes: 1 addition & 11 deletions server/db/mysql/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1592,17 +1592,7 @@ func (a *adapter) TopicsForUser(uid t.Uid, keepDeleted bool, opts *t.QueryOpt) (
args = append(args, t.StateDeleted)
}

if !ims.IsZero() {
// Use cache timestamp if provided: get newer entries only.
q += " AND updatedat>?"
args = append(args, ims)

if limit > 0 && limit < len(usrq) {
// No point in fetching more than the requested limit.
q += " ORDER BY updatedat LIMIT ?"
args = append(args, limit)
}
}
// Ignoring ims: we need all users to get LastSeen and UserAgent.

q = a.db.Rebind(q)

Expand Down
12 changes: 3 additions & 9 deletions server/db/rethinkdb/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1141,8 +1141,10 @@ func (a *adapter) TopicsForUser(uid t.Uid, keepDeleted bool, opts *t.QueryOpt) (
uid1, uid2, _ := t.ParseP2P(sub.Topic)
if uid1 == uid {
usrq = append(usrq, uid2.String())
sub.SetWith(uid2.UserId())
} else {
usrq = append(usrq, uid1.String())
sub.SetWith(uid1.UserId())
}
topq = append(topq, tname)
} else {
Expand Down Expand Up @@ -1220,15 +1222,7 @@ func (a *adapter) TopicsForUser(uid t.Uid, keepDeleted bool, opts *t.QueryOpt) (
q = q.Filter(rdb.Row.Field("State").Eq(t.StateDeleted).Not())
}

if !ims.IsZero() {
// Use cache timestamp if provided: get newer entries only.
q = q.Filter(rdb.Row.Field("UpdatedAt").Gt(ims))

if limit > 0 && limit < len(topq) {
// No point in fetching more than the requested limit.
q = q.OrderBy("UpdatedAt").Limit(limit)
}
}
// Ignoring ims: we need all users to get LastSeen and UserAgent.

cursor, err = q.Run(a.conn)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion server/init_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,10 @@ func initTopicP2P(t *Topic, sreg *sessionJoin) error {

uid := types.ParseUid(subs[i].User)
t.perUser[uid] = perUserData{
// Adapter already swapped the public values
// Adapter has already swapped the state, public, defaultAccess, lastSeen values.
public: subs[i].GetPublic(),
lastSeen: subs[i].GetLastSeen(),
lastUA: subs[i].GetUserAgent(),
topicName: types.ParseUid(subs[(i+1)%2].User).UserId(),

private: subs[i].Private,
Expand Down
29 changes: 19 additions & 10 deletions server/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ type perUserData struct {

// P2P only:
public interface{}
lastSeen time.Time
lastUA string
topicName string
deleted bool

Expand Down Expand Up @@ -1875,9 +1877,24 @@ func (t *Topic) replyGetDesc(sess *Session, asUid types.Uid, asChan bool, opts *
desc.State = types.StateOK.String()
}

if t.cat == types.TopicCatGrp && (pud.modeGiven & pud.modeWant).IsPresencer() {
desc.Online = t.isOnline()
if (pud.modeGiven & pud.modeWant).IsPresencer() {
if t.cat == types.TopicCatGrp {
desc.Online = t.isOnline()
} else if t.cat == types.TopicCatP2P {
// This is the timestamp when the other user logged off last time.
// It does not change while the topic is loaded into memory and that's OK most of the time
// because to stay in memory at least one of the users must be connected to topic.
// FIXME(gene): it breaks when user A stays active in one session and connects-disconnects
// from another session. The second session will not see correct LastSeen time and UserAgent.
if !pud.lastSeen.IsZero() {
desc.LastSeen = &MsgLastSeenInfo{
When: &pud.lastSeen,
UserAgent: pud.lastUA,
}
}
}
}

if ifUpdated {
desc.Private = pud.private
}
Expand Down Expand Up @@ -2299,14 +2316,6 @@ func (t *Topic) replyGetSub(sess *Session, asUid types.Uid, authLevel auth.Level
if t.cat == types.TopicCatGrp {
pud := t.perUser[uid]
mts.Online = pud.online > 0 && presencer
} else if t.cat == types.TopicCatP2P {
lastSeen := sub.GetLastSeen()
if !lastSeen.IsZero() {
mts.LastSeen = &MsgLastSeenInfo{
When: &lastSeen,
UserAgent: sub.GetUserAgent(),
}
}
}
}
}
Expand Down

0 comments on commit 344daed

Please sign in to comment.