Skip to content

Commit

Permalink
use slug as inveniordm community identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Nov 15, 2024
1 parent 1f9c38c commit bde8a04
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var versionCmd = &cobra.Command{
Short: "Print the version number of commonmeta",
Long: `All software has versions. This is commonmeta's`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Println("Commonmeta v0.5.15 -- HEAD")
cmd.Println("Commonmeta v0.5.16 -- HEAD")
},
}

Expand Down
55 changes: 41 additions & 14 deletions inveniordm/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ func Convert(data commonmeta.Data) (Inveniordm, error) {
}
if len(data.Subjects) > 0 {
for _, v := range data.Subjects {
ID := FOSMappings[v.Subject]
// ID := FOSMappings[v.Subject]
ID := ""
var scheme string
if ID != "" {
scheme = "FOS"
Expand Down Expand Up @@ -387,26 +388,17 @@ func Post(data commonmeta.Data, host string, apiKey string) ([]byte, error) {
if err != nil {
return nil, err
}
var communityID string
var communitySlug string
var communityIndex int
for i, v := range data.Relations {
if v.Type == "IsPartOf" && strings.HasPrefix(v.ID, "https://rogue-scholar.org/api/communities/") {
communityID = strings.Split(v.ID, "/")[5]
communitySlug = strings.Split(v.ID, "/")[5]
communityIndex = i
}
if communityIndex != 0 {
data.Relations = slices.Delete(data.Relations, i, i)
}
}
type Community struct {
ID string `json:"id"`
}
type Communities struct {
Communities []Community `json:"communities"`
}
community := Community{ID: communityID}
var communities Communities
communities.Communities = append(communities.Communities, community)

// create draft record
draftURL := url.URL{
Expand Down Expand Up @@ -451,14 +443,49 @@ func Post(data commonmeta.Data, host string, apiKey string) ([]byte, error) {
}
resp, err = client.Do(req)
defer resp.Body.Close()
record, _ := io.ReadAll(resp.Body)
if resp.StatusCode >= 400 {
return record, err
}

// add record to community
communityURL := url.URL{
Scheme: "https",
Host: host,
Path: "/api/communities/" + communitySlug,
}
req, _ = http.NewRequest("GET", communityURL.String(), nil)
req.Header = http.Header{
"Content-Type": {"application/json"},
"Authorization": {"Bearer " + apiKey},
}
client = &http.Client{
Timeout: time.Second * 10,
}
resp, err = client.Do(req)
defer resp.Body.Close()
body, _ = io.ReadAll(resp.Body)
if resp.StatusCode >= 400 {
return body, err
}

// add record to community
var community map[string]interface{}
err = json.Unmarshal(body, &community)
if err != nil {
return nil, err
}
communityID := community["id"].(string)
type Community struct {
ID string `json:"id"`
}
type Communities struct {
Communities []Community `json:"communities"`
}
com := Community{ID: communityID}
var communities Communities
communities.Communities = append(communities.Communities, com)
c, _ := json.Marshal(communities)
communityURL := url.URL{
communityURL = url.URL{
Scheme: "https",
Host: host,
Path: "/api/records/" + ID + "/communities",
Expand Down
5 changes: 2 additions & 3 deletions jsonfeed/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ type Authors []struct {
type Blog struct {
ID string `json:"id"`
Category string `json:"category"`
CommunityID string `json:"community_id"`
Description string `json:"description"`
Favicon string `json:"favicon"`
Funding Funding `json:"funding"`
Expand Down Expand Up @@ -284,9 +283,9 @@ func Read(content Content) (commonmeta.Data, error) {
Type: "IsPartOf",
})
}
if content.Blog.CommunityID != "" {
if content.Blog.Slug != "" {
data.Relations = append(data.Relations, commonmeta.Relation{
ID: utils.CommunityIDAsURL(content.Blog.CommunityID, "rogue-scholar.org"),
ID: utils.CommunitySlugAsURL(content.Blog.Slug, "rogue-scholar.org"),
Type: "IsPartOf",
})
}
Expand Down
8 changes: 4 additions & 4 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,15 @@ func ValidateISSN(issn string) (string, bool) {
return matched[1], true
}

// CommunityIDAsURL returns the InvenioRDM community_id expressed as URL
func CommunityIDAsURL(communityID string, host string) string {
// CommunitySlugAsURL returns the InvenioRDM community slug expressed as globally unique URL
func CommunitySlugAsURL(slug string, host string) string {
if host == "" {
host = "rogue-scholar.org"
}
if communityID == "" {
if slug == "" {
return ""
}
return fmt.Sprintf("https://%s/api/communities/%s", host, communityID)
return fmt.Sprintf("https://%s/api/communities/%s", host, slug)
}

// Sanitize removes all HTML tags except for a whitelist of allowed tags. Used for
Expand Down
6 changes: 3 additions & 3 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,9 @@ func ExampleGetLanguage() {
// deu
}

func ExampleCommunityIDAsURL() {
s := utils.CommunityIDAsURL("3cf0a553-9634-44fc-836b-7fa3e33d1107", "rogue-scholar.org")
func ExampleCommunitySlugAsURL() {
s := utils.CommunitySlugAsURL("irights", "rogue-scholar.org")
fmt.Println(s)
// Output:
// https://rogue-scholar.org/api/communities/3cf0a553-9634-44fc-836b-7fa3e33d1107
// https://rogue-scholar.org/api/communities/irights
}

0 comments on commit bde8a04

Please sign in to comment.