Skip to content

Commit

Permalink
fix ODir bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mk6i committed Sep 23, 2024
1 parent 92c37bc commit f2d4949
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 16 deletions.
2 changes: 1 addition & 1 deletion foodgroup/locate.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (s LocateService) SetKeywordInfo(ctx context.Context, sess *state.Session,
}
keywords[i] = string(tlv.Value)
i++
if i == len(body.TLVList) {
if i == len(keywords) {
break
}
}
Expand Down
97 changes: 96 additions & 1 deletion foodgroup/locate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func TestLocateService_SetKeywordInfo(t *testing.T) {
wantErr error
}{
{
name: "set keyword info",
name: "set exactly 5 interests",
userSession: newTestSession("test-user"),
inputSNAC: wire.SNACMessage{
Frame: wire.SNACFrame{
Expand Down Expand Up @@ -426,6 +426,101 @@ func TestLocateService_SetKeywordInfo(t *testing.T) {
},
},
},
{
name: "set less than 5 interests",
userSession: newTestSession("test-user"),
inputSNAC: wire.SNACMessage{
Frame: wire.SNACFrame{
RequestID: 1234,
},
Body: wire.SNAC_0x02_0x0F_LocateSetKeywordInfo{
TLVRestBlock: wire.TLVRestBlock{
TLVList: wire.TLVList{
wire.NewTLVBE(wire.ODirTLVInterest, "interest1"),
wire.NewTLVBE(wire.ODirTLVFirstName, "first_name"),
wire.NewTLVBE(wire.ODirTLVInterest, "interest2"),
wire.NewTLVBE(wire.ODirTLVLastName, "last_name"),
wire.NewTLVBE(wire.ODirTLVInterest, "interest3"),
wire.NewTLVBE(wire.ODirTLVInterest, "interest4"),
},
},
},
},
expectOutput: wire.SNACMessage{
Frame: wire.SNACFrame{
FoodGroup: wire.Locate,
SubGroup: wire.LocateSetKeywordReply,
RequestID: 1234,
},
Body: wire.SNAC_0x02_0x10_LocateSetKeywordReply{
Unknown: 1,
},
},
mockParams: mockParams{
profileManagerParams: profileManagerParams{
setKeywordsParams: setKeywordsParams{
{
screenName: state.NewIdentScreenName("test-user"),
keywords: [5]string{
"interest1",
"interest2",
"interest3",
"interest4",
},
},
},
},
},
},
{
name: "set more than 5 interests",
userSession: newTestSession("test-user"),
inputSNAC: wire.SNACMessage{
Frame: wire.SNACFrame{
RequestID: 1234,
},
Body: wire.SNAC_0x02_0x0F_LocateSetKeywordInfo{
TLVRestBlock: wire.TLVRestBlock{
TLVList: wire.TLVList{
wire.NewTLVBE(wire.ODirTLVInterest, "interest1"),
wire.NewTLVBE(wire.ODirTLVFirstName, "first_name"),
wire.NewTLVBE(wire.ODirTLVInterest, "interest2"),
wire.NewTLVBE(wire.ODirTLVLastName, "last_name"),
wire.NewTLVBE(wire.ODirTLVInterest, "interest3"),
wire.NewTLVBE(wire.ODirTLVInterest, "interest4"),
wire.NewTLVBE(wire.ODirTLVInterest, "interest5"),
wire.NewTLVBE(wire.ODirTLVInterest, "interest6"),
},
},
},
},
expectOutput: wire.SNACMessage{
Frame: wire.SNACFrame{
FoodGroup: wire.Locate,
SubGroup: wire.LocateSetKeywordReply,
RequestID: 1234,
},
Body: wire.SNAC_0x02_0x10_LocateSetKeywordReply{
Unknown: 1,
},
},
mockParams: mockParams{
profileManagerParams: profileManagerParams{
setKeywordsParams: setKeywordsParams{
{
screenName: state.NewIdentScreenName("test-user"),
keywords: [5]string{
"interest1",
"interest2",
"interest3",
"interest4",
"interest5",
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
12 changes: 10 additions & 2 deletions server/http/mgmt_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ func getVersionHandler(w http.ResponseWriter, bld config.Build) {
}
}

// getUserAccountHandler handles the GET /directory/category endpoint.
// getDirectoryCategoryHandler handles the GET /directory/category endpoint.
func getDirectoryCategoryHandler(w http.ResponseWriter, manager DirectoryManager, logger *slog.Logger) {
w.Header().Set("Content-Type", "application/json")
categories, err := manager.Categories()
Expand Down Expand Up @@ -678,11 +678,14 @@ func deleteDirectoryCategoryHandler(w http.ResponseWriter, r *http.Request, mana
switch {
case errors.Is(err, state.ErrKeywordCategoryNotFound):
errorMsg(w, "category not found", http.StatusNotFound)
return
case errors.Is(err, state.ErrKeywordInUse):
errorMsg(w, "can't delete because category in use by a user", http.StatusConflict)
return
default:
logger.Error("error in DELETE /directory/category/{id}", "err", err.Error())
errorMsg(w, "internal server error", http.StatusInternalServerError)
return
}
}

Expand Down Expand Up @@ -738,13 +741,15 @@ func postDirectoryKeywordHandler(w http.ResponseWriter, r *http.Request, manager
switch {
case errors.Is(err, state.ErrKeywordCategoryNotFound):
errorMsg(w, "category not found", http.StatusNotFound)
return
case errors.Is(err, state.ErrKeywordExists):
errorMsg(w, "keyword already exists", http.StatusConflict)
return
default:
logger.Error("error in POST /directory/keyword", "err", err.Error())
errorMsg(w, "internal server error", http.StatusInternalServerError)
return
}
return
}

w.WriteHeader(http.StatusCreated)
Expand All @@ -770,11 +775,14 @@ func deleteDirectoryKeywordHandler(w http.ResponseWriter, r *http.Request, manag
switch {
case errors.Is(err, state.ErrKeywordInUse):
errorMsg(w, "can't delete because category in use by a user", http.StatusConflict)
return
case errors.Is(err, state.ErrKeywordNotFound):
errorMsg(w, "keyword not found", http.StatusNotFound)
return
default:
logger.Error("error in DELETE /directory/keyword/{id}", "err", err.Error())
errorMsg(w, "internal server error", http.StatusInternalServerError)
return
}
}

Expand Down
24 changes: 12 additions & 12 deletions state/user_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1522,19 +1522,19 @@ func (f SQLiteUserStore) BuddyIconRefByName(screenName IdentScreenName) (*wire.B

func (f SQLiteUserStore) SetKeywords(name IdentScreenName, keywords [5]string) error {
q := `
WITH interests AS (SELECT CASE WHEN name = ? THEN id END AS aim_keyword1,
CASE WHEN name = ? THEN id END AS aim_keyword2,
CASE WHEN name = ? THEN id END AS aim_keyword3,
CASE WHEN name = ? THEN id END AS aim_keyword4,
CASE WHEN name = ? THEN id END AS aim_keyword5
WITH interests AS (SELECT CASE WHEN name = ? THEN id ELSE NULL END AS aim_keyword1,
CASE WHEN name = ? THEN id ELSE NULL END AS aim_keyword2,
CASE WHEN name = ? THEN id ELSE NULL END AS aim_keyword3,
CASE WHEN name = ? THEN id ELSE NULL END AS aim_keyword4,
CASE WHEN name = ? THEN id ELSE NULL END AS aim_keyword5
FROM aimKeyword
WHERE name IN (?, ?, ?, ?, ?))
UPDATE users
SET aim_keyword1 = (SELECT aim_keyword1 FROM interests WHERE aim_keyword1),
aim_keyword2 = (SELECT aim_keyword2 FROM interests WHERE aim_keyword2),
aim_keyword3 = (SELECT aim_keyword3 FROM interests WHERE aim_keyword3),
aim_keyword4 = (SELECT aim_keyword4 FROM interests WHERE aim_keyword4),
aim_keyword5 = (SELECT aim_keyword5 FROM interests WHERE aim_keyword5)
SET aim_keyword1 = (SELECT aim_keyword1 FROM interests WHERE aim_keyword1 IS NOT NULL),
aim_keyword2 = (SELECT aim_keyword2 FROM interests WHERE aim_keyword2 IS NOT NULL),
aim_keyword3 = (SELECT aim_keyword3 FROM interests WHERE aim_keyword3 IS NOT NULL),
aim_keyword4 = (SELECT aim_keyword4 FROM interests WHERE aim_keyword4 IS NOT NULL),
aim_keyword5 = (SELECT aim_keyword5 FROM interests WHERE aim_keyword5 IS NOT NULL)
WHERE identScreenName = ?
`

Expand Down Expand Up @@ -1766,6 +1766,8 @@ func (f SQLiteUserStore) DeleteKeyword(id uint8) error {
// Conceptually, the list looks like this:
//
// > Animals (top-level keyword, id=0)
// > Artificial Intelligence (keyword, id=3)
// > Cybersecurity (keyword, id=3)
// > Music (category, id=1)
// > Jazz (keyword, id=1)
// > Rock (keyword, id=1)
Expand All @@ -1774,8 +1776,6 @@ func (f SQLiteUserStore) DeleteKeyword(id uint8) error {
// > Soccer (keyword, id=2)
// > Tennis (keyword, id=2)
// > Technology (category, id=3)
// > Artificial Intelligence (keyword, id=3)
// > Cybersecurity (keyword, id=3)
// > Zoology (top-level keyword, id=0)
func (f SQLiteUserStore) InterestList() ([]wire.ODirKeywordListItem, error) {
q := `
Expand Down

0 comments on commit f2d4949

Please sign in to comment.