From 79ab2b304d93b1029515bd3f954db4e5a73f4168 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 30 Jan 2020 20:26:52 +0800 Subject: ... --- Timeline/Services/TimelineService.cs | 363 +++++++++++------------------------ 1 file changed, 114 insertions(+), 249 deletions(-) (limited to 'Timeline/Services/TimelineService.cs') diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index f43d2de5..89936aa2 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -1,13 +1,15 @@ -using Microsoft.EntityFrameworkCore; +using AutoMapper; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Threading.Tasks; using Timeline.Entities; -using Timeline.Models; using Timeline.Models.Http; using Timeline.Models.Validation; +using static Timeline.Resources.Services.TimelineService; namespace Timeline.Services { @@ -28,12 +30,7 @@ namespace Timeline.Services /// Username or the timeline name. See remarks of . /// A list of all posts. /// Thrown when is null. - /// - /// Thrown when timeline name is of bad format. - /// For normal timeline, it means name is an empty string. - /// For personal timeline, it means the username is of bad format, - /// the inner exception should be a . - /// + /// Thrown when is illegal. It is not a valid timeline name (for normal timeline service) or a valid username (for personal timeline service). /// /// Thrown when timeline does not exist. /// For normal timeline, it means the name does not exist. @@ -46,26 +43,20 @@ namespace Timeline.Services /// Create a new post in timeline. /// /// Username or the timeline name. See remarks of . - /// The author's username. + /// The author's id. /// The content. /// The time of the post. If null, then use current time. /// The info of the created post. - /// Thrown when or or is null. - /// - /// Thrown when timeline name is of bad format. - /// For normal timeline, it means name is an empty string. - /// For personal timeline, it means the username is of bad format, - /// the inner exception should be a . - /// + /// Thrown when or is null. + /// Thrown when is illegal. It is not a valid timeline name (for normal timeline service) or a valid username (for personal timeline service). /// /// Thrown when timeline does not exist. /// For normal timeline, it means the name does not exist. /// For personal timeline, it means the user of that username does not exist /// and the inner exception should be a . /// - /// Thrown if is of bad format. - /// Thrown if does not exist. - Task CreatePost(string name, string author, string content, DateTime? time); + /// Thrown if user with does not exist. + Task CreatePost(string name, long authorId, string content, DateTime? time); /// /// Delete a post @@ -73,12 +64,7 @@ namespace Timeline.Services /// Username or the timeline name. See remarks of . /// The id of the post to delete. /// Thrown when or is null. - /// - /// Thrown when timeline name is of bad format. - /// For normal timeline, it means name is an empty string. - /// For personal timeline, it means the username is of bad format, - /// the inner exception should be a . - /// + /// Thrown when is illegal. It is not a valid timeline name (for normal timeline service) or a valid username (for personal timeline service). /// /// Thrown when timeline does not exist. /// For normal timeline, it means the name does not exist. @@ -100,19 +86,14 @@ namespace Timeline.Services /// Username or the timeline name. See remarks of . /// The new properties. Null member means not to change. /// Thrown when or is null. - /// - /// Thrown when timeline name is of bad format. - /// For normal timeline, it means name is an empty string. - /// For personal timeline, it means the username is of bad format, - /// the inner exception should be a . - /// + /// Thrown when is illegal. It is not a valid timeline name (for normal timeline service) or a valid username (for personal timeline service). /// /// Thrown when timeline does not exist. /// For normal timeline, it means the name does not exist. /// For personal timeline, it means the user of that username does not exist /// and the inner exception should be a . /// - Task ChangeProperty(string name, TimelinePropertyChangeRequest newProperties); + Task ChangeProperty(string name, TimelinePatchRequest newProperties); /// /// Remove members to a timeline. @@ -121,24 +102,16 @@ namespace Timeline.Services /// A list of usernames of members to add. May be null. /// A list of usernames of members to remove. May be null. /// Thrown when is null. - /// - /// Thrown when timeline name is of bad format. - /// For normal timeline, it means name is an empty string. - /// For personal timeline, it means the username is of bad format, - /// the inner exception should be a . - /// + /// Thrown when is illegal. It is not a valid timeline name (for normal timeline service) or a valid username (for personal timeline service). + /// Thrown when names in or is not a valid username. /// /// Thrown when timeline does not exist. /// For normal timeline, it means the name does not exist. /// For personal timeline, it means the user of that username does not exist /// and the inner exception should be a . /// - /// - /// Thrown when an exception occurs on the user list. - /// The inner exception is - /// when one of the username is invalid. - /// The inner exception is - /// when one of the user to change does not exist. + /// + /// Thrown when one of the user to change does not exist. /// /// /// Operating on a username that is of bad format or does not exist always throws. @@ -153,42 +126,30 @@ namespace Timeline.Services /// Verify whether a visitor has the permission to read a timeline. /// /// Username or the timeline name. See remarks of . - /// The user to check on. Null means visitor without account. + /// The id of the user to check on. Null means visitor without account. /// True if can read, false if can't read. /// Thrown when is null. - /// - /// Thrown when timeline name is of bad format. - /// For normal timeline, it means name is an empty string. - /// For personal timeline, it means the username is of bad format, - /// the inner exception should be a . - /// + /// Thrown when is illegal. It is not a valid timeline name (for normal timeline service) or a valid username (for personal timeline service). /// /// Thrown when timeline does not exist. /// For normal timeline, it means the name does not exist. /// For personal timeline, it means the user of that username does not exist /// and the inner exception should be a . /// - /// - /// Thrown when is of bad format. - /// - /// - /// Thrown when does not exist. - /// - Task HasReadPermission(string name, string? username); + /// + /// This method does not check whether visitor is administrator. + /// Return false if user with visitor id does not exist. + /// + Task HasReadPermission(string name, long? visitorId); /// /// Verify whether a user has the permission to modify a post. /// /// Username or the timeline name. See remarks of . - /// The user to check on. + /// The id of the user to check on. /// True if can modify, false if can't modify. - /// Thrown when or is null. - /// - /// Thrown when timeline name is of bad format. - /// For normal timeline, it means name is an empty string. - /// For personal timeline, it means the username is of bad format, - /// the inner exception should be a . - /// + /// Thrown when is null. + /// Thrown when is illegal. It is not a valid timeline name (for normal timeline service) or a valid username (for personal timeline service). /// /// Thrown when timeline does not exist. /// For normal timeline, it means the name does not exist. @@ -198,47 +159,32 @@ namespace Timeline.Services /// /// Thrown when the post with given id does not exist or is deleted already. /// - /// - /// Thrown when is of bad format. - /// - /// - /// Thrown when does not exist. - /// /// /// This method does not check whether the user is administrator. /// It only checks whether he is the author of the post or the owner of the timeline. + /// Return false when user with modifier id does not exist. /// - Task HasPostModifyPermission(string name, long id, string username); + Task HasPostModifyPermission(string name, long id, long modifierId); /// /// Verify whether a user is member of a timeline. /// /// Username or the timeline name. See remarks of . - /// The user to check on. + /// The id of user to check on. /// True if it is a member, false if not. - /// Thrown when or is null. - /// - /// Thrown when timeline name is of bad format. - /// For normal timeline, it means name is an empty string. - /// For personal timeline, it means the username is of bad format, - /// the inner exception should be a . - /// + /// Thrown when is null. + /// Thrown when is illegal. It is not a valid timeline name (for normal timeline service) or a valid username (for personal timeline service). /// /// Thrown when timeline does not exist. /// For normal timeline, it means the name does not exist. /// For personal timeline, it means the user of that username does not exist /// and the inner exception should be a . /// - /// - /// Thrown when is not a valid username. - /// - /// - /// Thrown when user does not exist. - /// /// /// Timeline owner is also considered as a member. + /// Return false when user with user id does not exist. /// - Task IsMemberOf(string name, string username); + Task IsMemberOf(string name, long userId); } /// @@ -252,7 +198,7 @@ namespace Timeline.Services /// The name of the timeline. /// The timeline info. /// Thrown when is null. - /// + /// /// Thrown when timeline name is invalid. Currently it means it is an empty string. /// /// @@ -264,20 +210,12 @@ namespace Timeline.Services /// Create a timeline. /// /// The name of the timeline. - /// The owner of the timeline. + /// The id of owner of the timeline. /// Thrown when or is null. - /// - /// Thrown when timeline name is invalid. Currently it means it is an empty string. - /// - /// - /// Thrown when the timeline already exists. - /// - /// - /// Thrown when the username of the owner is not valid. - /// - /// - /// Thrown when the owner user does not exist. - Task CreateTimeline(string name, string owner); + /// Thrown when timeline name is invalid. Currently it means it is an empty string. + /// Thrown when the timeline already exists. + /// Thrown when the owner user does not exist. + Task CreateTimeline(string name, long owner); } public interface IPersonalTimelineService : IBaseTimelineService @@ -290,8 +228,8 @@ namespace Timeline.Services /// /// Thrown when is null. /// - /// - /// Thrown when is of bad format. Inner exception MUST be . + /// + /// Thrown when is of bad format. /// /// /// Thrown when the user does not exist. Inner exception MUST be . @@ -301,10 +239,12 @@ namespace Timeline.Services public abstract class BaseTimelineService : IBaseTimelineService { - protected BaseTimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IClock clock) + protected BaseTimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IUserService userService, IMapper mapper, IClock clock) { Clock = clock; Database = database; + UserService = userService; + Mapper = mapper; } protected IClock Clock { get; } @@ -313,6 +253,10 @@ namespace Timeline.Services protected DatabaseContext Database { get; } + protected IUserService UserService { get; } + + protected IMapper Mapper { get; } + /// /// Find the timeline id by the name. /// For details, see remarks. @@ -320,12 +264,7 @@ namespace Timeline.Services /// The username or the timeline name. See remarks. /// The id of the timeline entity. /// Thrown when is null. - /// - /// Thrown when timeline name is of bad format. - /// For normal timeline, it means name is an empty string. - /// For personal timeline, it means the username is of bad format, - /// the inner exception should be a . - /// + /// Thrown when is illegal. It is not a valid timeline name (for normal timeline service) or a valid username (for personal timeline service). /// /// Thrown when timeline does not exist. /// For normal timeline, it means the name does not exist. @@ -347,66 +286,60 @@ namespace Timeline.Services if (name == null) throw new ArgumentNullException(nameof(name)); + var timelineId = await FindTimelineId(name); var postEntities = await Database.TimelinePosts.OrderBy(p => p.Time).Where(p => p.TimelineId == timelineId && p.Content != null).ToListAsync(); + var posts = new List(); foreach (var entity in postEntities) { - posts.Add(new TimelinePostInfo + if (entity.Content != null) // otherwise it is deleted { - Id = entity.Id, - Content = entity.Content, - Author = (await Database.Users.Where(u => u.Id == entity.AuthorId).Select(u => new { u.Username }).SingleAsync()).Name, - Time = entity.Time - }); + var author = Mapper.Map(UserService.GetUserById(entity.AuthorId)); + posts.Add(new TimelinePostInfo + { + Id = entity.Id, + Content = entity.Content, + Author = author, + Time = entity.Time, + LastUpdated = entity.LastUpdated + }); + } } return posts; } - public async Task CreatePost(string name, string author, string content, DateTime? time) + public async Task CreatePost(string name, long authorId, string content, DateTime? time) { if (name == null) throw new ArgumentNullException(nameof(name)); - if (author == null) - throw new ArgumentNullException(nameof(author)); if (content == null) throw new ArgumentNullException(nameof(content)); - { - var (result, message) = UsernameValidator.Validate(author); - if (!result) - { - throw new UsernameBadFormatException(author, message); - } - } - var timelineId = await FindTimelineId(name); - - var authorEntity = Database.Users.Where(u => u.Username == author).Select(u => new { u.Id }).SingleOrDefault(); - if (authorEntity == null) - { - throw new UserNotExistException(author); - } - var authorId = authorEntity.Id; + var author = Mapper.Map(await UserService.GetUserById(authorId)); var currentTime = Clock.GetCurrentTime(); + var finalTime = time ?? currentTime; var postEntity = new TimelinePostEntity { Content = content, AuthorId = authorId, TimelineId = timelineId, - Time = time ?? currentTime, + Time = finalTime, LastUpdated = currentTime }; - Database.TimelinePosts.Add(postEntity); await Database.SaveChangesAsync(); - return new TimelinePostCreateResponse + return new TimelinePostInfo { Id = postEntity.Id, - Time = postEntity.Time + Content = content, + Author = author, + Time = finalTime, + LastUpdated = currentTime }; } @@ -426,7 +359,7 @@ namespace Timeline.Services await Database.SaveChangesAsync(); } - public async Task ChangeProperty(string name, TimelinePropertyChangeRequest newProperties) + public async Task ChangeProperty(string name, TimelinePatchRequest newProperties) { if (name == null) throw new ArgumentNullException(nameof(name)); @@ -455,27 +388,23 @@ namespace Timeline.Services if (name == null) throw new ArgumentNullException(nameof(name)); - // remove duplication and check the format of each username. - // Return a username->index map. - Dictionary? RemoveDuplicateAndCheckFormat(IList? list, TimelineMemberOperationUserException.MemberOperation operation) + List? RemoveDuplicateAndCheckFormat(IList? list, string paramName) { if (list != null) { - Dictionary result = new Dictionary(); + List result = new List(); var count = list.Count; for (var index = 0; index < count; index++) { var username = list[index]; - if (result.ContainsKey(username)) + if (result.Contains(username)) { continue; } var (validationResult, message) = UsernameValidator.Validate(username); if (!validationResult) - throw new TimelineMemberOperationUserException( - index, operation, username, - new UsernameBadFormatException(username, message)); - result.Add(username, index); + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ExceptionChangeMemberUsernameBadFormat, index), nameof(paramName)); + result.Add(username); } return result; } @@ -484,13 +413,13 @@ namespace Timeline.Services return null; } } - var simplifiedAdd = RemoveDuplicateAndCheckFormat(add, TimelineMemberOperationUserException.MemberOperation.Add); - var simplifiedRemove = RemoveDuplicateAndCheckFormat(remove, TimelineMemberOperationUserException.MemberOperation.Remove); + var simplifiedAdd = RemoveDuplicateAndCheckFormat(add, nameof(add)); + var simplifiedRemove = RemoveDuplicateAndCheckFormat(remove, nameof(remove)); // remove those both in add and remove if (simplifiedAdd != null && simplifiedRemove != null) { - var usersToClean = simplifiedRemove.Keys.Where(u => simplifiedAdd.ContainsKey(u)); + var usersToClean = simplifiedRemove.Where(u => simplifiedAdd.Contains(u)).ToList(); foreach (var u in usersToClean) { simplifiedAdd.Remove(u); @@ -500,26 +429,20 @@ namespace Timeline.Services var timelineId = await FindTimelineId(name); - async Task?> CheckExistenceAndGetId(Dictionary? map, TimelineMemberOperationUserException.MemberOperation operation) + async Task?> CheckExistenceAndGetId(List? list) { - if (map == null) + if (list == null) return null; List result = new List(); - foreach (var (username, index) in map) + foreach (var username in list) { - var user = await Database.Users.Where(u => u.Username == username).Select(u => new { u.Id }).SingleOrDefaultAsync(); - if (user == null) - { - throw new TimelineMemberOperationUserException(index, operation, username, - new UserNotExistException(username)); - } - result.Add(user.Id); + result.Add(await UserService.GetUserIdByUsername(username)); } return result; } - var userIdsAdd = await CheckExistenceAndGetId(simplifiedAdd, TimelineMemberOperationUserException.MemberOperation.Add); - var userIdsRemove = await CheckExistenceAndGetId(simplifiedRemove, TimelineMemberOperationUserException.MemberOperation.Remove); + var userIdsAdd = await CheckExistenceAndGetId(simplifiedAdd); + var userIdsRemove = await CheckExistenceAndGetId(simplifiedRemove); if (userIdsAdd != null) { @@ -536,30 +459,11 @@ namespace Timeline.Services await Database.SaveChangesAsync(); } - public async Task HasReadPermission(string name, string? username) + public async Task HasReadPermission(string name, long? visitorId) { if (name == null) throw new ArgumentNullException(nameof(name)); - long? userId = null; - if (username != null) - { - var (result, message) = UsernameValidator.Validate(username); - if (!result) - { - throw new UsernameBadFormatException(username); - } - - var user = await Database.Users.Where(u => u.Username == username).Select(u => new { u.Id }).SingleOrDefaultAsync(); - - if (user == null) - { - throw new UserNotExistException(username); - } - - userId = user.Id; - } - var timelineId = await FindTimelineId(name); var timelineEntity = await Database.Timelines.Where(t => t.Id == timelineId).Select(t => new { t.Visibility }).SingleAsync(); @@ -567,43 +471,24 @@ namespace Timeline.Services if (timelineEntity.Visibility == TimelineVisibility.Public) return true; - if (timelineEntity.Visibility == TimelineVisibility.Register && username != null) + if (timelineEntity.Visibility == TimelineVisibility.Register && visitorId != null) return true; - if (userId == null) + if (visitorId == null) { return false; } else { - var memberEntity = await Database.TimelineMembers.Where(m => m.UserId == userId && m.TimelineId == timelineId).SingleOrDefaultAsync(); + var memberEntity = await Database.TimelineMembers.Where(m => m.UserId == visitorId && m.TimelineId == timelineId).SingleOrDefaultAsync(); return memberEntity != null; } } - public async Task HasPostModifyPermission(string name, long id, string username) + public async Task HasPostModifyPermission(string name, long id, long modifierId) { if (name == null) throw new ArgumentNullException(nameof(name)); - if (username == null) - throw new ArgumentNullException(nameof(username)); - - { - var (result, message) = UsernameValidator.Validate(username); - if (!result) - { - throw new UsernameBadFormatException(username); - } - } - - var user = await Database.Users.Where(u => u.Username == username).Select(u => new { u.Id }).SingleOrDefaultAsync(); - - if (user == null) - { - throw new UserNotExistException(username); - } - - var userId = user.Id; var timelineId = await FindTimelineId(name); @@ -614,32 +499,13 @@ namespace Timeline.Services if (postEntity == null) throw new TimelinePostNotExistException(id); - return timelineEntity.OwnerId == userId || postEntity.AuthorId == userId; + return timelineEntity.OwnerId == modifierId || postEntity.AuthorId == modifierId; } - public async Task IsMemberOf(string name, string username) + public async Task IsMemberOf(string name, long userId) { if (name == null) throw new ArgumentNullException(nameof(name)); - if (username == null) - throw new ArgumentNullException(nameof(username)); - - { - var (result, message) = UsernameValidator.Validate(username); - if (!result) - { - throw new UsernameBadFormatException(username); - } - } - - var user = await Database.Users.Where(u => u.Username == username).Select(u => new { u.Id }).SingleOrDefaultAsync(); - - if (user == null) - { - throw new UserNotExistException(username); - } - - var userId = user.Id; var timelineId = await FindTimelineId(name); @@ -648,38 +514,33 @@ namespace Timeline.Services if (userId == timelineEntity.OwnerId) return true; - var timelineMemberEntity = await Database.TimelineMembers.Where(m => m.TimelineId == timelineId && m.UserId == userId).SingleOrDefaultAsync(); - - return timelineMemberEntity != null; + return await Database.TimelineMembers.AnyAsync(m => m.TimelineId == timelineId && m.UserId == userId); } } public class PersonalTimelineService : BaseTimelineService, IPersonalTimelineService { - public PersonalTimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IClock clock) - : base(loggerFactory, database, clock) + public PersonalTimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IUserService userService, IMapper mapper, IClock clock) + : base(loggerFactory, database, userService, mapper, clock) { } protected override async Task FindTimelineId(string name) { + long userId; + try { - var (result, message) = UsernameValidator.Validate(name); - if (!result) - { - throw new TimelineNameBadFormatException(name, new UsernameBadFormatException(name, message)); - } + userId = await UserService.GetUserIdByUsername(name); } - - var userEntity = await Database.Users.Where(u => u.Username == name).Select(u => new { u.Id }).SingleOrDefaultAsync(); - - if (userEntity == null) + catch (ArgumentException e) { - throw new TimelineNotExistException(name, new UserNotExistException(name)); + throw new ArgumentException(ExceptionFindTimelineUsernameBadFormat, nameof(name), e); + } + catch (UserNotExistException e) + { + throw new TimelineNotExistException(name, e); } - - var userId = userEntity.Id; var timelineEntity = await Database.Timelines.Where(t => t.OwnerId == userId && t.Name == null).Select(t => new { t.Id }).SingleOrDefaultAsync(); @@ -715,16 +576,20 @@ namespace Timeline.Services var timelineMemberEntities = await Database.TimelineMembers.Where(m => m.TimelineId == timelineId).Select(m => new { m.UserId }).ToListAsync(); - var memberUsernameTasks = timelineMemberEntities.Select(m => Database.Users.Where(u => u.Id == m.UserId).Select(u => u.Username).SingleAsync()).ToArray(); + var owner = Mapper.Map(await UserService.GetUserById(timelineEntity.OwnerId)); - var memberUsernames = await Task.WhenAll(memberUsernameTasks); + var members = new List(); + foreach (var memberEntity in timelineMemberEntities) + { + members.Add(Mapper.Map(await UserService.GetUserById(memberEntity.UserId))); + } return new BaseTimelineInfo { Description = timelineEntity.Description ?? "", - Owner = username, + Owner = owner, Visibility = timelineEntity.Visibility, - Members = memberUsernames.ToList() + Members = members }; } -- cgit v1.2.3