|
|
@ -21,10 +21,9 @@ import (
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// prefixes and suffixes.
|
|
|
|
// prefixes and suffixes.
|
|
|
|
const (
|
|
|
|
const (
|
|
|
|
SubscriptionPrefix = "subscription_prefix"
|
|
|
|
SubscriptionPrefix = "subscription_prefix"
|
|
|
|
SubscribedPrefix = "subscribed_prefix"
|
|
|
|
SubscribedPrefix = "subscribed_prefix"
|
|
|
@ -48,22 +47,35 @@ type UserMongoDriver struct {
|
|
|
|
// AddSubscriptionList Subscriber's handling of thresholds.
|
|
|
|
// AddSubscriptionList Subscriber's handling of thresholds.
|
|
|
|
func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string, userIDList []string) error {
|
|
|
|
func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string, userIDList []string) error {
|
|
|
|
// Check the number of lists in the key.
|
|
|
|
// Check the number of lists in the key.
|
|
|
|
filter := bson.M{SubscriptionPrefix + userID: bson.M{"$size": 1}}
|
|
|
|
pipeline := mongo.Pipeline{
|
|
|
|
result, err := u.userCollection.Find(context.Background(), filter)
|
|
|
|
{{"$match", bson.D{{"user_id", SubscriptionPrefix + userID}}}},
|
|
|
|
|
|
|
|
{{"$project", bson.D{{"count", bson.D{{"$size", "user_id_list"}}}}}},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform aggregate operations
|
|
|
|
|
|
|
|
cursor, err := u.userCollection.Aggregate(ctx, pipeline)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var newUserIDList []string
|
|
|
|
defer cursor.Close(ctx)
|
|
|
|
for result.Next(context.Background()) {
|
|
|
|
var cnt struct {
|
|
|
|
err := result.Decode(&newUserIDList)
|
|
|
|
Count int `bson:"count"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over aggregated results
|
|
|
|
|
|
|
|
for cursor.Next(ctx) {
|
|
|
|
|
|
|
|
err := cursor.Decode(&cnt)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var newUserIDList []string
|
|
|
|
// If the threshold is exceeded, pop out the previous MaximumSubscription - len(userIDList) and insert it.
|
|
|
|
// If the threshold is exceeded, pop out the previous MaximumSubscription - len(userIDList) and insert it.
|
|
|
|
if len(newUserIDList)+len(userIDList) > MaximumSubscription {
|
|
|
|
if cnt.Count+len(userIDList) > MaximumSubscription {
|
|
|
|
|
|
|
|
newUserIDList, err = u.GetAllSubscribeList(ctx, userID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
newUserIDList = newUserIDList[MaximumSubscription-len(userIDList):]
|
|
|
|
newUserIDList = newUserIDList[MaximumSubscription-len(userIDList):]
|
|
|
|
_, err := u.userCollection.UpdateOne(
|
|
|
|
_, err = u.userCollection.UpdateOne(
|
|
|
|
ctx,
|
|
|
|
ctx,
|
|
|
|
bson.M{"user_id": SubscriptionPrefix + userID},
|
|
|
|
bson.M{"user_id": SubscriptionPrefix + userID},
|
|
|
|
bson.M{"$set": bson.M{"user_id_list": newUserIDList}},
|
|
|
|
bson.M{"$set": bson.M{"user_id_list": newUserIDList}},
|
|
|
@ -71,16 +83,17 @@ func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//for i := 1; i <= MaximumSubscription-len(userIDList); i++ {
|
|
|
|
// Another way to subscribe to N before pop,Delete after testing
|
|
|
|
// _, err := u.userCollection.UpdateOne(
|
|
|
|
/*for i := 1; i <= MaximumSubscription-len(userIDList); i++ {
|
|
|
|
// ctx,
|
|
|
|
_, err := u.userCollection.UpdateOne(
|
|
|
|
// bson.M{"user_id": SubscriptionPrefix + userID},
|
|
|
|
ctx,
|
|
|
|
// bson.M{SubscriptionPrefix + userID: bson.M{"$pop": -1}},
|
|
|
|
bson.M{"user_id": SubscriptionPrefix + userID},
|
|
|
|
// )
|
|
|
|
bson.M{SubscriptionPrefix + userID: bson.M{"$pop": -1}},
|
|
|
|
// if err != nil {
|
|
|
|
)
|
|
|
|
// return err
|
|
|
|
if err != nil {
|
|
|
|
// }
|
|
|
|
return err
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
upsert := true
|
|
|
|
upsert := true
|
|
|
|
opts := &options.UpdateOptions{
|
|
|
|
opts := &options.UpdateOptions{
|
|
|
@ -139,3 +152,29 @@ func (u *UserMongoDriver) RemoveSubscribedListFromUser(ctx context.Context, user
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return utils.Wrap(err, "")
|
|
|
|
return utils.Wrap(err, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetAllSubscribeList Get all users subscribed by this user
|
|
|
|
|
|
|
|
func (u *UserMongoDriver) GetAllSubscribeList(ctx context.Context, userID string) (userIDList []string, err error) {
|
|
|
|
|
|
|
|
var user unrelation.UserModel
|
|
|
|
|
|
|
|
cursor := u.userCollection.FindOne(
|
|
|
|
|
|
|
|
ctx,
|
|
|
|
|
|
|
|
bson.M{"user_id": SubscriptionPrefix + userID})
|
|
|
|
|
|
|
|
err = cursor.Decode(&user)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.UserIDList, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetSubscribedList Get the user subscribed by those users
|
|
|
|
|
|
|
|
func (u *UserMongoDriver) GetSubscribedList(ctx context.Context, userID string) (userIDList []string, err error) {
|
|
|
|
|
|
|
|
var user unrelation.UserModel
|
|
|
|
|
|
|
|
cursor := u.userCollection.FindOne(
|
|
|
|
|
|
|
|
ctx,
|
|
|
|
|
|
|
|
bson.M{"user_id": SubscribedPrefix + userID})
|
|
|
|
|
|
|
|
err = cursor.Decode(&user)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.UserIDList, nil
|
|
|
|
|
|
|
|
}
|
|
|
|