Kaynağa Gözat

fix ODir bugs

Mike 1 yıl önce
ebeveyn
işleme
f2d49493eb
4 değiştirilmiş dosya ile 119 ekleme ve 16 silme
  1. 1 1
      foodgroup/locate.go
  2. 96 1
      foodgroup/locate_test.go
  3. 10 2
      server/http/mgmt_api.go
  4. 12 12
      state/user_store.go

+ 1 - 1
foodgroup/locate.go

@@ -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
 		}
 	}

+ 96 - 1
foodgroup/locate_test.go

@@ -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{
@@ -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) {

+ 10 - 2
server/http/mgmt_api.go

@@ -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()
@@ -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
 		}
 	}
 
@@ -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)
@@ -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
 		}
 	}
 

+ 12 - 12
state/user_store.go

@@ -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 = ?
 	`
 
@@ -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)
@@ -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 := `