// TopicCreateP2P given two users creates a p2p topic func (a *RethinkDbAdapter) TopicCreateP2P(initiator, invited *t.Subscription) error { initiator.Id = initiator.Topic + ":" + initiator.User // Don't care if the initiator changes own subscription _, err := rdb.DB(a.dbName).Table("subscriptions").Insert(initiator, rdb.InsertOpts{Conflict: "replace"}). RunWrite(a.conn) if err != nil { return err } // Ensure this is a new subscription. If one already exist, don't overwrite it invited.Id = invited.Topic + ":" + invited.User _, err = rdb.DB(a.dbName).Table("subscriptions").Insert(invited, rdb.InsertOpts{Conflict: "error"}). RunWrite(a.conn) if err != nil { // Is this a duplicate subscription? If so, ifnore it. Otherwise it's a genuine DB error if !rdb.IsConflictErr(err) { return err } } topic := &t.Topic{ ObjHeader: t.ObjHeader{Id: initiator.Topic}, Access: t.DefaultAccess{Auth: t.ModeP2P, Anon: t.ModeBanned}} topic.ObjHeader.MergeTimes(&initiator.ObjHeader) return a.TopicCreate(topic) }
// UsersForTopic loads users subscribed to the given topic func (a *RethinkDbAdapter) UsersForTopic(topic string) ([]t.Subscription, error) { // Fetch topic subscribers // Fetch all subscribed users. The number of users is not large q := rdb.DB(a.dbName).Table("subscriptions").GetAllByIndex("Topic", topic).Limit(MAX_RESULTS) //log.Printf("RethinkDbAdapter.UsersForTopic q: %+v", q) rows, err := q.Run(a.conn) if err != nil { return nil, err } // Fetch subscriptions var sub t.Subscription var subs []t.Subscription join := make(map[string]t.Subscription) usrq := make([]interface{}, 0, 16) for rows.Next(&sub) { join[sub.User] = sub usrq = append(usrq, sub.User) } //log.Printf("RethinkDbAdapter.UsersForTopic usrq: %+v, usrq) if len(usrq) > 0 { subs = make([]t.Subscription, 0, len(usrq)) // Fetch users by a list of subscriptions rows, err = rdb.DB(a.dbName).Table("users").GetAll(usrq...).Run(a.conn) if err != nil { return nil, err } var usr t.User for rows.Next(&usr) { if sub, ok := join[usr.Id]; ok { sub.ObjHeader.MergeTimes(&usr.ObjHeader) sub.SetPublic(usr.Public) subs = append(subs, sub) } } //log.Printf("RethinkDbAdapter.UsersForTopic users: %+v", subs) } return subs, nil }
// SubsForTopic fetches all subsciptions for a topic. func (a *RethinkDbAdapter) SubsForTopic(topic string) ([]t.Subscription, error) { //log.Println("Loading subscriptions for topic ", topic) // must load User.Public for p2p topics var p2p []t.User var err error if t.GetTopicCat(topic) == t.TopicCat_P2P { uid1, uid2, _ := t.ParseP2P(topic) if p2p, err = a.UserGetAll(uid1, uid2); err != nil { return nil, err } else if len(p2p) != 2 { return nil, errors.New("failed to load two p2p users") } } q := rdb.DB(a.dbName).Table("subscriptions").GetAllByIndex("Topic", topic).Limit(MAX_RESULTS) //log.Println("Loading subscription q=", q) rows, err := q.Run(a.conn) if err != nil { return nil, err } var subs []t.Subscription var ss t.Subscription for rows.Next(&ss) { if p2p != nil { if p2p[0].Id == ss.User { ss.SetPublic(p2p[1].Public) ss.SetWith(p2p[1].Id) } else { ss.SetPublic(p2p[0].Public) ss.SetWith(p2p[0].Id) } } subs = append(subs, ss) //log.Printf("SubsForTopic: loaded sub %#+v", ss) } return subs, rows.Err() }
// FindSubs returns a list of users who match given tags, such as "email:[email protected]" or "tel:18003287448". // Just search the 'users.Tags' for the given tags using respective index. func (a *RethinkDbAdapter) FindSubs(user t.Uid, query []interface{}) ([]t.Subscription, error) { // Query may contain redundant records, i.e. the same email twice. // User could be matched on multiple tags, i.e on email and phone#. Thus the query may // return duplicate users. Thus the need for distinct. if rows, err := rdb.DB(a.dbName).Table("users").GetAllByIndex("Tags", query...).Limit(MAX_RESULTS). Pluck("Id", "Access", "CreatedAt", "UpdatedAt", "Public", "Tags").Distinct().Run(a.conn); err != nil { return nil, err } else { index := make(map[string]struct{}) for _, q := range query { if tag, ok := q.(string); ok { index[tag] = struct{}{} } } var user t.User var sub t.Subscription var subs []t.Subscription for rows.Next(&user) { sub.CreatedAt = user.CreatedAt sub.UpdatedAt = user.UpdatedAt sub.User = user.Id sub.ModeWant, sub.ModeGiven = user.Access.Auth, user.Access.Auth sub.SetPublic(user.Public) tags := make([]string, 0, 1) for _, tag := range user.Tags { if _, ok := index[tag]; ok { tags = append(tags, tag) } } sub.Private = tags subs = append(subs, sub) } if err = rows.Err(); err != nil { return nil, err } return subs, nil } }
func (SubsObjMapper) Create(sub *types.Subscription) error { sub.InitTimes() _, err := adaptr.TopicShare([]types.Subscription{*sub}) return err }
// CreateP2P creates a P2P topic by generating two user's subsciptions to each other. func (TopicsObjMapper) CreateP2P(initiator, invited *types.Subscription) error { initiator.InitTimes() invited.InitTimes() return adaptr.TopicCreateP2P(initiator, invited) }
// topicInit reads an existing topic from database or creates a new topic func topicInit(sreg *sessionJoin, h *Hub) { var t *Topic timestamp := time.Now().UTC().Round(time.Millisecond) t = &Topic{name: sreg.topic, original: sreg.pkt.Topic, sessions: make(map[*Session]bool), broadcast: make(chan *ServerComMessage, 256), reg: make(chan *sessionJoin, 32), unreg: make(chan *sessionLeave, 32), meta: make(chan *metaReq, 32), perUser: make(map[types.Uid]perUserData), exit: make(chan *shutDown, 1), } // Request to load a 'me' topic. The topic always axists. if t.original == "me" { log.Println("hub: loading 'me' topic") t.cat = types.TopicCat_Me // 'me' has no owner, t.owner = nil // Ensure all requests to subscribe are automatically rejected t.accessAuth = types.ModeBanned t.accessAnon = types.ModeBanned user, err := store.Users.Get(sreg.sess.uid) if err != nil { log.Println("hub: cannot load user object for 'me'='" + t.name + "' (" + err.Error() + ")") sreg.sess.queueOut(ErrUnknown(sreg.pkt.Id, t.original, timestamp)) return } if err = t.loadSubscribers(); err != nil { log.Println("hub: cannot load subscribers for '" + t.name + "' (" + err.Error() + ")") sreg.sess.queueOut(ErrUnknown(sreg.pkt.Id, t.original, timestamp)) return } t.public = user.Public t.created = user.CreatedAt t.updated = user.UpdatedAt t.lastId = user.SeqId t.clearId = user.ClearId // Initiate User Agent with the UA of the creating session to report it later t.userAgent = sreg.sess.userAgent // Request to load a 'find' topic. The topic always exists. } else if t.original == "fnd" { log.Println("hub: loading 'fnd' topic") t.cat = types.TopicCat_Fnd // 'fnd' has no owner, t.owner = nil // The p2p topic cannot be accessed by anyone but the 2 participants. Access is checked at the session level. // Set default access in case user leaves - joins. t.accessAuth = types.ModeBanned t.accessAnon = types.ModeBanned if err := t.loadSubscribers(); err != nil { log.Println("hub: cannot load subscribers for '" + t.name + "' (" + err.Error() + ")") sreg.sess.queueOut(ErrUnknown(sreg.pkt.Id, t.original, timestamp)) return } // Public is not set. // t.public = nil t.created = t.perUser[sreg.sess.uid].created t.updated = t.perUser[sreg.sess.uid].updated // Publishing to Find is not supported // t.lastId = 0 // Request to attach to a firehause topic. Must be root. } else if t.original == "fh" { log.Println("hub: loading 'fh' topic") t.cat = types.TopicCat_Fh // 'fh' has no owner, t.owner = nil // The fh can be accessed by anyone with root access // t.accessAuth = types.ModeBanned // t.accessAnon = types.ModeBanned // Subscriptions are ephemeral, no need to load subscribers. // TODO(gene): handle public more intelligently // t.public = nil // Publishing to Fh is not supported, history is not supported. // t.lastId = 0 // Request to load an existing or create a new p2p topic, then attach to it. } else if strings.HasPrefix(t.original, "usr") || strings.HasPrefix(t.original, "p2p") { log.Println("hub: loading or creating p2p topic") // Handle the following cases: // 1. Neither topic nor subscriptions exist: create a new p2p topic & subscriptions. // 2. Topic exists, one of the subscriptions is missing: // 2.1 Requester's subscription is missing, recreate it. // 2.2 Other user's subscription is missing, treat like a new request for user 2. // 3. Topic exists, both subscriptions are missing: should not happen, fail. // 4. Topic and both subscriptions exist: attach to topic t.cat = types.TopicCat_P2P // Check if the topic already exists stopic, err := store.Topics.Get(t.name) if err != nil { log.Println("hub: error while loading topic '" + t.name + "' (" + err.Error() + ")") sreg.sess.queueOut(ErrUnknown(sreg.pkt.Id, t.name, timestamp)) return } // If topic exists, load subscriptions var subs []types.Subscription if stopic != nil { // Subs already have Public swapped if subs, err = store.Topics.GetSubs(t.name); err != nil { log.Println("hub: cannot load subscritions for '" + t.name + "' (" + err.Error() + ")") sreg.sess.queueOut(ErrUnknown(sreg.pkt.Id, t.name, timestamp)) return } // Case 3, fail if len(subs) == 0 { log.Println("hub: missing both subscriptions for '" + t.name + "' (SHOULD NEVER HAPPEN!)") sreg.sess.queueOut(ErrUnknown(sreg.pkt.Id, t.name, timestamp)) return } t.created = stopic.CreatedAt t.updated = stopic.UpdatedAt t.lastId = stopic.SeqId } // t.owner is blank for p2p topics // Ensure that other users are automatically rejected t.accessAuth = types.ModeP2P t.accessAnon = types.ModeBanned // t.public is not used for p2p topics since each user get a different public // Custom default access levels set in sreg.pkt.Init.DefaultAcs are ignored if stopic != nil && len(subs) == 2 { // Case 4. log.Println("hub: existing p2p topic") // t.clearId is not used in P2P topics, may change later for i := 0; i < 2; i++ { uid := types.ParseUid(subs[i].User) t.perUser[uid] = perUserData{ // Adapter already swapped the public values public: subs[i].GetPublic(), private: subs[i].Private, // lastSeenTag: subs[i].LastSeen, modeWant: subs[i].ModeWant, modeGiven: subs[i].ModeGiven} } } else { // Cases 1, 2 log.Println("hub: p2p new topic or one of the subs missing") var userData perUserData // Fetching records for both users. // Requester. userId1 := sreg.sess.uid // The other user. userId2 := types.ParseUserId(t.original) var u1, u2 int users, err := store.Users.GetAll(userId1, userId2) if err != nil { log.Println("hub: failed to load users for '" + t.name + "' (" + err.Error() + ")") sreg.sess.queueOut(ErrUnknown(sreg.pkt.Id, t.name, timestamp)) return } else if len(users) != 2 { // Invited user does not exist log.Println("hub: missing user for '" + t.name + "'") sreg.sess.queueOut(ErrUserNotFound(sreg.pkt.Id, t.name, timestamp)) return } else { // User records are unsorted, make sure we know who is who. if users[0].Uid() == userId1 { u1, u2 = 0, 1 } else { u1, u2 = 1, 0 } } // Figure out which subscriptions are missing: User1's, User2's or both. var sub1, sub2 *types.Subscription // Set to true if only requester's subscription was created var user1only bool if len(subs) == 1 { if subs[0].Uid() == userId1 { // User2's subscription is missing sub1 = &subs[0] } else { // User1's is missing sub2 = &subs[0] user1only = true } } if sub1 == nil { if sreg.pkt.Set != nil && sreg.pkt.Set.Desc != nil && !isNullValue(sreg.pkt.Set.Desc.Private) { userData.private = sreg.pkt.Set.Desc.Private // Init.DefaultAcs and Init.Public are ignored for p2p topics } // User may set non-default access to topic, just make sure it's no higher than the default if sreg.pkt.Set != nil && sreg.pkt.Set.Sub != nil && sreg.pkt.Set.Sub.Mode != "" { if err := userData.modeWant.UnmarshalText([]byte(sreg.pkt.Set.Sub.Mode)); err != nil { log.Println("hub: invalid access mode for topic '" + t.name + "': '" + sreg.pkt.Set.Sub.Mode + "'") userData.modeWant = types.ModeP2P } else { // FIXME(gene): check for Mode.Banned userData.modeWant &= types.ModeP2P } } else if users[u1].Access.Auth != types.ModeNone { userData.modeWant = users[u1].Access.Auth } else { userData.modeWant = types.ModeP2P } sub1 = &types.Subscription{ User: userId1.String(), Topic: t.name, ModeWant: userData.modeWant, ModeGiven: types.ModeP2P, Private: userData.private} // Swap Public to match swapped Public in subs returned from store.Topics.GetSubs sub1.SetPublic(users[u2].Public) } if sub2 == nil { sub2 = &types.Subscription{ User: userId2.String(), Topic: t.name, ModeWant: users[u2].Access.Auth, ModeGiven: types.ModeP2P, Private: nil} // Swap Public to match swapped Public in subs returned from store.Topics.GetSubs sub2.SetPublic(users[u1].Public) } // Create everything if stopic == nil { if err = store.Topics.CreateP2P(sub1, sub2); err != nil { log.Println("hub: databse error in creating subscriptions '" + t.name + "' (" + err.Error() + ")") sreg.sess.queueOut(ErrUnknown(sreg.pkt.Id, t.name, timestamp)) return } t.created = sub1.CreatedAt t.updated = sub1.UpdatedAt // t.lastId is not set (default 0) for new topics } else { // Recreate one of the subscriptions var subToMake *types.Subscription if user1only { subToMake = sub1 } else { subToMake = sub2 } if err = store.Subs.Create(subToMake); err != nil { log.Println("hub: databse error in re-subscribing user '" + t.name + "' (" + err.Error() + ")") sreg.sess.queueOut(ErrUnknown(sreg.pkt.Id, t.name, timestamp)) return } } // t.clearId is not currently used for p2p topics // Publics is already swapped userData.public = sub1.GetPublic() userData.modeWant = sub1.ModeWant userData.modeGiven = sub1.ModeGiven t.perUser[userId1] = userData t.perUser[userId2] = perUserData{ public: sub2.GetPublic(), modeWant: sub2.ModeWant, modeGiven: sub2.ModeGiven, } sreg.created = !user1only } t.original = t.name // Processing request to create a new generic (group) topic: } else if t.original == "new" { log.Println("hub: new group topic") t.cat = types.TopicCat_Grp // Generic topics have parameters stored in the topic object t.owner = sreg.sess.uid t.accessAuth = DEFAULT_AUTH_ACCESS t.accessAnon = DEFAULT_ANON_ACCESS // Owner/creator gets full access to the topic. Owner may change the default modeWant through 'set'. userData := perUserData{ modeGiven: types.ModeFull, modeWant: types.ModeFull} if sreg.pkt.Set != nil { // User sent initialization parameters if sreg.pkt.Set.Desc != nil { if !isNullValue(sreg.pkt.Set.Desc.Public) { t.public = sreg.pkt.Set.Desc.Public } if !isNullValue(sreg.pkt.Set.Desc.Private) { userData.private = sreg.pkt.Set.Desc.Private } // set default access if sreg.pkt.Set.Desc.DefaultAcs != nil { if auth, anon, err := parseTopicAccess(sreg.pkt.Set.Desc.DefaultAcs, t.accessAuth, t.accessAnon); err != nil { log.Println("hub: invalid access mode for topic '" + t.name + "': '" + err.Error() + "'") } else if auth&types.ModeOwner != 0 || anon&types.ModeOwner != 0 { log.Println("hub: OWNER default access in topic '" + t.name) } else { t.accessAuth, t.accessAnon = auth, anon } } } // Owner/creator may restrict own access to topic if sreg.pkt.Set.Sub == nil || sreg.pkt.Set.Sub.Mode == "" { userData.modeWant = types.ModeFull } else { if err := userData.modeWant.UnmarshalText([]byte(sreg.pkt.Set.Sub.Mode)); err != nil { log.Println("hub: invalid access mode for topic '" + t.name + "': '" + sreg.pkt.Set.Sub.Mode + "'") } } } t.perUser[t.owner] = userData t.created = timestamp t.updated = timestamp // t.lastId & t.clearId are not set for new topics stopic := &types.Topic{ ObjHeader: types.ObjHeader{Id: sreg.topic, CreatedAt: timestamp}, Access: types.DefaultAccess{Auth: t.accessAuth, Anon: t.accessAnon}, Public: t.public} // store.Topics.Create will add a subscription record for the topic creator stopic.GiveAccess(t.owner, userData.modeWant, userData.modeGiven) err := store.Topics.Create(stopic, t.owner, t.perUser[t.owner].private) if err != nil { log.Println("hub: cannot save new topic '" + t.name + "' (" + err.Error() + ")") // Error sent on "new" topic sreg.sess.queueOut(ErrUnknown(sreg.pkt.Id, t.original, timestamp)) return } t.original = t.name // keeping 'new' as original has no value to the client sreg.created = true } else { log.Println("hub: existing group topic") t.cat = types.TopicCat_Grp // TODO(gene): check and validate topic name stopic, err := store.Topics.Get(t.name) if err != nil { log.Println("hub: error while loading topic '" + t.name + "' (" + err.Error() + ")") sreg.sess.queueOut(ErrUnknown(sreg.pkt.Id, t.original, timestamp)) return } else if stopic == nil { log.Println("hub: topic '" + t.name + "' does not exist") sreg.sess.queueOut(ErrTopicNotFound(sreg.pkt.Id, t.original, timestamp)) return } if err = t.loadSubscribers(); err != nil { log.Println("hub: cannot load subscribers for '" + t.name + "' (" + err.Error() + ")") sreg.sess.queueOut(ErrUnknown(sreg.pkt.Id, t.original, timestamp)) return } // t.owner is set by loadSubscriptions t.accessAuth = stopic.Access.Auth t.accessAnon = stopic.Access.Anon t.public = stopic.Public t.created = stopic.CreatedAt t.updated = stopic.UpdatedAt t.lastId = stopic.SeqId t.clearId = stopic.ClearId } log.Println("hub: topic created or loaded: " + t.name) h.topicPut(t.name, t) h.topicsLive.Add(1) go t.run(h) sreg.loaded = true // Topic will check access rights, send invite to p2p user, send {ctrl} message to the initiator session t.reg <- sreg }
// TopicsForUser loads user's contact list: p2p and grp topics, except for 'me' subscription. func (a *RethinkDbAdapter) TopicsForUser(uid t.Uid) ([]t.Subscription, error) { // Fetch user's subscriptions // Subscription have Topic.UpdatedAt denormalized into Subscription.UpdatedAt q := rdb.DB(a.dbName).Table("subscriptions").GetAllByIndex("User", uid.String()).Limit(MAX_RESULTS) //log.Printf("RethinkDbAdapter.TopicsForUser q: %+v", q) rows, err := q.Run(a.conn) if err != nil { return nil, err } // Fetch subscriptions. Two queries are needed: users table (me & p2p) and topics table (p2p and grp). // Prepare a list of Separate subscriptions to users vs topics var sub t.Subscription join := make(map[string]t.Subscription) // Keeping these to make a join with table for .private and .access topq := make([]interface{}, 0, 16) usrq := make([]interface{}, 0, 16) for rows.Next(&sub) { tcat := t.GetTopicCat(sub.Topic) // 'me' subscription, skip if tcat == t.TopicCat_Me || tcat == t.TopicCat_Fnd { continue // p2p subscription, find the other user to get user.Public } else if tcat == t.TopicCat_P2P { uid1, uid2, _ := t.ParseP2P(sub.Topic) if uid1 == uid { usrq = append(usrq, uid2.String()) } else { usrq = append(usrq, uid1.String()) } topq = append(topq, sub.Topic) // grp subscription } else { topq = append(topq, sub.Topic) } join[sub.Topic] = sub } //log.Printf("RethinkDbAdapter.TopicsForUser topq, usrq: %+v, %+v", topq, usrq) var subs []t.Subscription if len(topq) > 0 || len(usrq) > 0 { subs = make([]t.Subscription, 0, len(join)) } if len(topq) > 0 { // Fetch grp & p2p topics rows, err = rdb.DB(a.dbName).Table("topics").GetAll(topq...).Run(a.conn) if err != nil { return nil, err } var top t.Topic for rows.Next(&top) { sub = join[top.Id] sub.ObjHeader.MergeTimes(&top.ObjHeader) sub.SetSeqId(top.SeqId) sub.SetHardClearId(top.ClearId) if t.GetTopicCat(sub.Topic) == t.TopicCat_Grp { // all done with a grp topic sub.SetPublic(top.Public) subs = append(subs, sub) } else { // put back the updated value of a p2p subsription, will process further below join[top.Id] = sub } } //log.Printf("RethinkDbAdapter.TopicsForUser 1: %#+v", subs) } // Fetch p2p users and join to p2p tables if len(usrq) > 0 { rows, err = rdb.DB(a.dbName).Table("users").GetAll(usrq...).Run(a.conn) if err != nil { return nil, err } var usr t.User for rows.Next(&usr) { uid2 := t.ParseUid(usr.Id) topic := uid.P2PName(uid2) if sub, ok := join[topic]; ok { sub.ObjHeader.MergeTimes(&usr.ObjHeader) sub.SetWith(uid2.UserId()) sub.SetPublic(usr.Public) sub.SetLastSeenAndUA(usr.LastSeen, usr.UserAgent) subs = append(subs, sub) } } //log.Printf("RethinkDbAdapter.TopicsForUser 2: %#+v", subs) } return subs, nil }