@@ -2,17 +2,14 @@ use std::sync::Arc;
2
2
3
3
use actix_web:: web:: { self } ;
4
4
use chrono:: prelude:: * ;
5
- use serde_json :: json ;
5
+ use prisma_client_rust :: Direction ;
6
6
7
7
use crate :: {
8
8
connectivity:: prisma:: spotify_history,
9
- services:: spotify:: helpers:: { self , get_or_make_device } ,
9
+ services:: spotify:: helpers:: { self , store_history } ,
10
10
structs:: {
11
11
self ,
12
- spotify:: {
13
- ArtistName , CurrentPlaying , DeviceRewrite , PlayerState ,
14
- SpotifyArtist ,
15
- } ,
12
+ spotify:: { ArtistName , CurrentPlaying , DeviceRewrite , PlayerState } ,
16
13
} ,
17
14
ServerState ,
18
15
} ;
@@ -111,6 +108,7 @@ pub(crate) async fn fetch_spotify_current(data: web::Data<ServerState>) {
111
108
. find_first ( vec ! [ spotify_history:: id:: equals(
112
109
current_playing. id. as_ref( ) . unwrap( ) . to_string( ) ,
113
110
) ] )
111
+ . order_by ( spotify_history:: listened_at:: order ( Direction :: Desc ) )
114
112
. exec ( )
115
113
. await
116
114
{
@@ -120,130 +118,28 @@ pub(crate) async fn fetch_spotify_current(data: web::Data<ServerState>) {
120
118
let date_minus_length =
121
119
( date. timestamp ( ) * 1000 ) - latest. length as i64 ;
122
120
123
- if date_minus_length >= listened_date
124
- && current_playing. progress . unwrap ( ) >= 10000
125
- {
126
- let dev = get_or_make_device (
127
- prisma,
128
- current_playing
129
- . device
130
- . as_ref ( )
131
- . unwrap ( )
132
- . name
133
- . to_string ( ) ,
134
- current_playing
135
- . device
136
- . as_ref ( )
137
- . unwrap ( )
138
- . device_type
139
- . to_string ( ) ,
140
- )
141
- . await ;
142
-
143
- let mut artists = Vec :: new ( ) ;
144
- for artist in current_playing. artists . as_ref ( ) . unwrap ( ) {
145
- artists. push ( json ! ( SpotifyArtist {
146
- name: artist. name. clone( ) ,
147
- } ) ) ;
148
- }
121
+ if current_playing. progress . unwrap ( ) < 10000 {
122
+ return ;
123
+ }
149
124
150
- let current_playing = current_playing. as_ref ( ) . clone ( ) ;
151
- let _ = prisma. spotify_history ( ) . create (
152
- current_playing. id . unwrap ( ) ,
153
- current_playing. name . unwrap ( ) ,
154
- current_playing. length . unwrap ( ) as i32 ,
155
- current_playing. image . unwrap ( ) ,
156
- crate :: connectivity:: prisma:: spotify_devices:: UniqueWhereParam :: IdEquals ( dev. id ) ,
157
- vec ! [
158
- spotify_history:: r#type:: set( current_playing. current_playing_type. as_ref( ) . unwrap( ) . to_string( ) ) ,
159
- spotify_history:: artists:: set( artists) ,
160
- spotify_history:: listened_at:: set( date) ,
161
- ]
162
- ) . exec ( ) . await ;
125
+ if date_minus_length >= listened_date {
126
+ store_history ( prisma, current_playing) . await ;
163
127
}
164
128
}
165
129
None => {
166
- if current_playing. progress . unwrap ( ) >= 10000 {
167
- let device = get_or_make_device (
168
- prisma,
169
- current_playing
170
- . device
171
- . as_ref ( )
172
- . unwrap ( )
173
- . name
174
- . to_string ( ) ,
175
- current_playing
176
- . device
177
- . as_ref ( )
178
- . unwrap ( )
179
- . device_type
180
- . to_string ( ) ,
181
- )
182
- . await ;
183
-
184
- let mut artists = Vec :: new ( ) ;
185
- for artist in current_playing. artists . as_ref ( ) . unwrap ( ) {
186
- artists. push ( json ! ( SpotifyArtist {
187
- name: artist. name. clone( ) ,
188
- } ) ) ;
189
- }
190
-
191
- let current_playing = current_playing. as_ref ( ) . clone ( ) ;
192
- let _ = prisma. spotify_history ( ) . create (
193
- current_playing. id . unwrap ( ) ,
194
- current_playing. name . unwrap ( ) ,
195
- current_playing. length . unwrap ( ) as i32 ,
196
- current_playing. image . unwrap ( ) ,
197
- crate :: connectivity:: prisma:: spotify_devices:: UniqueWhereParam :: IdEquals ( device. id ) ,
198
- vec ! [
199
- spotify_history:: r#type:: set( current_playing. current_playing_type. as_ref( ) . unwrap( ) . to_string( ) ) ,
200
- spotify_history:: artists:: set( artists) ,
201
- spotify_history:: listened_at:: set( date) ,
202
- ]
203
- ) . exec ( ) . await ;
130
+ if current_playing. progress . unwrap ( ) < 10000 {
131
+ return ;
204
132
}
133
+
134
+ store_history ( prisma, current_playing) . await ;
205
135
}
206
136
} ,
207
137
Err ( _) => {
208
- if current_playing. progress . unwrap ( ) >= 10000 {
209
- let device = get_or_make_device (
210
- prisma,
211
- current_playing
212
- . device
213
- . as_ref ( )
214
- . unwrap ( )
215
- . name
216
- . to_string ( ) ,
217
- current_playing
218
- . device
219
- . as_ref ( )
220
- . unwrap ( )
221
- . device_type
222
- . to_string ( ) ,
223
- )
224
- . await ;
225
-
226
- let mut artists = Vec :: new ( ) ;
227
- for artist in current_playing. artists . as_ref ( ) . unwrap ( ) {
228
- artists. push ( json ! ( SpotifyArtist {
229
- name: artist. name. clone( ) ,
230
- } ) ) ;
231
- }
232
-
233
- let current_playing = current_playing. as_ref ( ) . clone ( ) ;
234
- let _ = prisma. spotify_history ( ) . create (
235
- current_playing. id . unwrap ( ) ,
236
- current_playing. name . unwrap ( ) ,
237
- current_playing. length . unwrap ( ) as i32 ,
238
- current_playing. image . unwrap ( ) ,
239
- crate :: connectivity:: prisma:: spotify_devices:: UniqueWhereParam :: IdEquals ( device. id ) ,
240
- vec ! [
241
- spotify_history:: r#type:: set( current_playing. current_playing_type. as_ref( ) . unwrap( ) . to_string( ) ) ,
242
- spotify_history:: artists:: set( artists) ,
243
- spotify_history:: listened_at:: set( date) ,
244
- ]
245
- ) . exec ( ) . await ;
138
+ if current_playing. progress . unwrap ( ) < 10000 {
139
+ return ;
246
140
}
141
+
142
+ store_history ( prisma, current_playing) . await ;
247
143
}
248
144
}
249
145
}
0 commit comments