Merge pull request #773 from cake-tech/CW-316-Get-address-from-user's-pinned-Tweets

add address lookup for pinned tweet
This commit is contained in:
Omar Hatem 2023-02-14 15:18:20 +02:00 committed by GitHub
commit 4a94bc02e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 12 deletions

View file

@ -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('.')) {

View file

@ -10,7 +10,7 @@ class TwitterApi {
static const userPath = '/2/users/by/username/';
static Future<TwitterUser> 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<String, dynamic>);
return TwitterUser.fromJson(responseJSON);
}
}

View file

@ -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<Tweet>? tweets;
factory TwitterUser.fromJson(Map<String, dynamic> 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<String, dynamic>))
.toList()
: null,
);
}
}
class Tweet {
Tweet({
required this.id,
required this.text,
});
final String id;
final String text;
factory Tweet.fromJson(Map<String, dynamic> json) {
return Tweet(
id: json['id'] as String,
text: json['text'] as String,
);
}
}