Skip to content

Commit

Permalink
[enh] add snapshot search view
Browse files Browse the repository at this point in the history
  • Loading branch information
asciimoo committed Oct 1, 2024
1 parent f4f8cff commit 5e82dd4
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 19 deletions.
2 changes: 1 addition & 1 deletion config.yml_sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
app:
debug: true
bookmarks_per_page: 20
results_per_page: 20
disable_signup: false # set to true to restrict user creation to command line
server:
address: "127.0.0.1:7331"
Expand Down
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ type Config struct {
}

type App struct {
Debug bool `yaml:"debug"`
BookmarksPerPage int64 `yaml:"bookmarks_per_page"`
DisableSignup bool `yaml:"disable_signup"`
Debug bool `yaml:"debug"`
ResultsPerPage int64 `yaml:"results_per_page"`
DisableSignup bool `yaml:"disable_signup"`
}

type Server struct {
Expand Down
1 change: 1 addition & 0 deletions model/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Snapshot struct {
Key string `json:"key"`
Text string `json:"text"`
BookmarkID uint `json:"bookmark_id"`
Bookmark Bookmark `json:"bookmark"`
Size uint `json:"size"`
Resources []Resource `gorm:"many2many:snapshot_resources;" json:"resources"`
}
3 changes: 2 additions & 1 deletion templates/layout/base.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<a href="{{ URLFor "My bookmarks" }}" class="navbar-item{{ if eq .Page "my-bookmarks" }} is-active{{ end }}">My bookmarks</a>
{{ end }}
<a href="{{ URLFor "Public bookmarks" }}" class="navbar-item{{ if eq .Page "bookmarks" }} is-active{{ end }}">Public bookmarks</a>
<a href="{{ URLFor "Snapshots" }}" class="navbar-item{{ if eq .Page "snapshots" }} is-active{{ end }}">Snapshots</a>
</div>
<div class="navbar-end">
{{ if .User }}
Expand Down Expand Up @@ -215,7 +216,7 @@
{{ define "paging" }}
<div class="columns is-centered">
<div class="column is-narrow">
{{ if gt .Pageno 1 }}
{{ if and .Pageno (gt .Pageno 1) }}
<a href="?pageno={{ dec .Pageno }}" class="button is-primary is-medium is-outlined"><span class="icon"><i class="fas fa-angle-left"></i></span><span>Previous page</span></a>
{{ end }}
{{ if .HasNextPage }}
Expand Down
17 changes: 13 additions & 4 deletions templates/rss.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@ SPDX-License-Identifier: AGPLv3+
<title>Omnom</title>
<link>{{ .FullURLPrefix }}{{ URLFor "Index" }}</link>
<description>Bookmarking &amp; snapshotting</description>
{{ $type := .Type }}
{{ range .RSS }}
<item>
<title>{{ .Title }}</title>
<link>{{ $.FullURLPrefix }}{{ URLFor "Public bookmark" }}?id={{ .ID }}</link>
<pubDate>{{ .UpdatedAt | ToDateTime }}</pubDate>
<description>{{ .Notes }}</description>
{{- if eq $type "Bookmarks" }}
<title>{{ .Title }}</title>
<link>{{ $.FullURLPrefix }}{{ URLFor "Bookmark" }}?id={{ .ID }}</link>
<pubDate>{{ .UpdatedAt | ToDateTime }}</pubDate>
<description>{{ .Notes }}</description>
{{- end }}
{{ if eq $type "Snapshots" -}}
<title>{{ .Bookmark.Title }} (Size: {{ .Size | FormatSize }})</title>
<link>{{ $.FullURLPrefix }}{{ URLFor "Snapshot" }}?sid={{ .Key }}&bid={{ .Bookmark.ID }}</link>
<pubDate>{{ .UpdatedAt | ToDateTime }}</pubDate>
<description>{{ .Bookmark.Notes }}</description>
{{- end }}
</item>{{ end }}
</channel>
</rss>
32 changes: 32 additions & 0 deletions templates/snapshots.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{ define "content" }}
<div class="content">
<div class="is-pulled-right"><a href="{{ AddURLParam .URL "format=rss" }}">RSS<span class="icon"><i class="fas fa-rss"></i></span></a></div>
<h3 class="title">Snapshot search</h3>
<form action="" method="get">
<div class="columns">
<div class="column">
{{ block "textFilter" .}}{{ end }}
{{ block "submit" . }}{{ end }}
</div>
</div>
</form>
{{ if .SearchParams.Q }}
{{ if eq .SnapshotCount 0 }}
<h3 class="title">No snapshots found</h3>
{{ else }}
<h3 class="title">Results for "{{ .SearchParams.Q }}" ({{ .SnapshotCount }})</h3>
{{ range .Snapshots }}
<div class="box">
<h4 class="title"><a href="{{ URLFor "Snapshot" }}?sid={{ .Key }}&bid={{ .BookmarkID }}">{{ .Bookmark.Title }}</a></h4>
<p>
Original URL: <a href="{{ .Bookmark.URL }}" target="_blank">{{ Truncate .Bookmark.URL 100 }}</a><br />
{{ .UpdatedAt | ToDateTime }} - {{ .Size | FormatSize }}
</p>
</div>
{{ end }}
{{ end }}
{{ block "paging" .}}{{ end }}
{{ end }}
</div>
</div>
{{ end }}
17 changes: 17 additions & 0 deletions webapp/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ func init() {
Handler: logout,
Description: "Destroys user session",
},
&Endpoint{
Name: "Snapshots",
Path: "/snapshots",
Method: GET,
AuthRequired: false,
Handler: snapshots,
Description: "Search in snapshots by URL",
Args: []*EndpointArg{
&EndpointArg{
Name: "query",
Type: "string",
Required: false,
Description: "Search term to filter snapshots",
},
},
RSS: "Snapshots",
},
&Endpoint{
Name: "Public bookmarks",
Path: "/bookmarks",
Expand Down
12 changes: 6 additions & 6 deletions webapp/bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ const (
func bookmarks(c *gin.Context) {
var bs []*model.Bookmark
pageno := getPageno(c)
offset := (pageno - 1) * bookmarksPerPage
offset := (pageno - 1) * resultsPerPage
var bookmarkCount int64
cq := model.DB.Model(&model.Bookmark{}).Where("bookmarks.public = 1")
q := model.DB.Limit(int(bookmarksPerPage)).Offset(int(offset)).Where("bookmarks.public = 1").Preload("Snapshots").Preload("Tags").Preload("User")
q := model.DB.Limit(int(resultsPerPage)).Offset(int(offset)).Where("bookmarks.public = 1").Preload("Snapshots").Preload("Tags").Preload("User")
sp := &searchParams{}
hasSearch := false
if err := c.ShouldBind(sp); err != nil {
Expand Down Expand Up @@ -65,7 +65,7 @@ func bookmarks(c *gin.Context) {
"Bookmarks": bs,
"Pageno": pageno,
"BookmarkCount": bookmarkCount,
"HasNextPage": offset+bookmarksPerPage < bookmarkCount,
"HasNextPage": offset+resultsPerPage < bookmarkCount,
"SearchParams": sp,
"HasSearch": hasSearch,
"OrderBy": orderBy,
Expand All @@ -76,10 +76,10 @@ func myBookmarks(c *gin.Context) {
u, _ := c.Get("user")
var bs []*model.Bookmark
pageno := getPageno(c)
offset := (pageno - 1) * bookmarksPerPage
offset := (pageno - 1) * resultsPerPage
var bookmarkCount int64
cq := model.DB.Model(&model.Bookmark{}).Where("bookmarks.user_id = ?", u.(*model.User).ID)
q := model.DB.Limit(int(bookmarksPerPage)).Offset(int(offset)).Model(&model.Bookmark{}).Where("bookmarks.user_id = ?", u.(*model.User).ID).Preload("Snapshots").Preload("Tags").Preload("User")
q := model.DB.Limit(int(resultsPerPage)).Offset(int(offset)).Model(&model.Bookmark{}).Where("bookmarks.user_id = ?", u.(*model.User).ID).Preload("Snapshots").Preload("Tags").Preload("User")
sp := &searchParams{}
hasSearch := false
if err := c.ShouldBind(sp); err != nil {
Expand Down Expand Up @@ -117,7 +117,7 @@ func myBookmarks(c *gin.Context) {
"Bookmarks": bs,
"Pageno": pageno,
"BookmarkCount": bookmarkCount,
"HasNextPage": offset+bookmarksPerPage < bookmarkCount,
"HasNextPage": offset+resultsPerPage < bookmarkCount,
"SearchParams": sp,
"HasSearch": hasSearch,
"OrderBy": orderBy,
Expand Down
30 changes: 30 additions & 0 deletions webapp/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,33 @@ func deleteSnapshot(c *gin.Context) {
}
c.Redirect(http.StatusFound, baseURL("/edit_bookmark?id="+bid))
}

func snapshots(c *gin.Context) {
qs, ok := c.GetQuery("query")
if !ok {
render(c, http.StatusOK, "snapshots", nil)
return
}
var uid uint = 0
u, ok := c.Get("user")
if ok && u != nil {
uid = u.(*model.User).ID
}
var ss []*model.Snapshot
pageno := getPageno(c)
offset := (pageno - 1) * resultsPerPage
var sc int64
//cq := model.DB.Model(&model.Snapshot{}).Where("s.public = 1")
q := model.DB.Limit(int(resultsPerPage)).Offset(int(offset)).Joins("left join bookmarks on bookmarks.id = snapshots.bookmark_id").Where("bookmarks.url like ?", "%"+qs+"%").Where("bookmarks.public == true or bookmarks.user_id == ?", uid).Preload("Bookmark")
cq := model.DB.Model(&model.Snapshot{}).Joins("left join bookmarks on bookmarks.id = snapshots.bookmark_id").Where("bookmarks.url like ?", "%"+qs+"%").Where("bookmarks.public == true or bookmarks.user_id == ?", uid)
cq.Count(&sc)
q.Order("snapshots.created_at").Find(&ss)
render(c, http.StatusOK, "snapshots", map[string]interface{}{
"Snapshots": ss,
"SnapshotCount": sc,
"Pageno": pageno,
"SearchParams": searchParams{
Q: qs,
},
})
}
10 changes: 6 additions & 4 deletions webapp/webapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var tplFuncMap = template.FuncMap{
"FormatSize": formatSize,
}

var bookmarksPerPage int64 = 20
var resultsPerPage int64 = 20

func createRenderer() multitemplate.Renderer {
r := multitemplate.DynamicRender{}
Expand All @@ -94,6 +94,7 @@ func createRenderer() multitemplate.Renderer {
r.AddFromFilesFuncs("login", tplFuncMap, "templates/layout/base.tpl", "templates/login.tpl")
r.AddFromFilesFuncs("login-confirm", tplFuncMap, "templates/layout/base.tpl", "templates/login_confirm.tpl")
r.AddFromFilesFuncs("bookmarks", tplFuncMap, "templates/layout/base.tpl", "templates/bookmarks.tpl")
r.AddFromFilesFuncs("snapshots", tplFuncMap, "templates/layout/base.tpl", "templates/snapshots.tpl")
r.AddFromFilesFuncs("my-bookmarks", tplFuncMap, "templates/layout/base.tpl", "templates/my_bookmarks.tpl")
r.AddFromFilesFuncs("profile", tplFuncMap, "templates/layout/base.tpl", "templates/profile.tpl")
r.AddFromFilesFuncs("snapshotWrapper", tplFuncMap, "templates/layout/base.tpl", "templates/snapshot_wrapper.tpl")
Expand Down Expand Up @@ -183,7 +184,8 @@ func renderRSS(c *gin.Context, status int, vars map[string]interface{}) {
}
c.Header("Content-Type", "application/rss+xml; charset=utf-8")
tplVars := map[string]interface{}{
"RSS": vars[k.(string)],
"RSS": vars[k.(string)],
"Type": k.(string),
}
fullURLPrefix := ""
if strings.HasPrefix(baseURL("/"), "/") {
Expand Down Expand Up @@ -223,8 +225,8 @@ func Run(cfg *config.Config) {
if !cfg.App.Debug {
gin.SetMode(gin.ReleaseMode)
}
if cfg.App.BookmarksPerPage > 0 {
bookmarksPerPage = cfg.App.BookmarksPerPage
if cfg.App.ResultsPerPage > 0 {
resultsPerPage = cfg.App.ResultsPerPage
}
_ = e.SetTrustedProxies([]string{"127.0.0.1"})
sess := sessions.NewCookieStore([]byte("secret"))
Expand Down

0 comments on commit 5e82dd4

Please sign in to comment.