Browse Source

add keys to access creators, recipients of posts and actors of activities

Benton Edmondson 1 year ago
parent
commit
e910cd4414
6 changed files with 80 additions and 22 deletions
  1. 15 0
      pub/activity.go
  2. 1 1
      pub/actor.go
  3. 4 9
      pub/common.go
  4. 1 0
      pub/interfaces.go
  5. 17 2
      pub/post.go
  6. 42 10
      ui/ui.go

+ 15 - 0
pub/activity.go

@@ -125,3 +125,18 @@ func (a *Activity) Timestamp() time.Time {
 	}
 	return a.created
 }
+
+func (a *Activity) Name() string {
+	return a.target.Name()
+}
+
+func (a *Activity) Actor() Tangible {
+	if a.actorErr != nil {
+		return NewFailure(a.actorErr)
+	}
+	return a.actor
+}
+
+func (a *Activity) Target() Tangible {
+	return a.target
+}

+ 1 - 1
pub/actor.go

@@ -196,7 +196,7 @@ func (a *Actor) String(width int) string {
 	return output
 }
 
-func (a Actor) Preview(width int) string {
+func (a *Actor) Preview(width int) string {
 	output := a.header(width)
 
 	// TODO this needs to be truncated

+ 4 - 9
pub/common.go

@@ -13,20 +13,15 @@ var (
 	ErrWrongType = errors.New("item is the wrong type")
 )
 
-type TangibleWithName interface {
-	Tangible
-	Name() string
-}
-
-func getActors(o object.Object, key string, source *url.URL) []TangibleWithName {
+func getActors(o object.Object, key string, source *url.URL) []Tangible {
 	list, err := o.GetList(key)
 	if errors.Is(err, object.ErrKeyNotPresent) {
-		return []TangibleWithName{}
+		return []Tangible{}
 	} else if err != nil {
-		return []TangibleWithName{NewFailure(err)}
+		return []Tangible{NewFailure(err)}
 	}
 
-	output := make([]TangibleWithName, len(list))
+	output := make([]Tangible, len(list))
 	var wg sync.WaitGroup
 	for i := range list {
 		wg.Add(1)

+ 1 - 0
pub/interfaces.go

@@ -14,6 +14,7 @@ type Tangible interface {
 	Parents(quantity uint) ([]Tangible, Tangible)
 	Children() Container
 	Timestamp() time.Time
+	Name() string
 }
 
 type Container interface {

+ 17 - 2
pub/post.go

@@ -40,8 +40,8 @@ type Post struct {
 	attachments    []*Link
 	attachmentsErr error
 
-	creators    []TangibleWithName
-	recipients  []TangibleWithName
+	creators    []Tangible
+	recipients  []Tangible
 	comments    *Collection
 	commentsErr error
 }
@@ -278,3 +278,18 @@ func (p *Post) Timestamp() time.Time {
 		return p.created
 	}
 }
+
+func (p *Post) Name() string {
+	if p.titleErr != nil {
+		return style.Problem(p.titleErr)
+	}
+	return p.title
+}
+
+func (p *Post) Creators() []Tangible {
+	return p.creators
+}
+
+func (p *Post) Recipients() []Tangible {
+	return p.recipients
+}

+ 42 - 10
ui/ui.go

@@ -96,35 +96,67 @@ func (s *State) Update(input byte) {
 		if s.h.Current().feed.Contains(s.h.Current().index - 1) {
 			s.h.Current().index -= 1
 		}
-		s.output(s.view())
 		s.loadSurroundings()
 	case 'j': // down
 		if s.h.Current().feed.Contains(s.h.Current().index + 1) {
 			s.h.Current().index += 1
 		}
-		s.output(s.view())
 		s.loadSurroundings()
 	case 'g': // return to OP
 		if s.h.Current().feed.Contains(0) {
 			s.h.Current().index = 0
 		}
-		s.output(s.view())
 	case 'h': // back in history
 		s.h.Back()
-		s.output(s.view())
-	case 'l':
+	case 'l': // forward in history
 		s.h.Forward()
-		s.output(s.view())
 	case ' ': // select
 		s.switchTo(s.h.Current().feed.Get(s.h.Current().index))
-		s.output(s.view())
+	case 'c': // get creator of post
+		unwrapped := s.h.Current().feed.Get(s.h.Current().index)
+		if activity, ok := unwrapped.(*pub.Activity); ok {
+			unwrapped = activity.Target()
+		}
+		if post, ok := unwrapped.(*pub.Post); ok {
+			creators := post.Creators()
+			s.switchTo(creators)
+		}
+	case 'r': // get recipient of post
+		unwrapped := s.h.Current().feed.Get(s.h.Current().index)
+		if activity, ok := unwrapped.(*pub.Activity); ok {
+			unwrapped = activity.Target()
+		}
+		if post, ok := unwrapped.(*pub.Post); ok {
+			recipients := post.Recipients()
+			s.switchTo(recipients)
+		}
+	case 'a': // get actor of activity
+		if activity, ok := s.h.Current().feed.Get(s.h.Current().index).(*pub.Activity); ok {
+			actor := activity.Actor()
+			s.switchTo(actor)
+		}
 	}
-	// TODO: the catchall down here will be to look at s.feed.Get(s.index).References()
-	// for urls to switch to
+	s.output(s.view())
 }
 
-func (s *State) switchTo(item pub.Any) {
+func (s *State) switchTo(item any) {
 	switch narrowed := item.(type) {
+	case []pub.Tangible:
+		if len(narrowed) == 0 {
+			return
+		}
+		if len(narrowed) == 1 {
+			s.h.Add(&Page{
+				feed: feed.Create(narrowed[0]),
+				children: narrowed[0].Children(),
+				frontier: narrowed[0],
+			})
+		} else {
+			s.h.Add(&Page{
+				feed: feed.CreateAndAppend(narrowed),
+				index: 1,
+			})
+		}
 	case pub.Tangible:
 		s.h.Add(&Page{
 			feed: feed.Create(narrowed),