Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Say what author quick match actually changed instead of always claiming a match

Barnabas4bb3255868ba
diff --git a/lib/services/api_service.dart b/lib/services/api_service.dart
index 1fd83722..c0edba15 100644
--- a/lib/services/api_service.dart
+++ b/lib/services/api_service.dart
@@ -84,6 +84,20 @@ class MediaUploadResult {
const MediaUploadResult({required this.success, this.error});
}
+class AuthorQuickMatchResult {
+ final int statusCode;
+ final bool updated;
+ final Map<String, dynamic>? author;
+
+ const AuthorQuickMatchResult({
+ required this.statusCode,
+ this.updated = false,
+ this.author,
+ });
+
+ bool get found => statusCode == 200;
+}
+
class LibraryMetadataRemovalResult {
final int found;
final int removed;
@@ -1418,29 +1432,73 @@ class ApiService {
/// Quick-match an author against the configured provider (Audible).
/// Server fetches name/asin/description/image, updates the author server-side,
- /// and returns the updated author. Returns null if no match or on error.
+ /// and returns the author. Returns null if no match or on error. A successful
+ /// match can return the unchanged author when its stored details are already
+ /// current.
/// POST /api/authors/:id/match body: { q, region }
- /// Response: { updated: true, author: {...} } on match, { updated: false } on no match.
+ /// Response: { updated, author } on match; a missing provider match is 404.
Future<Map<String, dynamic>?> matchAuthor(
String authorId, {
required String q,
String region = 'us',
}) async {
+ final result = await quickMatchAuthor(authorId, q: q, region: region);
+ if (!result.found) return null;
+ return result.author;
+ }
+
+ /// Quick-match one author using the same ASIN-first request shape as the
+ /// Audiobookshelf web client. The full response is retained so batch callers
+ /// can distinguish a match with no changes from a missing author or a failed
+ /// request.
+ Future<AuthorQuickMatchResult> quickMatchAuthor(
+ String authorId, {
+ String? q,
+ String? asin,
+ String region = 'us',
+ }) async {
+ final trimmedAsin = asin?.trim() ?? '';
+ final trimmedQuery = q?.trim() ?? '';
+ if (trimmedAsin.isEmpty && trimmedQuery.isEmpty) {
+ return const AuthorQuickMatchResult(statusCode: 0);
+ }
+
try {
+ final body = <String, dynamic>{'region': region};
+ if (trimmedAsin.isNotEmpty) {
+ body['asin'] = trimmedAsin;
+ } else {
+ body['q'] = trimmedQuery;
+ }
final r = await _authPost(
Uri.parse('$_cleanBaseUrl/api/authors/$authorId/match'),
- body: jsonEncode({'q': q, 'region': region}),
+ body: jsonEncode(body),
+ timeout: const Duration(seconds: 30),
+ );
+ debugPrint(
+ '[API] quickMatchAuthor $authorId -> ${r.statusCode}: ${r.body}',
);
- debugPrint('[API] matchAuthor $authorId -> ${r.statusCode}: ${r.body}');
if (r.statusCode == 200) {
final data = jsonDecode(r.body) as Map<String, dynamic>;
- // Server signals no match with { updated: false } and no author payload.
- if (data['updated'] == false) return null;
- if (data['author'] is Map) return data['author'] as Map<String, dynamic>;
- return data;
+ final author = data['author'] is Map
+ ? Map<String, dynamic>.from(data['author'] as Map)
+ : null;
+ // Current ABS returns 404 when the provider has no match. Older
+ // servers returned 200 with {updated: false} and no author payload.
+ if (author == null && data['updated'] == false) {
+ return const AuthorQuickMatchResult(statusCode: 404);
+ }
+ return AuthorQuickMatchResult(
+ statusCode: r.statusCode,
+ updated: data['updated'] == true,
+ author: author,
+ );
}
- } catch (e) { debugPrint('matchAuthor error: $e'); }
- return null;
+ return AuthorQuickMatchResult(statusCode: r.statusCode);
+ } catch (e) {
+ debugPrint('quickMatchAuthor error: $e');
+ }
+ return const AuthorQuickMatchResult(statusCode: 0);
}
/// Set the author image from a remote URL.
diff --git a/lib/widgets/author_books_sheet.dart b/lib/widgets/author_books_sheet.dart
index 42efc996..a64688f7 100644
--- a/lib/widgets/author_books_sheet.dart
+++ b/lib/widgets/author_books_sheet.dart
@@ -128,7 +128,7 @@ class _AuthorBooksSheetState extends State<AuthorBooksSheet> {
final lib = context.read<LibraryProvider>();
final auth = context.watch<AuthProvider>();
final headers = lib.mediaHeaders;
- final canEdit = auth.isAdmin && !lib.isOffline;
+ final canEdit = auth.canUpdateMetadata && !lib.isOffline;
if (_isLoading) {
return Column(
diff --git a/lib/widgets/edit_author_sheet.dart b/lib/widgets/edit_author_sheet.dart
index 4db640b3..6e2cfdf1 100644
--- a/lib/widgets/edit_author_sheet.dart
+++ b/lib/widgets/edit_author_sheet.dart
@@ -4,6 +4,7 @@ import 'package:cached_network_image/cached_network_image.dart';
import '../l10n/app_localizations.dart';
import '../providers/auth_provider.dart';
import '../providers/library_provider.dart';
+import 'adaptive_modal.dart';
import 'overlay_toast.dart';
/// Opens a full-screen editor for an author (root admin only).
@@ -26,27 +27,24 @@ void showEditAuthorSheet(
required VoidCallback onUpdated,
required void Function(String mergedIntoId, String mergedIntoName) onMerged,
}) {
- showModalBottomSheet(
+ showAdaptiveSheetDialog(
context: context,
- isScrollControlled: true,
useSafeArea: true,
backgroundColor: Colors.transparent,
- builder: (ctx) => DraggableScrollableSheet(
- expand: false,
- initialChildSize: 0.85,
- minChildSize: 0.3,
- maxChildSize: 0.95,
- snap: true,
- builder: (ctx, sc) => _EditAuthorContent(
- authorId: authorId,
- currentName: currentName,
- currentDescription: currentDescription,
- currentAsin: currentAsin,
- currentImageUrl: currentImageUrl,
- scrollController: sc,
- onUpdated: onUpdated,
- onMerged: onMerged,
- ),
+ expand: false,
+ initialChildSize: 0.85,
+ minChildSize: 0.3,
+ maxChildSize: 0.95,
+ snap: true,
+ builder: (ctx, sc) => _EditAuthorContent(
+ authorId: authorId,
+ currentName: currentName,
+ currentDescription: currentDescription,
+ currentAsin: currentAsin,
+ currentImageUrl: currentImageUrl,
+ scrollController: sc,
+ onUpdated: onUpdated,
+ onMerged: onMerged,
),
);
}
@@ -130,13 +128,18 @@ class _EditAuthorContentState extends State<_EditAuthorContent>
setState(() { _matching = true; _matchResult = null; _matchEmpty = false; });
- final result = await api.matchAuthor(widget.authorId, q: q, region: _region);
+ final match = await api.quickMatchAuthor(
+ widget.authorId,
+ q: q,
+ region: _region,
+ );
+ final result = match.author;
if (!mounted) return;
setState(() {
_matching = false;
if (result == null || result.isEmpty) {
- _matchEmpty = true;
+ _matchEmpty = match.statusCode == 404;
} else {
_matchResult = result;
// Sync the Custom tab fields with the matched values so editing
@@ -150,12 +153,17 @@ class _EditAuthorContentState extends State<_EditAuthorContent>
}
});
- if (result != null && result.isNotEmpty) {
- // The match endpoint already updates the author server-side, so notify parent.
- widget.onUpdated();
- _showToast(AppLocalizations.of(context)!.authorMatched, icon: Icons.check_circle_rounded);
+ final l = AppLocalizations.of(context)!;
+ if (match.found) {
+ if (match.updated) widget.onUpdated();
+ _showToast(
+ match.updated ? l.authorMatched : l.quickMatchNoUpdates,
+ icon: Icons.check_circle_rounded,
+ );
+ } else if (match.statusCode == 404) {
+ _showToast(l.authorNoMatchFound, icon: Icons.search_off_rounded);
} else {
- _showToast(AppLocalizations.of(context)!.authorNoMatchFound, icon: Icons.search_off_rounded);
+ _showToast(l.failedToUpdateMetadata, icon: Icons.error_outline_rounded);
}
}