Skip to content

Commit 78b57b9

Browse files
committed
account edge for question optional
1 parent 508d616 commit 78b57b9

File tree

11 files changed

+129
-54
lines changed

11 files changed

+129
-54
lines changed

app/resources/question/question.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
package question
22

33
import (
4+
"github.com/Southclaws/opt"
5+
"github.com/rs/xid"
6+
47
"github.com/Southclaws/storyden/app/resources/account"
58
"github.com/Southclaws/storyden/app/resources/datagraph"
69
"github.com/Southclaws/storyden/internal/ent"
7-
"github.com/rs/xid"
810
)
911

1012
type Question struct {
1113
ID xid.ID
1214
Slug string
1315
Query string
1416
Result datagraph.Content
15-
Author account.Account
17+
Author opt.Optional[account.Account]
1618
}
1719

1820
func Map(in *ent.Question) (*Question, error) {
19-
authorEdge, err := in.Edges.AuthorOrErr()
20-
if err != nil {
21-
return nil, err
22-
}
21+
authorEdge := opt.NewPtr(in.Edges.Author)
2322

2423
result, err := datagraph.NewRichText(in.Result)
2524
if err != nil {
2625
return nil, err
2726
}
2827

29-
author, err := account.MapAccount(authorEdge)
28+
author, err := opt.MapErr(authorEdge, func(a ent.Account) (account.Account, error) {
29+
acc, err := account.MapAccount(&a)
30+
if err != nil {
31+
return account.Account{}, err
32+
}
33+
return *acc, nil
34+
})
3035
if err != nil {
3136
return nil, err
3237
}
@@ -36,6 +41,6 @@ func Map(in *ent.Question) (*Question, error) {
3641
Slug: in.Slug,
3742
Query: in.Query,
3843
Result: result,
39-
Author: *author,
44+
Author: author,
4045
}, nil
4146
}

app/resources/question/repo.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/Southclaws/fault"
77
"github.com/Southclaws/fault/fctx"
8+
"github.com/Southclaws/opt"
89
"github.com/Southclaws/storyden/app/resources/account"
910
"github.com/Southclaws/storyden/app/resources/datagraph"
1011
"github.com/Southclaws/storyden/internal/ent"
@@ -21,7 +22,7 @@ func New(db *ent.Client) *Repository {
2122
return &Repository{db: db}
2223
}
2324

24-
func (r *Repository) Store(ctx context.Context, accountID account.AccountID, query string, result datagraph.Content) (*Question, error) {
25+
func (r *Repository) Store(ctx context.Context, query string, result datagraph.Content, accountID opt.Optional[account.AccountID]) (*Question, error) {
2526
create := r.db.Question.Create()
2627
mutate := create.Mutation()
2728

@@ -30,7 +31,10 @@ func (r *Repository) Store(ctx context.Context, accountID account.AccountID, que
3031
mutate.SetSlug(slug)
3132
mutate.SetQuery(query)
3233
mutate.SetResult(result.HTML())
33-
mutate.SetAccountID(xid.ID(accountID))
34+
35+
accountID.Call(func(id account.AccountID) {
36+
mutate.SetAccountID(xid.ID(id))
37+
})
3438

3539
create.OnConflictColumns("slug").UpdateNewValues()
3640

app/services/semdex/asker/cached.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/Southclaws/fault/fctx"
1111
"go.uber.org/zap"
1212

13-
"github.com/Southclaws/storyden/app/resources/account"
1413
"github.com/Southclaws/storyden/app/resources/datagraph"
1514
"github.com/Southclaws/storyden/app/resources/question"
1615
"github.com/Southclaws/storyden/app/services/authentication/session"
@@ -69,11 +68,6 @@ func (a *cachedAsker) cachedResult(ctx context.Context, q *question.Question) (f
6968
}
7069

7170
func (a *cachedAsker) livePrompt(ctx context.Context, q string) (func(yield func(string, error) bool), error) {
72-
accountID, err := session.GetAccountID(ctx)
73-
if err != nil {
74-
return nil, fault.Wrap(err, fctx.With(ctx))
75-
}
76-
7771
iter, err := a.asker.Ask(ctx, q)
7872
if err != nil {
7973
return nil, fault.Wrap(err, fctx.With(ctx))
@@ -83,7 +77,7 @@ func (a *cachedAsker) livePrompt(ctx context.Context, q string) (func(yield func
8377
acc := []string{}
8478

8579
defer func() {
86-
err := a.cacheResult(ctx, accountID, q, acc)
80+
err := a.cacheResult(ctx, q, acc)
8781
if err != nil {
8882
a.logger.Error("failed to cache result", zap.Error(err))
8983
}
@@ -103,15 +97,17 @@ func (a *cachedAsker) livePrompt(ctx context.Context, q string) (func(yield func
10397
}, nil
10498
}
10599

106-
func (a *cachedAsker) cacheResult(ctx context.Context, accountID account.AccountID, q string, chunks []string) error {
100+
func (a *cachedAsker) cacheResult(ctx context.Context, q string, chunks []string) error {
101+
accountID := session.GetOptAccountID(ctx)
102+
107103
result := strings.Join(chunks, "")
108104

109105
acc, err := datagraph.NewRichTextFromMarkdown(result)
110106
if err != nil {
111107
return fault.Wrap(err, fctx.With(ctx))
112108
}
113109

114-
_, err = a.questions.Store(ctx, accountID, q, acc)
110+
_, err = a.questions.Store(ctx, q, acc, accountID)
115111
if err != nil {
116112
return fault.Wrap(err, fctx.With(ctx))
117113
}

app/transports/http/bindings/openapi_rbac/mapping.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ func (m *Mapping) DatagraphSearch() (bool, *rbac.Permission) {
427427
}
428428

429429
func (m *Mapping) DatagraphAsk() (bool, *rbac.Permission) {
430-
return true, &rbac.PermissionReadPublishedThreads
430+
return false, nil
431431
}
432432

433433
func (m *Mapping) EventList() (bool, *rbac.Permission) {

go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:
262262
github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
263263
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
264264
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
265+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
266+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
265267
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=
266268
github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81KkeA=
267269
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
@@ -438,6 +440,10 @@ github.com/shirou/gopsutil/v4 v4.24.10/go.mod h1:s4D/wg+ag4rG0WO7AiTj2BeYCRhym0v
438440
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
439441
github.com/speakeasy-api/openapi-overlay v0.9.0 h1:Wrz6NO02cNlLzx1fB093lBlYxSI54VRhy1aSutx0PQg=
440442
github.com/speakeasy-api/openapi-overlay v0.9.0/go.mod h1:f5FloQrHA7MsxYg9djzMD5h6dxrHjVVByWKh7an8TRc=
443+
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
444+
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
445+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
446+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
441447
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
442448
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
443449
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM=

internal/ent/migrate/schema.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/ent/mutation.go

+20-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/ent/question/where.go

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/ent/question_create.go

+36-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)