actor.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package pub
  2. import (
  3. "net/url"
  4. "mimicry/style"
  5. "errors"
  6. "mimicry/object"
  7. "time"
  8. "mimicry/client"
  9. "golang.org/x/exp/slices"
  10. "fmt"
  11. "strings"
  12. "mimicry/ansi"
  13. "mimicry/mime"
  14. "mimicry/render"
  15. )
  16. type Actor struct {
  17. kind string
  18. name string; nameErr error
  19. handle string; handleErr error
  20. id *url.URL
  21. bio string; bioErr error
  22. mediaType *mime.MediaType; mediaTypeErr error
  23. joined time.Time; joinedErr error
  24. pfp *Link; pfpErr error
  25. banner *Link; bannerErr error
  26. posts *Collection; postsErr error
  27. }
  28. func NewActor(input any, source *url.URL) (*Actor, error) {
  29. o, id, err := client.FetchUnknown(input, source)
  30. if err != nil { return nil, err }
  31. return NewActorFromObject(o, id)
  32. }
  33. func NewActorFromObject(o object.Object, id *url.URL) (*Actor, error) {
  34. a := &Actor{}
  35. a.id = id
  36. var err error
  37. if a.kind, err = o.GetString("type"); err != nil {
  38. return nil, err
  39. }
  40. if !slices.Contains([]string{
  41. "Application", "Group", "Organization", "Person", "Service",
  42. }, a.kind) {
  43. return nil, fmt.Errorf("%w: %s is not an Actor", ErrWrongType, a.kind)
  44. }
  45. a.name, a.nameErr = o.GetNatural("name", "en")
  46. a.handle, a.handleErr = o.GetString("preferredUsername")
  47. a.bio, a.bioErr = o.GetNatural("summary", "en")
  48. if a.bio == "" {
  49. a.bioErr = object.ErrKeyNotPresent
  50. }
  51. a.mediaType, a.mediaTypeErr = o.GetMediaType("mediaType")
  52. a.joined, a.joinedErr = o.GetTime("published")
  53. a.pfp, a.pfpErr = getBestLink(o, "icon", "image")
  54. a.banner, a.bannerErr = getBestLink(o, "image", "image")
  55. a.posts, a.postsErr = getCollection(o, "outbox", a.id)
  56. return a, nil
  57. }
  58. func (a *Actor) Kind() string {
  59. return a.kind
  60. }
  61. func (a *Actor) Parents(quantity uint) []Tangible {
  62. return []Tangible{}
  63. }
  64. func (a *Actor) Children(quantity uint) ([]Tangible, Container, uint) {
  65. if errors.Is(a.postsErr, object.ErrKeyNotPresent) {
  66. return []Tangible{}, nil, 0
  67. }
  68. if a.postsErr != nil {
  69. return []Tangible{
  70. NewFailure(a.postsErr),
  71. }, nil, 0
  72. }
  73. return a.posts.Harvest(quantity, 0)
  74. }
  75. // TODO: here is where I'd put forgery errors in
  76. func (a *Actor) Name() string {
  77. var output string
  78. if a.nameErr == nil {
  79. output = a.name
  80. } else if !errors.Is(a.nameErr, object.ErrKeyNotPresent) {
  81. output = style.Problem(a.nameErr)
  82. }
  83. if a.id != nil && !errors.Is(a.handleErr, object.ErrKeyNotPresent) {
  84. if output != "" { output += " " }
  85. if a.handleErr != nil {
  86. output += style.Problem(a.handleErr)
  87. } else {
  88. output += style.Italic("@" + a.handle + "@" + a.id.Host)
  89. }
  90. }
  91. if a.kind != "Person" {
  92. if output != "" { output += " " }
  93. output += "(" + strings.ToLower(a.kind) + ")"
  94. } else if output == "" {
  95. output = strings.ToLower(a.kind)
  96. }
  97. return style.Color(output)
  98. }
  99. func (a *Actor) header(width int) string {
  100. output := a.Name()
  101. if errors.Is(a.joinedErr, object.ErrKeyNotPresent) {
  102. // omit it
  103. } else if a.joinedErr != nil {
  104. output += "\njoined " + style.Problem(a.joinedErr)
  105. } else {
  106. output += "\njoined " + style.Color(a.joined.Format("2 Jan 2006"))
  107. }
  108. return ansi.Wrap(output, width)
  109. }
  110. func (a *Actor) center(width int) (string, bool) {
  111. if errors.Is(a.bioErr, object.ErrKeyNotPresent) {
  112. return "", false
  113. }
  114. if a.bioErr != nil {
  115. return ansi.Wrap(style.Problem(a.bioErr), width), true
  116. }
  117. mediaType := a.mediaType
  118. if errors.Is(a.mediaTypeErr, object.ErrKeyNotPresent) {
  119. mediaType = mime.Default()
  120. } else if a.mediaTypeErr != nil {
  121. return ansi.Wrap(style.Problem(a.mediaTypeErr), width), true
  122. }
  123. rendered, err := render.Render(a.bio, mediaType.Essence, width)
  124. if err != nil {
  125. return style.Problem(err), true
  126. }
  127. return rendered, true
  128. }
  129. func (a *Actor) footer(width int) (string, bool) {
  130. if errors.Is(a.postsErr, object.ErrKeyNotPresent) {
  131. return style.Problem(a.postsErr), true
  132. } else if a.postsErr != nil {
  133. return "", false
  134. } else if quantity, err := a.posts.Size(); errors.Is(err, object.ErrKeyNotPresent) {
  135. return "", false
  136. } else if err != nil {
  137. return style.Problem(err), true
  138. } else if quantity == 1 {
  139. return style.Color(fmt.Sprintf("%d post", quantity)), true
  140. } else {
  141. return style.Color(fmt.Sprintf("%d posts", quantity)), true
  142. }
  143. }
  144. func (a *Actor) String(width int) string {
  145. output := a.header(width)
  146. if body, present := a.center(width - 4); present {
  147. output += "\n\n" + ansi.Indent(body, " ", true) + "\n"
  148. }
  149. if footer, present := a.footer(width); present {
  150. output += "\n" + footer
  151. }
  152. return output
  153. }
  154. func (a Actor) Preview(width int) string {
  155. output := a.header(width)
  156. // TODO this needs to be truncated
  157. if body, present := a.center(width); present {
  158. output += "\n" + ansi.Snip(body, width, 4, style.Color("\u2026"))
  159. }
  160. if footer, present := a.footer(width); present {
  161. output += "\n" + footer
  162. }
  163. return output
  164. }