~mariusor/motley

822ea847cf1832399f3f57547cc24597909cfb68 — Marius Orcsik 5 months ago 83c23c3
Using plain tree model directly
3 files changed, 127 insertions(+), 20 deletions(-)

M fedbox.go
M go.mod
M ui.go
M fedbox.go => fedbox.go +118 -11
@@ 2,35 2,142 @@ package motley

import (
	pub "github.com/go-ap/activitypub"
	"github.com/go-ap/handlers"
	st "github.com/go-ap/storage"
	tree "github.com/mariusor/bubbles-tree"
	"github.com/openshift/osin"
)

type fedbox struct {
	iri pub.IRI
	tree map[pub.IRI]pub.Item
	iri  pub.IRI
	s    st.Store
	o    osin.Storage
}

func FedBOX(base pub.IRI, r st.Store, o osin.Storage) *fedbox {
	return &fedbox{tree: make(map[pub.IRI]pub.Item), iri: base, s: r, o: o}
}

func (f fedbox) getService() (pub.Item, error) {
	return f.s.Load(f.iri)
	col, err := f.s.Load(f.iri)
	if err != nil {
		return nil, err
	}
	var service pub.Item
	pub.OnObject(col, func(o *pub.Object) error {
		service = o
		return nil
	})
	return service, nil
}

func (f fedbox) Advance(to string) (tree.Treeish, error) {
func (f *fedbox) Advance(to string) (tree.Treeish, error) {
	f.iri = pub.IRI(to)
	return f, nil
}

func (f fedbox) State(what string) (tree.NodeState, error) {
	return 0, nil
func (f *fedbox) State(what string) (tree.NodeState, error) {
	var curNode pub.Item
	for iri, it := range f.tree {
		if iri.Equals(pub.IRI(what), false) {
			curNode = it
			break
		}
	}
	if curNode == nil {
		return 0, nil
	}
	var st tree.NodeState = tree.NodeVisible
	if pub.IsItemCollection(curNode) {
		st |= tree.NodeCollapsible
	}
	if _, col := handlers.Split(curNode.GetLink()); col != "" {
		st |= tree.NodeCollapsible
	}
	//fmt.Fprintf(os.Stderr, "%s state %d\n", what, st)
	return st, nil
}

func getObjectElements(ob pub.Object) []string {
	result := make([]string, 0)
	if ob.Likes != nil {
		result = append(result, ob.Likes.GetLink().String())
	}
	if ob.Shares != nil {
		result = append(result, ob.Shares.GetLink().String())
	}
	if ob.Replies != nil {
		result = append(result, ob.Replies.GetLink().String())
	}
	return result
}

func (f fedbox) Walk(depth int) ([]string, error) {
	result := []string{
		"ana",
		"are",
		"mere",
func getActorElements(act pub.Actor) []string {
	result := make([]string, 0)
	pub.OnObject(&act, func(o *pub.Object) error {
		result = append(result, getObjectElements(*o)...)
		return nil
	})
	result = append(result, act.Inbox.GetLink().String())
	result = append(result, act.Outbox.GetLink().String())
	if act.Liked != nil {
		result = append(result, act.Liked.GetLink().String())
	}
	return result, nil
	return result
}
func getItemElements(it pub.Item) []string {
	result := make([]string, 0)
	if pub.IsItemCollection(it) {
		pub.OnItemCollection(it, func(c *pub.ItemCollection) error {
			for _, it := range c.Collection() {
				result = append(getItemElements(it))
			}
			return nil
		})
	}
	if pub.ActorTypes.Contains(it.GetType()) {
		pub.OnActor(it, func(act *pub.Actor) error {
			result = append(result, getActorElements(*act)...)
			return nil
		})
	}
	if pub.ActivityTypes.Contains(it.GetType()) || pub.ObjectTypes.Contains(it.GetType()) {
		pub.OnObject(it, func(act *pub.Object) error {
			result = append(result, getObjectElements(*act)...)
			return nil
		})
	}
	return result
}

func (f *fedbox) Walk(depth int) ([]string, error) {
	//fmt.Fprintf(os.Stderr, "Walking %#v\n", *f)
	it, err := f.s.Load(f.iri)
	if err != nil {
		return nil, err
	}

	if it.IsCollection() {
		err = pub.OnCollectionIntf(it, func(col pub.CollectionInterface) error {
			for _, it := range col.Collection() {
				f.tree[it.GetLink()] = it
			}
			return nil
		})
		if err != nil {
			return nil, err
		}
	} else {
		f.tree[it.GetLink()] = it
	}
	//fmt.Fprintf(os.Stderr, "%#v\n", f.tree)
	var result []string
	for iri := range f.tree {
		result = append(result, iri.String())
		result = append(result, getItemElements(f.tree[iri])...)
	}

	//fmt.Fprintf(os.Stderr, "results %#v\n", result)
	return result, err
}

M go.mod => go.mod +1 -0
@@ 15,6 15,7 @@ require (
	github.com/go-ap/auth v0.0.0-20210403114547-c68fb3011ebd
	github.com/go-ap/errors v0.0.0-20210218123445-4466eca2f4ec
	github.com/go-ap/fedbox v0.0.0-20210415164852-b86be7df20e1
	github.com/go-ap/handlers v0.0.0-20210403114447-058e93c4936c // indirect
	github.com/go-ap/storage v0.0.0-20210403114403-99ae85c8cc72
	github.com/joho/godotenv v1.3.0
	github.com/mariusor/bubbles-tree v0.0.0-20210514170659-b437f1f8fd6a

M ui.go => ui.go +8 -9
@@ 124,9 124,13 @@ var (
	statusBarMessageHelpStyle      = newStyle(NewColorPair("#B6FFE4", "#B6FFE4"), Green, false)
	helpViewStyle                  = newStyle(statusBarNoteFg, NewColorPair("#1B1B1B", "#f2f2f2"), false)
)
//
//func Launch(base pub.IRI, r st.Store, o osin.Storage) error {
//	return tea.NewProgram(newModel(base, r, o)).Start()
//}

func Launch(base pub.IRI, r st.Store, o osin.Storage) error {
	return tea.NewProgram(newModel(base, r, o)).Start()
	return tea.NewProgram(tree.New(FedBOX(base, r, o))).Start()
}

func newModel(base pub.IRI, r st.Store, o osin.Storage) *model {


@@ 138,13 142,8 @@ func newModel(base pub.IRI, r st.Store, o osin.Storage) *model {
	m := new(model)
	m.commonModel = new(commonModel)

	f := fedbox{iri: base, s: r, o: o}
	m.f = f

	tree := tree.New(f)
	tree.Debug = true

	m.tree =        tree
	m.f = FedBOX(base, r, o)
	m.tree = tree.New(m.f)
	m.pager = newPagerModel(m.commonModel)
	return m
}


@@ 188,7 187,7 @@ func newPagerModel(common *commonModel) pagerModel {
}

type commonModel struct {
	f       fedbox
	f       *fedbox
	width   int
	height  int
}