Compare commits

...
17 Commits
Author SHA1 Message Date
gisle 332a1eb32b Fixed rpdata-as2storyimport not compiling. 2019-03-17 21:32:21 +01:00
gisle db4d46c368 Added story to comment changes.
the build was successful
2019-03-17 20:44:30 +01:00
gisle a8c8610ca2 models: Fixed wrong database mapping for comment source.
the build was successful
2019-01-17 21:19:13 +01:00
gisle d09c17d30d graph2: Changed addComment error message on can-comment check to be consistent with others.
the build was successful
2019-01-16 22:07:00 +01:00
gisle e3e3ea07db graph2: Changed Chapter to not return any comments if mode is disabled, even if there are comments.
the build was successful
2019-01-16 22:05:40 +01:00
gisle 22592439da graph2, models: Added removeComment mutation.
the build was successful
2019-01-16 22:03:28 +01:00
gisle dfa9fb4025 graph2: Changed Comment to return null instead of a zero-date if it's cleared.
the build was successful
2019-01-16 21:50:53 +01:00
gisle 0e2a68a2d4 graph2, models: Added editComment mutation. 2019-01-16 21:45:00 +01:00
gisle 1eefa65527 models: Fixed wrong database mapping for comment id. 2019-01-16 21:41:36 +01:00
gisle f9b6af58c9 graph2: Changed Comment character resolver to use loader.
the build was successful
2019-01-15 21:42:44 +01:00
gisle 56ecf48b57 graph2, models: Added addComment mutation.
the build was successful
2019-01-15 21:34:57 +01:00
gisle 06fbfc90c5 graph2, model: Added Comment type to Change. 2019-01-14 19:58:05 +01:00
gisle 39a4fef506 models, graph2: Added comment removal on chapter removal.
the build was successful
2019-01-14 18:01:43 +01:00
gisle f9fe0bb5f8 models, graph2: Added comment options to chapter mutations.
the build was successful
2019-01-14 17:55:38 +01:00
gisle d22ab387dd models, graph2: Added Comment type and queries, and hooked it up to chapter.
the build was successful
2019-01-14 17:03:16 +01:00
gisle 53a86c0b9c store, config: Added option to disable space.
the build was successful
2019-01-12 12:13:24 +01:00
gisle e0b93c6b4b graph2: Changed story tagging permissions to allow anyone to tag open stories.
the build was successful
2019-01-12 11:38:19 +01:00
32 changed files with 751 additions and 30 deletions
+1 -1
View File
@@ -104,7 +104,7 @@ func main() {
}
}
chapter, err := chapters.Add(story, title, result.Author[5:], result.Source, publishDate, &fictionalDate)
chapter, err := chapters.Add(story, title, result.Author[5:], result.Source, publishDate, &fictionalDate, models.ChapterCommentModeDisabled)
if err != nil {
log.Fatalln(err)
}
+2
View File
@@ -64,6 +64,8 @@ func logListedChanges() {
log.Printf("\tTag: (%s,%s)", object.Kind, object.Name)
case models.Chapter:
log.Printf("\tChapter: %s", object.ID)
case models.Comment:
log.Printf("\tComment: %s", object.ID)
}
}
}
+7
View File
@@ -35,6 +35,13 @@ models:
model: git.aiterp.net/rpdata/api/models/logs.Filter
LogImporter:
model: git.aiterp.net/rpdata/api/models.LogImporter
Comment:
model: git.aiterp.net/rpdata/api/models.Comment
fields:
fictionalDate:
resolver: true
ChapterCommentMode:
model: git.aiterp.net/rpdata/api/models.ChapterCommentMode
Chapter:
model: git.aiterp.net/rpdata/api/models.Chapter
fields:
+4
View File
@@ -34,6 +34,10 @@ func (r *rootResolver) Log() LogResolver {
return &types.LogResolver
}
func (r *rootResolver) Comment() CommentResolver {
return &types.CommentResolver
}
func (r *rootResolver) Chapter() ChapterResolver {
return &types.ChapterResolver
}
+13 -2
View File
@@ -7,6 +7,7 @@ import (
"git.aiterp.net/rpdata/api/models/changekeys"
"git.aiterp.net/rpdata/api/models/changes"
"git.aiterp.net/rpdata/api/models/comments"
"git.aiterp.net/rpdata/api/internal/auth"
"git.aiterp.net/rpdata/api/models/stories"
@@ -48,7 +49,12 @@ func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAd
return models.Chapter{}, errors.New("Story is not open")
}
chapter, err := chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate)
commentMode := models.ChapterCommentModeDisabled
if input.CommentMode != nil {
commentMode = *input.CommentMode
}
chapter, err := chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate, commentMode)
if err != nil {
return models.Chapter{}, errors.New("Failed to create chapter: " + err.Error())
}
@@ -117,7 +123,7 @@ func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterE
input.FictionalDate = &time.Time{}
}
chapter, err = chapters.Edit(chapter, input.Title, input.Source, input.FictionalDate)
chapter, err = chapters.Edit(chapter, input.Title, input.Source, input.FictionalDate, input.CommentMode, input.CommentsLocked)
if err != nil {
return models.Chapter{}, errors.New("Failed to edit chapter: " + err.Error())
}
@@ -150,6 +156,11 @@ func (r *mutationResolver) RemoveChapter(ctx context.Context, input input.Chapte
return models.Chapter{}, errors.New("Failed to remove chapter: " + err.Error())
}
err = comments.RemoveChapter(chapter)
if err != nil {
return models.Chapter{}, errors.New("Chapter was removed, but comment removal failed: " + err.Error())
}
go func() {
story, err := stories.FindID(chapter.StoryID)
if err != nil {
+175
View File
@@ -0,0 +1,175 @@
package queries
import (
"context"
"errors"
"log"
"time"
"git.aiterp.net/rpdata/api/models/stories"
"git.aiterp.net/rpdata/api/models/characters"
"git.aiterp.net/rpdata/api/models/changekeys"
"git.aiterp.net/rpdata/api/models/changes"
"git.aiterp.net/rpdata/api/graph2/input"
"git.aiterp.net/rpdata/api/internal/auth"
"git.aiterp.net/rpdata/api/models/chapters"
"git.aiterp.net/rpdata/api/models/comments"
"git.aiterp.net/rpdata/api/models"
)
// Queries
func (r *resolver) Comment(ctx context.Context, id string) (models.Comment, error) {
return comments.Find(id)
}
// Mutations
func (r *mutationResolver) AddComment(ctx context.Context, input input.CommentAddInput) (models.Comment, error) {
chapter, err := chapters.FindID(input.ChapterID)
if err != nil {
return models.Comment{}, errors.New("Chapter not found")
}
token := auth.TokenFromContext(ctx)
if !token.Permitted("member", "story.edit") {
return models.Comment{}, errors.New("Unauthorized")
}
if !chapter.CanComment() {
return models.Comment{}, errors.New("Comments are disabled or locked")
}
var characterPtr *models.Character
if input.CharacterID != nil {
character, err := characters.FindID(*input.CharacterID)
if err != nil {
return models.Comment{}, errors.New("Character not found")
} else if character.Author != token.UserID {
return models.Comment{}, errors.New("That is not your character")
}
characterPtr = &character
}
fictionalDate := time.Time{}
if input.FictionalDate != nil {
fictionalDate = *input.FictionalDate
}
subject := ""
if input.Subject != nil {
subject = *input.Subject
}
comment, err := comments.Add(chapter, subject, token.UserID, input.Source, input.CharacterName, characterPtr, time.Now(), fictionalDate)
if err != nil {
return models.Comment{}, errors.New("Failed to add comment: " + err.Error())
}
go func() {
story, err := stories.FindID(chapter.StoryID)
if err != nil {
log.Println("WARNING: Couldn't log comment change:", err)
return
}
changes.Submit("Comment", "add", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter, story)
}()
return comment, nil
}
func (r *mutationResolver) EditComment(ctx context.Context, input input.CommentEditInput) (models.Comment, error) {
comment, err := comments.Find(input.CommentID)
if err != nil {
return models.Comment{}, errors.New("Comment not found")
}
token := auth.TokenFromContext(ctx)
if !token.PermittedUser(comment.Author, "member", "story.edit") {
return models.Comment{}, errors.New("You cannot edit this comment")
}
chapter, err := chapters.FindID(comment.ChapterID)
if err != nil {
return models.Comment{}, errors.New("Comment's chapter not found")
}
if !chapter.CanComment() {
return models.Comment{}, errors.New("Comments are disabled or locked")
}
if input.ClearFictionalDate != nil && *input.ClearFictionalDate == true {
input.FictionalDate = &time.Time{}
}
if input.CharacterID != nil && *input.CharacterID != "" {
character, err := characters.FindID(*input.CharacterID)
if err != nil {
return models.Comment{}, errors.New("Character not found")
} else if character.Author != token.UserID {
return models.Comment{}, errors.New("That is not your character")
}
}
comment, err = comments.Edit(comment, input.Source, input.CharacterName, input.CharacterID, input.Subject, input.FictionalDate)
if err != nil {
return models.Comment{}, errors.New("Could not post comment: " + err.Error())
}
go func() {
story, err := stories.FindID(chapter.StoryID)
if err != nil {
log.Println("WARNING: Couldn't log comment change:", err)
return
}
changes.Submit("Comment", "edit", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter, story)
}()
return comment, nil
}
func (r *mutationResolver) RemoveComment(ctx context.Context, input input.CommentRemoveInput) (models.Comment, error) {
comment, err := comments.Find(input.CommentID)
if err != nil {
return models.Comment{}, errors.New("Comment not found")
}
token := auth.TokenFromContext(ctx)
if !token.PermittedUser(comment.Author, "member", "story.edit") {
return models.Comment{}, errors.New("You cannot remove this comment")
}
chapter, err := chapters.FindID(comment.ChapterID)
if err != nil {
return models.Comment{}, errors.New("Comment's chapter not found")
}
if !chapter.CanComment() {
return models.Comment{}, errors.New("Comments are disabled or locked")
}
err = comments.Remove(comment)
if err != nil {
return models.Comment{}, errors.New("Failed to remove comment: " + err.Error())
}
go func() {
story, err := stories.FindID(chapter.StoryID)
if err != nil {
log.Println("WARNING: Couldn't log comment change:", err)
return
}
changes.Submit("Comment", "remove", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter, story)
}()
return comment, nil
}
+16 -4
View File
@@ -79,8 +79,14 @@ func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTag
return models.Story{}, errors.New("Story not found")
}
if !token.PermittedUser(story.Author, "member", "story.edit") {
return models.Story{}, errors.New("You are not permitted to edit this story")
if story.Open {
if !token.Permitted("member") {
return models.Story{}, errors.New("You are not permitted to edit this story")
}
} else {
if !token.PermittedUser(story.Author, "member", "story.edit") {
return models.Story{}, errors.New("You are not permitted to edit this story")
}
}
story, err = stories.AddTag(story, input.Tag)
@@ -101,8 +107,14 @@ func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.Story
return models.Story{}, errors.New("Story not found")
}
if !token.PermittedUser(story.Author, "member", "story.edit") {
return models.Story{}, errors.New("You are not permitted to edit this story")
if story.Open {
if !token.Permitted("member") {
return models.Story{}, errors.New("You are not permitted to edit this story")
}
} else {
if !token.PermittedUser(story.Author, "member", "story.edit") {
return models.Story{}, errors.New("You are not permitted to edit this story")
}
}
story, err = stories.RemoveTag(story, input.Tag)
+14
View File
@@ -41,6 +41,10 @@ type Query {
chapter(id: String!): Chapter!
# Find comment by ID
comment(id: String!): Comment!
# Find all distinct tags used in stories
tags: [Tag!]!
@@ -97,6 +101,16 @@ type Mutation {
removeChapter(input: ChapterRemoveInput!): Chapter!
"Add a comment to a chapter."
addComment(input: CommentAddInput!): Comment!
"Edit a comment in a chapter."
editComment(input: CommentEditInput!): Comment!
"Remove a comemnt in a chapter."
removeComment(input: CommentRemoveInput!): Comment!
# Add a new log
addLog(input: LogAddInput!): Log!
+1 -1
View File
@@ -27,7 +27,7 @@ type Change {
objects: [ChangeObject!]!
}
union ChangeObject = Character | Channel | Log | Post | Story | Tag | Chapter
union ChangeObject = Character | Channel | Log | Post | Story | Tag | Chapter | Comment
"""
A change key is a key associated with a change, used for filtering
+38
View File
@@ -20,6 +20,35 @@ type Chapter {
# The date of edit.
editedDate: Date!
"The comment mode."
commentMode: ChapterCommentMode!
"Whether the author has locket comments."
commentsLocked: Boolean!
"A shorthand for checking whether a logged-in user can comment on this chapter."
canComment: Boolean!
"Get all chapter comments."
comments(limit: Int): [Comment!]!
}
"""
The possibles modes of comments for a chapter.
"""
enum ChapterCommentMode {
"Comments are disabled and hidden."
Disabled
"Comments should be shown as typical news article comments."
Article
"Comments should be shown in a compact form suitable for many small messages."
Chat
"Comments are going to be shown as omni-tool messages."
Message
}
# Input for addChapter mutation
@@ -41,6 +70,9 @@ input ChapterAddInput {
# Optionally, assign a fictional date for the chapter
fictionalDate: Date
"The comment mode for the chapter."
commentMode: ChapterCommentMode
}
# Input for editChapter mutation
@@ -59,6 +91,12 @@ input ChapterEditInput {
# Remove the fictional date for a chapter
clearFictionalDate: Boolean
"Set the comment mode for the chapter"
commentMode: ChapterCommentMode
"Set whether the chapter comments are locked"
commentsLocked: Boolean
}
# Input for moveChapter mutation
+91
View File
@@ -0,0 +1,91 @@
"""
A Comment represents a comment to a story chapter.
"""
type Comment {
"A unique ID of the change."
id: String!
"subject"
subject: String!
"The chapter ID the comment belongs to."
chapterId: String!
"The comment's author."
author: String!
"The displayed name of the character. This may be the same as character.name, but it does not need to be."
characterName: String!
"The character associated with the comment."
character: Character
"The fictional (IC) date of the comment."
fictionalDate: Date
"The date of creation."
createdDate: Date!
"The date of the last edit."
editedDate: Date!
"The markdown source of the comment."
source: String!
}
"""
Input for the addComment mutation
"""
input CommentAddInput {
"What chapter to comment on."
chapterId: String!
"The markdown source of the comment."
source: String!
"The name of the character associated with this comment."
characterName: String!
"Optioanlly, a character ID."
characterId: String
"Optional in-universe date of the comment."
fictionalDate: Date
"Optional subject to add to the comment, only shown in some modes."
subject: String
}
"""
Input for the editComment mutation
"""
input CommentEditInput {
"What comment to edit."
commentId: String!
"Change the markdown source."
source: String
"Change the name of the character associated with this comment."
characterName: String
"Change the character assocaited with this comment."
characterId: String
"Change the optional in-universe date of the comment."
fictionalDate: Date
"Set to clear the fictional date."
clearFictionalDate: Boolean
"Change the optional subject for the comment, only shown in some modes."
subject: String
}
"""
Input for the removeComment mutation
"""
input CommentRemoveInput {
"What comment to edit."
commentId: String!
}
+19
View File
@@ -2,9 +2,11 @@ package types
import (
"context"
"errors"
"time"
"git.aiterp.net/rpdata/api/models"
"git.aiterp.net/rpdata/api/models/comments"
)
type chapterResolver struct{}
@@ -17,5 +19,22 @@ func (r *chapterResolver) FictionalDate(ctx context.Context, chapter *models.Cha
return &chapter.FictionalDate, nil
}
func (r *chapterResolver) Comments(ctx context.Context, chapter *models.Chapter, limit *int) ([]models.Comment, error) {
limitValue := 0
if limit != nil {
if *limit < 0 {
return nil, errors.New("Limit cannot be negative")
}
limitValue = *limit
}
if !chapter.CommentMode.IsEnabled() {
return nil, nil
}
return comments.ListChapterID(chapter.ID, limitValue)
}
// ChapterResolver is a resolver
var ChapterResolver chapterResolver
+41
View File
@@ -0,0 +1,41 @@
package types
import (
"context"
"errors"
"time"
"git.aiterp.net/rpdata/api/internal/loader"
"git.aiterp.net/rpdata/api/models"
)
type commentResolver struct{}
func (r *commentResolver) Character(ctx context.Context, obj *models.Comment) (*models.Character, error) {
if obj.CharacterID == "" {
return nil, nil
}
loader := loader.FromContext(ctx)
if loader == nil {
return nil, errors.New("no loader")
}
character, err := loader.Character("id", obj.CharacterID)
if err != nil {
return nil, err
}
return &character, nil
}
func (r *commentResolver) FictionalDate(ctx context.Context, obj *models.Comment) (*time.Time, error) {
if obj.FictionalDate.IsZero() {
return nil, nil
}
return &obj.FictionalDate, nil
}
// CommentResolver is a resolver
var CommentResolver commentResolver
+1
View File
@@ -14,6 +14,7 @@ var global *Config
// Config is configuration
type Config struct {
Space struct {
Enabled bool `json:"enabled"`
Host string `json:"host"`
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
+5 -3
View File
@@ -26,9 +26,11 @@ func Init() error {
}
sconf := conf.Space
err = ConnectSpace(sconf.Host, sconf.AccessKey, sconf.SecretKey, sconf.Bucket, sconf.MaxSize, sconf.Root)
if err != nil {
return err
if sconf.Enabled {
err = ConnectSpace(sconf.Host, sconf.AccessKey, sconf.SecretKey, sconf.Bucket, sconf.MaxSize, sconf.Root)
if err != nil {
return err
}
}
hasInitialized = true
+12
View File
@@ -43,6 +43,10 @@ func ConnectSpace(host, accessKey, secretKey, bucket string, maxSize int64, root
// UploadFile uploads the file to the space. This does not do any checks on it, so the endpoints should
// ensure that's all okay.
func UploadFile(ctx context.Context, folder string, name string, mimeType string, reader io.Reader, size int64) (string, error) {
if spaceClient == nil {
return "", errors.New("This functionality is not enabled")
}
path := folder + "/" + name
if size > spaceMaxSize {
@@ -69,6 +73,10 @@ func UploadFile(ctx context.Context, folder string, name string, mimeType string
// RemoveFile removes a file from the space
func RemoveFile(folder string, name string) error {
if spaceClient == nil {
return errors.New("This functionality is not enabled")
}
path := folder + "/" + name
return spaceClient.RemoveObject(spaceBucket, spaceRoot+"/"+path)
@@ -76,6 +84,10 @@ func RemoveFile(folder string, name string) error {
// DownloadFile opens a file for download, using the same path format as the UploadFile function. Remember to Close it!
func DownloadFile(ctx context.Context, path string) (io.ReadCloser, error) {
if spaceClient == nil {
return nil, errors.New("This functionality is not enabled")
}
return spaceClient.GetObjectWithContext(ctx, spaceBucket, spaceRoot+"/"+path, minio.GetObjectOptions{})
}
+3 -1
View File
@@ -24,12 +24,14 @@ const (
ChangeModelTag ChangeModel = "Tag"
// ChangeModelChapter is a value of ChangeModel
ChangeModelChapter ChangeModel = "Chapter"
// ChangeModelComment is a value of ChangeModel
ChangeModelComment ChangeModel = "Comment"
)
// IsValid returns true if the underlying string is one of the correct values.
func (e ChangeModel) IsValid() bool {
switch e {
case ChangeModelCharacter, ChangeModelChannel, ChangeModelLog, ChangeModelPost, ChangeModelStory, ChangeModelTag, ChangeModelChapter:
case ChangeModelCharacter, ChangeModelChannel, ChangeModelLog, ChangeModelPost, ChangeModelStory, ChangeModelTag, ChangeModelChapter, ChangeModelComment:
return true
}
return false
+5 -1
View File
@@ -19,6 +19,7 @@ type Change struct {
Stories []Story `bson:"stories"`
Tags []Tag `bson:"tags"`
Chapters []Chapter `bson:"chapters"`
Comments []Comment `bson:"comments"`
}
// ChangeKey is a key for a change that can be used when subscribing to them.
@@ -29,7 +30,7 @@ type ChangeKey struct {
// Objects makes a combined, mixed array of all the models stored in this change.
func (change *Change) Objects() []interface{} {
data := make([]interface{}, 0, len(change.Logs)+len(change.Characters)+len(change.Channels)+len(change.Posts)+len(change.Stories)+len(change.Tags)+len(change.Chapters))
data := make([]interface{}, 0, len(change.Logs)+len(change.Characters)+len(change.Channels)+len(change.Posts)+len(change.Stories)+len(change.Tags)+len(change.Chapters)+len(change.Comments))
for _, log := range change.Logs {
data = append(data, log)
@@ -52,6 +53,9 @@ func (change *Change) Objects() []interface{} {
for _, chapter := range change.Chapters {
data = append(data, chapter)
}
for _, comment := range change.Comments {
data = append(data, comment)
}
return data
}
+2
View File
@@ -22,6 +22,7 @@ func TestMany(t *testing.T) {
},
},
models.Post{ID: "P5"},
models.Comment{ID: "SCC1234"},
}
expectations := []models.ChangeKey{
@@ -33,6 +34,7 @@ func TestMany(t *testing.T) {
{Model: "Character", ID: "C17"},
{Model: "Channel", ID: "#Stuff"},
{Model: "Post", ID: "P5"},
{Model: "Comment", ID: "SCC1234"},
}
results := changekeys.Many(data...)
+5
View File
@@ -28,6 +28,11 @@ func TestOne(t *testing.T) {
models.Log{ID: "2018-10-23_210303325_RedrockAgency", ShortID: "L807"},
models.ChangeKey{Model: "Log", ID: "2018-10-23_210303325_RedrockAgency"},
},
{
"Comment_SCC1234",
models.Comment{ID: "SCC1234"},
models.ChangeKey{Model: "Comment", ID: "SCC1234"},
},
}
for _, row := range table {
+4
View File
@@ -81,6 +81,10 @@ func Submit(model, op, author string, listed bool, keys []models.ChangeKey, obje
change.Chapters = append(change.Chapters, object)
case []models.Chapter:
change.Chapters = append(change.Chapters, object...)
case models.Comment:
change.Comments = append(change.Comments, object)
case []models.Comment:
change.Comments = append(change.Comments, object...)
default:
log.Printf("Warning: unrecognized object in change: %#+v", object)
}
+57
View File
@@ -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
View File
@@ -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()
}
+10 -8
View File
@@ -8,15 +8,17 @@ import (
)
// Add adds a new chapter.
func Add(story models.Story, title, author, source string, createdDate time.Time, finctionalDate *time.Time) (models.Chapter, error) {
func Add(story models.Story, title, author, source string, createdDate time.Time, finctionalDate *time.Time, commentMode models.ChapterCommentMode) (models.Chapter, error) {
chapter := models.Chapter{
ID: makeChapterID(),
StoryID: story.ID,
Title: title,
Author: author,
Source: source,
CreatedDate: createdDate,
EditedDate: createdDate,
ID: makeChapterID(),
StoryID: story.ID,
Title: title,
Author: author,
Source: source,
CreatedDate: createdDate,
EditedDate: createdDate,
CommentMode: commentMode,
CommentsLocked: false,
}
if finctionalDate != nil {
+9 -1
View File
@@ -9,7 +9,7 @@ import (
// Edit edits a chapter, and updates EditedDate. While many Edit functions cheat if there's nothing to
// change, this functill will due to EditedDate.
func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Time) (models.Chapter, error) {
func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Time, commentMode *models.ChapterCommentMode, commentsLocked *bool) (models.Chapter, error) {
now := time.Now()
changes := bson.M{"editedDate": now}
@@ -28,6 +28,14 @@ func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Tim
changes["fictionalDate"] = *fictionalDate
edited.FictionalDate = *fictionalDate
}
if commentMode != nil && *commentMode != chapter.CommentMode {
changes["commentMode"] = *commentMode
edited.CommentMode = *commentMode
}
if commentsLocked != nil && *commentsLocked != chapter.CommentsLocked {
changes["commentsLocked"] = *commentsLocked
edited.CommentsLocked = *commentsLocked
}
err := collection.UpdateId(chapter.ID, bson.M{"$set": changes})
if err != nil {
+17
View File
@@ -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:"source"`
}
+35
View File
@@ -0,0 +1,35 @@
package comments
import (
"time"
"git.aiterp.net/rpdata/api/models"
)
// Add adds a comment.
func Add(chapter models.Chapter, subject, author, source, characterName string, character *models.Character, createdDate time.Time, fictionalDate time.Time) (models.Comment, error) {
characterID := ""
if character != nil {
characterID = character.ID
}
comment := models.Comment{
ID: makeCommentID(),
ChapterID: chapter.ID,
Subject: subject,
Author: author,
CharacterName: characterName,
CharacterID: characterID,
FictionalDate: fictionalDate,
CreatedDate: createdDate,
EditedDate: createdDate,
Source: source,
}
err := collection.Insert(comment)
if err != nil {
return models.Comment{}, err
}
return comment, nil
}
+66
View File
@@ -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")
})
}
+44
View File
@@ -0,0 +1,44 @@
package comments
import (
"time"
"git.aiterp.net/rpdata/api/models"
"github.com/globalsign/mgo/bson"
)
// Edit edits a comment.
func Edit(comment models.Comment, source, characterName, characterID, subject *string, fictionalDate *time.Time) (models.Comment, error) {
changes := make(bson.M, 6)
comment.EditedDate = time.Now()
changes["editedDate"] = comment.EditedDate
if source != nil {
comment.Source = *source
changes["source"] = *source
}
if characterName != nil {
comment.CharacterName = *characterName
changes["characterName"] = *characterName
}
if characterID != nil {
comment.CharacterID = *characterID
changes["characterId"] = *characterID
}
if subject != nil {
comment.Subject = *subject
changes["subject"] = *subject
}
if fictionalDate != nil {
comment.FictionalDate = *fictionalDate
changes["fictionalDate"] = *fictionalDate
}
err := collection.UpdateId(comment.ID, bson.M{"$set": changes})
if err != nil {
return models.Comment{}, err
}
return comment, nil
}
+11
View File
@@ -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})
}
+11
View File
@@ -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)
}
+17
View File
@@ -0,0 +1,17 @@
package comments
import (
"git.aiterp.net/rpdata/api/models"
"github.com/globalsign/mgo/bson"
)
// Remove removes one comment.
func Remove(comment models.Comment) error {
return collection.RemoveId(comment.ID)
}
// RemoveChapter removes all comments for the given chapter.
func RemoveChapter(chapter models.Chapter) error {
_, err := collection.RemoveAll(bson.M{"chapterId": chapter.ID})
return err
}