本文整理汇总了Golang中github.com/tinode/chat/server/store/types.Subscription类的典型用法代码示例。如果您正苦于以下问题:Golang Subscription类的具体用法?Golang Subscription怎么用?Golang Subscription使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Subscription类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TopicCreateP2P
// 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)
}
开发者ID:ycaihua,项目名称:chat,代码行数:27,代码来源:adapter.go
示例2: UsersForTopic
// 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
}
开发者ID:ycaihua,项目名称:chat,代码行数:45,代码来源:adapter.go
示例3: SubsForTopic
// 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()
}
开发者ID:ycaihua,项目名称:chat,代码行数:41,代码来源:adapter.go
示例4: FindSubs
// 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
}
}
开发者ID:ycaihua,项目名称:chat,代码行数:40,代码来源:adapter.go
示例5: Create
func (SubsObjMapper) Create(sub *types.Subscription) error {
sub.InitTimes()
_, err := adaptr.TopicShare([]types.Subscription{*sub})
return err
}
开发者ID:steelannelida,项目名称:tinode-chat,代码行数:6,代码来源:store.go
示例6: CreateP2P
// 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)
}
开发者ID:steelannelida,项目名称:tinode-chat,代码行数:7,代码来源:store.go
示例7: topicInit
//.........这里部分代码省略.........
// 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 {
开发者ID:ycaihua,项目名称:chat,代码行数:67,代码来源:hub.go
示例8: TopicsForUser
// 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
}
开发者ID:ycaihua,项目名称:chat,代码行数:98,代码来源:adapter.go
注:本文中的github.com/tinode/chat/server/store/types.Subscription类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论