Files
Teti/Teti/Services/UserManagementService.cs
T
Alby96 d2ca019d64 Creazione progetto InstaArchive WinUI 3 (.NET 8)
Aggiunta di tutti i file sorgente, configurazione e risorse per la nuova app desktop InstaArchive. Implementati servizi per monitoraggio e archiviazione automatica di contenuti Instagram (post, storie, reels, highlights) con persistenza locale, gestione utenti, impostazioni avanzate, dashboard e interfaccia moderna in italiano. Integrazione MVVM, rate limiting, iniezione metadati e funzionalità di import/export.
2026-01-07 14:03:34 +01:00

160 lines
5.1 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using InstaArchive.Models;
using InstaArchive.Repositories;
namespace InstaArchive.Services;
public class UserManagementService
{
private readonly FileBasedUserRepository _userRepository;
private readonly SettingsService _settingsService;
public UserManagementService(FileBasedUserRepository userRepository, SettingsService settingsService)
{
_userRepository = userRepository;
_settingsService = settingsService;
}
public async Task<InstagramUser> AddUserAsync(long userId, string username)
{
var user = new InstagramUser
{
UserId = userId,
CurrentUsername = username,
UsernameHistoryJson = JsonConvert.SerializeObject(new List<UsernameHistoryEntry>
{
new() { Username = username, ChangedAt = DateTime.UtcNow }
})
};
await _userRepository.AddUserAsync(user);
await CreateUserDirectoryStructureAsync(user);
return user;
}
public async Task UpdateUserAsync(InstagramUser user)
{
var existing = await _userRepository.GetUserByIdAsync(user.UserId);
if (existing == null)
{
throw new InvalidOperationException($"User with ID {user.UserId} not found");
}
// Check if username changed
if (existing.CurrentUsername != user.CurrentUsername)
{
var history = JsonConvert.DeserializeObject<List<UsernameHistoryEntry>>(
existing.UsernameHistoryJson
) ?? new List<UsernameHistoryEntry>();
history.Add(new UsernameHistoryEntry
{
Username = user.CurrentUsername,
ChangedAt = DateTime.UtcNow
});
user.UsernameHistoryJson = JsonConvert.SerializeObject(history);
}
await _userRepository.UpdateUserAsync(user);
await SaveUserMetadataAsync(user);
}
public async Task<InstagramUser?> GetUserAsync(long userId)
{
return await _userRepository.GetUserByIdAsync(userId);
}
public async Task<List<InstagramUser>> GetAllUsersAsync()
{
return await _userRepository.GetAllUsersAsync();
}
public async Task DeleteUserAsync(long userId)
{
await _userRepository.DeleteUserAsync(userId);
}
private async Task CreateUserDirectoryStructureAsync(InstagramUser user)
{
var settings = _settingsService.GetSettings();
var basePath = user.CustomBasePath ?? settings.BasePath;
var userPath = Path.Combine(basePath, user.UserId.ToString());
Directory.CreateDirectory(userPath);
Directory.CreateDirectory(Path.Combine(userPath, "Feed"));
Directory.CreateDirectory(Path.Combine(userPath, "Stories"));
Directory.CreateDirectory(Path.Combine(userPath, "Reels"));
Directory.CreateDirectory(Path.Combine(userPath, "Highlights"));
await SaveUserMetadataAsync(user);
}
private async Task SaveUserMetadataAsync(InstagramUser user)
{
var settings = _settingsService.GetSettings();
var basePath = user.CustomBasePath ?? settings.BasePath;
var userPath = Path.Combine(basePath, user.UserId.ToString());
var metadataPath = Path.Combine(userPath, "user_metadata.json");
var metadata = new
{
user.UserId,
user.CurrentUsername,
user.Biography,
user.ProfilePictureUrl,
user.AddedDate,
user.LastUpdated,
UsernameHistory = JsonConvert.DeserializeObject<List<UsernameHistoryEntry>>(
user.UsernameHistoryJson
),
Configuration = new
{
user.MonitorPosts,
user.MonitorStories,
user.MonitorReels,
user.MonitorHighlights,
user.StoriesCheckInterval,
user.PostsCheckInterval,
user.CustomBasePath
}
};
var json = JsonConvert.SerializeObject(metadata, Formatting.Indented);
await File.WriteAllTextAsync(metadataPath, json);
}
public async Task<string> ExportUsersAsync(string exportPath)
{
var users = await GetAllUsersAsync();
var json = JsonConvert.SerializeObject(users, Formatting.Indented);
await File.WriteAllTextAsync(exportPath, json);
return exportPath;
}
public async Task ImportUsersAsync(string importPath)
{
var json = await File.ReadAllTextAsync(importPath);
var users = JsonConvert.DeserializeObject<List<InstagramUser>>(json);
if (users != null)
{
foreach (var user in users)
{
var existing = await _userRepository.GetUserByIdAsync(user.UserId);
if (existing == null)
{
await _userRepository.AddUserAsync(user);
await CreateUserDirectoryStructureAsync(user);
}
}
}
}
}