Skip to content
Open
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
23 changes: 16 additions & 7 deletions lib/src/api/threads.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,24 @@ class APIThreads {
int? sourceId,
String? page,
FeedSort? sort,
bool combined = false,
bool includeBoosts = false,
}) async {
switch (client.software) {
case ServerSoftware.mbin:
final path = switch (source) {
FeedSource.all => '/entries',
FeedSource.local => '/entries',
FeedSource.subscribed => '/entries/subscribed',
FeedSource.moderated => '/entries/moderated',
FeedSource.favorited => '/entries/favourited',
FeedSource.community => '/magazine/${sourceId!}/entries',
FeedSource.user => '/users/${sourceId!}/entries',
FeedSource.all => '/${combined ? 'combined' : 'entries'}',
FeedSource.local => '/${combined ? 'combined' : 'entries'}',
FeedSource.subscribed =>
'/${combined ? 'combined' : 'entries'}/subscribed',
FeedSource.moderated =>
'/${combined ? 'combined' : 'entries'}/moderated',
FeedSource.favorited =>
'/${combined ? 'combined' : 'entries'}/favourited',
FeedSource.community =>
'/magazine/${sourceId!}/${combined ? 'combined' : 'entries'}',
FeedSource.user =>
'/users/${sourceId!}/${combined ? 'content' : 'entries'}',
FeedSource.domain => '/domain/${sourceId!}/entries',
FeedSource.feed => throw Exception(
'Feeds source not allowed for mbin',
Expand All @@ -64,10 +71,12 @@ class APIThreads {
'sort': mbinGetSort(sort)?.name,
'time': mbinGetSortTime(sort),
if (source == FeedSource.local) 'federation': 'local',
if (combined && includeBoosts) 'includeBoosts': 'true',
};

final response = await client.get(path, queryParams: query);

if (combined) return PostListModel.fromMbinCombined(response.bodyJson);
return PostListModel.fromMbinEntries(response.bodyJson);

case ServerSoftware.lemmy:
Expand Down
15 changes: 15 additions & 0 deletions lib/src/models/post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ abstract class PostListModel with _$PostListModel {
nextPage: mbinCalcNextPaginationPage(json['pagination']! as JsonMap),
);

factory PostListModel.fromMbinCombined(JsonMap json) => PostListModel(
items: (json['items']! as List<dynamic>)
.map((content) {
if (content['entry'] != null) {
return PostModel.fromMbinEntry(content['entry'] as JsonMap);
}
if (content['post'] != null) {
return PostModel.fromMbinPost(content['post'] as JsonMap);
}
})
.nonNulls
.toList(),
nextPage: mbinCalcNextPaginationPage(json['pagination']! as JsonMap),
);

factory PostListModel.fromLemmy(
JsonMap json, {
required List<(String, int)> langCodeIdPairs,
Expand Down
68 changes: 10 additions & 58 deletions lib/src/screens/feed/feed_agregator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,7 @@ class FeedInputState {
final FeedSource source;
final int? sourceId;
List<PostModel> _leftover = [];
List<PostModel> _combinedThreadsLeftover = [];
List<PostModel> _combinedMicroblogsLeftover = [];
String? _nextPage = '';
String? _combinedPage = '';

Future<(List<PostModel>, String?)> fetchPage(
AppController ac,
Expand Down Expand Up @@ -235,57 +232,15 @@ class FeedInputState {
_nextPage = postListModel.nextPage;
return ([..._leftover, ...postListModel.items], postListModel.nextPage);
case FeedView.combined:
final threadFuture =
_nextPage != null && _combinedThreadsLeftover.length < 25
? ac.api.threads.list(
source,
sourceId: sourceId,
page: nullIfEmpty(_nextPage!),
sort: sort,
)
: Future<PostListModel?>.value();
final microblogFuture =
_combinedPage != null && _combinedMicroblogsLeftover.length < 25
? ac.api.microblogs.list(
source,
sourceId: sourceId,
page: nullIfEmpty(_combinedPage!),
sort: sort,
)
: Future<PostListModel?>.value();
final results = await Future.wait([threadFuture, microblogFuture]);

final postLists = results
.map((postListModel) => postListModel?.items ?? <PostModel>[])
.toList();
final merged = merge(ac.serverSoftware, [
[..._combinedThreadsLeftover, ...postLists[0]],
[..._combinedMicroblogsLeftover, ...postLists[1]],
], sort);

// get next page if new request was sent
if (_combinedMicroblogsLeftover.length < 25) {
_combinedPage = results.last?.nextPage;
}
if (_combinedThreadsLeftover.length < 25) {
_nextPage = results.first?.nextPage;
}

_combinedThreadsLeftover = merged.$2.isNotEmpty ? merged.$2.first : [];
_combinedMicroblogsLeftover = merged.$2.length > 1
? merged.$2.last
: [];

// if final page of input also return leftover posts
final result = [..._leftover, ...merged.$1];
if (_nextPage == null) {
result.addAll(_combinedThreadsLeftover);
}
if (_combinedPage == null) {
result.addAll(_combinedMicroblogsLeftover);
}

return (result, _nextPage ?? _combinedPage);
final postListModel = await ac.api.threads.list(
source,
sourceId: sourceId,
page: nullIfEmpty(_nextPage!),
sort: sort,
combined: true,
);
_nextPage = postListModel.nextPage;
return ([..._leftover, ...postListModel.items], postListModel.nextPage);
}
}

Expand Down Expand Up @@ -381,10 +336,7 @@ class FeedAggregator {
for (final input in inputs) {
input
.._leftover = []
.._nextPage = ''
.._combinedPage = ''
.._combinedThreadsLeftover = []
.._combinedMicroblogsLeftover = [];
.._nextPage = '';
}
}

Expand Down
Loading