add address lookup for pinned tweet
Serhii committed
Feb 9, 2023 at 12:23 UTC
624036d00cda371d010a2d74733b427c4c5391bf
3 files changed
+25
-7
lib/entities/parse_address_from_domain.dart
+8
-3
@@ -44,10 +44,15 @@ class AddressResolver {
44
if (text.startsWith('@') && !text.substring(1).contains('@')) {
45
final formattedName = text.substring(1);
46
final twitterUser = await TwitterApi.lookupUserByName(userName: formattedName);
47
- final address = extractAddressByType(
47
+ final addressFromBio = extractAddressByType(
48
raw: twitterUser.description ?? '', type: CryptoCurrency.fromString(ticker));
49
- if (address != null) {
50
- return ParsedAddress.fetchTwitterAddress(address: address, name: text);
49
+ final addressFromPinnedTweet = extractAddressByType(
50
+ raw: twitterUser.pinnedTweet ?? '', type: CryptoCurrency.fromString(ticker));
51
+ if (addressFromBio != null) {
52
+ return ParsedAddress.fetchTwitterAddress(address: addressFromBio, name: text);
53
+ }
54
+ if (addressFromPinnedTweet != null) {
55
+ return ParsedAddress.fetchTwitterAddress(address: addressFromPinnedTweet, name: text);
56
}
57
}
58
if (!text.startsWith('@') && text.contains('@') && !text.contains('.')) {
lib/twitter/twitter_api.dart
+12
-2
@@ -10,7 +10,7 @@ class TwitterApi {
10
static const userPath = '/2/users/by/username/';
11
12
static Future<TwitterUser> lookupUserByName({required String userName}) async {
13
- final queryParams = {'user.fields': 'description'};
13
+ final queryParams = {'user.fields': 'description', 'expansions': 'pinned_tweet_id'};
14
15
final headers = {'authorization': 'Bearer $twitterBearerToken'};
16
@@ -32,6 +32,16 @@ class TwitterApi {
32
throw Exception(responseJSON['errors'][0]['detail']);
33
}
34
35
- return TwitterUser.fromJson(responseJSON['data'] as Map<String, dynamic>);
35
+ final user = responseJSON['data'] as Map<String, dynamic>;
36
+
37
+ try {
38
+ if (responseJSON['includes'] != null) {
39
+ user['pinnedTweet'] = responseJSON['includes']['tweets'][0]['text'];
40
+ }
41
+ } catch (e) {
42
+ print('responseJSON[includes][tweets][0][text] $e');
43
+ }
44
+
45
+ return TwitterUser.fromJson(user);
46
}
47
}
lib/twitter/twitter_user.dart
+5
-2
@@ -1,16 +1,19 @@
1
class TwitterUser {
2
- TwitterUser({required this.id, required this.username, required this.name, this.description});
2
+ TwitterUser({required this.id, required this.username, required this.name, this.description,
3
+ this.pinnedTweet});
4
5
final String id;
6
final String username;
7
final String name;
8
final String? description;
9
+ final String? pinnedTweet;
10
11
factory TwitterUser.fromJson(Map<String, dynamic> json) {
12
return TwitterUser(
13
id: json['id'] as String,
14
username: json['username'] as String,
15
name: json['name'] as String,
14
- description: json['description'] as String?);
16
+ description: json['description'] as String?,
17
+ pinnedTweet: json['pinnedTweet'] as String?);
18
}
19
}