diff --git a/lib/entities/parse_address_from_domain.dart b/lib/entities/parse_address_from_domain.dart index 6574d26fd..fce9d9a56 100644 --- a/lib/entities/parse_address_from_domain.dart +++ b/lib/entities/parse_address_from_domain.dart @@ -44,10 +44,24 @@ class AddressResolver { if (text.startsWith('@') && !text.substring(1).contains('@')) { final formattedName = text.substring(1); final twitterUser = await TwitterApi.lookupUserByName(userName: formattedName); - final address = extractAddressByType( - raw: twitterUser.description ?? '', type: CryptoCurrency.fromString(ticker)); - if (address != null) { - return ParsedAddress.fetchTwitterAddress(address: address, name: text); + final addressFromBio = extractAddressByType( + raw: twitterUser.description, type: CryptoCurrency.fromString(ticker)); + if (addressFromBio != null) { + return ParsedAddress.fetchTwitterAddress(address: addressFromBio, name: text); + } + final tweets = twitterUser.tweets; + if (tweets != null) { + var subString = StringBuffer(); + tweets.forEach((item) { + subString.writeln(item.text); + }); + final userTweetsText = subString.toString(); + final addressFromPinnedTweet = + extractAddressByType(raw: userTweetsText, type: CryptoCurrency.fromString(ticker)); + + if (addressFromPinnedTweet != null) { + return ParsedAddress.fetchTwitterAddress(address: addressFromPinnedTweet, name: text); + } } } if (!text.startsWith('@') && text.contains('@') && !text.contains('.')) { diff --git a/lib/twitter/twitter_api.dart b/lib/twitter/twitter_api.dart index 27fb7d1a2..41f5df61d 100644 --- a/lib/twitter/twitter_api.dart +++ b/lib/twitter/twitter_api.dart @@ -10,7 +10,7 @@ class TwitterApi { static const userPath = '/2/users/by/username/'; static Future lookupUserByName({required String userName}) async { - final queryParams = {'user.fields': 'description'}; + final queryParams = {'user.fields': 'description', 'expansions': 'pinned_tweet_id'}; final headers = {'authorization': 'Bearer $twitterBearerToken'}; @@ -32,6 +32,6 @@ class TwitterApi { throw Exception(responseJSON['errors'][0]['detail']); } - return TwitterUser.fromJson(responseJSON['data'] as Map); + return TwitterUser.fromJson(responseJSON); } } diff --git a/lib/twitter/twitter_user.dart b/lib/twitter/twitter_user.dart index c4bda7859..ac373fd62 100644 --- a/lib/twitter/twitter_user.dart +++ b/lib/twitter/twitter_user.dart @@ -1,16 +1,45 @@ class TwitterUser { - TwitterUser({required this.id, required this.username, required this.name, this.description}); + TwitterUser( + {required this.id, + required this.username, + required this.name, + required this.description, + this.tweets}); final String id; final String username; final String name; - final String? description; + final String description; + final List? tweets; factory TwitterUser.fromJson(Map json) { return TwitterUser( - id: json['id'] as String, - username: json['username'] as String, - name: json['name'] as String, - description: json['description'] as String?); + id: json['data']['id'] as String, + username: json['data']['username'] as String, + name: json['data']['name'] as String, + description: json['data']['description'] as String? ?? '', + tweets: json['includes'] != null + ? List.from(json['includes']['tweets'] as List) + .map((e) => Tweet.fromJson(e as Map)) + .toList() + : null, + ); + } +} + +class Tweet { + Tweet({ + required this.id, + required this.text, + }); + + final String id; + final String text; + + factory Tweet.fromJson(Map json) { + return Tweet( + id: json['id'] as String, + text: json['text'] as String, + ); } }