actor.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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, func(input any, source *url.URL) Tangible {
  60. activity, err := NewActivity(input, source)
  61. if err != nil {
  62. return NewFailure(err)
  63. }
  64. if id == nil {
  65. return NewFailure(errors.New("activity was performed by a different actor (this actor has no identifier)"))
  66. }
  67. if activity.ActorIdentifier() == nil || activity.ActorIdentifier().String() != id.String() {
  68. return NewFailure(errors.New("activity was performed by a different actor"))
  69. }
  70. return activity
  71. })
  72. return a, nil
  73. }
  74. func (a *Actor) Parents(quantity uint) ([]Tangible, Tangible) {
  75. return []Tangible{}, nil
  76. }
  77. func (a *Actor) Children() Container {
  78. /* the if is necessary because my understanding is
  79. the first nil is a (*Collection)(nil) whereas
  80. the second is (Container)(nil) */
  81. if a.posts == nil {
  82. return nil
  83. } else {
  84. return a.posts
  85. }
  86. }
  87. func (a *Actor) Name() string {
  88. var output string
  89. if a.nameErr == nil {
  90. output = a.name
  91. } else if !errors.Is(a.nameErr, object.ErrKeyNotPresent) {
  92. output = style.Problem(a.nameErr)
  93. }
  94. if a.id != nil && !errors.Is(a.handleErr, object.ErrKeyNotPresent) {
  95. if output != "" {
  96. output += " "
  97. }
  98. if a.handleErr != nil {
  99. output += style.Problem(a.handleErr)
  100. } else {
  101. output += style.Italic("@" + a.handle + "@" + a.id.Host)
  102. }
  103. }
  104. if a.kind != "Person" {
  105. if output != "" {
  106. output += " "
  107. }
  108. output += "(" + strings.ToLower(a.kind) + ")"
  109. } else if output == "" {
  110. output = strings.ToLower(a.kind)
  111. }
  112. return style.Color(output)
  113. }
  114. func (a *Actor) header(width int) string {
  115. output := a.Name()
  116. if errors.Is(a.joinedErr, object.ErrKeyNotPresent) {
  117. // omit it
  118. } else if a.joinedErr != nil {
  119. output += "\njoined " + style.Problem(a.joinedErr)
  120. } else {
  121. output += "\njoined " + style.Color(a.joined.Format("2 Jan 2006"))
  122. }
  123. return ansi.Wrap(output, width)
  124. }
  125. func (a *Actor) center(width int) (string, bool) {
  126. if errors.Is(a.bioErr, object.ErrKeyNotPresent) {
  127. return "", false
  128. }
  129. if a.bioErr != nil {
  130. return ansi.Wrap(style.Problem(a.bioErr), width), true
  131. }
  132. rendered := a.bio.Render(width)
  133. return rendered, true
  134. }
  135. func (a *Actor) footer(width int) (string, bool) {
  136. if a.postsErr != nil {
  137. return style.Problem(a.postsErr), true
  138. } else if quantity, err := a.posts.Size(); errors.Is(err, object.ErrKeyNotPresent) {
  139. return "", false
  140. } else if err != nil {
  141. return style.Problem(err), true
  142. } else if quantity == 1 {
  143. return style.Color(fmt.Sprintf("%d post", quantity)), true
  144. } else {
  145. return style.Color(fmt.Sprintf("%d posts", quantity)), true
  146. }
  147. }
  148. func (a *Actor) String(width int) string {
  149. output := a.header(width)
  150. body, bodyPresent := a.center(width - 4)
  151. if bodyPresent {
  152. output += "\n\n" + ansi.Indent(body, " ", true)
  153. }
  154. if footer, present := a.footer(width); present {
  155. if bodyPresent {
  156. output += "\n"
  157. }
  158. output += "\n" + footer
  159. }
  160. return output
  161. }
  162. func (a *Actor) Preview(width int) string {
  163. output := a.header(width)
  164. if body, present := a.center(width); present {
  165. output += "\n" + ansi.Snip(body, width, 4, style.Color("\u2026"))
  166. }
  167. if footer, present := a.footer(width); present {
  168. output += "\n" + footer
  169. }
  170. return output
  171. }
  172. func (a *Actor) Timestamp() time.Time {
  173. if a.joinedErr != nil {
  174. return time.Time{}
  175. } else {
  176. return a.joined
  177. }
  178. }
  179. func (a *Actor) Identifier() *url.URL {
  180. return a.id
  181. }
  182. func (a *Actor) Banner() (string, *mime.MediaType, bool) {
  183. if a.bannerErr != nil {
  184. return "", nil, false
  185. }
  186. return a.banner.SelectWithDefaultMediaType(mime.UnknownSubtype("image"))
  187. }
  188. func (a *Actor) ProfilePic() (string, *mime.MediaType, bool) {
  189. if a.pfpErr != nil {
  190. return "", nil, false
  191. }
  192. return a.pfp.SelectWithDefaultMediaType(mime.UnknownSubtype("image"))
  193. }
  194. func (a *Actor) SelectLink(input int) (string, *mime.MediaType, bool) {
  195. input -= 1
  196. if len(a.bioLinks) <= input {
  197. return "", nil, false
  198. }
  199. return a.bioLinks[input], mime.Unknown(), true
  200. }