diff --git a/Makefile b/Makefile index 4434898..d2396b6 100644 --- a/Makefile +++ b/Makefile @@ -13,5 +13,5 @@ sqlc: queries.sql schema.sql build: go build -run: build - ./gostart -name startdev +run: sqlc build + ./gostart -name startdev -db ./test.db diff --git a/app.go b/app.go index 0166c6f..0b2a085 100644 --- a/app.go +++ b/app.go @@ -30,8 +30,9 @@ func (a *App) getOwner(r *http.Request) (*tailcfg.Node, error) { ownerExists, err := a.queries.GetOwner(a.ctx, ownerID) if err != nil || ownerExists.ID != ownerID { _, err = a.queries.AddOwner(a.ctx, data.AddOwnerParams{ - ID: int64(who.Node.ID), - Name: who.Node.ComputedName, + ID: int64(who.Node.ID), + Name: who.Node.ComputedName, + ShowShared: false, }) if err != nil { log.Fatal("adding owner failed: ", err) diff --git a/data/models.go b/data/models.go index da7c5ab..da21c8d 100644 --- a/data/models.go +++ b/data/models.go @@ -29,10 +29,11 @@ type Link struct { } type Owner struct { - ID int64 `json:"id"` - CreatedAt time.Time `json:"created_at"` - LastUsed time.Time `json:"last_used"` - Name string `json:"name"` + ID int64 `json:"id"` + CreatedAt time.Time `json:"created_at"` + LastUsed time.Time `json:"last_used"` + Name string `json:"name"` + ShowShared bool `json:"show_shared"` } type PullRequest struct { diff --git a/data/queries.sql.go b/data/queries.sql.go index 1086b4e..4f40cd4 100644 --- a/data/queries.sql.go +++ b/data/queries.sql.go @@ -69,23 +69,25 @@ func (q *Queries) AddLink(ctx context.Context, arg AddLinkParams) (Link, error) } const addOwner = `-- name: AddOwner :one -insert into owners (id, name) -values (?, ?) returning id, created_at, last_used, name +insert into owners (id, name, show_shared) +values (?, ?, ?) returning id, created_at, last_used, name, show_shared ` type AddOwnerParams struct { - ID int64 `json:"id"` - Name string `json:"name"` + ID int64 `json:"id"` + Name string `json:"name"` + ShowShared bool `json:"show_shared"` } func (q *Queries) AddOwner(ctx context.Context, arg AddOwnerParams) (Owner, error) { - row := q.db.QueryRowContext(ctx, addOwner, arg.ID, arg.Name) + row := q.db.QueryRowContext(ctx, addOwner, arg.ID, arg.Name, arg.ShowShared) var i Owner err := row.Scan( &i.ID, &i.CreatedAt, &i.LastUsed, &i.Name, + &i.ShowShared, ) return i, err } @@ -299,7 +301,8 @@ func (q *Queries) GetAllLinks(ctx context.Context) ([]Link, error) { const getAllLinksForOwner = `-- name: GetAllLinksForOwner :many select id, owner_id, created_at, url, name, clicked, logo_url, shared from links -where owner_id = ? or shared = true +where owner_id = ? + or shared = true ` func (q *Queries) GetAllLinksForOwner(ctx context.Context, ownerID int64) ([]Link, error) { @@ -502,7 +505,9 @@ func (q *Queries) GetIconByLinkID(ctx context.Context, arg GetIconByLinkIDParams } const getLinkByID = `-- name: GetLinkByID :one -select id, owner_id, created_at, url, name, clicked, logo_url, shared from links where id = ? +select id, owner_id, created_at, url, name, clicked, logo_url, shared +from links +where id = ? ` func (q *Queries) GetLinkByID(ctx context.Context, id int64) (Link, error) { @@ -522,7 +527,7 @@ func (q *Queries) GetLinkByID(ctx context.Context, id int64) (Link, error) { } const getOwner = `-- name: GetOwner :one -select id, created_at, last_used, name +select id, created_at, last_used, name, show_shared from owners where id = ? ` @@ -535,6 +540,7 @@ func (q *Queries) GetOwner(ctx context.Context, id int64) (Owner, error) { &i.CreatedAt, &i.LastUsed, &i.Name, + &i.ShowShared, ) return i, err } diff --git a/flake.nix b/flake.nix index 6c1e349..62dc2b3 100644 --- a/flake.nix +++ b/flake.nix @@ -19,7 +19,7 @@ in { gostart = pkgs.buildGoModule { pname = "gostart"; - version = "v0.1.5"; + version = "v0.1.6"; src = ./.; vendorSha256 = diff --git a/handlers.go b/handlers.go index e38f6d7..9c61b04 100644 --- a/handlers.go +++ b/handlers.go @@ -350,7 +350,7 @@ var templateFuncs = template.FuncMap{ func index(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - owner, err := app.getOwner(r) + systemOwner, err := app.getOwner(r) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -367,6 +367,21 @@ func index(w http.ResponseWriter, r *http.Request) { return } + owner, err := app.queries.GetOwner(ctx, ownerID) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // TODO: maybe I can do this with an sql join... + filteredLinks := []data.Link{} + for _, l := range links { + if !owner.ShowShared && l.OwnerID != ownerID { + continue + } + filteredLinks = append(filteredLinks, l) + } + prs, err := app.queries.GetAllPullRequests(dbCtx, ownerID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -380,9 +395,10 @@ func index(w http.ResponseWriter, r *http.Request) { } stuff := &Page{ - Node: *owner, + Node: *systemOwner, + System: owner, Title: "StartPage", - Links: links, + Links: filteredLinks, PullRequests: prs, Watches: app.watches.forID(ownerID), CurrentLimits: app.watches.GetLimits(), diff --git a/page.go b/page.go index f35da85..b67981a 100644 --- a/page.go +++ b/page.go @@ -97,6 +97,7 @@ func getData(q GQLQuery, token string) (*WatchResult, error) { type Page struct { Title string + System data.Owner PullRequests []data.PullRequest Links []data.Link Node tailcfg.Node diff --git a/queries.sql b/queries.sql index a5e4239..1249e36 100644 --- a/queries.sql +++ b/queries.sql @@ -1,6 +1,6 @@ -- name: AddOwner :one -insert into owners (id, name) -values (?, ?) returning *; +insert into owners (id, name, show_shared) +values (?, ?, ?) returning *; -- name: GetOwner :one select * @@ -29,7 +29,8 @@ where id = ? -- name: GetAllLinksForOwner :many select * from links -where owner_id = ? or shared = true; +where owner_id = ? + or shared = true; -- name: GetAllLinks :many select * @@ -40,7 +41,9 @@ insert into links (owner_id, url, name, logo_url, shared) values (?, ?, ?, ?, ?) returning *; -- name: GetLinkByID :one -select * from links where id = ?; +select * +from links +where id = ?; -- name: DeleteLink :exec delete diff --git a/schema.sql b/schema.sql index 0faee7e..4096a9b 100644 --- a/schema.sql +++ b/schema.sql @@ -1,9 +1,10 @@ create table owners ( - id integer primary key not null, - created_at datetime default current_timestamp not null, - last_used datetime default current_timestamp not null, - name text not null unique + id integer primary key not null, + created_at datetime default current_timestamp not null, + last_used datetime default current_timestamp not null, + name text not null unique, + show_shared bool default false not null ); create table watch_items ( diff --git a/templates/main.html b/templates/main.html index 34f4d18..1c28a96 100644 --- a/templates/main.html +++ b/templates/main.html @@ -55,6 +55,7 @@
Remaining queries: {{.CurrentLimits.Remaining}}
Limit resets in {{remaining .CurrentLimits.ResetAt}} minutes.
diff --git a/tmp.go b/tmp.go
index efaaf6e..4f3bfb3 100644
--- a/tmp.go
+++ b/tmp.go
@@ -16,8 +16,9 @@ func tmpDBPopulate(db *sql.DB) error {
ownerID := int64(57395170551826799)
_, err := app.queries.AddOwner(app.ctx, data.AddOwnerParams{
- ID: 57395170551826799,
- Name: "europa.humpback-trout.ts.net.",
+ ID: 57395170551826799,
+ Name: "europa.humpback-trout.ts.net.",
+ ShowShared: true,
})
if err != nil {
return err