Skip to content

Commit 7ad9060

Browse files
authored
Merge pull request #264 from Monilprajapati/docsFix
Docs cache reset logic and articles sorting
2 parents f1f75c4 + e5fcc28 commit 7ad9060

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

shared.go

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21199,12 +21199,16 @@ func GetDocs(resp http.ResponseWriter, request *http.Request) {
2119921199

2120021200
cacheKey = fmt.Sprintf("%s_%s", cacheKey, path)
2120121201

21202-
cache, err := GetCache(ctx, cacheKey)
21203-
if err == nil {
21204-
cacheData := []byte(cache.([]uint8))
21205-
resp.WriteHeader(200)
21206-
resp.Write(cacheData)
21207-
return
21202+
resetCache := request.URL.Query().Get("resetCache") == "true"
21203+
21204+
if !resetCache {
21205+
cache, err := GetCache(ctx, cacheKey)
21206+
if err == nil {
21207+
cacheData := []byte(cache.([]uint8))
21208+
resp.WriteHeader(200)
21209+
resp.Write(cacheData)
21210+
return
21211+
}
2120821212
}
2120921213

2121021214
owner := "shuffle"
@@ -21371,13 +21375,18 @@ func GetDocList(resp http.ResponseWriter, request *http.Request) {
2137121375
}
2137221376

2137321377
cacheKey := fmt.Sprintf("docs_list_%s", path)
21374-
cache, err := GetCache(ctx, cacheKey)
2137521378
result := FileList{}
21376-
if err == nil {
21377-
cacheData := []byte(cache.([]uint8))
21378-
resp.WriteHeader(200)
21379-
resp.Write(cacheData)
21380-
return
21379+
21380+
resetCache := request.URL.Query().Get("resetCache") == "true"
21381+
21382+
if !resetCache {
21383+
cache, err := GetCache(ctx, cacheKey)
21384+
if err == nil {
21385+
cacheData := []byte(cache.([]uint8))
21386+
resp.WriteHeader(200)
21387+
resp.Write(cacheData)
21388+
return
21389+
}
2138121390
}
2138221391

2138321392
client := github.NewClient(nil)
@@ -21404,6 +21413,25 @@ func GetDocList(resp http.ResponseWriter, request *http.Request) {
2140421413
continue
2140521414
}
2140621415

21416+
publishedDate := time.Now().Unix()
21417+
if path == "articles" {
21418+
21419+
commits, resp, err := client.Repositories.ListCommits(ctx, owner, repo, &github.CommitsListOptions{
21420+
Path: fmt.Sprintf("%s/%s", path, *item.Name),
21421+
})
21422+
21423+
if err != nil {
21424+
log.Printf("[WARNING] Failed getting commits for %s: %s", *item.Name, err)
21425+
if resp != nil {
21426+
log.Printf("[DEBUG] Response status: %d", resp.StatusCode)
21427+
}
21428+
} else {
21429+
if len(commits) > 0 {
21430+
publishedDate = commits[len(commits)-1].Commit.Author.Date.Unix()
21431+
}
21432+
}
21433+
}
21434+
2140721435
// FIXME: Scuffed readtime calc
2140821436
// Average word length = 5. Space = 1. 5+1 = 6 avg.
2140921437
// Words = *item.Size/6/250
@@ -21412,6 +21440,7 @@ func GetDocList(resp http.ResponseWriter, request *http.Request) {
2141221440
githubResp := GithubResp{
2141321441
Name: (*item.Name)[0 : len(*item.Name)-3],
2141421442
Contributors: []GithubAuthor{},
21443+
PublishedDate: publishedDate,
2141521444
Edited: "",
2141621445
ReadTime: *item.Size / 6 / 250,
2141721446
Link: fmt.Sprintf("https://github.com/%s/%s/blob/master/%s/%s", owner, repo, path, *item.Name),
@@ -21420,6 +21449,13 @@ func GetDocList(resp http.ResponseWriter, request *http.Request) {
2142021449
names = append(names, githubResp)
2142121450
}
2142221451

21452+
if path == "articles" {
21453+
// Sort articles by published date (newest first)
21454+
sort.Slice(names, func(i, j int) bool {
21455+
return names[i].PublishedDate > names[j].PublishedDate
21456+
})
21457+
}
21458+
2142321459
//log.Printf(names)
2142421460
result.Success = true
2142521461
result.Reason = "Success"

0 commit comments

Comments
 (0)