diff --git a/lib/entities/parse_address_from_domain.dart b/lib/entities/parse_address_from_domain.dart index 20ccb46c6..debd930cf 100644 --- a/lib/entities/parse_address_from_domain.dart +++ b/lib/entities/parse_address_from_domain.dart @@ -45,14 +45,23 @@ class AddressResolver { final formattedName = text.substring(1); final twitterUser = await TwitterApi.lookupUserByName(userName: formattedName); final addressFromBio = extractAddressByType( - raw: twitterUser.description ?? '', type: CryptoCurrency.fromString(ticker)); - final addressFromPinnedTweet = extractAddressByType( - raw: twitterUser.pinnedTweet ?? '', type: CryptoCurrency.fromString(ticker)); + raw: twitterUser.data.description, type: CryptoCurrency.fromString(ticker)); if (addressFromBio != null) { return ParsedAddress.fetchTwitterAddress(address: addressFromBio, name: text); } - if (addressFromPinnedTweet != null) { - return ParsedAddress.fetchTwitterAddress(address: addressFromPinnedTweet, name: text); + final tweets = twitterUser.includes?.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 202e3d217..41f5df61d 100644 --- a/lib/twitter/twitter_api.dart +++ b/lib/twitter/twitter_api.dart @@ -32,16 +32,6 @@ class TwitterApi { throw Exception(responseJSON['errors'][0]['detail']); } - final user = responseJSON['data'] as Map; - - try { - if (responseJSON['includes'] != null) { - user['pinnedTweet'] = responseJSON['includes']['tweets'][0]['text']; - } - } catch (e) { - print('responseJSON[includes][tweets][0][text] $e'); - } - - return TwitterUser.fromJson(user); + return TwitterUser.fromJson(responseJSON); } } diff --git a/lib/twitter/twitter_user.dart b/lib/twitter/twitter_user.dart index bf5d7282a..626f9cd7f 100644 --- a/lib/twitter/twitter_user.dart +++ b/lib/twitter/twitter_user.dart @@ -1,19 +1,73 @@ class TwitterUser { - TwitterUser({required this.id, required this.username, required this.name, this.description, - this.pinnedTweet}); + TwitterUser({ + required this.data, + this.includes, + }); - final String id; - final String username; - final String name; - final String? description; - final String? pinnedTweet; + late final Data data; + late final Includes? includes; - 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?, - pinnedTweet: json['pinnedTweet'] as String?); + TwitterUser.fromJson(Map json) { + data = Data.fromJson(json['data'] as Map); + includes = json['includes'] != null + ? Includes.fromJson(json['includes'] as Map) + : null; + } +} + +class Data { + Data({ + required this.name, + required this.id, + required this.pinnedTweetId, + required this.description, + required this.username, + }); + + late final String name; + late final String id; + late final String? pinnedTweetId; + late final String description; + late final String username; + + Data.fromJson(Map json) { + name = json['name'] as String; + id = json['id'] as String; + pinnedTweetId = json['pinned_tweet_id'] as String?; + description = json['description'] as String; + username = json['username'] as String; + } +} + +class Includes { + Includes({ + required this.tweets, + }); + + late final List tweets; + + Includes.fromJson(Map json) { + tweets = List.from(json['tweets'] as Iterable) + .map((e) => Tweets.fromJson(e as Map)) + .toList(); + } +} + +class Tweets { + Tweets({ + required this.editHistoryTweetIds, + required this.id, + required this.text, + }); + + late final List editHistoryTweetIds; + late final String id; + late final String text; + + Tweets.fromJson(Map json) { + editHistoryTweetIds = + List.castFrom(json['edit_history_tweet_ids'] as List); + id = json['id'] as String; + text = json['text'] as String; } }