Skip to content

Commit

Permalink
Fixes for v3
Browse files Browse the repository at this point in the history
  • Loading branch information
richmahn committed Dec 6, 2021
1 parent 5d57215 commit 909a968
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
4 changes: 2 additions & 2 deletions models/catalog_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,10 @@ func queryForCatalogV3(e Engine, subject string) (Door43MetadataList, error) {
if err := dm.LoadAttributes(); err != nil {
return nil, err
}
unique := false
unique := true
for j := 0; j < i; j++ {
if dms[j].Repo.LowerName == dm.Repo.LowerName {
unique = true
unique = false
}
}
if unique {
Expand Down
31 changes: 23 additions & 8 deletions modules/convert/catalog_next.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
)

// ToCatalogV3Resource converts a Door43Metadata to a api.CatalogV3 resource entry
Expand Down Expand Up @@ -42,9 +44,9 @@ func ToCatalogV3Resource(dm *models.Door43Metadata) *api.CatalogV3Resource {
}
}

var checking map[string]string
if val, ok := (*dm.Metadata)["dublin_core"].(map[string]interface{})["checking"]; ok && val != nil {
checking = val.(map[string]string)
var checking map[string]interface{}
if val, ok := (*dm.Metadata)["checking"]; ok && val != nil {
checking = val.(map[string]interface{})
}

var comment string
Expand All @@ -69,17 +71,30 @@ func ToCatalogV3Resource(dm *models.Door43Metadata) *api.CatalogV3Resource {
(*dm.Metadata)["dublin_core"].(map[string]interface{})["type"].(string),
format,
(*dm.Metadata)["dublin_core"].(map[string]interface{})["conformsto"].(string))
gitRepo, _ := git.OpenRepository(dm.Repo.RepoPath())
var formatModified time.Time
if gitRepo != nil {
var commit *git.Commit
if dm.ReleaseID == 0 {
commit, _ = gitRepo.GetBranchCommit(dm.Repo.DefaultBranch)
} else {
commit, _ = gitRepo.GetTagCommit(dm.BranchOrTag)
}
if commit != nil {
formatModified = commit.Author.When
}
}
formats = append(formats, map[string]interface{}{
"format": format,
"modified": time.Now(),
"modified": formatModified,
"signature": "",
"size": 0,
"ur": "",
"ur": util.URLJoin(dm.Repo.HTMLURL(), "archive", dm.BranchOrTag+".zip"),
})

var projects []map[string]interface{}
if val, ok := (*dm.Metadata)["dublin_core"].(map[string]interface{})["projects"]; ok && val != nil {
projects = val.([]map[string]interface{})
var projects []interface{}
if val, ok := (*dm.Metadata)["projects"]; ok && val != nil {
projects = val.([]interface{})
}

var relation []interface{}
Expand Down
4 changes: 2 additions & 2 deletions modules/structs/catalog_next.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ type CatalogV3Resource struct {
Title string `json:"title"`
Subject string `json:"subject"`
Version string `json:"version"`
Checking map[string]string `json:"checking"`
Checking map[string]interface{} `json:"checking"`
Comment string `json:"comment"`
Contributor []interface{} `json:"contributor"`
Creator string `json:"creator"`
Description string `json:"description"`
Formats []map[string]interface{} `json:"formats"`
Issued time.Time `json:"issued"`
Modified time.Time `json:"modified"`
Projects []map[string]interface{} `json:"projects"`
Projects []interface{} `json:"projects"`
Publisher string `json:"publisher"`
Relation []interface{} `json:"relation"`
Rights string `json:"rights"`
Expand Down
42 changes: 40 additions & 2 deletions routers/api/catalog/v3/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package v3

import (
"fmt"
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
)

// CatalogV3 search the catalog via options
Expand All @@ -30,7 +32,7 @@ func CatalogV3(ctx *context.APIContext) {
// "$ref": "#/responses/CatalogSearchResultsV5"
// "422":
// "$ref": "#/responses/validationError"

fmt.Printf("\n\nSTART!!!\n\n")
subject := ctx.Query("subject", "")

prodDMs, err := models.QueryForCatalogV3(subject)
Expand All @@ -44,9 +46,13 @@ func CatalogV3(ctx *context.APIContext) {

languages := []*api.CatalogV3Language{}
var currentLang *api.CatalogV3Language
var allLastUpdated timeutil.TimeStamp
var langLastUpdated timeutil.TimeStamp
fmt.Printf("lastUpdated: %d, %s\n", allLastUpdated, allLastUpdated.AsTime())
for _, dm := range prodDMs {
langInfo := (*dm.Metadata)["dublin_core"].(map[string]interface{})["language"].(map[string]interface{})
if currentLang == nil || currentLang.Identifier != langInfo["identifier"].(string) {
langLastUpdated = 0
currentLang = &api.CatalogV3Language{
Identifier: langInfo["identifier"].(string),
Title: langInfo["title"].(string),
Expand All @@ -55,9 +61,41 @@ func CatalogV3(ctx *context.APIContext) {
languages = append(languages, currentLang)
}
currentLang.Resources = append(currentLang.Resources, convert.ToCatalogV3Resource(dm))
fmt.Printf("%d, %s\n", dm.ReleaseDateUnix, dm.ReleaseDateUnix.AsTime())
if dm.ReleaseDateUnix > allLastUpdated {
fmt.Printf("%d => %d, %s => %s", allLastUpdated, dm.ReleaseDateUnix, allLastUpdated.AsTime(), dm.ReleaseDateUnix.AsTime())
allLastUpdated = dm.ReleaseDateUnix
}
if dm.ReleaseDateUnix > langLastUpdated {
langLastUpdated = dm.ReleaseDateUnix
currentLang.LastUpdated = langLastUpdated.AsTime()
}
}

ctx.JSON(http.StatusOK, api.CatalogV3{
Languages: languages,
Catalogs: []map[string]string{
{
"identifier": "langnames",
"modified": "2016-10-03",
"url": "https://td.unfoldingword.org/exports/langnames.json",
},
{
"identifier": "temp-langnames",
"modified": "2016-10-03",
"url": "https://td.unfoldingword.org/api/templanguages/",
},
{
"identifier": "approved-temp-langnames",
"modified": "2016-10-03",
"url": "https://td.unfoldingword.org/api/templanguages/assignment/changed/",
},
{
"identifier": "new-language-questions",
"modified": "2016-10-03",
"url": "https://td.unfoldingword.org/api/questionnaire/",
},
},
Languages: languages,
LastUpdated: allLastUpdated.AsTime(),
})
}

0 comments on commit 909a968

Please sign in to comment.