redesign user object

Serhii committed Feb 13, 2023 at 00:38 UTC 5c663c55965549ad683c525f373ddc28cab2388b
3 files changed +85 -32
lib/entities/parse_address_from_domain.dart
+14 -5
@@ -45,14 +45,23 @@ class AddressResolver {
45 final formattedName = text.substring(1);
46 final twitterUser = await TwitterApi.lookupUserByName(userName: formattedName);
47 final addressFromBio = extractAddressByType(
48 - raw: twitterUser.description ?? '', type: CryptoCurrency.fromString(ticker));
49 - final addressFromPinnedTweet = extractAddressByType(
50 - raw: twitterUser.pinnedTweet ?? '', type: CryptoCurrency.fromString(ticker));
48 + raw: twitterUser.data.description, type: CryptoCurrency.fromString(ticker));
49 if (addressFromBio != null) {
50 return ParsedAddress.fetchTwitterAddress(address: addressFromBio, name: text);
51 }
54 - if (addressFromPinnedTweet != null) {
55 - return ParsedAddress.fetchTwitterAddress(address: addressFromPinnedTweet, name: text);
52 + final tweets = twitterUser.includes?.tweets;
53 + if (tweets != null) {
54 + var subString = StringBuffer();
55 + tweets.forEach((item) {
56 + subString.writeln(item.text);
57 + });
58 + final userTweetsText = subString.toString();
59 + final addressFromPinnedTweet =
60 + extractAddressByType(raw: userTweetsText, type: CryptoCurrency.fromString(ticker));
61 +
62 + if (addressFromPinnedTweet != null) {
63 + return ParsedAddress.fetchTwitterAddress(address: addressFromPinnedTweet, name: text);
64 + }
65 }
66 }
67 if (!text.startsWith('@') && text.contains('@') && !text.contains('.')) {
lib/twitter/twitter_api.dart
+1 -11
@@ -32,16 +32,6 @@ class TwitterApi {
32 throw Exception(responseJSON['errors'][0]['detail']);
33 }
34
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);
35 + return TwitterUser.fromJson(responseJSON);
36 }
37 }
lib/twitter/twitter_user.dart
+70 -16
@@ -1,19 +1,73 @@
1 class TwitterUser {
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,
16 - description: json['description'] as String?,
17 - pinnedTweet: json['pinnedTweet'] as String?);
2 + TwitterUser({
3 + required this.data,
4 + this.includes,
5 + });
6 +
7 + late final Data data;
8 + late final Includes? includes;
9 +
10 + TwitterUser.fromJson(Map<String, dynamic> json) {
11 + data = Data.fromJson(json['data'] as Map<String, dynamic>);
12 + includes = json['includes'] != null
13 + ? Includes.fromJson(json['includes'] as Map<String, dynamic>)
14 + : null;
15 + }
16 +}
17 +
18 +class Data {
19 + Data({
20 + required this.name,
21 + required this.id,
22 + required this.pinnedTweetId,
23 + required this.description,
24 + required this.username,
25 + });
26 +
27 + late final String name;
28 + late final String id;
29 + late final String? pinnedTweetId;
30 + late final String description;
31 + late final String username;
32 +
33 + Data.fromJson(Map<String, dynamic> json) {
34 + name = json['name'] as String;
35 + id = json['id'] as String;
36 + pinnedTweetId = json['pinned_tweet_id'] as String?;
37 + description = json['description'] as String;
38 + username = json['username'] as String;
39 + }
40 +}
41 +
42 +class Includes {
43 + Includes({
44 + required this.tweets,
45 + });
46 +
47 + late final List<Tweets> tweets;
48 +
49 + Includes.fromJson(Map<String, dynamic> json) {
50 + tweets = List.from(json['tweets'] as Iterable<dynamic>)
51 + .map((e) => Tweets.fromJson(e as Map<String, dynamic>))
52 + .toList();
53 + }
54 +}
55 +
56 +class Tweets {
57 + Tweets({
58 + required this.editHistoryTweetIds,
59 + required this.id,
60 + required this.text,
61 + });
62 +
63 + late final List<String> editHistoryTweetIds;
64 + late final String id;
65 + late final String text;
66 +
67 + Tweets.fromJson(Map<String, dynamic> json) {
68 + editHistoryTweetIds =
69 + List.castFrom<dynamic, String>(json['edit_history_tweet_ids'] as List<dynamic>);
70 + id = json['id'] as String;
71 + text = json['text'] as String;
72 }
73 }