Skip to content

Commit

Permalink
Tweet filter not required for createTweets
Browse files Browse the repository at this point in the history
  • Loading branch information
HarlJo committed Mar 26, 2024
1 parent 7259093 commit 3327fbb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 47 deletions.
80 changes: 47 additions & 33 deletions lib/client/authenticatedClient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,21 @@ class Twitter {
return replies;
}

static List<TweetChain> createTweets(List<dynamic> addEntries, FilterModel filterModel, [bool isPinned = false]) {
static List<TweetChain> createTweets(FilterModel? filterModel, List<dynamic> addEntries, [bool isPinned = false]) {
List<TweetChain> replies = [];

for (var entry in addEntries) {
var entryId = entry['entryId'] as String;
if (entryId.startsWith('tweet-')) {
var result = entry['content']['itemContent']['tweet_results']['result'];
TweetWithCard? tweet = TweetWithCard.fromGraphqlJson(result);
if (tweet != null) {
if (!FilterTweetRegEx(filterModel, tweet)) {
replies.add(
TweetChain(id: result['rest_id'] ?? result['tweet']['rest_id'], tweets: [tweet], isPinned: isPinned));
}

if (filterModel != null && !FilterTweetRegEx(filterModel, tweet)) {
replies.add(
TweetChain(id: result['rest_id'] ?? result['tweet']['rest_id'], tweets: [tweet], isPinned: isPinned));
} else if (filterModel == null) {
replies.add(
TweetChain(id: result['rest_id'] ?? result['tweet']['rest_id'], tweets: [tweet], isPinned: isPinned));
}
}

Expand All @@ -328,11 +330,13 @@ class Twitter {
if (item['item']['itemContent']['tweet_results']?['result'] != null) {
if (item['item']['itemContent']['tweet_results']['result']['tweet'] == null) {
var tweet = TweetWithCard.fromGraphqlJson(item['item']['itemContent']['tweet_results']['result']);
if (!FilterTweetRegEx(filterModel, tweet)) tweets.add(tweet);
if (filterModel != null && !FilterTweetRegEx(filterModel, tweet)) tweets.add(tweet);
if (filterModel == null) tweets.add(tweet);
} else {
var tweet =
TweetWithCard.fromGraphqlJson(item['item']['itemContent']['tweet_results']['result']['tweet']);
if (!FilterTweetRegEx(filterModel, tweet)) tweets.add(tweet);
if (filterModel != null && !FilterTweetRegEx(filterModel, tweet)) tweets.add(tweet);
if (filterModel == null) tweets.add(tweet);
}
}
}
Expand Down Expand Up @@ -554,14 +558,17 @@ class Twitter {
return List.from(jsonDecode(result)).map((e) => Trends.fromJson(e)).toList(growable: false);
}

static Future<TweetStatus> getTimelineTweets(String id, String type, List<String> pinnedTweets,
{int count = 10,
String? cursor,
bool includeReplies = true,
bool includeRetweets = true,
required int Function() getTweetsCounter,
required void Function() incrementTweetsCounter,
required FilterModel filterModel}) async {
static Future<TweetStatus> getTimelineTweets(
String id,
String type,
List<String> pinnedTweets, {
int count = 10,
String? cursor,
bool includeReplies = true,
bool includeRetweets = true,
required int Function() getTweetsCounter,
required void Function() incrementTweetsCounter,
}) async {
bool showPinnedTweet = true;
var query = {
...defaultParams,
Expand Down Expand Up @@ -590,9 +597,16 @@ class Twitter {
var result = json.decode(response.body);
//if this page is not first one on the profile page, dont add pinned tweet
if (variables['cursor'] != null) showPinnedTweet = false;
return createTimelineChains(result, 'tweet', pinnedTweets, includeReplies == false, includeReplies, showPinnedTweet,
getTweetsCounter, incrementTweetsCounter,
filterModel: filterModel);
return createTimelineChains(
result,
'tweet',
pinnedTweets,
includeReplies == false,
includeReplies,
showPinnedTweet,
getTweetsCounter,
incrementTweetsCounter,
);
}

static Future<TweetStatus> getTweets(String id, String type, List<String> pinnedTweets,
Expand Down Expand Up @@ -767,10 +781,10 @@ class Twitter {
String? cursorBottom = getCursor(addEntries, repEntries, 'cursor-bottom', 'Bottom');
String? cursorTop = getCursor(addEntries, repEntries, 'cursor-top', 'Top');

var chains = createTweets(addEntries, filterModel);
var chains = createTweets(filterModel, addEntries);
// var debugTweets = json.encode(chains);
//var debugTweets2 = json.encode(addEntries);
var pinnedChains = createTweets(addPinnedEntries, filterModel, true);
var pinnedChains = createTweets(filterModel, addPinnedEntries, true);

// Order all the conversations by newest first (assuming the ID is an incrementing key),
// and create a chain from them
Expand All @@ -794,15 +808,15 @@ class Twitter {
}

static TweetStatus createTimelineChains(
Map<String, dynamic> result,
String tweetIndicator,
List<String> pinnedTweets,
bool mapToThreads,
bool includeReplies,
bool showPinnedTweet,
int Function() getTweetsCounter,
void Function() increaseTweetCounter,
{required FilterModel filterModel}) {
Map<String, dynamic> result,
String tweetIndicator,
List<String> pinnedTweets,
bool mapToThreads,
bool includeReplies,
bool showPinnedTweet,
int Function() getTweetsCounter,
void Function() increaseTweetCounter,
) {
var instructions = List.from(result["data"]["home"]["home_timeline_urt"]['instructions']);
var addEntriesInstructions = instructions.firstWhereOrNull((e) => e['type'] == 'TimelineAddEntries');
if (addEntriesInstructions == null) {
Expand All @@ -818,10 +832,10 @@ class Twitter {

String? cursorBottom = getCursor(addEntries, repEntries, 'cursor-bottom', 'Bottom');
String? cursorTop = getCursor(addEntries, repEntries, 'cursor-top', 'Top');
var chains = createTweets(addEntries, filterModel);
var chains = createTweets(null, addEntries);
// var debugTweets = json.encode(chains);
//var debugTweets2 = json.encode(addEntries);
var pinnedChains = createTweets(addPinnedEntries, filterModel, true);
var pinnedChains = createTweets(null, addPinnedEntries, true);

// Order all the conversations by newest first (assuming the ID is an incrementing key),
// and create a chain from them
Expand All @@ -838,7 +852,7 @@ class Twitter {
if (chains.length < 5) increaseTweetCounter();
//As soon as there is no tweet left that passes regex critera and we also reached maximum attemps
//to find them, than stop loading more.
if (chains.length <= 5 && getTweetsCounter() > filterModel.GetLoadTweetsCounter()) {
if (chains.length <= 5) {
cursorBottom = null;
}

Expand Down
29 changes: 15 additions & 14 deletions lib/forYou.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,15 @@ class _ForYouTweetsState extends State<ForYouTweets> with AutomaticKeepAliveClie
late PagingController<String?, TweetChain> _pagingController;
static const int pageSize = 20;
int loadTweetsCounter = 0;
late FilterModel filterModel;
@override
bool get wantKeepAlive => true;

@override
void initState() {
super.initState();
filterModel = FilterModel(widget.user.idStr!, widget.pref);
_pagingController = PagingController(firstPageKey: null);
_pagingController.addPageRequestListener((cursor) {
_loadTweets(cursor, filterModel);
_loadTweets(cursor);
});
}

Expand All @@ -64,15 +62,18 @@ class _ForYouTweetsState extends State<ForYouTweets> with AutomaticKeepAliveClie
return loadTweetsCounter;
}

Future _loadTweets(String? cursor, FilterModel filterModel) async {
Future _loadTweets(String? cursor) async {
try {
var result = await Twitter.getTimelineTweets(widget.user.idStr!, widget.type, widget.pinnedTweets,
cursor: cursor,
count: pageSize,
includeReplies: widget.includeReplies,
getTweetsCounter: getLoadTweetsCounter,
incrementTweetsCounter: incrementLoadTweetsCounter,
filterModel: filterModel);
var result = await Twitter.getTimelineTweets(
widget.user.idStr!,
widget.type,
widget.pinnedTweets,
cursor: cursor,
count: pageSize,
includeReplies: widget.includeReplies,
getTweetsCounter: getLoadTweetsCounter,
incrementTweetsCounter: incrementLoadTweetsCounter,
);

if (!mounted) {
return;
Expand Down Expand Up @@ -128,18 +129,18 @@ class _ForYouTweetsState extends State<ForYouTweets> with AutomaticKeepAliveClie
error: _pagingController.error[0],
stackTrace: _pagingController.error[1],
prefix: L10n.of(context).unable_to_load_the_tweets,
onRetry: () => _loadTweets(_pagingController.firstPageKey, this.filterModel),
onRetry: () => _loadTweets(_pagingController.firstPageKey),
),
newPageErrorIndicatorBuilder: (context) => FullPageErrorWidget(
error: _pagingController.error[0],
stackTrace: _pagingController.error[1],
prefix: L10n.of(context).unable_to_load_the_next_page_of_tweets,
onRetry: () => _loadTweets(_pagingController.nextPageKey, this.filterModel),
onRetry: () => _loadTweets(_pagingController.nextPageKey),
),
noItemsFoundIndicatorBuilder: (context) {
return Center(
child: Text(
L10n.of(context).could_not_find_any_tweets_by_this_user,
L10n.of(context).unable_to_load_the_tweets_for_the_feed,
),
);
},
Expand Down

0 comments on commit 3327fbb

Please sign in to comment.