|
@ -23,7 +23,7 @@ type Post struct { |
|
|
Kind string `bson:"kind"` |
|
|
Kind string `bson:"kind"` |
|
|
Nick string `bson:"nick"` |
|
|
Nick string `bson:"nick"` |
|
|
Text string `bson:"text"` |
|
|
Text string `bson:"text"` |
|
|
Index int `bson:"index"` |
|
|
|
|
|
|
|
|
Position int `bson:"position"` |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Edit the post
|
|
|
// Edit the post
|
|
@ -67,40 +67,40 @@ func (post *Post) Edit(time *time.Time, kind *string, nick *string, text *string |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Move the post
|
|
|
// Move the post
|
|
|
func (post *Post) Move(targetIndex int) error { |
|
|
|
|
|
if targetIndex < 1 { |
|
|
|
|
|
return errors.New("Invalid index") |
|
|
|
|
|
|
|
|
func (post *Post) Move(toPosition int) error { |
|
|
|
|
|
if toPosition < 1 { |
|
|
|
|
|
return errors.New("Invalid position") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
postMutex.Lock() |
|
|
postMutex.Lock() |
|
|
defer postMutex.Unlock() |
|
|
defer postMutex.Unlock() |
|
|
|
|
|
|
|
|
// To avoid problems, only allow target indices that are allowed. If it's 1, then there is bound to
|
|
|
// To avoid problems, only allow target indices that are allowed. If it's 1, then there is bound to
|
|
|
// be a post at the index.
|
|
|
|
|
|
if targetIndex > 1 { |
|
|
|
|
|
|
|
|
// be a post at the position.
|
|
|
|
|
|
if toPosition > 1 { |
|
|
existingPost := Post{} |
|
|
existingPost := Post{} |
|
|
err := postCollection.Find(bson.M{"logId": post.LogID, "index": targetIndex}).One(&existingPost) |
|
|
|
|
|
|
|
|
err := postCollection.Find(bson.M{"logId": post.LogID, "position": toPosition}).One(&existingPost) |
|
|
|
|
|
|
|
|
if err != nil || existingPost.Index != targetIndex { |
|
|
|
|
|
return errors.New("No post found at the index") |
|
|
|
|
|
|
|
|
if err != nil || existingPost.Position != toPosition { |
|
|
|
|
|
return errors.New("No post found at the position") |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
query := bson.M{"logId": post.LogID} |
|
|
query := bson.M{"logId": post.LogID} |
|
|
operation := bson.M{"$inc": bson.M{"index": 1}} |
|
|
|
|
|
|
|
|
operation := bson.M{"$inc": bson.M{"position": 1}} |
|
|
|
|
|
|
|
|
if targetIndex < post.Index { |
|
|
|
|
|
|
|
|
if toPosition < post.Position { |
|
|
query["$and"] = []bson.M{ |
|
|
query["$and"] = []bson.M{ |
|
|
bson.M{"index": bson.M{"$gte": targetIndex}}, |
|
|
|
|
|
bson.M{"index": bson.M{"$lt": post.Index}}, |
|
|
|
|
|
|
|
|
bson.M{"position": bson.M{"$gte": toPosition}}, |
|
|
|
|
|
bson.M{"position": bson.M{"$lt": post.Position}}, |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
query["$and"] = []bson.M{ |
|
|
query["$and"] = []bson.M{ |
|
|
bson.M{"index": bson.M{"$gt": post.Index}}, |
|
|
|
|
|
bson.M{"index": bson.M{"$lte": targetIndex}}, |
|
|
|
|
|
|
|
|
bson.M{"position": bson.M{"$gt": post.Position}}, |
|
|
|
|
|
bson.M{"position": bson.M{"$lte": toPosition}}, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
operation["$inc"] = bson.M{"index": -1} |
|
|
|
|
|
|
|
|
operation["$inc"] = bson.M{"position": -1} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
_, err := postCollection.UpdateAll(query, operation) |
|
|
_, err := postCollection.UpdateAll(query, operation) |
|
@ -108,12 +108,12 @@ func (post *Post) Move(targetIndex int) error { |
|
|
return errors.New("moving others: " + err.Error()) |
|
|
return errors.New("moving others: " + err.Error()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
err = postCollection.UpdateId(post.ID, bson.M{"$set": bson.M{"index": targetIndex}}) |
|
|
|
|
|
|
|
|
err = postCollection.UpdateId(post.ID, bson.M{"$set": bson.M{"position": toPosition}}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return errors.New("moving: " + err.Error()) |
|
|
return errors.New("moving: " + err.Error()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
post.Index = targetIndex |
|
|
|
|
|
|
|
|
post.Position = toPosition |
|
|
|
|
|
|
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
@ -128,7 +128,7 @@ func ListPostIDs(ids ...string) ([]Post, error) { |
|
|
return listPosts(bson.M{"_id": bson.M{"$in": ids}}) |
|
|
return listPosts(bson.M{"_id": bson.M{"$in": ids}}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// RemovePost removes a post, moving all subsequent post up one index
|
|
|
|
|
|
|
|
|
// RemovePost removes a post, moving all subsequent post up one position
|
|
|
func RemovePost(id string) (Post, error) { |
|
|
func RemovePost(id string) (Post, error) { |
|
|
postMutex.Lock() |
|
|
postMutex.Lock() |
|
|
defer postMutex.Unlock() |
|
|
defer postMutex.Unlock() |
|
@ -143,7 +143,7 @@ func RemovePost(id string) (Post, error) { |
|
|
return Post{}, err |
|
|
return Post{}, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
_, err = postCollection.UpdateAll(bson.M{"logId": post.LogID, "index": bson.M{"$gt": post.Index}}, bson.M{"$inc": bson.M{"index": -1}}) |
|
|
|
|
|
|
|
|
_, err = postCollection.UpdateAll(bson.M{"logId": post.LogID, "position": bson.M{"$gt": post.Position}}, bson.M{"$inc": bson.M{"position": -1}}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return Post{}, err |
|
|
return Post{}, err |
|
|
} |
|
|
} |
|
@ -186,7 +186,7 @@ func init() { |
|
|
postCollection.EnsureIndexKey("logId") |
|
|
postCollection.EnsureIndexKey("logId") |
|
|
postCollection.EnsureIndexKey("time") |
|
|
postCollection.EnsureIndexKey("time") |
|
|
postCollection.EnsureIndexKey("kind") |
|
|
postCollection.EnsureIndexKey("kind") |
|
|
postCollection.EnsureIndexKey("index") |
|
|
|
|
|
|
|
|
postCollection.EnsureIndexKey("position") |
|
|
|
|
|
|
|
|
err := postCollection.EnsureIndex(mgo.Index{ |
|
|
err := postCollection.EnsureIndex(mgo.Index{ |
|
|
Key: []string{"$text:text"}, |
|
|
Key: []string{"$text:text"}, |
|
|