GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

341 lines
8.1 KiB

package services
import (
"context"
"errors"
"git.aiterp.net/rpdata/api/internal/auth"
"git.aiterp.net/rpdata/api/models"
"git.aiterp.net/rpdata/api/models/changekeys"
"git.aiterp.net/rpdata/api/models/channels"
"git.aiterp.net/rpdata/api/repositories"
"git.aiterp.net/rpdata/api/services/parsers"
"time"
)
type LogService struct {
logs repositories.LogRepository
posts repositories.PostRepository
changeService *ChangeService
channelService *ChannelService
}
func (s *LogService) Find(ctx context.Context, id string) (*models.Log, error) {
return s.logs.Find(ctx, id)
}
func (s *LogService) FindPosts(ctx context.Context, id string) (*models.Post, error) {
return s.posts.Find(ctx, id)
}
func (s *LogService) List(ctx context.Context, filter *models.LogFilter) ([]*models.Log, error) {
if filter == nil {
filter = &models.LogFilter{}
}
return s.logs.List(ctx, *filter)
}
func (s *LogService) ListPosts(ctx context.Context, filter *models.PostFilter) ([]*models.Post, error) {
// Some sanity checks to avoid querying an insame amount of posts.
if filter == nil {
filter = &models.PostFilter{Limit: 100}
} else {
if (filter.Limit <= 0 || filter.Limit > 512) && (filter.LogID == nil && len(filter.IDs) == 0) {
return nil, errors.New("a limit of 0 (no limit) or >512 without a logId or a set of IDs is not allowed")
}
if len(filter.IDs) > 100 {
return nil, errors.New("you may not query for more than 100 ids, split your query")
}
}
return s.posts.List(ctx, *filter)
}
func (s *LogService) Create(ctx context.Context, title, description, channelName, eventName string, open bool) (*models.Log, error) {
if channelName == "" {
return nil, errors.New("channel name cannot be empty")
}
log := &models.Log{
Title: title,
Description: description,
ChannelName: channelName,
EventName: eventName,
Date: time.Now(),
Open: open,
}
if err := auth.CheckPermission(ctx, "add", log); err != nil {
return nil, err
}
_, err := s.channelService.Ensure(ctx, channelName)
if err != nil {
return nil, err
}
log, err = s.logs.Insert(ctx, *log)
if err != nil {
return nil, err
}
s.changeService.Submit(ctx, models.ChangeModelLog, "add", true, changekeys.Listed(log), log)
return log, nil
}
// Import creates new logs from common formats.
func (s *LogService) Import(ctx context.Context, importer models.LogImporter, date time.Time, tz *time.Location, channelName string, data string) ([]*models.Log, error) {
if err := auth.CheckPermission(ctx, "add", &models.Log{}); err != nil {
return nil, err
}
results := make([]*models.Log, 0, 8)
eventName := ""
if channel, err := channels.FindName(channelName); err != nil {
eventName = channel.EventName
}
_, err := s.channelService.Ensure(ctx, channelName)
if err != nil {
return nil, err
}
date = date.In(tz)
switch importer {
case models.LogImporterMircLike:
{
if date.IsZero() {
return nil, errors.New("date is not optional for mirc-like logs")
}
parsed, err := parsers.MircLog(data, date, true)
if err != nil {
return nil, err
}
parsed.Log.EventName = eventName
parsed.Log.ChannelName = channelName
log, err := s.logs.Insert(ctx, parsed.Log)
if err != nil {
return nil, err
}
for _, post := range parsed.Posts {
post.LogID = log.ShortID
}
posts, err := s.posts.InsertMany(ctx, parsed.Posts...)
if err != nil {
return nil, err
}
s.changeService.Submit(ctx, models.ChangeModelLog, "add", true, changekeys.Listed(log), log)
s.changeService.Submit(ctx, models.ChangeModelPost, "add", true, changekeys.Listed(log, posts), log, posts)
results = append(results, log)
}
case models.LogImporterForumLog:
{
parseResults, err := parsers.ForumLog(data, tz)
if err != nil {
return nil, err
}
for _, parsed := range parseResults {
log, err := s.logs.Insert(ctx, parsed.Log)
if err != nil {
return nil, err
}
parsed.Log.EventName = eventName
parsed.Log.ChannelName = channelName
for _, post := range parsed.Posts {
post.LogID = log.ShortID
}
posts, err := s.posts.InsertMany(ctx, parsed.Posts...)
if err != nil {
return nil, err
}
s.changeService.Submit(ctx, models.ChangeModelLog, "add", true, changekeys.Listed(log), log)
s.changeService.Submit(ctx, models.ChangeModelPost, "add", true, changekeys.Listed(log, posts), log, posts)
}
}
default:
{
return nil, errors.New("Invalid importer: " + importer.String())
}
}
return results, nil
}
func (s *LogService) Update(ctx context.Context, id string, update models.LogUpdate) (*models.Log, error) {
log, err := s.logs.Find(ctx, id)
if err != nil {
return nil, err
}
if err := auth.CheckPermission(ctx, "edit", log); err != nil {
return nil, err
}
log, err = s.logs.Update(ctx, *log, update)
if err != nil {
return nil, err
}
s.changeService.Submit(ctx, models.ChangeModelLog, "edit", true, changekeys.Listed(log), log)
return log, nil
}
func (s *LogService) AddPost(ctx context.Context, logId string, time time.Time, kind, nick, text string) (*models.Post, error) {
if kind == "" || nick == "" || time.IsZero() {
return nil, errors.New("kind, nick and time must be non-empty")
}
log, err := s.logs.Find(ctx, logId)
if err != nil {
return nil, err
}
post := &models.Post{
LogID: log.ShortID,
Kind: kind,
Nick: nick,
Text: text,
Time: time,
}
if err := auth.CheckPermission(ctx, "add", post); err != nil {
return nil, err
}
post, err = s.posts.Insert(ctx, *post)
if err != nil {
return nil, err
}
s.changeService.Submit(ctx, models.ChangeModelPost, "add", true, changekeys.Many(log, post), log, post)
return post, nil
}
func (s *LogService) EditPost(ctx context.Context, id string, update models.PostUpdate) (*models.Post, error) {
if (update.Kind != nil && *update.Kind == "") || (update.Nick != nil && *update.Nick == "") || (update.Text != nil && *update.Text == "") {
return nil, errors.New("kind, nick and time must be non-empty")
}
post, err := s.posts.Find(ctx, id)
if err != nil {
return nil, err
}
if err := auth.CheckPermission(ctx, "edit", post); err != nil {
return nil, err
}
post, err = s.posts.Update(ctx, *post, update)
if err != nil {
return nil, err
}
go func() {
log, err := s.logs.Find(context.Background(), post.LogID)
if err != nil {
return
}
s.changeService.Submit(ctx, models.ChangeModelPost, "edit", true, changekeys.Many(log, post), post)
}()
return post, nil
}
func (s *LogService) MovePost(ctx context.Context, id string, position int) ([]*models.Post, error) {
if position < 1 {
return nil, repositories.ErrInvalidPosition
}
post, err := s.posts.Find(ctx, id)
if err != nil {
return nil, err
}
if err := auth.CheckPermission(ctx, "move", post); err != nil {
return nil, err
}
posts, err := s.posts.Move(ctx, *post, position)
if err != nil {
return nil, err
}
go func() {
if len(posts) == 0 {
return
}
log, err := s.logs.Find(context.Background(), posts[0].LogID)
if err != nil {
return
}
s.changeService.Submit(ctx, models.ChangeModelPost, "move", true, changekeys.Many(log, posts), posts)
}()
return posts, nil
}
func (s *LogService) DeletePost(ctx context.Context, id string) (*models.Post, error) {
post, err := s.posts.Find(ctx, id)
if err != nil {
return nil, err
}
if err := auth.CheckPermission(ctx, "remove", post); err != nil {
return nil, err
}
err = s.posts.Delete(ctx, *post)
if err != nil {
return nil, err
}
go func() {
log, err := s.logs.Find(context.Background(), post.LogID)
if err != nil {
return
}
s.changeService.Submit(ctx, models.ChangeModelPost, "remove", true, changekeys.Many(log, post), post)
}()
return post, nil
}
func (s *LogService) Delete(ctx context.Context, id string) (*models.Log, error) {
log, err := s.logs.Find(ctx, id)
if err != nil {
return nil, err
}
if err := auth.CheckPermission(ctx, "remove", log); err != nil {
return nil, err
}
err = s.logs.Delete(ctx, *log)
if err != nil {
return nil, err
}
s.changeService.Submit(ctx, models.ChangeModelLog, "remove", true, changekeys.Listed(log), log)
return log, nil
}