Allow for not displaying shared links on specific hosts

This commit is contained in:
Aaron Bieber 2022-12-21 20:51:54 -07:00
parent c4e2729144
commit e31139add1
No known key found for this signature in database
11 changed files with 61 additions and 30 deletions

View File

@ -13,5 +13,5 @@ sqlc: queries.sql schema.sql
build: build:
go build go build
run: build run: sqlc build
./gostart -name startdev ./gostart -name startdev -db ./test.db

5
app.go
View File

@ -30,8 +30,9 @@ func (a *App) getOwner(r *http.Request) (*tailcfg.Node, error) {
ownerExists, err := a.queries.GetOwner(a.ctx, ownerID) ownerExists, err := a.queries.GetOwner(a.ctx, ownerID)
if err != nil || ownerExists.ID != ownerID { if err != nil || ownerExists.ID != ownerID {
_, err = a.queries.AddOwner(a.ctx, data.AddOwnerParams{ _, err = a.queries.AddOwner(a.ctx, data.AddOwnerParams{
ID: int64(who.Node.ID), ID: int64(who.Node.ID),
Name: who.Node.ComputedName, Name: who.Node.ComputedName,
ShowShared: false,
}) })
if err != nil { if err != nil {
log.Fatal("adding owner failed: ", err) log.Fatal("adding owner failed: ", err)

View File

@ -29,10 +29,11 @@ type Link struct {
} }
type Owner struct { type Owner struct {
ID int64 `json:"id"` ID int64 `json:"id"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
LastUsed time.Time `json:"last_used"` LastUsed time.Time `json:"last_used"`
Name string `json:"name"` Name string `json:"name"`
ShowShared bool `json:"show_shared"`
} }
type PullRequest struct { type PullRequest struct {

View File

@ -69,23 +69,25 @@ func (q *Queries) AddLink(ctx context.Context, arg AddLinkParams) (Link, error)
} }
const addOwner = `-- name: AddOwner :one const addOwner = `-- name: AddOwner :one
insert into owners (id, name) insert into owners (id, name, show_shared)
values (?, ?) returning id, created_at, last_used, name values (?, ?, ?) returning id, created_at, last_used, name, show_shared
` `
type AddOwnerParams struct { type AddOwnerParams struct {
ID int64 `json:"id"` ID int64 `json:"id"`
Name string `json:"name"` Name string `json:"name"`
ShowShared bool `json:"show_shared"`
} }
func (q *Queries) AddOwner(ctx context.Context, arg AddOwnerParams) (Owner, error) { 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 var i Owner
err := row.Scan( err := row.Scan(
&i.ID, &i.ID,
&i.CreatedAt, &i.CreatedAt,
&i.LastUsed, &i.LastUsed,
&i.Name, &i.Name,
&i.ShowShared,
) )
return i, err return i, err
} }
@ -299,7 +301,8 @@ func (q *Queries) GetAllLinks(ctx context.Context) ([]Link, error) {
const getAllLinksForOwner = `-- name: GetAllLinksForOwner :many const getAllLinksForOwner = `-- name: GetAllLinksForOwner :many
select id, owner_id, created_at, url, name, clicked, logo_url, shared select id, owner_id, created_at, url, name, clicked, logo_url, shared
from links 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) { 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 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) { 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 const getOwner = `-- name: GetOwner :one
select id, created_at, last_used, name select id, created_at, last_used, name, show_shared
from owners from owners
where id = ? where id = ?
` `
@ -535,6 +540,7 @@ func (q *Queries) GetOwner(ctx context.Context, id int64) (Owner, error) {
&i.CreatedAt, &i.CreatedAt,
&i.LastUsed, &i.LastUsed,
&i.Name, &i.Name,
&i.ShowShared,
) )
return i, err return i, err
} }

View File

@ -19,7 +19,7 @@
in { in {
gostart = pkgs.buildGoModule { gostart = pkgs.buildGoModule {
pname = "gostart"; pname = "gostart";
version = "v0.1.5"; version = "v0.1.6";
src = ./.; src = ./.;
vendorSha256 = vendorSha256 =

View File

@ -350,7 +350,7 @@ var templateFuncs = template.FuncMap{
func index(w http.ResponseWriter, r *http.Request) { func index(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
owner, err := app.getOwner(r) systemOwner, err := app.getOwner(r)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
@ -367,6 +367,21 @@ func index(w http.ResponseWriter, r *http.Request) {
return 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) prs, err := app.queries.GetAllPullRequests(dbCtx, ownerID)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
@ -380,9 +395,10 @@ func index(w http.ResponseWriter, r *http.Request) {
} }
stuff := &Page{ stuff := &Page{
Node: *owner, Node: *systemOwner,
System: owner,
Title: "StartPage", Title: "StartPage",
Links: links, Links: filteredLinks,
PullRequests: prs, PullRequests: prs,
Watches: app.watches.forID(ownerID), Watches: app.watches.forID(ownerID),
CurrentLimits: app.watches.GetLimits(), CurrentLimits: app.watches.GetLimits(),

View File

@ -97,6 +97,7 @@ func getData(q GQLQuery, token string) (*WatchResult, error) {
type Page struct { type Page struct {
Title string Title string
System data.Owner
PullRequests []data.PullRequest PullRequests []data.PullRequest
Links []data.Link Links []data.Link
Node tailcfg.Node Node tailcfg.Node

View File

@ -1,6 +1,6 @@
-- name: AddOwner :one -- name: AddOwner :one
insert into owners (id, name) insert into owners (id, name, show_shared)
values (?, ?) returning *; values (?, ?, ?) returning *;
-- name: GetOwner :one -- name: GetOwner :one
select * select *
@ -29,7 +29,8 @@ where id = ?
-- name: GetAllLinksForOwner :many -- name: GetAllLinksForOwner :many
select * select *
from links from links
where owner_id = ? or shared = true; where owner_id = ?
or shared = true;
-- name: GetAllLinks :many -- name: GetAllLinks :many
select * select *
@ -40,7 +41,9 @@ insert into links (owner_id, url, name, logo_url, shared)
values (?, ?, ?, ?, ?) returning *; values (?, ?, ?, ?, ?) returning *;
-- name: GetLinkByID :one -- name: GetLinkByID :one
select * from links where id = ?; select *
from links
where id = ?;
-- name: DeleteLink :exec -- name: DeleteLink :exec
delete delete

View File

@ -1,9 +1,10 @@
create table owners create table owners
( (
id integer primary key not null, id integer primary key not null,
created_at datetime default current_timestamp not null, created_at datetime default current_timestamp not null,
last_used datetime default current_timestamp not null, last_used datetime default current_timestamp not null,
name text not null unique name text not null unique,
show_shared bool default false not null
); );
create table watch_items create table watch_items
( (

View File

@ -55,6 +55,7 @@
<body> <body>
<header> <header>
<h1>{{.Title}} for {{.Node.ComputedName}}</h1> <h1>{{.Title}} for {{.Node.ComputedName}}</h1>
{{if .System.ShowShared}}Showing shared links.{{end}}
<p> <p>
Remaining queries: <b>{{.CurrentLimits.Remaining}}</b><br /> Remaining queries: <b>{{.CurrentLimits.Remaining}}</b><br />
Limit resets in <b>{{remaining .CurrentLimits.ResetAt}}</b> minutes. Limit resets in <b>{{remaining .CurrentLimits.ResetAt}}</b> minutes.

5
tmp.go
View File

@ -16,8 +16,9 @@ func tmpDBPopulate(db *sql.DB) error {
ownerID := int64(57395170551826799) ownerID := int64(57395170551826799)
_, err := app.queries.AddOwner(app.ctx, data.AddOwnerParams{ _, err := app.queries.AddOwner(app.ctx, data.AddOwnerParams{
ID: 57395170551826799, ID: 57395170551826799,
Name: "europa.humpback-trout.ts.net.", Name: "europa.humpback-trout.ts.net.",
ShowShared: true,
}) })
if err != nil { if err != nil {
return err return err