Просмотр исходного кода

fix(googlereader): accept consistent read and kept-unread tags in edit-tag

Removing read or kept-unread failed whenever a read state was already
set, so a=read&r=kept-unread and a=kept-unread&r=read were rejected.
Only reject tags that ask for opposite states. Same for starred.
Fred 8 часов назад
Родитель
Сommit
dd85af8906

+ 2 - 0
internal/googlereader/edit_tag_integration_test.go

@@ -32,6 +32,8 @@ func TestEditTagEndpoint(t *testing.T) {
 		{"remove read", read, false, url.Values{"r": {readStream}}, unread, false},
 		{"add kept-unread", read, false, url.Values{"a": {keptUnreadStream}}, unread, false},
 		{"remove kept-unread", unread, false, url.Values{"r": {keptUnreadStream}}, read, false},
+		{"add read and remove kept-unread", unread, false, url.Values{"a": {readStream}, "r": {keptUnreadStream}}, read, false},
+		{"add kept-unread and remove read", read, false, url.Values{"a": {keptUnreadStream}, "r": {readStream}}, unread, false},
 		{"add starred", unread, false, url.Values{"a": {starredStream}}, unread, true},
 		{"remove starred", unread, true, url.Values{"r": {starredStream}}, unread, false},
 		{"add read and starred", unread, false, url.Values{"a": {readStream, starredStream}}, read, true},

+ 7 - 5
internal/googlereader/handler.go

@@ -1280,18 +1280,20 @@ func (h *greaderHandler) markAllAsReadHandler(w http.ResponseWriter, r *http.Req
 	response.Text(w, r, "OK")
 }
 
+// checkAndSimplifyTags returns the read and starred states requested by the added and removed tags.
+// kept-unread is stored as ReadStream=false, so a=read&r=kept-unread both mean read and are accepted.
+// Tags are rejected only when they ask for opposite states, like a=read&r=read.
 func checkAndSimplifyTags(addTags []Stream, removeTags []Stream) (map[StreamType]bool, error) {
 	tags := make(map[StreamType]bool)
 	for _, s := range addTags {
 		switch s.Type {
 		case ReadStream:
-			// kept-unread is stored as ReadStream=false.
 			if read, ok := tags[ReadStream]; ok && !read {
 				return nil, errSimultaneously
 			}
 			tags[ReadStream] = true
 		case KeptUnreadStream:
-			if _, ok := tags[ReadStream]; ok {
+			if read, ok := tags[ReadStream]; ok && read {
 				return nil, errSimultaneously
 			}
 			tags[ReadStream] = false
@@ -1306,17 +1308,17 @@ func checkAndSimplifyTags(addTags []Stream, removeTags []Stream) (map[StreamType
 	for _, s := range removeTags {
 		switch s.Type {
 		case ReadStream:
-			if _, ok := tags[ReadStream]; ok {
+			if read, ok := tags[ReadStream]; ok && read {
 				return nil, errSimultaneously
 			}
 			tags[ReadStream] = false
 		case KeptUnreadStream:
-			if _, ok := tags[ReadStream]; ok {
+			if read, ok := tags[ReadStream]; ok && !read {
 				return nil, errSimultaneously
 			}
 			tags[ReadStream] = true
 		case StarredStream:
-			if _, ok := tags[StarredStream]; ok {
+			if starred, ok := tags[StarredStream]; ok && starred {
 				return nil, fmt.Errorf("googlereader: %s should not be supplied for add and remove simultaneously", starredStreamSuffix)
 			}
 			tags[StarredStream] = false

+ 7 - 0
internal/googlereader/handler_test.go

@@ -33,6 +33,11 @@ func TestCheckAndSimplifyTags(t *testing.T) {
 		{"add starred", []Stream{starred}, nil, map[StreamType]bool{StarredStream: true}},
 		{"remove starred", nil, []Stream{starred}, map[StreamType]bool{StarredStream: false}},
 		{"add read twice", []Stream{read, read}, nil, map[StreamType]bool{ReadStream: true}},
+		{"add kept-unread twice", []Stream{keptUnread, keptUnread}, nil, map[StreamType]bool{ReadStream: false}},
+		{"remove read twice", nil, []Stream{read, read}, map[StreamType]bool{ReadStream: false}},
+		{"remove starred twice", nil, []Stream{starred, starred}, map[StreamType]bool{StarredStream: false}},
+		{"add read and remove kept-unread", []Stream{read}, []Stream{keptUnread}, map[StreamType]bool{ReadStream: true}},
+		{"add kept-unread and remove read", []Stream{keptUnread}, []Stream{read}, map[StreamType]bool{ReadStream: false}},
 		{"add read and starred", []Stream{read, starred}, nil, map[StreamType]bool{ReadStream: true, StarredStream: true}},
 		{"add read and remove starred", []Stream{read}, []Stream{starred}, map[StreamType]bool{ReadStream: true, StarredStream: false}},
 		{"like and broadcast are ignored", []Stream{like, broadcast}, []Stream{like, broadcast}, map[StreamType]bool{}},
@@ -65,6 +70,8 @@ func TestCheckAndSimplifyTagsRejectsConflicts(t *testing.T) {
 		{"add read then kept-unread", []Stream{read, keptUnread}, nil},
 		{"add kept-unread then read", []Stream{keptUnread, read}, nil},
 		{"add and remove kept-unread", []Stream{keptUnread}, []Stream{keptUnread}},
+		{"remove read and kept-unread", nil, []Stream{read, keptUnread}},
+		{"remove kept-unread and read", nil, []Stream{keptUnread, read}},
 		{"add and remove starred", []Stream{starred}, []Stream{starred}},
 	}