Skip to content

Commit

Permalink
fix empty links causing confusing thread hydration behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Southclaws committed Nov 29, 2023
1 parent 3d4ed87 commit 6747d2c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
5 changes: 2 additions & 3 deletions app/services/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,10 @@ func (s *service) Archive(ctx context.Context, slug datagraph.ClusterSlug) (*dat

func (s *service) hydrateLink(ctx context.Context, partial Partial) (opts []cluster.Option) {
text, textOK := partial.Content.Get()
url, urlOK := partial.URL.Get()

if !textOK && !urlOK {
if !textOK && !partial.URL.Ok() {
return
}

return s.hydrator.HydrateCluster(ctx, text, url)
return s.hydrator.HydrateCluster(ctx, text, partial.URL)
}
7 changes: 7 additions & 0 deletions app/services/hydrator/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/Southclaws/fault"
"github.com/Southclaws/fault/fctx"
"github.com/Southclaws/fault/ftag"
"go.uber.org/fx"
"go.uber.org/zap"

Expand All @@ -18,6 +19,8 @@ import (
"github.com/Southclaws/storyden/app/services/url"
)

var errEmptyLink = fault.New("empty link")

type Service interface {
Fetch(ctx context.Context, url string) (*link.Link, error)
}
Expand Down Expand Up @@ -50,6 +53,10 @@ func New(
}

func (s *service) Fetch(ctx context.Context, url string) (*link.Link, error) {
if url == "" {
return nil, fault.Wrap(errEmptyLink, fctx.With(ctx), ftag.With(ftag.InvalidArgument))
}

r, err := s.lr.Search(ctx, link.WithURL(url))
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx))
Expand Down
21 changes: 11 additions & 10 deletions app/services/hydrator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go.uber.org/fx"
"go.uber.org/zap"

"github.com/Southclaws/opt"
"github.com/Southclaws/storyden/app/resources/asset"
"github.com/Southclaws/storyden/app/resources/cluster"
"github.com/Southclaws/storyden/app/resources/item"
Expand All @@ -17,10 +18,10 @@ import (
)

type Service interface {
HydrateThread(ctx context.Context, body, url string) []thread.Option
HydrateReply(ctx context.Context, body, url string) []reply.Option
HydrateCluster(ctx context.Context, body, url string) []cluster.Option
HydrateItem(ctx context.Context, body, url string) []item.Option
HydrateThread(ctx context.Context, body string, url opt.Optional[string]) []thread.Option
HydrateReply(ctx context.Context, body string, url opt.Optional[string]) []reply.Option
HydrateCluster(ctx context.Context, body string, url opt.Optional[string]) []cluster.Option
HydrateItem(ctx context.Context, body string, url opt.Optional[string]) []item.Option
}

func Build() fx.Option {
Expand Down Expand Up @@ -51,7 +52,7 @@ func New(
}
}

func (s *service) HydrateThread(ctx context.Context, body, url string) []thread.Option {
func (s *service) HydrateThread(ctx context.Context, body string, url opt.Optional[string]) []thread.Option {
short, links, assets := s.hydrate(ctx, body, url)

return []thread.Option{
Expand All @@ -61,7 +62,7 @@ func (s *service) HydrateThread(ctx context.Context, body, url string) []thread.
}
}

func (s *service) HydrateReply(ctx context.Context, body, url string) []reply.Option {
func (s *service) HydrateReply(ctx context.Context, body string, url opt.Optional[string]) []reply.Option {
short, links, assets := s.hydrate(ctx, body, url)

return []reply.Option{
Expand All @@ -71,7 +72,7 @@ func (s *service) HydrateReply(ctx context.Context, body, url string) []reply.Op
}
}

func (s *service) HydrateCluster(ctx context.Context, body, url string) []cluster.Option {
func (s *service) HydrateCluster(ctx context.Context, body string, url opt.Optional[string]) []cluster.Option {
_, links, assets := s.hydrate(ctx, body, url)

return []cluster.Option{
Expand All @@ -80,7 +81,7 @@ func (s *service) HydrateCluster(ctx context.Context, body, url string) []cluste
}
}

func (s *service) HydrateItem(ctx context.Context, body, url string) []item.Option {
func (s *service) HydrateItem(ctx context.Context, body string, url opt.Optional[string]) []item.Option {
_, links, assets := s.hydrate(ctx, body, url)

return []item.Option{
Expand All @@ -91,10 +92,10 @@ func (s *service) HydrateItem(ctx context.Context, body, url string) []item.Opti

// hydrate takes the body and primary URL of a piece of content and fetches all
// the links and produces a short summary of the post's body text.
func (s *service) hydrate(ctx context.Context, body, url string) (string, []xid.ID, []asset.AssetID) {
func (s *service) hydrate(ctx context.Context, body string, urls opt.Optional[string]) (string, []xid.ID, []asset.AssetID) {
structured := extractor.Destructure(body)

urls := append([]string{url}, structured.Links...)
urls = append(urls, structured.Links...)

links := []xid.ID{}
assets := []asset.AssetID{}
Expand Down
5 changes: 2 additions & 3 deletions app/services/item_crud/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,10 @@ func (s *service) Archive(ctx context.Context, slug datagraph.ItemSlug) (*datagr

func (s *service) hydrateLink(ctx context.Context, partial Partial) (opts []item.Option) {
text, textOK := partial.Content.Get()
url, urlOK := partial.URL.Get()

if !textOK && !urlOK {
if !textOK && !partial.URL.Ok() {
return
}

return s.hydrator.HydrateItem(ctx, text, url)
return s.hydrator.HydrateItem(ctx, text, partial.URL)
}
2 changes: 1 addition & 1 deletion app/services/reply/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ func (s *service) hydrate(ctx context.Context, partial Partial) (opts []reply.Op
return
}

return s.hydrator.HydrateReply(ctx, body, "")
return s.hydrator.HydrateReply(ctx, body, opt.NewEmpty[string]())
}
5 changes: 2 additions & 3 deletions app/services/thread/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ func (s *service) Create(ctx context.Context,

func (s *service) hydrate(ctx context.Context, partial Partial) (opts []thread.Option) {
body, bodyOK := partial.Body.Get()
url, urlOK := partial.URL.Get()

if !bodyOK && !urlOK {
if !bodyOK && !partial.URL.Ok() {
return
}

return s.hydrator.HydrateThread(ctx, body, url)
return s.hydrator.HydrateThread(ctx, body, partial.URL)
}

0 comments on commit 6747d2c

Please sign in to comment.