Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/src/core/network/lemmy_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class LemmyApi {

/// Fetches a list of posts from the Lemmy API
Future<Map<String, dynamic>> getPosts({
int? page,
String? cursor,
int? limit,
FeedListType? feedListType,
Expand All @@ -214,17 +215,19 @@ class LemmyApi {
bool? showHidden,
bool? showSaved,
}) async {
final queryParams = {
Map<String, dynamic> queryParams = {
'type_': feedListType?.value,
'sort': postSortType?.value,
'page_cursor': cursor,
'page': page,
'limit': limit,
'community_name': communityName,
'community_id': communityId,
'saved_only': showSaved,
'show_hidden': showHidden,
};

if (showSaved == true) queryParams['saved_only'] = showSaved;
if (showHidden == true) queryParams['show_hidden'] = showHidden;

final json = await _request(HttpMethod.get, '/api/v3/post/list', queryParams);
return {
'posts': json['posts'].map<ThunderPost>((pv) => ThunderPost.fromLemmyPostView(pv)).toList(),
Expand Down
3 changes: 2 additions & 1 deletion lib/src/features/feed/presentation/bloc/feed_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:bloc/bloc.dart';
import 'package:bloc_concurrency/bloc_concurrency.dart';
import 'package:equatable/equatable.dart';
import 'package:stream_transform/stream_transform.dart';
import 'package:thunder/src/core/network/lemmy_api.dart';

import 'package:thunder/src/features/account/account.dart';
import 'package:thunder/src/features/comment/comment.dart';
Expand Down Expand Up @@ -655,7 +656,7 @@ class FeedBloc extends Bloc<FeedEvent, FeedState> {
));
} catch (e) {
debugPrint('Error fetching feed: $e');
return emit(state.copyWith(status: FeedStatus.failure, message: e.toString()));
return emit(state.copyWith(status: FeedStatus.failure, message: e is LemmyApiException ? e.message : e.toString()));
}
}

Expand Down
16 changes: 14 additions & 2 deletions lib/src/features/post/data/repositories/post_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,13 @@ class PostRepositoryImpl implements PostRepository {
}) async {
switch (account.platform) {
case ThreadiversePlatform.lemmy:
return await lemmy.getPosts(
cursor: cursor,
// Use page-based pagination for Lemmy for 0.19.x instances as there are some performance issues with cursor-based pagination.
// See https://github.com/LemmyNet/lemmy/issues/6171, https://lemmy.world/post/40266465/21176898
// TODO: Once 1.x.x is released, we can switch back to cursor-based pagination.
final page = cursor != null ? int.tryParse(cursor) ?? 1 : 1;

final response = await lemmy.getPosts(
page: page,
limit: limit,
postSortType: postSortType,
feedListType: feedListType,
Expand All @@ -176,6 +181,13 @@ class PostRepositoryImpl implements PostRepository {
showHidden: showHidden,
showSaved: showSaved,
);

// Return next page as string cursor for Lemmy
final nextPage = response['posts'].isNotEmpty ? (page + 1).toString() : null;
return {
'posts': response['posts'],
'next_page': nextPage,
};
case ThreadiversePlatform.piefed:
// PieFed uses integer page-based pagination. The cursor in this case is the page number.
final page = cursor != null ? int.tryParse(cursor) ?? 1 : 1;
Expand Down