activity.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package apmodels
  2. import (
  3. "net/url"
  4. "time"
  5. "github.com/go-fed/activity/streams"
  6. "github.com/go-fed/activity/streams/vocab"
  7. "github.com/owncast/owncast/core/data"
  8. )
  9. // PrivacyAudience represents the audience for an activity.
  10. type PrivacyAudience = string
  11. const (
  12. // PUBLIC is an audience meaning anybody can view the item.
  13. PUBLIC PrivacyAudience = "https://www.w3.org/ns/activitystreams#Public"
  14. )
  15. // MakeNotePublic ses the required proeprties to make this note seen as public.
  16. func MakeNotePublic(note vocab.ActivityStreamsNote) vocab.ActivityStreamsNote {
  17. public, _ := url.Parse(PUBLIC)
  18. to := streams.NewActivityStreamsToProperty()
  19. to.AppendIRI(public)
  20. note.SetActivityStreamsTo(to)
  21. audience := streams.NewActivityStreamsAudienceProperty()
  22. audience.AppendIRI(public)
  23. note.SetActivityStreamsAudience(audience)
  24. return note
  25. }
  26. // MakeNoteDirect sets the required properties to make this note seen as a
  27. // direct message.
  28. func MakeNoteDirect(note vocab.ActivityStreamsNote, toIRI *url.URL) vocab.ActivityStreamsNote {
  29. to := streams.NewActivityStreamsCcProperty()
  30. to.AppendIRI(toIRI)
  31. to.AppendIRI(toIRI)
  32. note.SetActivityStreamsCc(to)
  33. // Mastodon requires a tag with a type of "mention" and href of the account
  34. // for a message to be a "Direct Message".
  35. tagProperty := streams.NewActivityStreamsTagProperty()
  36. tag := streams.NewTootHashtag()
  37. tagTypeProperty := streams.NewJSONLDTypeProperty()
  38. tagTypeProperty.AppendXMLSchemaString("Mention")
  39. tag.SetJSONLDType(tagTypeProperty)
  40. tagHrefProperty := streams.NewActivityStreamsHrefProperty()
  41. tagHrefProperty.Set(toIRI)
  42. tag.SetActivityStreamsHref(tagHrefProperty)
  43. tagProperty.AppendTootHashtag(tag)
  44. tagProperty.AppendTootHashtag(tag)
  45. note.SetActivityStreamsTag(tagProperty)
  46. return note
  47. }
  48. // MakeActivityDirect sets the required properties to make this activity seen
  49. // as a direct message.
  50. func MakeActivityDirect(activity vocab.ActivityStreamsCreate, toIRI *url.URL) vocab.ActivityStreamsCreate {
  51. to := streams.NewActivityStreamsCcProperty()
  52. to.AppendIRI(toIRI)
  53. to.AppendIRI(toIRI)
  54. activity.SetActivityStreamsCc(to)
  55. // Mastodon requires a tag with a type of "mention" and href of the account
  56. // for a message to be a "Direct Message".
  57. tagProperty := streams.NewActivityStreamsTagProperty()
  58. tag := streams.NewTootHashtag()
  59. tagTypeProperty := streams.NewJSONLDTypeProperty()
  60. tagTypeProperty.AppendXMLSchemaString("Mention")
  61. tag.SetJSONLDType(tagTypeProperty)
  62. tagHrefProperty := streams.NewActivityStreamsHrefProperty()
  63. tagHrefProperty.Set(toIRI)
  64. tag.SetActivityStreamsHref(tagHrefProperty)
  65. tagProperty.AppendTootHashtag(tag)
  66. tagProperty.AppendTootHashtag(tag)
  67. activity.SetActivityStreamsTag(tagProperty)
  68. return activity
  69. }
  70. // MakeActivityPublic sets the required properties to make this activity
  71. // seen as public.
  72. func MakeActivityPublic(activity vocab.ActivityStreamsCreate) vocab.ActivityStreamsCreate {
  73. // TO the public if we're not treating ActivityPub as "private".
  74. if !data.GetFederationIsPrivate() {
  75. public, _ := url.Parse(PUBLIC)
  76. to := streams.NewActivityStreamsToProperty()
  77. to.AppendIRI(public)
  78. activity.SetActivityStreamsTo(to)
  79. audience := streams.NewActivityStreamsAudienceProperty()
  80. audience.AppendIRI(public)
  81. activity.SetActivityStreamsAudience(audience)
  82. }
  83. return activity
  84. }
  85. // MakeCreateActivity will return a new Create activity with the provided ID.
  86. func MakeCreateActivity(activityID *url.URL) vocab.ActivityStreamsCreate {
  87. activity := streams.NewActivityStreamsCreate()
  88. id := streams.NewJSONLDIdProperty()
  89. id.Set(activityID)
  90. activity.SetJSONLDId(id)
  91. return activity
  92. }
  93. // MakeUpdateActivity will return a new Update activity with the provided aID.
  94. func MakeUpdateActivity(activityID *url.URL) vocab.ActivityStreamsUpdate {
  95. activity := streams.NewActivityStreamsUpdate()
  96. id := streams.NewJSONLDIdProperty()
  97. id.Set(activityID)
  98. activity.SetJSONLDId(id)
  99. // CC the public if we're not treating ActivityPub as "private".
  100. if !data.GetFederationIsPrivate() {
  101. public, _ := url.Parse(PUBLIC)
  102. cc := streams.NewActivityStreamsCcProperty()
  103. cc.AppendIRI(public)
  104. activity.SetActivityStreamsCc(cc)
  105. }
  106. return activity
  107. }
  108. // MakeNote will return a new Note object.
  109. func MakeNote(text string, noteIRI *url.URL, attributedToIRI *url.URL) vocab.ActivityStreamsNote {
  110. note := streams.NewActivityStreamsNote()
  111. content := streams.NewActivityStreamsContentProperty()
  112. content.AppendXMLSchemaString(text)
  113. note.SetActivityStreamsContent(content)
  114. id := streams.NewJSONLDIdProperty()
  115. id.Set(noteIRI)
  116. note.SetJSONLDId(id)
  117. published := streams.NewActivityStreamsPublishedProperty()
  118. published.Set(time.Now())
  119. note.SetActivityStreamsPublished(published)
  120. attributedTo := attributedToIRI
  121. attr := streams.NewActivityStreamsAttributedToProperty()
  122. attr.AppendIRI(attributedTo)
  123. note.SetActivityStreamsAttributedTo(attr)
  124. return note
  125. }