From 32734c32b8439c1a34130269a29ca8271b7be619 Mon Sep 17 00:00:00 2001 From: or-else Date: Thu, 11 Oct 2018 13:25:27 +0300 Subject: [PATCH] start reporting default access in fnd results --- server/db/mysql/adapter.go | 16 ++++++++-------- server/db/rethinkdb/adapter.go | 6 ++---- server/topic.go | 16 +++++++++++++--- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/server/db/mysql/adapter.go b/server/db/mysql/adapter.go index 8336abc86..2ae953340 100644 --- a/server/db/mysql/adapter.go +++ b/server/db/mysql/adapter.go @@ -1275,7 +1275,7 @@ func (a *adapter) FindUsers(uid t.Uid, req, opt []string) ([]t.Subscription, err index[tag] = struct{}{} } - query := "SELECT u.id,u.createdat,u.updatedat,u.public,u.tags,COUNT(*) AS matches " + + query := "SELECT u.id,u.createdat,u.updatedat,u.access,u.public,u.tags,COUNT(*) AS matches " + "FROM users AS u LEFT JOIN usertags AS t ON t.userid=u.id " + "WHERE t.tag IN (?" + strings.Repeat(",?", len(req)+len(opt)-1) + ") " + "GROUP BY u.id,u.createdat,u.updatedat,u.public,u.tags " @@ -1297,13 +1297,14 @@ func (a *adapter) FindUsers(uid t.Uid, req, opt []string) ([]t.Subscription, err var userId int64 var public interface{} + var access t.DefaultAccess var userTags t.StringSlice var ignored int var sub t.Subscription var subs []t.Subscription thisUser := store.DecodeUid(uid) for rows.Next() { - if err = rows.Scan(&userId, &sub.CreatedAt, &sub.UpdatedAt, &public, &userTags, &ignored); err != nil { + if err = rows.Scan(&userId, &sub.CreatedAt, &sub.UpdatedAt, &access, &public, &userTags, &ignored); err != nil { subs = nil break } @@ -1314,8 +1315,7 @@ func (a *adapter) FindUsers(uid t.Uid, req, opt []string) ([]t.Subscription, err } sub.User = store.EncodeUid(userId).String() sub.SetPublic(fromJSON(public)) - // TODO: maybe report default access to user - // sub.SetDefaultAccess(user.Access.Auth, user.Access.Anon) + sub.SetDefaultAccess(access.Auth, access.Anon) foundTags := make([]string, 0, 1) for _, tag := range userTags { if _, ok := index[tag]; ok { @@ -1341,7 +1341,7 @@ func (a *adapter) FindTopics(req, opt []string) ([]t.Subscription, error) { index[tag] = struct{}{} } - query := "SELECT t.name AS topic,t.createdat,t.updatedat,t.public,t.tags,COUNT(*) AS matches " + + query := "SELECT t.name AS topic,t.createdat,t.updatedat,t.access,t.public,t.tags,COUNT(*) AS matches " + "FROM topics AS t LEFT JOIN topictags AS tt ON t.name=tt.topic " + "WHERE tt.tag IN (?" + strings.Repeat(",?", len(req)+len(opt)-1) + ") " + "GROUP BY t.name,t.createdat,t.updatedat,t.public,t.tags " @@ -1359,20 +1359,20 @@ func (a *adapter) FindTopics(req, opt []string) ([]t.Subscription, error) { return nil, err } + var access t.DefaultAccess var public interface{} var topicTags t.StringSlice var ignored int var sub t.Subscription var subs []t.Subscription for rows.Next() { - if err = rows.Scan(&sub.Topic, &sub.CreatedAt, &sub.UpdatedAt, &public, &topicTags, &ignored); err != nil { + if err = rows.Scan(&sub.Topic, &sub.CreatedAt, &sub.UpdatedAt, &access, &public, &topicTags, &ignored); err != nil { subs = nil break } sub.SetPublic(fromJSON(public)) - // TODO: maybe report default access to user - // sub.SetDefaultAccess(user.Access.Auth, user.Access.Anon) + sub.SetDefaultAccess(access.Auth, access.Anon) foundTags := make([]string, 0, 1) for _, tag := range topicTags { if _, ok := index[tag]; ok { diff --git a/server/db/rethinkdb/adapter.go b/server/db/rethinkdb/adapter.go index 5ce3f9b13..0b847dc84 100644 --- a/server/db/rethinkdb/adapter.go +++ b/server/db/rethinkdb/adapter.go @@ -1073,8 +1073,7 @@ func (a *adapter) FindUsers(uid t.Uid, req, opt []string) ([]t.Subscription, err sub.UpdatedAt = user.UpdatedAt sub.User = user.Id sub.SetPublic(user.Public) - // TODO: maybe report default access to user - // sub.SetDefaultAccess(user.Access.Auth, user.Access.Anon) + sub.SetDefaultAccess(user.Access.Auth, user.Access.Anon) tags := make([]string, 0, 1) for _, tag := range user.Tags { if _, ok := index[tag]; ok { @@ -1138,8 +1137,7 @@ func (a *adapter) FindTopics(req, opt []string) ([]t.Subscription, error) { sub.UpdatedAt = topic.UpdatedAt sub.Topic = topic.Id sub.SetPublic(topic.Public) - // TODO: maybe report default access to user - // sub.SetDefaultAccess(user.Access.Auth, user.Access.Anon) + sub.SetDefaultAccess(topic.Access.Auth, topic.Access.Anon) tags := make([]string, 0, 1) for _, tag := range topic.Tags { if _, ok := index[tag]; ok { diff --git a/server/topic.go b/server/topic.go index 8c4abb602..feaa2f951 100644 --- a/server/topic.go +++ b/server/topic.go @@ -483,6 +483,7 @@ func (t *Topic) run(hub *Hub) { case meta := <-t.meta: // Request to get/set topic metadata asUid := types.ParseUserId(meta.pkt.from) + authLevel := auth.Level(meta.pkt.authLvl) switch { case meta.pkt.Get != nil: // Get request @@ -492,7 +493,7 @@ func (t *Topic) run(hub *Hub) { } } if meta.what&constMsgMetaSub != 0 { - if err := t.replyGetSub(meta.sess, asUid, meta.pkt.Get.Id, meta.pkt.Get.Sub); err != nil { + if err := t.replyGetSub(meta.sess, asUid, authLevel, meta.pkt.Get.Id, meta.pkt.Get.Sub); err != nil { log.Printf("topic[%s] meta.Get.Sub failed: %s", t.name, err) } } @@ -610,6 +611,7 @@ func (t *Topic) run(hub *Hub) { // Session subscribed to a topic, created == true if topic was just created and {pres} needs to be announced func (t *Topic) handleSubscription(h *Hub, sreg *sessionJoin) error { asUid := types.ParseUserId(sreg.pkt.from) + authLevel := auth.Level(sreg.pkt.authLvl) msgsub := sreg.pkt.Sub getWhat := 0 @@ -694,7 +696,7 @@ func (t *Topic) handleSubscription(h *Hub, sreg *sessionJoin) error { if getWhat&constMsgMetaSub != 0 { // Send get.sub response as a separate {meta} packet - if err := t.replyGetSub(sreg.sess, asUid, sreg.pkt.id, msgsub.Get.Sub); err != nil { + if err := t.replyGetSub(sreg.sess, asUid, authLevel, sreg.pkt.id, msgsub.Get.Sub); err != nil { log.Printf("topic[%s] handleSubscription Get.Sub failed: %v", t.name, err) } } @@ -1438,7 +1440,7 @@ func (t *Topic) replySetDesc(sess *Session, asUid types.Uid, set *MsgClientSet) // replyGetSub is a response to a get.sub request on a topic - load a list of subscriptions/subscribers, // send it just to the session as a {meta} packet -func (t *Topic) replyGetSub(sess *Session, asUid types.Uid, id string, req *MsgGetOpts) error { +func (t *Topic) replyGetSub(sess *Session, asUid types.Uid, authLevel auth.Level, id string, req *MsgGetOpts) error { now := types.TimeNow() if req != nil && (req.SinceId != 0 || req.BeforeId != 0) { @@ -1626,6 +1628,14 @@ func (t *Topic) replyGetSub(sess *Session, asUid types.Uid, id string, req *MsgG mts.Acs.Want = sub.ModeWant.String() mts.Acs.Given = sub.ModeGiven.String() } + } else if defacs := sub.GetDefaultAccess(); defacs != nil { + // Topic 'fnd' + switch authLevel { + case auth.LevelAnon: + mts.Acs.Mode = defacs.Anon.String() + case auth.LevelAuth, auth.LevelRoot: + mts.Acs.Mode = defacs.Auth.String() + } } // Returning public and private only if they have changed since ifModified