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.
 
 

93 lines
1.9 KiB

package logs
import (
"errors"
"time"
"git.aiterp.net/rpdata/api/internal/importers/mirclike"
"git.aiterp.net/rpdata/api/models/posts"
"git.aiterp.net/rpdata/api/models/channels"
"git.aiterp.net/rpdata/api/internal/importers/forumlog"
"git.aiterp.net/rpdata/api/models"
)
// An ImportedLog contains data about an imported log.
type ImportedLog struct {
Log models.Log
Posts []models.Post
}
// Import makes a log and posts object from different formats.
func Import(importer models.LogImporter, date time.Time, tz *time.Location, channelName string, data string) ([]ImportedLog, error) {
results := make([]ImportedLog, 0, 8)
eventName := ""
if channel, err := channels.FindName(channelName); err != nil {
eventName = channel.EventName
}
date = date.In(tz)
switch importer {
case models.LogImporterMircLike:
{
if date.IsZero() {
return nil, errors.New("Date is not optional for mirc-like logs")
}
parsedLog, parsedPosts, err := mirclike.ParseLog(data, date, true)
if err != nil {
return nil, err
}
log, err := Add(parsedLog.Date, channelName, "", eventName, "", false)
if err != nil {
return nil, err
}
posts, err := posts.AddMany(log, parsedPosts)
if err != nil {
return nil, err
}
results = append(results, ImportedLog{
Log: log,
Posts: posts,
})
}
case models.LogImporterForumLog:
{
parseResults, err := forumlog.ParseLogs(data, tz)
if err != nil {
return nil, err
}
for _, result := range parseResults {
log, err := Add(result.Log.Date, channelName, "", eventName, "", false)
if err != nil {
return nil, err
}
posts, err := posts.AddMany(log, result.Posts)
if err != nil {
return nil, err
}
results = append(results, ImportedLog{
Log: log,
Posts: posts,
})
}
}
default:
{
return nil, errors.New("Invalid importer: " + importer.String())
}
}
return results, nil
}