models, graph2: Added Comment type and queries, and hooked it up to chapter.
the build was successful
the build was successful
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// ChapterCommentMode represents the kind of tags.
|
||||
type ChapterCommentMode string
|
||||
|
||||
const (
|
||||
// ChapterCommentModeDisabled is a chapter comment mode, see GraphQL documentation.
|
||||
ChapterCommentModeDisabled ChapterCommentMode = "Disabled"
|
||||
|
||||
// ChapterCommentModeArticle is a chapter comment mode, see GraphQL documentation.
|
||||
ChapterCommentModeArticle ChapterCommentMode = "Article"
|
||||
|
||||
// ChapterCommentModeChat is a chapter comment mode, see GraphQL documentation.
|
||||
ChapterCommentModeChat ChapterCommentMode = "Chat"
|
||||
|
||||
// ChapterCommentModeMessage is a chapter comment mode, see GraphQL documentation.
|
||||
ChapterCommentModeMessage ChapterCommentMode = "Message"
|
||||
)
|
||||
|
||||
// UnmarshalGQL unmarshals
|
||||
func (e *ChapterCommentMode) UnmarshalGQL(v interface{}) error {
|
||||
str, ok := v.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("enums must be strings")
|
||||
}
|
||||
|
||||
*e = ChapterCommentMode(str)
|
||||
switch *e {
|
||||
case ChapterCommentModeDisabled, ChapterCommentModeArticle, ChapterCommentModeChat, ChapterCommentModeMessage:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("%s is not a valid ChapterCommentMode", str)
|
||||
}
|
||||
}
|
||||
|
||||
// IsEnabled returns true if comments are enabled.
|
||||
func (e ChapterCommentMode) IsEnabled() bool {
|
||||
return e != ChapterCommentModeDisabled && len(e) != 0
|
||||
}
|
||||
|
||||
// MarshalGQL turns it into a JSON string
|
||||
func (e ChapterCommentMode) MarshalGQL(w io.Writer) {
|
||||
// Backwards compatibility: Empty value means disabled.
|
||||
if len(e) == 0 {
|
||||
w.Write(disabledChapterCommentModeBytes)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprint(w, "\""+string(e)+"\"")
|
||||
}
|
||||
|
||||
var disabledChapterCommentModeBytes = []byte(`"Disabled"`)
|
||||
+15
-8
@@ -4,12 +4,19 @@ import "time"
|
||||
|
||||
// A Chapter is a part of a story.
|
||||
type Chapter struct {
|
||||
ID string `bson:"_id"`
|
||||
StoryID string `bson:"storyId"`
|
||||
Title string `bson:"title"`
|
||||
Author string `bson:"author"`
|
||||
Source string `bson:"source"`
|
||||
CreatedDate time.Time `bson:"createdDate"`
|
||||
FictionalDate time.Time `bson:"fictionalDate,omitempty"`
|
||||
EditedDate time.Time `bson:"editedDate"`
|
||||
ID string `bson:"_id"`
|
||||
StoryID string `bson:"storyId"`
|
||||
Title string `bson:"title"`
|
||||
Author string `bson:"author"`
|
||||
Source string `bson:"source"`
|
||||
CreatedDate time.Time `bson:"createdDate"`
|
||||
FictionalDate time.Time `bson:"fictionalDate,omitempty"`
|
||||
EditedDate time.Time `bson:"editedDate"`
|
||||
CommentMode ChapterCommentMode `bson:"commentMode"`
|
||||
CommentsLocked bool `bson:"commentsLocked"`
|
||||
}
|
||||
|
||||
// CanComment returns true if the chapter can be commented to.
|
||||
func (chapter *Chapter) CanComment() bool {
|
||||
return !chapter.CommentsLocked && chapter.CommentMode.IsEnabled()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
// A Comment is a comment on a chapter.
|
||||
type Comment struct {
|
||||
ID string `bson:"id"`
|
||||
ChapterID string `bson:"chapterId"`
|
||||
Subject string `bson:"subject"`
|
||||
Author string `bson:"author"`
|
||||
CharacterName string `bson:"characterName"`
|
||||
CharacterID string `bson:"characterId"`
|
||||
FictionalDate time.Time `bson:"fictionalDate"`
|
||||
CreatedDate time.Time `bson:"createdDate"`
|
||||
EditedDate time.Time `bson:"editeddDate"`
|
||||
Source string `bson:"sources"`
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package comments
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"strconv"
|
||||
|
||||
"git.aiterp.net/rpdata/api/internal/store"
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo"
|
||||
)
|
||||
|
||||
var collection *mgo.Collection
|
||||
|
||||
func find(query interface{}) (models.Comment, error) {
|
||||
comment := models.Comment{}
|
||||
err := collection.Find(query).One(&comment)
|
||||
|
||||
return comment, err
|
||||
}
|
||||
|
||||
func list(query interface{}, limit int) ([]models.Comment, error) {
|
||||
allocSize := 32
|
||||
if limit >= 0 {
|
||||
allocSize = limit
|
||||
} else {
|
||||
limit = 0
|
||||
}
|
||||
|
||||
comments := make([]models.Comment, 0, allocSize)
|
||||
err := collection.Find(query).Sort("createdDate").Limit(limit).All(&comments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return comments, nil
|
||||
}
|
||||
|
||||
func makeCommentID() string {
|
||||
result := "SCC"
|
||||
offset := 0
|
||||
data := make([]byte, 48)
|
||||
|
||||
rand.Read(data)
|
||||
for len(result) < 32 {
|
||||
result += strconv.FormatUint(binary.LittleEndian.Uint64(data[offset:]), 36)
|
||||
offset += 8
|
||||
|
||||
if offset >= 48 {
|
||||
rand.Read(data)
|
||||
offset = 0
|
||||
}
|
||||
}
|
||||
|
||||
return result[:32]
|
||||
}
|
||||
|
||||
func init() {
|
||||
store.HandleInit(func(db *mgo.Database) {
|
||||
collection = db.C("story.comments")
|
||||
|
||||
collection.EnsureIndexKey("chapterId")
|
||||
collection.EnsureIndexKey("author")
|
||||
collection.EnsureIndexKey("createdDate")
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package comments
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// Find finds a comment by ID.
|
||||
func Find(id string) (models.Comment, error) {
|
||||
return find(bson.M{"_id": id})
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package comments
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// ListChapterID lists all comments by chapter-ID
|
||||
func ListChapterID(chapterID string, limit int) ([]models.Comment, error) {
|
||||
return list(bson.M{"chapterId": chapterID}, limit)
|
||||
}
|
||||
Reference in New Issue
Block a user