federationrepository.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. package federationrepository
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "net/url"
  7. "time"
  8. "github.com/go-fed/activity/streams"
  9. "github.com/go-fed/activity/streams/vocab"
  10. "github.com/owncast/owncast/models"
  11. "github.com/owncast/owncast/services/apfederation/apmodels"
  12. "github.com/owncast/owncast/services/apfederation/resolvers"
  13. "github.com/owncast/owncast/storage/data"
  14. "github.com/owncast/owncast/storage/sqlstorage"
  15. "github.com/owncast/owncast/utils"
  16. "github.com/pkg/errors"
  17. log "github.com/sirupsen/logrus"
  18. )
  19. type FederationRepository struct {
  20. datastore *data.Store
  21. }
  22. func New(datastore *data.Store) *FederationRepository {
  23. r := &FederationRepository{
  24. datastore: datastore,
  25. }
  26. return r
  27. }
  28. // NOTE: This is temporary during the transition period.
  29. var temporaryGlobalInstance *FederationRepository
  30. // GetUserRepository will return the user repository.
  31. func Get() *FederationRepository {
  32. if temporaryGlobalInstance == nil {
  33. i := New(data.GetDatastore())
  34. temporaryGlobalInstance = i
  35. }
  36. return temporaryGlobalInstance
  37. }
  38. // GetFollowerCount will return the number of followers we're keeping track of.
  39. func (f *FederationRepository) GetFollowerCount() (int64, error) {
  40. ctx := context.Background()
  41. return f.datastore.GetQueries().GetFollowerCount(ctx)
  42. }
  43. // GetFederationFollowers will return a slice of the followers we keep track of locally.
  44. func (f *FederationRepository) GetFederationFollowers(limit int, offset int) ([]models.Follower, int, error) {
  45. ctx := context.Background()
  46. total, err := f.datastore.GetQueries().GetFollowerCount(ctx)
  47. if err != nil {
  48. return nil, 0, errors.Wrap(err, "unable to fetch total number of followers")
  49. }
  50. followersResult, err := f.datastore.GetQueries().GetFederationFollowersWithOffset(ctx, sqlstorage.GetFederationFollowersWithOffsetParams{
  51. Limit: int32(limit),
  52. Offset: int32(offset),
  53. })
  54. if err != nil {
  55. return nil, 0, err
  56. }
  57. followers := make([]models.Follower, 0)
  58. for _, row := range followersResult {
  59. singleFollower := models.Follower{
  60. Name: row.Name.String,
  61. Username: row.Username,
  62. Image: row.Image.String,
  63. ActorIRI: row.Iri,
  64. Inbox: row.Inbox,
  65. Timestamp: utils.NullTime(row.CreatedAt),
  66. }
  67. followers = append(followers, singleFollower)
  68. }
  69. return followers, int(total), nil
  70. }
  71. // GetPendingFollowRequests will return pending follow requests.
  72. func (f *FederationRepository) GetPendingFollowRequests() ([]models.Follower, error) {
  73. pendingFollowersResult, err := f.datastore.GetQueries().GetFederationFollowerApprovalRequests(context.Background())
  74. if err != nil {
  75. return nil, err
  76. }
  77. followers := make([]models.Follower, 0)
  78. for _, row := range pendingFollowersResult {
  79. singleFollower := models.Follower{
  80. Name: row.Name.String,
  81. Username: row.Username,
  82. Image: row.Image.String,
  83. ActorIRI: row.Iri,
  84. Inbox: row.Inbox,
  85. Timestamp: utils.NullTime{Time: row.CreatedAt.Time, Valid: true},
  86. }
  87. followers = append(followers, singleFollower)
  88. }
  89. return followers, nil
  90. }
  91. // GetBlockedAndRejectedFollowers will return blocked and rejected followers.
  92. func (f *FederationRepository) GetBlockedAndRejectedFollowers() ([]models.Follower, error) {
  93. pendingFollowersResult, err := f.datastore.GetQueries().GetRejectedAndBlockedFollowers(context.Background())
  94. if err != nil {
  95. return nil, err
  96. }
  97. followers := make([]models.Follower, 0)
  98. for _, row := range pendingFollowersResult {
  99. singleFollower := models.Follower{
  100. Name: row.Name.String,
  101. Username: row.Username,
  102. Image: row.Image.String,
  103. ActorIRI: row.Iri,
  104. DisabledAt: utils.NullTime{Time: row.DisabledAt.Time, Valid: true},
  105. Timestamp: utils.NullTime{Time: row.CreatedAt.Time, Valid: true},
  106. }
  107. followers = append(followers, singleFollower)
  108. }
  109. return followers, nil
  110. }
  111. // AddFollow will save a follow to the datastore.
  112. func (f *FederationRepository) AddFollow(follow apmodels.ActivityPubActor, approved bool) error {
  113. log.Traceln("Saving", follow.ActorIri, "as a follower.")
  114. var image string
  115. if follow.Image != nil {
  116. image = follow.Image.String()
  117. }
  118. followRequestObject, err := apmodels.Serialize(follow.RequestObject)
  119. if err != nil {
  120. return errors.Wrap(err, "error serializing follow request object")
  121. }
  122. return f.createFollow(follow.ActorIri.String(), follow.Inbox.String(), follow.FollowRequestIri.String(), follow.Name, follow.Username, image, followRequestObject, approved)
  123. }
  124. // RemoveFollow will remove a follow from the datastore.
  125. func (f *FederationRepository) RemoveFollow(unfollow apmodels.ActivityPubActor) error {
  126. log.Traceln("Removing", unfollow.ActorIri, "as a follower.")
  127. return f.removeFollow(unfollow.ActorIri)
  128. }
  129. // GetFollower will return a single follower/request given an IRI.
  130. func (f *FederationRepository) GetFollower(iri string) (*apmodels.ActivityPubActor, error) {
  131. result, err := f.datastore.GetQueries().GetFollowerByIRI(context.Background(), iri)
  132. if err != nil {
  133. return nil, err
  134. }
  135. followIRI, err := url.Parse(result.Request)
  136. if err != nil {
  137. return nil, errors.Wrap(err, "error parsing follow request IRI")
  138. }
  139. iriURL, err := url.Parse(result.Iri)
  140. if err != nil {
  141. return nil, errors.Wrap(err, "error parsing actor IRI")
  142. }
  143. inbox, err := url.Parse(result.Inbox)
  144. if err != nil {
  145. return nil, errors.Wrap(err, "error parsing acting inbox")
  146. }
  147. image, _ := url.Parse(result.Image.String)
  148. var disabledAt *time.Time
  149. if result.DisabledAt.Valid {
  150. disabledAt = &result.DisabledAt.Time
  151. }
  152. follower := apmodels.ActivityPubActor{
  153. ActorIri: iriURL,
  154. Inbox: inbox,
  155. Name: result.Name.String,
  156. Username: result.Username,
  157. Image: image,
  158. FollowRequestIri: followIRI,
  159. DisabledAt: disabledAt,
  160. }
  161. return &follower, nil
  162. }
  163. // ApprovePreviousFollowRequest will approve a follow request.
  164. func (f *FederationRepository) ApprovePreviousFollowRequest(iri string) error {
  165. return f.datastore.GetQueries().ApproveFederationFollower(context.Background(), sqlstorage.ApproveFederationFollowerParams{
  166. Iri: iri,
  167. ApprovedAt: sql.NullTime{
  168. Time: time.Now(),
  169. Valid: true,
  170. },
  171. })
  172. }
  173. // BlockOrRejectFollower will block an existing follower or reject a follow request.
  174. func (f *FederationRepository) BlockOrRejectFollower(iri string) error {
  175. return f.datastore.GetQueries().RejectFederationFollower(context.Background(), sqlstorage.RejectFederationFollowerParams{
  176. Iri: iri,
  177. DisabledAt: sql.NullTime{
  178. Time: time.Now(),
  179. Valid: true,
  180. },
  181. })
  182. }
  183. func (f *FederationRepository) createFollow(actor, inbox, request, name, username, image string, requestObject []byte, approved bool) error {
  184. tx, err := f.datastore.DB.Begin()
  185. if err != nil {
  186. log.Debugln(err)
  187. }
  188. defer func() {
  189. _ = tx.Rollback()
  190. }()
  191. var approvedAt sql.NullTime
  192. if approved {
  193. approvedAt = sql.NullTime{
  194. Time: time.Now(),
  195. Valid: true,
  196. }
  197. }
  198. if err = f.datastore.GetQueries().WithTx(tx).AddFollower(context.Background(), sqlstorage.AddFollowerParams{
  199. Iri: actor,
  200. Inbox: inbox,
  201. Name: sql.NullString{String: name, Valid: true},
  202. Username: username,
  203. Image: sql.NullString{String: image, Valid: true},
  204. ApprovedAt: approvedAt,
  205. Request: request,
  206. RequestObject: requestObject,
  207. }); err != nil {
  208. log.Errorln("error creating new federation follow: ", err)
  209. }
  210. return tx.Commit()
  211. }
  212. // UpdateFollower will update the details of a stored follower given an IRI.
  213. func (f *FederationRepository) UpdateFollower(actorIRI string, inbox string, name string, username string, image string) error {
  214. f.datastore.DbLock.Lock()
  215. defer f.datastore.DbLock.Unlock()
  216. tx, err := f.datastore.DB.Begin()
  217. if err != nil {
  218. log.Debugln(err)
  219. }
  220. defer func() {
  221. _ = tx.Rollback()
  222. }()
  223. if err = f.datastore.GetQueries().WithTx(tx).UpdateFollowerByIRI(context.Background(), sqlstorage.UpdateFollowerByIRIParams{
  224. Inbox: inbox,
  225. Name: sql.NullString{String: name, Valid: true},
  226. Username: username,
  227. Image: sql.NullString{String: image, Valid: true},
  228. Iri: actorIRI,
  229. }); err != nil {
  230. return fmt.Errorf("error updating follower %s %s", actorIRI, err)
  231. }
  232. return tx.Commit()
  233. }
  234. func (f *FederationRepository) removeFollow(actor *url.URL) error {
  235. f.datastore.DbLock.Lock()
  236. defer f.datastore.DbLock.Unlock()
  237. tx, err := f.datastore.DB.Begin()
  238. if err != nil {
  239. return err
  240. }
  241. defer func() {
  242. _ = tx.Rollback()
  243. }()
  244. if err := f.datastore.GetQueries().WithTx(tx).RemoveFollowerByIRI(context.Background(), actor.String()); err != nil {
  245. return err
  246. }
  247. return tx.Commit()
  248. }
  249. // GetOutboxPostCount will return the number of posts in the outbox.
  250. func (f *FederationRepository) GetOutboxPostCount() (int64, error) {
  251. ctx := context.Background()
  252. return f.datastore.GetQueries().GetLocalPostCount(ctx)
  253. }
  254. // GetOutbox will return an instance of the outbox populated by stored items.
  255. func (f *FederationRepository) GetOutbox(limit int, offset int) (vocab.ActivityStreamsOrderedCollection, error) {
  256. collection := streams.NewActivityStreamsOrderedCollection()
  257. orderedItems := streams.NewActivityStreamsOrderedItemsProperty()
  258. r := resolvers.Get()
  259. rows, err := f.datastore.GetQueries().GetOutboxWithOffset(
  260. context.Background(),
  261. sqlstorage.GetOutboxWithOffsetParams{Limit: int32(limit), Offset: int32(offset)},
  262. )
  263. if err != nil {
  264. return collection, err
  265. }
  266. for _, value := range rows {
  267. createCallback := func(c context.Context, activity vocab.ActivityStreamsCreate) error {
  268. orderedItems.AppendActivityStreamsCreate(activity)
  269. return nil
  270. }
  271. if err := r.Resolve(context.Background(), value, createCallback); err != nil {
  272. return collection, err
  273. }
  274. }
  275. return collection, nil
  276. }
  277. // AddToOutbox will store a single payload to the persistence layer.
  278. func (f *FederationRepository) AddToOutbox(iri string, itemData []byte, typeString string, isLiveNotification bool) error {
  279. tx, err := f.datastore.DB.Begin()
  280. if err != nil {
  281. log.Debugln(err)
  282. }
  283. defer func() {
  284. _ = tx.Rollback()
  285. }()
  286. if err = f.datastore.GetQueries().WithTx(tx).AddToOutbox(context.Background(), sqlstorage.AddToOutboxParams{
  287. Iri: iri,
  288. Value: itemData,
  289. Type: typeString,
  290. LiveNotification: sql.NullBool{Bool: isLiveNotification, Valid: true},
  291. }); err != nil {
  292. return fmt.Errorf("error creating new item in federation outbox %s", err)
  293. }
  294. return tx.Commit()
  295. }
  296. // GetObjectByIRI will return a string representation of a single object by the IRI.
  297. func (f *FederationRepository) GetObjectByIRI(iri string) (string, bool, time.Time, error) {
  298. row, err := f.datastore.GetQueries().GetObjectFromOutboxByIRI(context.Background(), iri)
  299. return string(row.Value), row.LiveNotification.Bool, row.CreatedAt.Time, err
  300. }
  301. // GetLocalPostCount will return the number of posts existing locally.
  302. func (f *FederationRepository) GetLocalPostCount() (int64, error) {
  303. ctx := context.Background()
  304. return f.datastore.GetQueries().GetLocalPostCount(ctx)
  305. }
  306. // SaveInboundFediverseActivity will save an event to the ap_inbound_activities table.
  307. func (f *FederationRepository) SaveInboundFediverseActivity(objectIRI string, actorIRI string, eventType string, timestamp time.Time) error {
  308. if err := f.datastore.GetQueries().AddToAcceptedActivities(context.Background(), sqlstorage.AddToAcceptedActivitiesParams{
  309. Iri: objectIRI,
  310. Actor: actorIRI,
  311. Type: eventType,
  312. Timestamp: timestamp,
  313. }); err != nil {
  314. return errors.Wrap(err, "error saving event "+objectIRI)
  315. }
  316. return nil
  317. }
  318. // GetInboundActivities will return a collection of saved, federated activities
  319. // limited and offset by the values provided to support pagination.
  320. func (f *FederationRepository) GetInboundActivities(limit int, offset int) ([]models.FederatedActivity, int, error) {
  321. ctx := context.Background()
  322. rows, err := f.datastore.GetQueries().GetInboundActivitiesWithOffset(ctx, sqlstorage.GetInboundActivitiesWithOffsetParams{
  323. Limit: int32(limit),
  324. Offset: int32(offset),
  325. })
  326. if err != nil {
  327. return nil, 0, err
  328. }
  329. activities := make([]models.FederatedActivity, 0)
  330. total, err := f.datastore.GetQueries().GetInboundActivityCount(context.Background())
  331. if err != nil {
  332. return nil, 0, errors.Wrap(err, "unable to fetch total activity count")
  333. }
  334. for _, row := range rows {
  335. singleActivity := models.FederatedActivity{
  336. IRI: row.Iri,
  337. ActorIRI: row.Actor,
  338. Type: row.Type,
  339. Timestamp: row.Timestamp,
  340. }
  341. activities = append(activities, singleActivity)
  342. }
  343. return activities, int(total), nil
  344. }
  345. // HasPreviouslyHandledInboundActivity will return if we have previously handled
  346. // an inbound federated activity.
  347. func (f *FederationRepository) HasPreviouslyHandledInboundActivity(iri string, actorIRI string, eventType string) (bool, error) {
  348. exists, err := f.datastore.GetQueries().DoesInboundActivityExist(context.Background(), sqlstorage.DoesInboundActivityExistParams{
  349. Iri: iri,
  350. Actor: actorIRI,
  351. Type: eventType,
  352. })
  353. if err != nil {
  354. return false, err
  355. }
  356. return exists > 0, nil
  357. }