From 3bd08b1ceab22ea5f18902b25f1c3a23c2892173 Mon Sep 17 00:00:00 2001 From: Maxr1998 Date: Wed, 6 Dec 2023 00:52:11 +0100 Subject: [PATCH] :recycle: Add user ID to offline listens log --- lib/services/finamp_user_helper.dart | 5 +++- lib/services/offline_listen_helper.dart | 33 +++++++++++++++---------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/lib/services/finamp_user_helper.dart b/lib/services/finamp_user_helper.dart index 03124be25..f8662919f 100644 --- a/lib/services/finamp_user_helper.dart +++ b/lib/services/finamp_user_helper.dart @@ -13,6 +13,9 @@ class FinampUserHelper { /// Checks if there are any saved users. bool get isUsersEmpty => _finampUserBox.isEmpty; + /// Loads the id from CurrentUserId. Returns null if no id is stored. + String? get currentUserId => _currentUserIdBox.get("CurrentUserId"); + /// Loads the FinampUser with the id from CurrentUserId. Returns null if no /// user exists. FinampUser? get currentUser => @@ -61,4 +64,4 @@ class FinampUserHelper { _finampUserBox.delete(id); } -} +} \ No newline at end of file diff --git a/lib/services/offline_listen_helper.dart b/lib/services/offline_listen_helper.dart index e743c329b..66a182fdf 100644 --- a/lib/services/offline_listen_helper.dart +++ b/lib/services/offline_listen_helper.dart @@ -2,12 +2,15 @@ import 'dart:convert'; import 'dart:io'; import 'package:audio_service/audio_service.dart'; +import 'package:finamp/services/finamp_user_helper.dart'; +import 'package:get_it/get_it.dart'; import 'package:logging/logging.dart'; import 'package:path_provider/path_provider.dart'; /// Logs offline listens or failed submissions to a file. class OfflineListenLogHelper { final _logger = Logger("OfflineListenLogHelper"); + final _finampUserHelper = GetIt.instance(); Future get _logDirectory async { if (!Platform.isAndroid) { @@ -25,32 +28,36 @@ class OfflineListenLogHelper { } Future logOfflineListen(MediaItem item) async { - final timestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000; final itemJson = item.extras!["itemJson"]; - final id = itemJson["Id"]; - final name = itemJson["Name"]; - final albumArtist = itemJson["AlbumArtist"]; - final album = itemJson["Album"]; - final trackMbid = itemJson["ProviderIds"]?["MusicBrainzTrack"]; - await _logOfflineListen(timestamp, id, name, albumArtist, album, trackMbid); + await _logOfflineListen( + timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000, + itemId: itemJson["Id"], + name: itemJson["Name"], + artist: itemJson["AlbumArtist"], + album: itemJson["Album"], + trackMbid: itemJson["ProviderIds"]?["MusicBrainzTrack"], + userId: _finampUserHelper.currentUserId, + ); } - Future _logOfflineListen( - int timestamp, - String id, - String name, + Future _logOfflineListen({ + required int timestamp, + required String itemId, + required String name, String? artist, String? album, String? trackMbid, - ) async { + String? userId, + }) async { final data = { 'timestamp': timestamp, - 'id': id, + 'item_id': itemId, 'title': name, 'artist': artist, 'album': album, 'track_mbid': trackMbid, + 'user_id': userId, }; final content = json.encode(data) + Platform.lineTerminator;