From 4810327874b65b675beb7317f61f33aad1194500 Mon Sep 17 00:00:00 2001 From: seantywork Date: Wed, 24 Jul 2024 09:32:50 +0900 Subject: [PATCH] Add author, timestamp, sort logic --- ctl/base.go | 27 ++++++---- pkg/dbquery/dbquery.go | 26 ++++++++-- pkg/edition/edition.go | 10 ++-- pkg/edition/edition_etc.go | 8 +-- public/js/common.js | 98 +++++++++++++++++++++++++++++++++++++ public/js/index/index.js | 19 ++++--- public/js/mypage/article.js | 14 +++--- public/js/mypage/index.js | 7 --- view/index/index.html | 1 + view/mypage/article.html | 2 +- view/mypage/index.html | 1 + 11 files changed, 168 insertions(+), 45 deletions(-) create mode 100644 public/js/common.js diff --git a/ctl/base.go b/ctl/base.go index 9e1a489..64f7796 100644 --- a/ctl/base.go +++ b/ctl/base.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "time" "github.com/gin-gonic/gin" pkgauth "github.com/seantywork/sorrylinus-again/pkg/auth" @@ -14,9 +15,11 @@ import ( type EntryStruct struct { Entry []struct { - Title string `json:"title"` - Id string `json:"id"` - Type string `json:"type"` + Title string `json:"title"` + Id string `json:"id"` + Type string `json:"type"` + Timestamp time.Time `json:"timestamp"` + Author string `json:"author"` } `json:"entry"` } @@ -254,17 +257,23 @@ func GetMediaEntry(c *gin.Context) { for k, v := range em { + t, _ := time.Parse("2006-01-02-15-04-05", v.CreatedAt) + if v.Type == "article" { entry.Entry = append(entry.Entry, struct { - Title string `json:"title"` - Id string `json:"id"` - Type string `json:"type"` + Title string `json:"title"` + Id string `json:"id"` + Type string `json:"type"` + Timestamp time.Time `json:"timestamp"` + Author string `json:"author"` }{ - Title: v.PlainName, - Id: k, - Type: "article", + Title: v.PlainName, + Id: k, + Type: "article", + Timestamp: t, + Author: v.Author, }) } else { diff --git a/pkg/dbquery/dbquery.go b/pkg/dbquery/dbquery.go index 08ed3d7..86c29dd 100644 --- a/pkg/dbquery/dbquery.go +++ b/pkg/dbquery/dbquery.go @@ -28,9 +28,11 @@ type SessionStruct struct { type MediaStruct struct { ISPublic bool `json:"is_public"` + CreatedAt string `json:"created_at"` Type string `json:"type"` Extension string `json:"extension"` PlainName string `json:"plain_name"` + Author string `json:"author"` AllowedId []string `json:"allowed_id"` } @@ -493,14 +495,20 @@ func MakeSessionForUser(session_key string, id string, duration_seconds int) err return nil } -func UploadArticle(content string, plain_name string, new_name string) error { +func UploadArticle(author string, content string, plain_name string, new_name string) error { ms := MediaStruct{} + c_time := time.Now() + + c_time_fmt := c_time.Format("2006-01-02-15-04-05") + ms.ISPublic = true ms.Type = "article" ms.PlainName = plain_name ms.Extension = "json" + ms.CreatedAt = c_time_fmt + ms.Author = author this_file_path := mediaPath + new_name + ".json" @@ -645,7 +653,7 @@ func GetArticle(media_key string) (string, error) { } -func UploadImage(c *gin.Context, file *multipart.FileHeader, filename string, new_filename string, extension string) error { +func UploadImage(c *gin.Context, author string, file *multipart.FileHeader, filename string, new_filename string, extension string) error { ms := MediaStruct{} @@ -653,10 +661,16 @@ func UploadImage(c *gin.Context, file *multipart.FileHeader, filename string, ne this_image_path := imagePath + new_filename + "." + extension + c_time := time.Now() + + c_time_fmt := c_time.Format("2006-01-02-15-04-05") + ms.ISPublic = true ms.Type = "image" ms.PlainName = filename ms.Extension = extension + ms.CreatedAt = c_time_fmt + ms.Author = author jb, err := json.Marshal(ms) @@ -755,7 +769,7 @@ func DownloadImage(c *gin.Context, watchId string) error { return nil } -func UploadVideo(c *gin.Context, file *multipart.FileHeader, filename string, new_filename string, extension string) error { +func UploadVideo(c *gin.Context, author string, file *multipart.FileHeader, filename string, new_filename string, extension string) error { ms := MediaStruct{} @@ -763,10 +777,16 @@ func UploadVideo(c *gin.Context, file *multipart.FileHeader, filename string, ne this_video_path := videoPath + new_filename + "." + extension + c_time := time.Now() + + c_time_fmt := c_time.Format("2006-01-02-15-04-05") + ms.ISPublic = true ms.Type = "video" ms.PlainName = filename ms.Extension = extension + ms.CreatedAt = c_time_fmt + ms.Author = author jb, err := json.Marshal(ms) diff --git a/pkg/edition/edition.go b/pkg/edition/edition.go index 54b5997..d2d9d26 100644 --- a/pkg/edition/edition.go +++ b/pkg/edition/edition.go @@ -23,7 +23,7 @@ type ArticleInfo struct { func PostArticleUpload(c *gin.Context) { - _, my_type, _ := pkgauth.WhoAmI(c) + _, my_type, my_id := pkgauth.WhoAmI(c) if my_type != "admin" { @@ -64,7 +64,7 @@ func PostArticleUpload(c *gin.Context) { plain_name := pkgauth.SanitizePlainNameValue(a_info.Title) - err = dbquery.UploadArticle(a_info.Content, plain_name, new_file_name) + err = dbquery.UploadArticle(my_id, a_info.Content, plain_name, new_file_name) if err != nil { @@ -163,7 +163,7 @@ func GetArticleContentById(c *gin.Context) { func PostMediaUpload(c *gin.Context) { - _, my_type, _ := pkgauth.WhoAmI(c) + _, my_type, my_id := pkgauth.WhoAmI(c) if my_type != "admin" { @@ -221,11 +221,11 @@ func PostMediaUpload(c *gin.Context) { if mediaType == "image" { - err = dbquery.UploadImage(c, file, v_fname, file_name, mediaExt) + err = dbquery.UploadImage(c, my_id, file, v_fname, file_name, mediaExt) } else if mediaType == "video" { - err = dbquery.UploadVideo(c, file, v_fname, file_name, mediaExt) + err = dbquery.UploadVideo(c, my_id, file, v_fname, file_name, mediaExt) } diff --git a/pkg/edition/edition_etc.go b/pkg/edition/edition_etc.go index 0bcad25..f29de94 100644 --- a/pkg/edition/edition_etc.go +++ b/pkg/edition/edition_etc.go @@ -15,7 +15,7 @@ import ( func PostVideoUpload(c *gin.Context) { - _, my_type, _ := pkgauth.WhoAmI(c) + _, my_type, my_id := pkgauth.WhoAmI(c) if my_type != "admin" { @@ -62,7 +62,7 @@ func PostVideoUpload(c *gin.Context) { file_name, _ := pkgutils.GetRandomHex(32) - err := pkgdbq.UploadVideo(c, file, v_fname, file_name, extension) + err := pkgdbq.UploadVideo(c, my_id, file, v_fname, file_name, extension) if err != nil { @@ -163,7 +163,7 @@ func GetVideoContentByID(c *gin.Context) { func PostImageUpload(c *gin.Context) { - _, my_type, _ := pkgauth.WhoAmI(c) + _, my_type, my_id := pkgauth.WhoAmI(c) if my_type != "admin" { @@ -210,7 +210,7 @@ func PostImageUpload(c *gin.Context) { file_name, _ := pkgutils.GetRandomHex(32) - err := dbquery.UploadImage(c, file, v_fname, file_name, extension) + err := dbquery.UploadImage(c, my_id, file, v_fname, file_name, extension) if err != nil { diff --git a/public/js/common.js b/public/js/common.js new file mode 100644 index 0000000..a45971f --- /dev/null +++ b/public/js/common.js @@ -0,0 +1,98 @@ + +function delayMs (ms) { + + return new Promise(function(res) { + setTimeout(res, ms) + }) +} + + +function getNewDateSortedList(flag, fieldName, orgList){ + + + let newList = [] + + if(flag != "asc" && flag != "desc"){ + return orgList + } + + + + for(let i = 0; i < orgList.length; i ++){ + + + let d1 = Date.parse(orgList[i][fieldName]) + + if(newList.length == 0){ + + newList.push(orgList[i]) + + continue + } + + for(let j = 0; j < newList.length; j ++){ + + + let d2 = Date.parse(newList[j][fieldName]) + + + if(flag == "asc"){ + + if(d1 < d2){ + + newList.splice(j, 0, orgList[i]) + + break + } + + if (d1 >= d2 && (j != newList.length - 1)){ + + continue + + } else if (d1 >= d2 && (j == newList.length - 1)){ + + newList.push(orgList[i]) + + break + } + + + } else if(flag == "desc"){ + + + if(d1 > d2){ + + newList.splice(j, 0, orgList[i]) + + break + } + + if (d1 <= d2 && (j != newList.length - 1)){ + + continue + + } else if (d1 <= d2 && (j == newList.length - 1)){ + + newList.push(orgList[i]) + + break + } + + + } + + + + + } + + } + + + return newList + + + + + +} \ No newline at end of file diff --git a/public/js/index/index.js b/public/js/index/index.js index 69e91e6..9d2e671 100644 --- a/public/js/index/index.js +++ b/public/js/index/index.js @@ -49,14 +49,17 @@ async function getContentList(){ } else { - for(let i = 0; i < contentEntry.entry.length; i ++){ - - contentReader.innerHTML += ` - - ${contentEntry.entry[i].title} - -
- ` + let sortedEntry = getNewDateSortedList("desc", "timestamp", contentEntry.entry) + + for(let i = 0; i < sortedEntry.length; i ++){ + + contentReader.innerHTML += ` + + ${sortedEntry[i].title} + [${sortedEntry[i].author}:${sortedEntry[i].timestamp}] + +
+ ` } } diff --git a/public/js/mypage/article.js b/public/js/mypage/article.js index d80e229..ba0f211 100644 --- a/public/js/mypage/article.js +++ b/public/js/mypage/article.js @@ -294,17 +294,15 @@ async function getArticleList(){ contentReader.innerHTML = "" - for(let i = 0; i < contentEntry.entry.length; i ++){ + let sortedEntry = getNewDateSortedList("desc", "timestamp", contentEntry.entry) - if(contentEntry.entry[i].type != "article"){ - continue - } + for(let i = 0; i < sortedEntry.length; i ++){ contentReader.innerHTML += ` - - ${contentEntry.entry[i].title} - - + + ${sortedEntry[i].title} + [${sortedEntry[i].author}:${sortedEntry[i].timestamp}] +
` articleCount += 1 diff --git a/public/js/mypage/index.js b/public/js/mypage/index.js index 73873ee..7e085fd 100644 --- a/public/js/mypage/index.js +++ b/public/js/mypage/index.js @@ -339,13 +339,6 @@ async function initCCTV(){ } -function delayMs (ms) { - - return new Promise(function(res) { - setTimeout(res, ms) - }) -} - async function testCCTV(){ diff --git a/view/index/index.html b/view/index/index.html index 604a616..a72ce17 100644 --- a/view/index/index.html +++ b/view/index/index.html @@ -46,6 +46,7 @@ My Page + diff --git a/view/mypage/article.html b/view/mypage/article.html index e576d15..b7d19ea 100644 --- a/view/mypage/article.html +++ b/view/mypage/article.html @@ -34,7 +34,7 @@
- + diff --git a/view/mypage/index.html b/view/mypage/index.html index 90cc4d4..caa9009 100644 --- a/view/mypage/index.html +++ b/view/mypage/index.html @@ -76,6 +76,7 @@ + -- 2.43.0