From 52acf41e331ddbd66befed4692c804b754ba7d5c Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 30 Jan 2020 20:26:52 +0800 Subject: ... --- Timeline/Services/UserAvatarService.cs | 48 ++++++++++++---------------------- 1 file changed, 16 insertions(+), 32 deletions(-) (limited to 'Timeline/Services/UserAvatarService.cs') diff --git a/Timeline/Services/UserAvatarService.cs b/Timeline/Services/UserAvatarService.cs index ac7dd857..39b408e6 100644 --- a/Timeline/Services/UserAvatarService.cs +++ b/Timeline/Services/UserAvatarService.cs @@ -11,7 +11,6 @@ using System.Linq; using System.Threading.Tasks; using Timeline.Entities; using Timeline.Helpers; -using Timeline.Models.Validation; namespace Timeline.Services { @@ -61,36 +60,27 @@ namespace Timeline.Services public interface IUserAvatarService { /// - /// Get the etag of a user's avatar. + /// Get the etag of a user's avatar. Warning: This method does not check the user existence. /// - /// The username of the user to get avatar etag of. + /// The id of the user to get avatar etag of. /// The etag. - /// Thrown if is null. - /// Thrown if the is of bad format. - /// Thrown if the user does not exist. - Task GetAvatarETag(string username); + Task GetAvatarETag(long id); /// - /// Get avatar of a user. If the user has no avatar set, a default one is returned. + /// Get avatar of a user. If the user has no avatar set, a default one is returned. Warning: This method does not check the user existence. /// - /// The username of the user to get avatar of. + /// The id of the user to get avatar of. /// The avatar info. - /// Thrown if is null. - /// Thrown if the is of bad format. - /// Thrown if the user does not exist. - Task GetAvatar(string username); + Task GetAvatar(long id); /// - /// Set avatar for a user. + /// Set avatar for a user. Warning: This method does not check the user existence. /// - /// The username of the user to set avatar for. + /// The id of the user to set avatar for. /// The avatar. Can be null to delete the saved avatar. - /// Throw if is null. /// Thrown if any field in is null when is not null. - /// Thrown if the is of bad format. - /// Thrown if the user does not exist. /// Thrown if avatar is of bad format. - Task SetAvatar(string username, Avatar? avatar); + Task SetAvatar(long id, Avatar? avatar); } // TODO! : Make this configurable. @@ -104,7 +94,6 @@ namespace Timeline.Services private DateTime _cacheLastModified; private string _cacheETag = default!; - [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "DI.")] public DefaultUserAvatarProvider(IWebHostEnvironment environment, IETagGenerator eTagGenerator) { _avatarPath = Path.Combine(environment.ContentRootPath, "default-avatar.png"); @@ -195,22 +184,18 @@ namespace Timeline.Services _clock = clock; } - public async Task GetAvatarETag(string username) + public async Task GetAvatarETag(long id) { - var userId = await DatabaseExtensions.CheckAndGetUser(_database.Users, username); - - var eTag = (await _database.UserAvatars.Where(a => a.UserId == userId).Select(a => new { a.ETag }).SingleOrDefaultAsync())?.ETag; + var eTag = (await _database.UserAvatars.Where(a => a.UserId == id).Select(a => new { a.ETag }).SingleOrDefaultAsync())?.ETag; if (eTag == null) return await _defaultUserAvatarProvider.GetDefaultAvatarETag(); else return eTag; } - public async Task GetAvatar(string username) + public async Task GetAvatar(long id) { - var userId = await DatabaseExtensions.CheckAndGetUser(_database.Users, username); - - var avatarEntity = await _database.UserAvatars.Where(a => a.UserId == userId).Select(a => new { a.Type, a.Data, a.LastModified }).SingleOrDefaultAsync(); + var avatarEntity = await _database.UserAvatars.Where(a => a.UserId == id).Select(a => new { a.Type, a.Data, a.LastModified }).SingleOrDefaultAsync(); if (avatarEntity != null) { @@ -240,7 +225,7 @@ namespace Timeline.Services return defaultAvatar; } - public async Task SetAvatar(string username, Avatar? avatar) + public async Task SetAvatar(long id, Avatar? avatar) { if (avatar != null) { @@ -250,8 +235,7 @@ namespace Timeline.Services throw new ArgumentException(Resources.Services.UserAvatarService.ExceptionAvatarTypeNullOrEmpty, nameof(avatar)); } - var userId = await DatabaseExtensions.CheckAndGetUser(_database.Users, username); - var avatarEntity = await _database.UserAvatars.Where(a => a.UserId == userId).SingleOrDefaultAsync(); + var avatarEntity = await _database.UserAvatars.Where(a => a.UserId == id).SingleOrDefaultAsync(); if (avatar == null) { @@ -281,7 +265,7 @@ namespace Timeline.Services avatarEntity.Data = avatar.Data; avatarEntity.ETag = await _eTagGenerator.Generate(avatar.Data); avatarEntity.LastModified = _clock.GetCurrentTime(); - avatarEntity.UserId = userId; + avatarEntity.UserId = id; if (create) { _database.UserAvatars.Add(avatarEntity); -- cgit v1.2.3