post.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package kinds
  2. import (
  3. "net/url"
  4. "strings"
  5. "time"
  6. "mimicry/shared"
  7. "mimicry/style"
  8. "mimicry/request"
  9. "fmt"
  10. )
  11. type Post map[string]any
  12. // TODO: make the Post references *Post because why not
  13. func (p Post) Kind() (string, error) {
  14. kind, err := shared.Get[string](p, "type")
  15. return strings.ToLower(kind), err
  16. }
  17. func (p Post) Title() (string, error) {
  18. title, err := shared.GetNatural(p, "name", "en")
  19. return strings.TrimSpace(title), err
  20. }
  21. func (p Post) Body() (string, error) {
  22. body, err := shared.GetNatural(p, "content", "en")
  23. return strings.TrimSpace(body), err
  24. }
  25. func (p Post) BodyPreview() (string, error) {
  26. body, err := p.Body()
  27. return fmt.Sprintf("%s…", string([]rune(body)[:280])), err
  28. }
  29. func (p Post) Identifier() (*url.URL, error) {
  30. return shared.GetURL(p, "id")
  31. }
  32. func (p Post) Created() (time.Time, error) {
  33. return shared.GetTime(p, "published")
  34. }
  35. func (p Post) Updated() (time.Time, error) {
  36. return shared.GetTime(p, "updated")
  37. }
  38. func (p Post) Category() string {
  39. return "post"
  40. }
  41. func (p Post) Creators() []Actor {
  42. // TODO: this line needs an existence check
  43. attributedTo, ok := p["attributedTo"]
  44. if !ok {
  45. return []Actor{}
  46. }
  47. // if not an array, make it an array
  48. attributions := []any{}
  49. if attributedToList, isList := attributedTo.([]any); isList {
  50. attributions = attributedToList
  51. } else {
  52. attributions = []any{attributedTo}
  53. }
  54. output := []Actor{}
  55. for _, el := range attributions {
  56. switch narrowed := el.(type) {
  57. case shared.JSON:
  58. source, err := p.Identifier()
  59. if err != nil { continue }
  60. resolved, err := Create(narrowed, source)
  61. if err != nil { continue }
  62. actor, isActor := resolved.(Actor)
  63. if !isActor { continue }
  64. output = append(output, actor)
  65. case string:
  66. url, err := url.Parse(narrowed)
  67. if err != nil { continue }
  68. response, err := request.Fetch(url)
  69. if err != nil { continue }
  70. // this step will be implicit after merge
  71. structured, err := Create(response, url)
  72. if err != nil { continue }
  73. actor, isActor := structured.(Actor)
  74. if !isActor { continue }
  75. output = append(output, actor)
  76. default: continue
  77. }
  78. }
  79. return output
  80. }
  81. func (p Post) String() string {
  82. output := ""
  83. if title, err := p.Title(); err == nil {
  84. output += style.Bold(title)
  85. output += "\n"
  86. }
  87. if body, err := p.BodyPreview(); err == nil {
  88. output += body
  89. output += "\n"
  90. }
  91. if created, err := p.Created(); err == nil {
  92. output += time.Now().Sub(created).String()
  93. }
  94. if creators := p.Creators(); len(creators) != 0 {
  95. output += " "
  96. for _, creator := range creators {
  97. if name, err := creator.InlineName(); err == nil {
  98. output += style.Bold(name) + ", "
  99. }
  100. }
  101. }
  102. return strings.TrimSpace(output)
  103. }