@@ -58,6 +58,10 @@ func (r *mutationResolver) ImportLog(ctx context.Context, input graphcore.LogImp
|
|||||||
return r.s.Logs.Import(ctx, input.Importer, date, tz, input.ChannelName, input.Data)
|
return r.s.Logs.Import(ctx, input.Importer, date, tz, input.ChannelName, input.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *mutationResolver) SplitLog(ctx context.Context, input graphcore.LogSplitInput) (*models.Log, error) {
|
||||||
|
return r.s.Logs.SplitLog(ctx, input.LogID, input.StartPostID)
|
||||||
|
}
|
||||||
|
|
||||||
func (r *mutationResolver) EditLog(ctx context.Context, input graphcore.LogEditInput) (*models.Log, error) {
|
func (r *mutationResolver) EditLog(ctx context.Context, input graphcore.LogEditInput) (*models.Log, error) {
|
||||||
update := models.LogUpdate{
|
update := models.LogUpdate{
|
||||||
Open: input.Open,
|
Open: input.Open,
|
||||||
|
|||||||
@@ -117,6 +117,9 @@ type Mutation {
|
|||||||
# Import a log
|
# Import a log
|
||||||
importLog(input: LogImportInput!): [Log!]!
|
importLog(input: LogImportInput!): [Log!]!
|
||||||
|
|
||||||
|
# Split a log
|
||||||
|
splitLog(input: LogSplitInput!): Log!
|
||||||
|
|
||||||
# Edit a log
|
# Edit a log
|
||||||
editLog(input: LogEditInput!): Log!
|
editLog(input: LogEditInput!): Log!
|
||||||
|
|
||||||
|
|||||||
@@ -122,6 +122,17 @@ input LogImportInput {
|
|||||||
data: String!
|
data: String!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
"""
|
||||||
|
Input for splitLog mutation.
|
||||||
|
"""
|
||||||
|
input LogSplitInput {
|
||||||
|
"Log ID"
|
||||||
|
logId: String!
|
||||||
|
|
||||||
|
"After Post ID"
|
||||||
|
startPostId: String!
|
||||||
|
}
|
||||||
|
|
||||||
enum LogImporter {
|
enum LogImporter {
|
||||||
"""
|
"""
|
||||||
mIRC-like: This importer parses logs that copied from mIRC posts without spaces.
|
mIRC-like: This importer parses logs that copied from mIRC posts without spaces.
|
||||||
|
|||||||
@@ -203,6 +203,89 @@ func (s *LogService) Update(ctx context.Context, id string, update models.LogUpd
|
|||||||
return log, nil
|
return log, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *LogService) SplitLog(ctx context.Context, logId string, startPostId string) (*models.Log, error) {
|
||||||
|
// Find log
|
||||||
|
l, err := s.logs.Find(ctx, logId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := auth.CheckPermission(ctx, "add", l); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find posts
|
||||||
|
posts, err := s.ListPosts(ctx, &models.PostFilter{LogID: &l.ShortID})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(posts) == 0 {
|
||||||
|
return nil, errors.New("cannot split empty log")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cut the posts slice.
|
||||||
|
firstPost := posts[0]
|
||||||
|
cutPosts := posts[len(posts):]
|
||||||
|
for i, post := range posts {
|
||||||
|
if post.ID == startPostId {
|
||||||
|
cutPosts = posts[i:]
|
||||||
|
firstPost = post
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(cutPosts) == 0 {
|
||||||
|
return nil, errors.New("post not found")
|
||||||
|
}
|
||||||
|
if len(cutPosts) == len(posts) || firstPost.Time.Equal(l.Date) {
|
||||||
|
return nil, errors.New("cannot move posts")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new log
|
||||||
|
newLog := &models.Log{
|
||||||
|
Date: firstPost.Time,
|
||||||
|
ChannelName: l.ChannelName,
|
||||||
|
EventName: l.EventName,
|
||||||
|
}
|
||||||
|
newLog, err = s.logs.Insert(ctx, *newLog)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put the cut posts in the new log
|
||||||
|
newPosts := make([]*models.Post, 0, len(cutPosts))
|
||||||
|
for _, post := range cutPosts {
|
||||||
|
postCopy := *post
|
||||||
|
postCopy.ID = ""
|
||||||
|
postCopy.LogID = newLog.ShortID
|
||||||
|
|
||||||
|
newPost, err := s.posts.Insert(ctx, postCopy)
|
||||||
|
if err != nil {
|
||||||
|
_ = s.logs.Delete(ctx, *newLog)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
newPosts = append(newPosts, newPost)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove the posts from the old log (this can't error because that'll make a mess)
|
||||||
|
for _, post := range cutPosts {
|
||||||
|
err := s.posts.Delete(ctx, *post)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to delete post %s: %s", post.ID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Submit the changes
|
||||||
|
s.changeService.Submit(ctx, models.ChangeModelPost, "remove", true, changekeys.Many(l, cutPosts), cutPosts)
|
||||||
|
s.changeService.Submit(ctx, models.ChangeModelLog, "add", true, changekeys.Listed(newLog), newLog)
|
||||||
|
s.changeService.Submit(ctx, models.ChangeModelPost, "add", true, changekeys.Many(newLog, newPosts), newPosts)
|
||||||
|
|
||||||
|
// Refresh character lists.
|
||||||
|
_ = s.refreshLogCharacters(ctx, *l, nil)
|
||||||
|
_ = s.refreshLogCharacters(ctx, *newLog, nil)
|
||||||
|
|
||||||
|
return newLog, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *LogService) AddPost(ctx context.Context, logId string, time time.Time, kind, nick, text string) (*models.Post, error) {
|
func (s *LogService) AddPost(ctx context.Context, logId string, time time.Time, kind, nick, text string) (*models.Post, error) {
|
||||||
if kind == "" || nick == "" || time.IsZero() {
|
if kind == "" || nick == "" || time.IsZero() {
|
||||||
return nil, errors.New("kind, nick and time must be non-empty")
|
return nil, errors.New("kind, nick and time must be non-empty")
|
||||||
|
|||||||
Reference in New Issue
Block a user