Skip to content

Commit e5fcc28

Browse files
Docs cache reset logic and articles sorting
1 parent 89502c4 commit e5fcc28

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
@@ -21140,12 +21140,16 @@ func GetDocs(resp http.ResponseWriter, request *http.Request) {
2114021140

2114121141
cacheKey = fmt.Sprintf("%s_%s", cacheKey, path)
2114221142

21143-
cache, err := GetCache(ctx, cacheKey)
21144-
if err == nil {
21145-
cacheData := []byte(cache.([]uint8))
21146-
resp.WriteHeader(200)
21147-
resp.Write(cacheData)
21148-
return
21143+
resetCache := request.URL.Query().Get("resetCache") == "true"
21144+
21145+
if !resetCache {
21146+
cache, err := GetCache(ctx, cacheKey)
21147+
if err == nil {
21148+
cacheData := []byte(cache.([]uint8))
21149+
resp.WriteHeader(200)
21150+
resp.Write(cacheData)
21151+
return
21152+
}
2114921153
}
2115021154

2115121155
owner := "shuffle"
@@ -21312,13 +21316,18 @@ func GetDocList(resp http.ResponseWriter, request *http.Request) {
2131221316
}
2131321317

2131421318
cacheKey := fmt.Sprintf("docs_list_%s", path)
21315-
cache, err := GetCache(ctx, cacheKey)
2131621319
result := FileList{}
21317-
if err == nil {
21318-
cacheData := []byte(cache.([]uint8))
21319-
resp.WriteHeader(200)
21320-
resp.Write(cacheData)
21321-
return
21320+
21321+
resetCache := request.URL.Query().Get("resetCache") == "true"
21322+
21323+
if !resetCache {
21324+
cache, err := GetCache(ctx, cacheKey)
21325+
if err == nil {
21326+
cacheData := []byte(cache.([]uint8))
21327+
resp.WriteHeader(200)
21328+
resp.Write(cacheData)
21329+
return
21330+
}
2132221331
}
2132321332

2132421333
client := github.NewClient(nil)
@@ -21345,6 +21354,25 @@ func GetDocList(resp http.ResponseWriter, request *http.Request) {
2134521354
continue
2134621355
}
2134721356

21357+
publishedDate := time.Now().Unix()
21358+
if path == "articles" {
21359+
21360+
commits, resp, err := client.Repositories.ListCommits(ctx, owner, repo, &github.CommitsListOptions{
21361+
Path: fmt.Sprintf("%s/%s", path, *item.Name),
21362+
})
21363+
21364+
if err != nil {
21365+
log.Printf("[WARNING] Failed getting commits for %s: %s", *item.Name, err)
21366+
if resp != nil {
21367+
log.Printf("[DEBUG] Response status: %d", resp.StatusCode)
21368+
}
21369+
} else {
21370+
if len(commits) > 0 {
21371+
publishedDate = commits[len(commits)-1].Commit.Author.Date.Unix()
21372+
}
21373+
}
21374+
}
21375+
2134821376
// FIXME: Scuffed readtime calc
2134921377
// Average word length = 5. Space = 1. 5+1 = 6 avg.
2135021378
// Words = *item.Size/6/250
@@ -21353,6 +21381,7 @@ func GetDocList(resp http.ResponseWriter, request *http.Request) {
2135321381
githubResp := GithubResp{
2135421382
Name: (*item.Name)[0 : len(*item.Name)-3],
2135521383
Contributors: []GithubAuthor{},
21384+
PublishedDate: publishedDate,
2135621385
Edited: "",
2135721386
ReadTime: *item.Size / 6 / 250,
2135821387
Link: fmt.Sprintf("https://github.com/%s/%s/blob/master/%s/%s", owner, repo, path, *item.Name),
@@ -21361,6 +21390,13 @@ func GetDocList(resp http.ResponseWriter, request *http.Request) {
2136121390
names = append(names, githubResp)
2136221391
}
2136321392

21393+
if path == "articles" {
21394+
// Sort articles by published date (newest first)
21395+
sort.Slice(names, func(i, j int) bool {
21396+
return names[i].PublishedDate > names[j].PublishedDate
21397+
})
21398+
}
21399+
2136421400
//log.Printf(names)
2136521401
result.Success = true
2136621402
result.Reason = "Success"

0 commit comments

Comments
 (0)