This commit is contained in:
@@ -18,3 +18,14 @@ func (character *Character) Nick() *string {
|
||||
|
||||
return &character.Nicks[0]
|
||||
}
|
||||
|
||||
// HasNick gets whether the character has the nick.
|
||||
func (character *Character) HasNick(nick string) bool {
|
||||
for i := range character.Nicks {
|
||||
if nick == character.Nicks[i] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.aiterp.net/rpdata/api/model/counter"
|
||||
"git.aiterp.net/rpdata/api/internal/counter"
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package posts
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.aiterp.net/rpdata/api/internal/counter"
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
)
|
||||
|
||||
// Add creates a new post.
|
||||
func Add(log models.Log, time time.Time, kind, nick, text string) (models.Post, error) {
|
||||
if kind == "" || nick == "" || text == "" {
|
||||
return models.Post{}, errors.New("Missing/empty parameters")
|
||||
}
|
||||
|
||||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
|
||||
position, err := counter.Next("next_post_id", log.ShortID)
|
||||
if err != nil {
|
||||
return models.Post{}, err
|
||||
}
|
||||
|
||||
post := models.Post{
|
||||
ID: generateID(time),
|
||||
Position: position,
|
||||
LogID: log.ShortID,
|
||||
Time: time,
|
||||
Kind: kind,
|
||||
Nick: nick,
|
||||
Text: text,
|
||||
}
|
||||
|
||||
err = collection.Insert(post)
|
||||
if err != nil {
|
||||
return models.Post{}, err
|
||||
}
|
||||
|
||||
return post, nil
|
||||
}
|
||||
|
||||
func generateID(time time.Time) string {
|
||||
data := make([]byte, 4)
|
||||
rand.Read(data)
|
||||
|
||||
return "P" + strconv.FormatInt(time.UnixNano(), 36) + strconv.FormatInt(int64(binary.LittleEndian.Uint32(data)), 36)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package posts
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// Edit edits a post and returns the result if the edit succeeded.
|
||||
func Edit(post models.Post, time *time.Time, kind *string, nick *string, text *string) (models.Post, error) {
|
||||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
|
||||
changes := bson.M{}
|
||||
|
||||
if time != nil && !time.IsZero() && !time.Equal(post.Time) {
|
||||
changes["time"] = *time
|
||||
post.Time = *time
|
||||
}
|
||||
if kind != nil && *kind != "" && *kind != post.Kind {
|
||||
changes["kind"] = *kind
|
||||
post.Kind = *kind
|
||||
}
|
||||
if nick != nil && *nick != "" && *nick != post.Nick {
|
||||
changes["nick"] = *nick
|
||||
post.Nick = *nick
|
||||
}
|
||||
if text != nil && *text != "" && *text != post.Text {
|
||||
changes["text"] = *text
|
||||
post.Text = *text
|
||||
}
|
||||
|
||||
if len(changes) == 0 {
|
||||
return post, nil
|
||||
}
|
||||
|
||||
err := collection.UpdateId(post.ID, bson.M{"$set": changes})
|
||||
if err != nil {
|
||||
return models.Post{}, err
|
||||
}
|
||||
|
||||
return post, nil
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package posts
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// Move the post
|
||||
func Move(post models.Post, toPosition int) ([]models.Post, error) {
|
||||
if toPosition < 1 {
|
||||
return nil, errors.New("Invalid position")
|
||||
}
|
||||
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
// To avoid problems, only allow target indices that are allowed. If it's 1, then there is bound to
|
||||
// be a post at the position.
|
||||
if toPosition > 1 {
|
||||
existing := models.Post{}
|
||||
err := collection.Find(bson.M{"logId": post.LogID, "position": toPosition}).One(&existing)
|
||||
|
||||
if err != nil || existing.Position != toPosition {
|
||||
return nil, errors.New("No post found at the position")
|
||||
}
|
||||
}
|
||||
|
||||
query := bson.M{"logId": post.LogID}
|
||||
operation := bson.M{"$inc": bson.M{"position": 1}}
|
||||
|
||||
if toPosition < post.Position {
|
||||
query["$and"] = []bson.M{
|
||||
bson.M{"position": bson.M{"$gte": toPosition}},
|
||||
bson.M{"position": bson.M{"$lt": post.Position}},
|
||||
}
|
||||
} else {
|
||||
query["$and"] = []bson.M{
|
||||
bson.M{"position": bson.M{"$gt": post.Position}},
|
||||
bson.M{"position": bson.M{"$lte": toPosition}},
|
||||
}
|
||||
|
||||
operation["$inc"] = bson.M{"position": -1}
|
||||
}
|
||||
|
||||
_, err := collection.UpdateAll(query, operation)
|
||||
if err != nil {
|
||||
return nil, errors.New("Moving others failed: " + err.Error())
|
||||
}
|
||||
|
||||
err = collection.UpdateId(post.ID, bson.M{"$set": bson.M{"position": toPosition}})
|
||||
if err != nil {
|
||||
return nil, errors.New("Moving failed: " + err.Error() + " (If you see this on the page, please let me know ASAP)")
|
||||
}
|
||||
|
||||
from, to := post.Position, toPosition
|
||||
if to < from {
|
||||
from, to = to, from
|
||||
}
|
||||
|
||||
posts := make([]models.Post, 0, (to-from)+1)
|
||||
err = collection.Find(bson.M{"logId": post.LogID, "position": bson.M{"$gte": from, "$lte": to}}).Sort("position").All(&posts)
|
||||
if err != nil {
|
||||
return nil, errors.New("The move completed successfully, but finding the moved posts failed: " + err.Error())
|
||||
}
|
||||
|
||||
return posts, nil
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package posts
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// Remove removes a post, moving all subsequent post up one position
|
||||
func Remove(post models.Post) (models.Post, error) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
err := collection.RemoveId(post.ID)
|
||||
if err != nil {
|
||||
return models.Post{}, err
|
||||
}
|
||||
|
||||
_, err = collection.UpdateAll(bson.M{"logId": post.LogID, "position": bson.M{"$gt": post.Position}}, bson.M{"$inc": bson.M{"position": -1}})
|
||||
if err != nil {
|
||||
return models.Post{}, err
|
||||
}
|
||||
|
||||
return post, nil
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package posts
|
||||
Reference in New Issue
Block a user