actor.go 4.7 KB

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