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:
go build
run: build
./gostart -name startdev
run: sqlc build
./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)
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)

View File

@ -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 {

View File

@ -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
}

View File

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

View File

@ -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(),

View File

@ -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

View File

@ -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

View File

@ -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
(

View File

@ -55,6 +55,7 @@
<body>
<header>
<h1>{{.Title}} for {{.Node.ComputedName}}</h1>
{{if .System.ShowShared}}Showing shared links.{{end}}
<p>
Remaining queries: <b>{{.CurrentLimits.Remaining}}</b><br />
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)
_, 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