1
1
use serde_json:: json;
2
2
3
- use crate :: { error:: Errors , resources:: fav_list:: FavoriteList } ;
3
+ use crate :: {
4
+ error:: Errors ,
5
+ resources:: { fav_list:: FavoriteList , notification:: NotificationsData } ,
6
+ } ;
4
7
8
+ #[ derive( Debug ) ]
5
9
pub struct MyProfile {
6
- pub client : reqwest:: Client ,
7
- pub fav_lists : FavoriteList ,
10
+ pub ( crate ) client : reqwest:: Client ,
11
+ pub ( crate ) fav_lists : FavoriteList ,
8
12
}
9
13
10
14
impl MyProfile {
11
- pub async fn create_fav_list ( self , list_name : & str ) -> Result < MyProfile , Errors > {
15
+ pub async fn create_fav_list ( & self , list_name : & str ) -> Result < & MyProfile , Errors > {
12
16
let query = json ! ( { "name" : list_name } ) ;
13
17
14
18
let query = serde_json:: to_string ( & query) ?;
@@ -17,26 +21,26 @@ impl MyProfile {
17
21
. get ( "https://leetcode.com/list/api/" )
18
22
. body ( query)
19
23
. send ( )
20
- . await ?; // let info = Self::s
24
+ . await ?;
21
25
Ok ( self )
22
26
}
23
- pub fn show_fav_lists ( self ) -> FavoriteList {
24
- self . fav_lists
27
+ pub fn show_lists ( & self ) -> FavoriteList {
28
+ self . fav_lists . clone ( )
25
29
}
26
30
27
31
pub async fn rename_fav_list (
28
- self ,
32
+ & self ,
29
33
prev_list_name : & str ,
30
34
new_list_name : & str ,
31
- ) -> Result < MyProfile , Errors > {
35
+ ) -> Result < & MyProfile , Errors > {
32
36
let id_hash = if let Some ( id) = Self :: get_id_hash ( & self , prev_list_name) . await {
33
37
id
34
38
} else {
35
39
return Err ( Errors :: ApiError ( "Provided name doesn't found" . into ( ) ) ) ;
36
40
} ;
37
41
let query = json ! ( {
38
- "favorite_id_hash" : id_hash,
39
- "is_public_favorite" : false ,
42
+ "favorite_id_hash" : id_hash. 0 ,
43
+ "is_public_favorite" : id_hash . 1 ,
40
44
"name" : new_list_name
41
45
} ) ;
42
46
@@ -49,7 +53,32 @@ impl MyProfile {
49
53
. await ?;
50
54
Ok ( self )
51
55
}
52
- pub async fn set_accessibility ( self , list_name : & str ) -> Result < MyProfile , Errors > {
56
+
57
+ pub async fn set_public ( & self , list_name : & str ) -> Result < & MyProfile , Errors > {
58
+ let id_hash = if let Some ( id) = Self :: get_id_hash ( & self , list_name) . await {
59
+ id
60
+ } else {
61
+ return Err ( Errors :: ApiError (
62
+ "Provided name doesn't found in lists" . into ( ) ,
63
+ ) ) ;
64
+ } ;
65
+ let query = json ! ( {
66
+ "favorite_id_hash" : id_hash. 0 ,
67
+ "is_public_favorite" : true ,
68
+ "name" : list_name
69
+ } ) ;
70
+
71
+ let query = serde_json:: to_string ( & query) ?;
72
+
73
+ self . client
74
+ . put ( "https://leetcode.com/list/api/" )
75
+ . body ( query)
76
+ . send ( )
77
+ . await ?;
78
+ Ok ( self )
79
+ }
80
+
81
+ pub async fn set_private ( & self , list_name : & str ) -> Result < & MyProfile , Errors > {
53
82
let id_hash = if let Some ( id) = Self :: get_id_hash ( & self , list_name) . await {
54
83
id
55
84
} else {
@@ -58,7 +87,7 @@ impl MyProfile {
58
87
) ) ;
59
88
} ;
60
89
let query = json ! ( {
61
- "favorite_id_hash" : id_hash,
90
+ "favorite_id_hash" : id_hash. 0 ,
62
91
"is_public_favorite" : true ,
63
92
"name" : list_name
64
93
} ) ;
@@ -82,10 +111,10 @@ impl MyProfile {
82
111
) ) ;
83
112
} ;
84
113
85
- Ok ( format ! ( "https://leetcode.com/list/{}" , id_hash) )
114
+ Ok ( format ! ( "https://leetcode.com/list/{}" , id_hash. 0 ) )
86
115
}
87
116
88
- pub async fn delete_list ( self , list_name : & str ) -> Result < MyProfile , Errors > {
117
+ pub async fn delete_list ( & self , list_name : & str ) -> Result < & MyProfile , Errors > {
89
118
let id_hash = if let Some ( id) = Self :: get_id_hash ( & self , list_name) . await {
90
119
id
91
120
} else {
@@ -95,21 +124,72 @@ impl MyProfile {
95
124
} ;
96
125
97
126
self . client
98
- . delete ( format ! ( "https://leetcode.com/list/api/{}" , id_hash) )
127
+ . delete ( format ! ( "https://leetcode.com/list/api/{}" , id_hash. 0 ) )
99
128
. send ( )
100
129
. await ?;
101
130
102
131
Ok ( self )
103
132
}
104
133
105
- async fn get_id_hash ( & self , list_name : & str ) -> Option < String > {
134
+ async fn get_id_hash ( & self , list_name : & str ) -> Option < ( String , bool ) > {
106
135
for favourite in & self . fav_lists . data . favoritesLists . allFavorites {
107
136
if favourite. name == list_name. to_string ( ) {
108
- return Some ( favourite. idHash . clone ( ) ) ;
137
+ return Some ( ( favourite. idHash . clone ( ) , favourite . isPublicFavorite . clone ( ) ) ) ;
109
138
}
110
139
}
111
140
None
112
141
}
142
+
143
+ pub async fn get_notifications ( & self ) -> Result < NotificationsData , Errors > {
144
+ let operation_name = "fetchNotifications" ;
145
+ let variables = json ! ( { "first" : 10 } ) ;
146
+ let query = r#"query fetchNotifications($first: Int!, $after: String) {
147
+ notifications(first: $first, after: $after) {
148
+ edges {
149
+ node {
150
+ id
151
+ notificationId
152
+ modifiedDate
153
+ actioned
154
+ notificationData {
155
+ id
156
+ content
157
+ type
158
+ metadata
159
+ __typename
160
+ }
161
+ __typename
162
+ }
163
+ __typename
164
+ }
165
+ pageInfo {
166
+ endCursor
167
+ hasNextPage
168
+ __typename
169
+ }
170
+ __typename
171
+ }
172
+ }"# ;
173
+
174
+ let json_data = json ! ( {
175
+ "operationName" : operation_name,
176
+ "variables" : variables,
177
+ "query" : query,
178
+ } ) ;
179
+
180
+ let query = serde_json:: to_string ( & json_data) ?;
181
+
182
+ let problem_info = self
183
+ . client
184
+ . post ( "https://leetcode.com/graphql/" )
185
+ . body ( query)
186
+ . send ( )
187
+ . await ?
188
+ . text ( )
189
+ . await ?;
190
+
191
+ Ok ( serde_json:: from_str :: < NotificationsData > ( & problem_info) ?)
192
+ }
113
193
}
114
194
115
195
pub struct UserProfile {
0 commit comments