|
@ -280,12 +280,78 @@ func (s *LogService) SplitLog(ctx context.Context, logId string, startPostId str |
|
|
s.changeService.Submit(ctx, models.ChangeModelPost, "add", true, changekeys.Many(newLog, newPosts), newPosts) |
|
|
s.changeService.Submit(ctx, models.ChangeModelPost, "add", true, changekeys.Many(newLog, newPosts), newPosts) |
|
|
|
|
|
|
|
|
// Refresh character lists.
|
|
|
// Refresh character lists.
|
|
|
_ = s.refreshLogCharacters(ctx, *l, nil) |
|
|
|
|
|
_ = s.refreshLogCharacters(ctx, *newLog, nil) |
|
|
|
|
|
|
|
|
_, _ = s.refreshLogCharacters(ctx, *l, nil) |
|
|
|
|
|
newLog2, err := s.refreshLogCharacters(ctx, *newLog, nil) |
|
|
|
|
|
if err == nil { |
|
|
|
|
|
newLog = newLog2 |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return newLog, nil |
|
|
return newLog, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *LogService) MergeLogs(ctx context.Context, targetID string, sourceID string, removeAfter bool) (*models.Log, error) { |
|
|
|
|
|
// Check permissions
|
|
|
|
|
|
if err := auth.CheckPermission(ctx, "edit", &models.Log{}); err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
if removeAfter { |
|
|
|
|
|
if err := auth.CheckPermission(ctx, "remove", &models.Log{}); err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Merge log posts into log.
|
|
|
|
|
|
source, err := s.logs.Find(ctx, sourceID) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, errors.New("could not find source log: " + err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
target, err := s.logs.Find(ctx, targetID) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, errors.New("could not find target log: " + err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Get the source posts.
|
|
|
|
|
|
posts, err := s.posts.List(ctx, models.PostFilter{LogID: &source.ShortID}) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, errors.New("could not fetch source posts: " + err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Associate the posts with the target logs
|
|
|
|
|
|
for _, post := range posts { |
|
|
|
|
|
post.ID = "" |
|
|
|
|
|
post.LogID = target.ShortID |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Insert them
|
|
|
|
|
|
posts, err = s.posts.InsertMany(ctx, posts...) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, errors.New("could not insert posts into target: " + err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Remove other log
|
|
|
|
|
|
if removeAfter { |
|
|
|
|
|
err = s.logs.Delete(ctx, *source) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, errors.New("posts have been inserted, but could not remove source: " + err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
s.changeService.Submit(ctx, models.ChangeModelLog, "remove", true, changekeys.Listed(source), source) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Refresh characters
|
|
|
|
|
|
target2, err := s.refreshLogCharacters(ctx, *target, nil) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Printf("Failed to update characters in log %s: %s", target.ID, err) |
|
|
|
|
|
} else { |
|
|
|
|
|
target = target2 |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Submit changes after the target
|
|
|
|
|
|
s.changeService.Submit(ctx, models.ChangeModelPost, "add", true, changekeys.Many(target, posts), target, posts) |
|
|
|
|
|
|
|
|
|
|
|
return target, 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") |
|
@ -313,9 +379,11 @@ func (s *LogService) AddPost(ctx context.Context, logId string, time time.Time, |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
err = s.refreshLogCharacters(ctx, *l, nil) |
|
|
|
|
|
|
|
|
l2, err := s.refreshLogCharacters(ctx, *l, nil) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Printf("Failed to update characters in log %s: %s", l.ID, err) |
|
|
log.Printf("Failed to update characters in log %s: %s", l.ID, err) |
|
|
|
|
|
} else { |
|
|
|
|
|
l = l2 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
s.changeService.Submit(ctx, models.ChangeModelPost, "add", true, changekeys.Many(l, post), post) |
|
|
s.changeService.Submit(ctx, models.ChangeModelPost, "add", true, changekeys.Many(l, post), post) |
|
@ -348,7 +416,7 @@ func (s *LogService) EditPost(ctx context.Context, id string, update models.Post |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
err = s.refreshLogCharacters(ctx, *l, nil) |
|
|
|
|
|
|
|
|
_, err = s.refreshLogCharacters(ctx, *l, nil) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Printf("Failed to update characters in log %s: %s", l.ID, err) |
|
|
log.Printf("Failed to update characters in log %s: %s", l.ID, err) |
|
|
} |
|
|
} |
|
@ -415,7 +483,7 @@ func (s *LogService) DeletePost(ctx context.Context, id string) (*models.Post, e |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
err = s.refreshLogCharacters(ctx, *l, nil) |
|
|
|
|
|
|
|
|
_, err = s.refreshLogCharacters(ctx, *l, nil) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Printf("Failed to update characters in log %s: %s", l.ID, err) |
|
|
log.Printf("Failed to update characters in log %s: %s", l.ID, err) |
|
|
} |
|
|
} |
|
@ -575,7 +643,8 @@ func (s *LogService) RefreshAllLogCharacters(ctx context.Context) error { |
|
|
l := logs[i] |
|
|
l := logs[i] |
|
|
|
|
|
|
|
|
eg.Go(func() error { |
|
|
eg.Go(func() error { |
|
|
return s.refreshLogCharacters(ctx, *l, characterMap) |
|
|
|
|
|
|
|
|
_, err := s.refreshLogCharacters(ctx, *l, characterMap) |
|
|
|
|
|
return err |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
err = eg.Wait() |
|
|
err = eg.Wait() |
|
@ -588,14 +657,14 @@ func (s *LogService) RefreshAllLogCharacters(ctx context.Context) error { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (s *LogService) RefreshLogCharacters(ctx context.Context, log models.Log) error { |
|
|
|
|
|
|
|
|
func (s *LogService) RefreshLogCharacters(ctx context.Context, log models.Log) (*models.Log, error) { |
|
|
return s.refreshLogCharacters(ctx, log, nil) |
|
|
return s.refreshLogCharacters(ctx, log, nil) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (s *LogService) refreshLogCharacters(ctx context.Context, log models.Log, characterMap map[string]*models.Character) error { |
|
|
|
|
|
|
|
|
func (s *LogService) refreshLogCharacters(ctx context.Context, log models.Log, characterMap map[string]*models.Character) (*models.Log, error) { |
|
|
posts, err := s.ListPosts(ctx, &models.PostFilter{LogID: &log.ShortID}) |
|
|
posts, err := s.ListPosts(ctx, &models.PostFilter{LogID: &log.ShortID}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil |
|
|
|
|
|
|
|
|
return &log, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
counts := make(map[string]int) |
|
|
counts := make(map[string]int) |
|
@ -639,7 +708,7 @@ func (s *LogService) refreshLogCharacters(ctx context.Context, log models.Log, c |
|
|
if characterMap == nil { |
|
|
if characterMap == nil { |
|
|
characters, err := s.characterService.List(ctx, models.CharacterFilter{Nicks: nicks}) |
|
|
characters, err := s.characterService.List(ctx, models.CharacterFilter{Nicks: nicks}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return err |
|
|
|
|
|
|
|
|
return nil, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
characterMap = s.makeCharacterMap(characters) |
|
|
characterMap = s.makeCharacterMap(characters) |
|
@ -659,8 +728,7 @@ func (s *LogService) refreshLogCharacters(ctx context.Context, log models.Log, c |
|
|
log.CharacterIDs = append(log.CharacterIDs, character.ID) |
|
|
log.CharacterIDs = append(log.CharacterIDs, character.ID) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
_, err = s.logs.Update(ctx, log, models.LogUpdate{CharacterIDs: log.CharacterIDs}) |
|
|
|
|
|
return err |
|
|
|
|
|
|
|
|
return s.logs.Update(ctx, log, models.LogUpdate{CharacterIDs: log.CharacterIDs}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (s *LogService) makeCharacterMap(characters []*models.Character) map[string]*models.Character { |
|
|
func (s *LogService) makeCharacterMap(characters []*models.Character) map[string]*models.Character { |
|
|