From 24ce00aff954f8b424475b53a67009fabb0405bc Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Sun, 10 Apr 2016 09:50:18 -0400 Subject: [PATCH] Added Recent to see recently edited/viewed things Former-commit-id: f915533dc401cb532111a12ee550438b6265239a [formerly ff18889a0c2dfaca85a44fb0a39c0ab811eb7a2f] [formerly 5063cff7610e06e8e6f9c1dfb333e5d57b2c0cf6 [formerly c07807a85674865be3224fa0492a44efea18b690 [formerly 6987950b78c22011146602379e3ef208c7b6fdae]]] Former-commit-id: a6b81d4b3d0027fce4ad88bb084a1edcac8b68a6 [formerly a8e46c713549b3dd79febbd58255b1ce66b6f5fb] Former-commit-id: 413d956e82465767fb93961fb997efabda41b402 Former-commit-id: ee01e10499000767a2095987e1395afeefc57581 --- main.go | 3 ++ routes.go | 41 ++++++++++++--- templates/base.tmpl | 116 +++++++++++++++++++++---------------------- templates/index.tmpl | 10 +++- templates/list.tmpl | 12 ++++- templates/view.tmpl | 10 +++- 6 files changed, 123 insertions(+), 69 deletions(-) diff --git a/main.go b/main.go index b43616e..b7ed25f 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "path/filepath" "github.com/boltdb/bolt" + "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" ) @@ -125,6 +126,8 @@ Options:`) r := gin.Default() r.LoadHTMLGlob(path.Join(RuntimeArgs.SourcePath, "templates/*")) + store := sessions.NewCookieStore([]byte("secret")) + r.Use(sessions.Sessions("mysession", store)) r.GET("/", newNote) r.HEAD("/", func(c *gin.Context) { c.Status(200) }) r.GET("/:title", editNote) diff --git a/routes.go b/routes.go index 308c675..02b2db9 100644 --- a/routes.go +++ b/routes.go @@ -16,6 +16,7 @@ import ( "time" "github.com/boltdb/bolt" + "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" "github.com/microcosm-cc/bluemonday" "github.com/russross/blackfriday" @@ -225,6 +226,29 @@ func getCodeType(title string) string { return "" } +func getRecentlyEdited(title string, c *gin.Context) []string { + session := sessions.Default(c) + var recentlyEdited string + v := session.Get("recentlyEdited") + editedThings := []string{} + if v == nil { + recentlyEdited = title + } else { + editedThings = strings.Split(v.(string), "|||") + fmt.Println(editedThings) + fmt.Println(v.(string)) + fmt.Println(title) + if !stringInSlice(title, editedThings) { + recentlyEdited = v.(string) + "|||" + title + } else { + recentlyEdited = v.(string) + } + } + session.Set("recentlyEdited", recentlyEdited) + session.Save() + return editedThings +} + func editNote(c *gin.Context) { title := c.Param("title") if title == "ws" { @@ -261,6 +285,7 @@ func editNote(c *gin.Context) { splitStrings := strings.Split(title, ".") suffix := splitStrings[len(splitStrings)-1] CodeType := getCodeType(title) + c.HTML(http.StatusOK, "index.tmpl", gin.H{ "Title": title, "WikiName": RuntimeArgs.WikiName, @@ -275,6 +300,7 @@ func editNote(c *gin.Context) { "Coding": len(CodeType) > 0, "CodeType": CodeType, "Suffix": suffix, + "RecentlyEdited": getRecentlyEdited(title, c), }) } } @@ -296,7 +322,8 @@ func everythingElse(c *gin.Context) { p := WikiData{strings.ToLower(title), "", []string{}, []string{}, false, ""} p.save("") } - renderMarkdown(c, currentText, title, versions, "", totalTime, encrypted, noprompt == "-1", len(locked) > 0) + + renderMarkdown(c, currentText, title, versions, "", totalTime, encrypted, noprompt == "-1", len(locked) > 0, getRecentlyEdited(title, c)) } else if option == "/raw" { version := c.DefaultQuery("version", "-1") versionNum, _ := strconv.Atoi(version) @@ -312,7 +339,7 @@ func everythingElse(c *gin.Context) { c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") c.Data(200, contentType(title), []byte(currentText)) } else if title == "ls" && option == "/"+RuntimeArgs.AdminKey && len(RuntimeArgs.AdminKey) > 1 { - renderMarkdown(c, listEverything(), "ls", nil, RuntimeArgs.AdminKey, time.Now().Sub(time.Now()), false, false, false) + renderMarkdown(c, listEverything(), "ls", nil, RuntimeArgs.AdminKey, time.Now().Sub(time.Now()), false, false, false, []string{}) } else if option == "/list" { renderList(c, title) } else if title == "static" { @@ -331,7 +358,7 @@ func serveStaticFile(c *gin.Context, option string) { } } -func renderMarkdown(c *gin.Context, currentText string, title string, versions []versionsInfo, AdminKey string, totalTime time.Duration, encrypted bool, noprompt bool, locked bool) { +func renderMarkdown(c *gin.Context, currentText string, title string, versions []versionsInfo, AdminKey string, totalTime time.Duration, encrypted bool, noprompt bool, locked bool, recentlyEdited []string) { originalText := currentText CodeType := getCodeType(title) if CodeType == "markdown" { @@ -386,6 +413,7 @@ func renderMarkdown(c *gin.Context, currentText string, title string, versions [ "LockedOrEncrypted": locked || encrypted, "Coding": len(CodeType) > 0, "CodeType": CodeType, + "RecentlyEdited": recentlyEdited, }) } @@ -469,9 +497,10 @@ func renderList(c *gin.Context, title string) { listItems[i] = template.HTML([]byte(newHTML)) } c.HTML(http.StatusOK, "list.tmpl", gin.H{ - "Title": title, - "WikiName": RuntimeArgs.WikiName, - "ListItems": listItems, + "Title": title, + "WikiName": RuntimeArgs.WikiName, + "ListItems": listItems, + "RecentlyEdited": getRecentlyEdited(title, c), }) } diff --git a/templates/base.tmpl b/templates/base.tmpl index 879260f..feccdf3 100644 --- a/templates/base.tmpl +++ b/templates/base.tmpl @@ -26,66 +26,64 @@ --> + + {{end}} diff --git a/templates/index.tmpl b/templates/index.tmpl index 4307e0b..16bb1ce 100644 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -126,8 +126,16 @@
  • List
  • View
  • List
  • + diff --git a/templates/view.tmpl b/templates/view.tmpl index 39de818..22df699 100644 --- a/templates/view.tmpl +++ b/templates/view.tmpl @@ -105,8 +105,16 @@ body {
  • List