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

  1. package logs
  2. import (
  3. "errors"
  4. "time"
  5. "git.aiterp.net/rpdata/api/internal/importers/mirclike"
  6. "git.aiterp.net/rpdata/api/models/posts"
  7. "git.aiterp.net/rpdata/api/models/channels"
  8. "git.aiterp.net/rpdata/api/internal/importers/forumlog"
  9. "git.aiterp.net/rpdata/api/models"
  10. )
  11. // An ImportedLog contains data about an imported log.
  12. type ImportedLog struct {
  13. Log models.Log
  14. Posts []models.Post
  15. }
  16. // Import makes a log and posts object from different formats.
  17. func Import(importer models.LogImporter, date time.Time, tz *time.Location, channelName string, data string) ([]ImportedLog, error) {
  18. results := make([]ImportedLog, 0, 8)
  19. eventName := ""
  20. if channel, err := channels.FindName(channelName); err != nil {
  21. eventName = channel.EventName
  22. }
  23. date = date.In(tz)
  24. switch importer {
  25. case models.LogImporterMircLike:
  26. {
  27. if date.IsZero() {
  28. return nil, errors.New("Date is not optional for mirc-like logs")
  29. }
  30. parsedLog, parsedPosts, err := mirclike.ParseLog(data, date, true)
  31. if err != nil {
  32. return nil, err
  33. }
  34. log, err := Add(parsedLog.Date, channelName, "", eventName, "", false)
  35. if err != nil {
  36. return nil, err
  37. }
  38. posts, err := posts.AddMany(log, parsedPosts)
  39. if err != nil {
  40. return nil, err
  41. }
  42. results = append(results, ImportedLog{
  43. Log: log,
  44. Posts: posts,
  45. })
  46. }
  47. case models.LogImporterForumLog:
  48. {
  49. parseResults, err := forumlog.ParseLogs(data, tz)
  50. if err != nil {
  51. return nil, err
  52. }
  53. for _, result := range parseResults {
  54. log, err := Add(result.Log.Date, channelName, "", eventName, "", false)
  55. if err != nil {
  56. return nil, err
  57. }
  58. posts, err := posts.AddMany(log, result.Posts)
  59. if err != nil {
  60. return nil, err
  61. }
  62. results = append(results, ImportedLog{
  63. Log: log,
  64. Posts: posts,
  65. })
  66. }
  67. }
  68. default:
  69. {
  70. return nil, errors.New("Invalid importer: " + importer.String())
  71. }
  72. }
  73. return results, nil
  74. }