Skip to content

Commit

Permalink
Implement saving the continue point for kavita read command
Browse files Browse the repository at this point in the history
  • Loading branch information
l7ssha committed Nov 12, 2024
1 parent c19d71f commit 2b6268d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/src/commands/kavita.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ final kavita = ChatGroup('kavita', 'Kavita related commands', children: [
ChatCommand(
'read',
'Read series',
id('kavita-read', (ChatContext context, int seriesId, [KavitaUserConfig? config]) async {
id('kavita-read', (ChatContext context, int seriesId,
[bool saveReadProgress = true, KavitaUserConfig? config]) async {
final client = await getKavitaClient(config, context);

final continuePoint = await client.getContinuePoint(seriesId);
final paginator = await pagination.factories(
await generateReadingPaginationFactories(continuePoint, client).toList(),
startIndex: continuePoint.pagesRead);
await generateReadingPaginationFactories(continuePoint, client, seriesId, saveReadProgress).toList(),
startIndex: continuePoint.pagesRead,
userId: context.user.id);

return context.respond(paginator);
}),
Expand Down
24 changes: 24 additions & 0 deletions lib/src/modules/kavita.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ class AuthenticatedKavitaClient {
return (body['series'] as List<dynamic>).map((e) => SeriesItem.fromJson(e as Map<String, dynamic>));
}

Future<bool> saveContinuePoint(int seriesId, int volumeId, int chapterId, int page) async {
final result = await _post('/api/Reader/progress',
body: {
'seriesId': seriesId,
'volumeId': volumeId,
'chapterId': chapterId,
'pageNum': page,
},
authToken: true);

return result.body == 'true';
}

Future<ContinuePoint> getContinuePoint(int seriesId) async {
final result = await _get("/api/reader/continue-point",
parameters: {
Expand Down Expand Up @@ -143,6 +156,17 @@ class AuthenticatedKavitaClient {
return await http.get(uri, headers: _makeHeaders(authToken: authToken));
}

Future<http.Response> _post(String path,
{Object? body,
Map<String, String> parameters = const {},
bool authToken = false,
bool authApiKey = false}) async {
return await http.post(
Uri.parse('$baseUrl$path').replace(queryParameters: _makeQueryParameters(parameters, authApiKey: authApiKey)),
headers: _makeHeaders(authToken: authToken),
body: jsonEncode(body));
}

Map<String, String> _makeQueryParameters(Map<String, String> parameters, {bool authApiKey = false}) => {
...parameters,
if (authApiKey) 'apiKey': apiKey,
Expand Down
10 changes: 8 additions & 2 deletions lib/src/util/kavita.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ Stream<MessageBuilder> getSearchEmbedPages(Iterable<SeriesItem> items, Authentic
}

Stream<FutureOr<MessageBuilder> Function()> generateReadingPaginationFactories(
ContinuePoint continuePoint, AuthenticatedKavitaClient client) async* {
ContinuePoint continuePoint, AuthenticatedKavitaClient client, int seriesId, bool saveReadProgress) async* {
for (var i = 0; i < continuePoint.pages; i += 1) {
yield () => generateReadingPage(i, continuePoint.chapterId, client);
yield () {
if (saveReadProgress) {
client.saveContinuePoint(seriesId, continuePoint.volumeId, continuePoint.chapterId, i + 1);
}

return generateReadingPage(i, continuePoint.chapterId, client);
};
}
}

Expand Down

0 comments on commit 2b6268d

Please sign in to comment.