Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 48 additions & 12 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -21140,12 +21140,16 @@ func GetDocs(resp http.ResponseWriter, request *http.Request) {

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

cache, err := GetCache(ctx, cacheKey)
if err == nil {
cacheData := []byte(cache.([]uint8))
resp.WriteHeader(200)
resp.Write(cacheData)
return
resetCache := request.URL.Query().Get("resetCache") == "true"

if !resetCache {
cache, err := GetCache(ctx, cacheKey)
if err == nil {
cacheData := []byte(cache.([]uint8))
resp.WriteHeader(200)
resp.Write(cacheData)
return
}
}

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

cacheKey := fmt.Sprintf("docs_list_%s", path)
cache, err := GetCache(ctx, cacheKey)
result := FileList{}
if err == nil {
cacheData := []byte(cache.([]uint8))
resp.WriteHeader(200)
resp.Write(cacheData)
return

resetCache := request.URL.Query().Get("resetCache") == "true"

if !resetCache {
cache, err := GetCache(ctx, cacheKey)
if err == nil {
cacheData := []byte(cache.([]uint8))
resp.WriteHeader(200)
resp.Write(cacheData)
return
}
}

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

publishedDate := time.Now().Unix()
if path == "articles" {

commits, resp, err := client.Repositories.ListCommits(ctx, owner, repo, &github.CommitsListOptions{
Path: fmt.Sprintf("%s/%s", path, *item.Name),
})

if err != nil {
log.Printf("[WARNING] Failed getting commits for %s: %s", *item.Name, err)
if resp != nil {
log.Printf("[DEBUG] Response status: %d", resp.StatusCode)
}
} else {
if len(commits) > 0 {
publishedDate = commits[len(commits)-1].Commit.Author.Date.Unix()
}
}
}

// FIXME: Scuffed readtime calc
// Average word length = 5. Space = 1. 5+1 = 6 avg.
// Words = *item.Size/6/250
Expand All @@ -21353,6 +21381,7 @@ func GetDocList(resp http.ResponseWriter, request *http.Request) {
githubResp := GithubResp{
Name: (*item.Name)[0 : len(*item.Name)-3],
Contributors: []GithubAuthor{},
PublishedDate: publishedDate,
Edited: "",
ReadTime: *item.Size / 6 / 250,
Link: fmt.Sprintf("https://github.com/%s/%s/blob/master/%s/%s", owner, repo, path, *item.Name),
Expand All @@ -21361,6 +21390,13 @@ func GetDocList(resp http.ResponseWriter, request *http.Request) {
names = append(names, githubResp)
}

if path == "articles" {
// Sort articles by published date (newest first)
sort.Slice(names, func(i, j int) bool {
return names[i].PublishedDate > names[j].PublishedDate
})
}

//log.Printf(names)
result.Success = true
result.Reason = "Success"
Expand Down
Loading