Skip to content

Commit 7a518f5

Browse files
authored
Rewrite/redirect '#<topic>' search expressions to 'topic:<topic>' (#8227)
1 parent 12f2c66 commit 7a518f5

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

app/lib/search/backend.dart

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@ import 'package:meta/meta.dart';
1919
// ignore: implementation_imports
2020
import 'package:pana/src/dartdoc/pub_dartdoc_data.dart';
2121
import 'package:pool/pool.dart';
22-
import 'package:pub_dev/publisher/backend.dart';
23-
24-
import 'package:pub_dev/search/search_client.dart';
25-
import 'package:pub_dev/service/download_counts/backend.dart';
26-
import 'package:pub_dev/shared/popularity_storage.dart';
27-
import 'package:pub_dev/shared/redis_cache.dart';
28-
import 'package:pub_dev/shared/utils.dart';
2922
import 'package:retry/retry.dart';
3023

24+
import '../../publisher/backend.dart';
25+
import '../../service/download_counts/backend.dart';
3126
import '../../service/topics/models.dart';
27+
import '../../shared/popularity_storage.dart';
28+
import '../../shared/redis_cache.dart';
29+
import '../../shared/utils.dart';
3230
import '../package/backend.dart';
3331
import '../package/model_properties.dart';
3432
import '../package/models.dart';
@@ -48,6 +46,7 @@ import 'dart_sdk_mem_index.dart';
4846
import 'flutter_sdk_mem_index.dart';
4947
import 'models.dart';
5048
import 'result_combiner.dart';
49+
import 'search_client.dart';
5150
import 'search_service.dart';
5251
import 'text_utils.dart';
5352

@@ -549,9 +548,28 @@ SearchForm? canonicalizeSearchForm(SearchForm form) {
549548
}
550549
if (newTags != null) {
551550
return form.change(query: query.change(tagsPredicate: newTags).toString());
552-
} else {
553-
return null;
554551
}
552+
553+
final newQueryText = form.parsedQuery.text?.split(' ').map((p) {
554+
if (p.startsWith('#') && p.length > 1) {
555+
final topic = p.substring(1);
556+
// Checking the surface format, and skipping the change if the
557+
// text would be an invalid topic.
558+
if (!isValidTopicFormat(topic)) {
559+
return p;
560+
}
561+
// NOTE: We don't know if this topic exists or spelled correctly.
562+
// We should consider restricting the updates to existing
563+
// topics only (TBD).
564+
return 'topic:$topic';
565+
}
566+
return p;
567+
}).join(' ');
568+
if (newQueryText != form.parsedQuery.text) {
569+
return form.change(query: newQueryText);
570+
}
571+
572+
return null;
555573
}
556574

557575
/// Creates the index-related API data structure from the extracted dartdoc data.

app/lib/service/topics/models.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'dart:io';
77

88
import 'package:json_annotation/json_annotation.dart';
99
import 'package:logging/logging.dart';
10-
import 'package:meta/meta.dart';
1110
import 'package:path/path.dart' as p;
1211
import 'package:pub_dev/frontend/static_files.dart';
1312
import 'package:yaml/yaml.dart';
@@ -92,7 +91,6 @@ class CanonicalTopicFileContent {
9291
}
9392

9493
/// True, if [topic] is formatted like a valid topic.
95-
@visibleForTesting
9694
bool isValidTopicFormat(String topic) =>
9795
RegExp(r'^[a-z0-9-]{2,32}$').hasMatch(topic) &&
9896
!topic.contains('--') &&

app/test/frontend/handlers/redirects_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,19 @@ void main() {
117117
'https://api.flutter.dev/',
118118
);
119119
});
120+
121+
testWithProfile('search canonicalization: topic name', fn: () async {
122+
await expectRedirectResponse(
123+
await issueGet('/packages?q=topic%3Awidgets'),
124+
'/packages?q=topic%3Awidget',
125+
);
126+
});
127+
128+
testWithProfile('search canonicalization: topic shortcut', fn: () async {
129+
await expectRedirectResponse(
130+
await issueGet('/packages?q=%23hash'),
131+
'/packages?q=topic%3Ahash',
132+
);
133+
});
120134
});
121135
}

app/test/search/backend_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,12 @@ void main() {
6969
'topic:widget abc',
7070
);
7171
});
72+
73+
test('query with topic shortcut `#`', () {
74+
expect(
75+
canonicalizeSearchForm(_parse('#widget #testing'))?.query,
76+
'topic:widget topic:testing',
77+
);
78+
});
7279
});
7380
}

0 commit comments

Comments
 (0)