Finish pub.database implementation
This commit is contained in:
parent
325448fadf
commit
fccc924403
1 changed files with 89 additions and 8 deletions
|
@ -128,7 +128,7 @@ func (m *myDB) Update(c context.Context,
|
||||||
func (m *myDB) Delete(c context.Context,
|
func (m *myDB) Delete(c context.Context,
|
||||||
id *url.URL) error {
|
id *url.URL) error {
|
||||||
// Remove a payload in our in-memory map.
|
// Remove a payload in our in-memory map.
|
||||||
m.Delete(c, id)
|
m.content.Delete(id.String())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,14 +154,16 @@ func (m *myDB) getOrderedCollectionPage(inboxIRI *url.URL) (vocab.ActivityStream
|
||||||
// to an edited OrderedCollectionPage. Implementation is left as an
|
// to an edited OrderedCollectionPage. Implementation is left as an
|
||||||
// exercise for the reader.
|
// exercise for the reader.
|
||||||
func (m *myDB) applyDiffOrderedCollection(storedInbox vocab.ActivityStreamsOrderedCollection, inbox vocab.ActivityStreamsOrderedCollectionPage) vocab.ActivityStreamsOrderedCollectionPage {
|
func (m *myDB) applyDiffOrderedCollection(storedInbox vocab.ActivityStreamsOrderedCollection, inbox vocab.ActivityStreamsOrderedCollectionPage) vocab.ActivityStreamsOrderedCollectionPage {
|
||||||
|
// TODO
|
||||||
|
return inbox
|
||||||
}
|
}
|
||||||
|
|
||||||
// saveToContent is a helper method to save an
|
// saveToContent is a helper method to save an
|
||||||
// ActivityStream type. Implementation is left as an exercise for the
|
// ActivityStream type. Implementation is left as an exercise for the
|
||||||
// reader.
|
// reader.
|
||||||
func (m *myDB) saveToContent(updatedInbox vocab.ActivityStreamsOrderedCollectionPage) error {
|
func (m *myDB) saveToContent(c context.Context, inbox vocab.ActivityStreamsOrderedCollectionPage) error {
|
||||||
|
m.Create(c, inbox)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *myDB) InboxContains(c context.Context,
|
func (m *myDB) InboxContains(c context.Context,
|
||||||
|
@ -236,7 +238,7 @@ func (m *myDB) SetInbox(c context.Context,
|
||||||
// saveToContent is a helper method to save an
|
// saveToContent is a helper method to save an
|
||||||
// ActivityStream type. Implementation is left as an exercise for the
|
// ActivityStream type. Implementation is left as an exercise for the
|
||||||
// reader.
|
// reader.
|
||||||
return m.saveToContent(updatedInbox)
|
return m.saveToContent(c, updatedInbox)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *myDB) GetOutbox(c context.Context,
|
func (m *myDB) GetOutbox(c context.Context,
|
||||||
|
@ -246,8 +248,30 @@ func (m *myDB) GetOutbox(c context.Context,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *myDB) SetOutbox(c context.Context,
|
func (m *myDB) SetOutbox(c context.Context,
|
||||||
inbox vocab.ActivityStreamsOrderedCollectionPage) error {
|
outbox vocab.ActivityStreamsOrderedCollectionPage) error {
|
||||||
// Similar to `SetInbox`, but for the outbox. See `SetInbox`.
|
// Similar to `SetInbox`, but for the outbox. See `SetInbox`.
|
||||||
|
|
||||||
|
// The goal here is to set an outbox at the specified IRI, with any
|
||||||
|
// changes to the page made persistent. Since the outbox has been Locked,
|
||||||
|
// it is OK to assume that no other concurrent goroutine has changed the
|
||||||
|
// outbox in the meantime.
|
||||||
|
|
||||||
|
// getOrderedCollection is a helper method to fetch an
|
||||||
|
// OrderedCollection. It is not implemented in this tutorial, and
|
||||||
|
// uses the map m.content to do the lookup.
|
||||||
|
storedOutbox, err := m.getOrderedCollection(outbox.GetJSONLDId().GetIRI())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// applyDiffOrderedCollection is a helper method to apply changes due
|
||||||
|
// to an edited OrderedCollectionPage. Implementation is left as an
|
||||||
|
// exercise for the reader.
|
||||||
|
updatedOutbox := m.applyDiffOrderedCollection(storedOutbox, outbox)
|
||||||
|
|
||||||
|
// saveToContent is a helper method to save an
|
||||||
|
// ActivityStream type. Implementation is left as an exercise for the
|
||||||
|
// reader.
|
||||||
|
return m.saveToContent(c, updatedOutbox)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *myDB) ActorForOutbox(c context.Context,
|
func (m *myDB) ActorForOutbox(c context.Context,
|
||||||
|
@ -285,6 +309,13 @@ func (m *myDB) NewID(c context.Context,
|
||||||
return t.GetJSONLDId().GetIRI(), nil
|
return t.GetJSONLDId().GetIRI(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getPerson is a helper method that returns an actor on this server
|
||||||
|
// with a Person ActivityStreams type. It is not implemented in this tutorial.
|
||||||
|
func (m *myDB) getPerson(actorIRI *url.URL) (person vocab.ActivityStreamsPerson, err error) {
|
||||||
|
val, _ := m.content.Load(actorIRI.String())
|
||||||
|
return val.(*content).data.(vocab.ActivityStreamsPerson), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *myDB) Followers(c context.Context,
|
func (m *myDB) Followers(c context.Context,
|
||||||
actorIRI *url.URL) (followers vocab.ActivityStreamsCollection, err error) {
|
actorIRI *url.URL) (followers vocab.ActivityStreamsCollection, err error) {
|
||||||
// Get the followers collection from the actor with `actorIRI`.
|
// Get the followers collection from the actor with `actorIRI`.
|
||||||
|
@ -316,15 +347,65 @@ func (m *myDB) Followers(c context.Context,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *myDB) Following(c context.Context,
|
func (m *myDB) Following(c context.Context,
|
||||||
actorIRI *url.URL) (followers vocab.ActivityStreamsCollection, err error) {
|
actorIRI *url.URL) (following vocab.ActivityStreamsCollection, err error) {
|
||||||
// Get the following collection from the actor with `actorIRI`.
|
// Get the following collection from the actor with `actorIRI`.
|
||||||
|
|
||||||
// Implementation is similar to `Followers`. See `Followers`.
|
// Implementation is similar to `Followers`. See `Followers`.
|
||||||
|
|
||||||
|
// getPerson is a helper method that returns an actor on this server
|
||||||
|
// with a Person ActivityStreams type. It is not implemented in this tutorial.
|
||||||
|
var person vocab.ActivityStreamsPerson
|
||||||
|
person, err = m.getPerson(actorIRI)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Let's get their following property, ensure it exists, and then
|
||||||
|
// fetch it with a familiar helper method.
|
||||||
|
f := person.GetActivityStreamsFollowing()
|
||||||
|
if f == nil {
|
||||||
|
err = errors.New("no following collection")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Note: at this point f is not the OrderedCollection itself yet. It is
|
||||||
|
// an opaque box (it could be an IRI, an OrderedCollection, or something
|
||||||
|
// extending an OrderedCollection).
|
||||||
|
followingId, err := pub.ToId(f)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val, err := m.getOrderedCollection(followingId)
|
||||||
|
following = val.(vocab.ActivityStreamsCollection)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *myDB) Liked(c context.Context,
|
func (m *myDB) Liked(c context.Context,
|
||||||
actorIRI *url.URL) (followers vocab.ActivityStreamsCollection, err error) {
|
actorIRI *url.URL) (liked vocab.ActivityStreamsCollection, err error) {
|
||||||
// Get the liked collection from the actor with `actorIRI`.
|
// Get the liked collection from the actor with `actorIRI`.
|
||||||
|
|
||||||
// Implementation is similar to `Followers`. See `Followers`.
|
// Implementation is similar to `Followers`. See `Followers`.
|
||||||
|
|
||||||
|
// getPerson is a helper method that returns an actor on this server
|
||||||
|
// with a Person ActivityStreams type. It is not implemented in this tutorial.
|
||||||
|
var person vocab.ActivityStreamsPerson
|
||||||
|
person, err = m.getPerson(actorIRI)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Let's get their liked property, ensure it exists, and then
|
||||||
|
// fetch it with a familiar helper method.
|
||||||
|
f := person.GetActivityStreamsLiked()
|
||||||
|
if f == nil {
|
||||||
|
err = errors.New("no liked collection")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Note: at this point f is not the OrderedCollection itself yet. It is
|
||||||
|
// an opaque box (it could be an IRI, an OrderedCollection, or something
|
||||||
|
// extending an OrderedCollection).
|
||||||
|
likedId, err := pub.ToId(f)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val, err := m.getOrderedCollection(likedId)
|
||||||
|
liked = val.(vocab.ActivityStreamsCollection)
|
||||||
|
return
|
||||||
}
|
}
|
Reference in a new issue