Skip to content

Commit 5dfefae

Browse files
Remove ls from dockerfile (#2)
Fix issue with spotify history repeat inserting
1 parent 0d591bd commit 5dfefae

File tree

3 files changed

+63
-126
lines changed

3 files changed

+63
-126
lines changed

Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ WORKDIR /app
1010
COPY . .
1111

1212
RUN cargo prisma generate
13-
14-
RUN ls -lha src/connectivity/
1513
RUN cargo build --release
1614

1715
FROM alpine

src/modules/spotify.rs

Lines changed: 17 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@ use std::sync::Arc;
22

33
use actix_web::web::{self};
44
use chrono::prelude::*;
5-
use serde_json::json;
5+
use prisma_client_rust::Direction;
66

77
use crate::{
88
connectivity::prisma::spotify_history,
9-
services::spotify::helpers::{self, get_or_make_device},
9+
services::spotify::helpers::{self, store_history},
1010
structs::{
1111
self,
12-
spotify::{
13-
ArtistName, CurrentPlaying, DeviceRewrite, PlayerState,
14-
SpotifyArtist,
15-
},
12+
spotify::{ArtistName, CurrentPlaying, DeviceRewrite, PlayerState},
1613
},
1714
ServerState,
1815
};
@@ -111,6 +108,7 @@ pub(crate) async fn fetch_spotify_current(data: web::Data<ServerState>) {
111108
.find_first(vec![spotify_history::id::equals(
112109
current_playing.id.as_ref().unwrap().to_string(),
113110
)])
111+
.order_by(spotify_history::listened_at::order(Direction::Desc))
114112
.exec()
115113
.await
116114
{
@@ -120,130 +118,28 @@ pub(crate) async fn fetch_spotify_current(data: web::Data<ServerState>) {
120118
let date_minus_length =
121119
(date.timestamp() * 1000) - latest.length as i64;
122120

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+
}
149124

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;
163127
}
164128
}
165129
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;
204132
}
133+
134+
store_history(prisma, current_playing).await;
205135
}
206136
},
207137
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;
246140
}
141+
142+
store_history(prisma, current_playing).await;
247143
}
248144
}
249145
}

src/services/spotify/helpers.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
use std::io::Error;
1+
use std::{io::Error, sync::Arc};
22

3+
use chrono::{DateTime, FixedOffset, Utc};
34
use envconfig::Envconfig;
45
use redis::{aio::ConnectionManager, AsyncCommands};
56
use serde_json::json;
67

78
use crate::{
89
config::Config,
910
connectivity::{
10-
prisma::{spotify_devices, PrismaClient},
11+
prisma::{spotify_devices, spotify_history, PrismaClient},
1112
valkey::ValkeyManager,
1213
},
1314
structs::spotify::{
14-
AuthorizationData, CurrentPlaying, SpotifyAccount, SpotifyTokens,
15+
AuthorizationData, CurrentPlaying, SpotifyAccount, SpotifyArtist,
16+
SpotifyTokens,
1517
},
1618
};
1719

@@ -185,3 +187,44 @@ pub async fn get_or_make_device(
185187
.unwrap(),
186188
}
187189
}
190+
191+
pub async fn store_history(
192+
prisma: &mut &PrismaClient,
193+
current_playing: Arc<CurrentPlaying>,
194+
) {
195+
let date: DateTime<FixedOffset> =
196+
Utc::now().with_timezone(&FixedOffset::east_opt(0).unwrap());
197+
198+
let dev = get_or_make_device(
199+
prisma,
200+
current_playing.device.as_ref().unwrap().name.to_string(),
201+
current_playing
202+
.device
203+
.as_ref()
204+
.unwrap()
205+
.device_type
206+
.to_string(),
207+
)
208+
.await;
209+
210+
let mut artists = Vec::new();
211+
for artist in current_playing.artists.as_ref().unwrap() {
212+
artists.push(json!(SpotifyArtist {
213+
name: artist.name.clone(),
214+
}));
215+
}
216+
217+
let current_playing = current_playing.as_ref().clone();
218+
let _ = prisma.spotify_history().create(
219+
current_playing.id.unwrap(),
220+
current_playing.name.unwrap(),
221+
current_playing.length.unwrap() as i32,
222+
current_playing.image.unwrap(),
223+
crate::connectivity::prisma::spotify_devices::UniqueWhereParam::IdEquals(dev.id),
224+
vec![
225+
spotify_history::r#type::set(current_playing.current_playing_type.as_ref().unwrap().to_string()),
226+
spotify_history::artists::set(artists),
227+
spotify_history::listened_at::set(date),
228+
]
229+
).exec().await;
230+
}

0 commit comments

Comments
 (0)