public async Task SetFields(int projectId, int characterId, Dictionary <int, string> requestFieldValues) { var character = await LoadProjectSubEntityAsync <Character>(projectId, characterId); character.RequestMasterAccess(CurrentUserId, acl => acl.CanEditRoles); character.EnsureProjectActive(); var changedFields = FieldSaveHelper.SaveCharacterFields(CurrentUserId, character, requestFieldValues, FieldDefaultValueGenerator); MarkChanged(character); FieldsChangedEmail email = null; if (changedFields.Any()) { var user = await UserRepository.GetById(CurrentUserId); email = EmailHelpers.CreateFieldsEmail( character, s => s.FieldChange, user, changedFields); } await UnitOfWork.SaveChangesAsync(); if (email != null) { await EmailService.Email(email); } }
//TODO: move character operations to a separate service. public async Task EditCharacter(int currentUserId, int characterId, int projectId, string name, bool isPublic, IReadOnlyCollection <int> parentCharacterGroupIds, bool isAcceptingClaims, string contents, bool hidePlayerForCharacter, IDictionary <int, string> characterFields, bool isHot) { var character = await LoadProjectSubEntityAsync <Character>(projectId, characterId); character.RequestMasterAccess(currentUserId, acl => acl.CanEditRoles); character.EnsureProjectActive(); var changedAttributes = new Dictionary <string, PreviousAndNewValue>(); changedAttributes.Add("Имя персонажа", new PreviousAndNewValue(name, character.CharacterName.Trim())); character.CharacterName = name.Trim(); character.IsAcceptingClaims = isAcceptingClaims; character.IsPublic = isPublic; var newDescription = new MarkdownString(contents); changedAttributes.Add("Описание персонажа", new PreviousAndNewValue(newDescription, character.Description)); character.Description = newDescription; character.HidePlayerForCharacter = hidePlayerForCharacter; character.IsHot = isHot; character.IsActive = true; character.ParentCharacterGroupIds = await ValidateCharacterGroupList(projectId, Required(parentCharacterGroupIds), ensureNotSpecial : true); var changedFields = FieldSaveHelper.SaveCharacterFields(currentUserId, character, characterFields, FieldDefaultValueGenerator); MarkChanged(character); MarkTreeModified(character.Project); //TODO: Can be smarter FieldsChangedEmail email = null; changedAttributes = changedAttributes .Where(attr => attr.Value.DisplayString != attr.Value.PreviousDisplayString) .ToDictionary(x => x.Key, x => x.Value); if (changedFields.Any() || changedAttributes.Any()) { var user = await UserRepository.GetById(currentUserId); email = EmailHelpers.CreateFieldsEmail( character, s => s.FieldChange, user, changedFields, changedAttributes); } await UnitOfWork.SaveChangesAsync(); if (email != null) { await EmailService.Email(email); } }
public async Task Email(FieldsChangedEmail model) { var projectEmailEnabled = model.GetEmailEnabled(); if (!projectEmailEnabled) { return; } var recipients = model .GetRecipients() .Select(r => new RecepientData( r.GetDisplayName(), r.Email, new Dictionary <string, string> { { ChangedFieldsKey, GetChangedFieldsInfoForUser(model, r) } })) .Where(r => !string.IsNullOrEmpty(r.RecipientSpecificValues[ChangedFieldsKey])) //don't email if no changes are visible to user rights .ToList(); string Target(bool forMessageBody) => model.IsCharacterMail ? $@"персонаж{(forMessageBody ? "a" : "")} {model.Character.CharacterName}" : $"заявк{(forMessageBody ? "и" : "a")} {model.Claim?.Name} {(forMessageBody ? $", игрок {model.Claim?.Player.GetDisplayName()}" : "")}"; var linkString = _uriService.Get(model.GetLinkable()); if (recipients.Any()) { var text = $@"{StandartGreeting()}, Данные {Target(true)} были изменены. Новые значения: {MessageService.GetUserDependentValue(ChangedFieldsKey)} Для просмотра всех данных перейдите на страницу {(model.IsCharacterMail ? "персонажа" : "заявки")}: {linkString} {model.Initiator.GetDisplayName()} "; //All emails related to claim should have the same title, even if the change was made to a character Claim claim = model.IsCharacterMail ? model.Character.ApprovedClaim : model.Claim; var subject = claim != null ? model.GetClaimEmailTitle(claim) : $"{model.ProjectName}: {Target(false)}"; await MessageService.SendEmails(subject, new MarkdownString(text), model.Initiator.ToRecepientData(), recipients); } }
public async Task Email(FieldsChangedEmail model) { var projectEmailEnabled = model.GetEmailEnabled(); if (!projectEmailEnabled) { return; } IList <MailRecipient> recipients = model .GetRecipients() .Select(r => new MailRecipient( r, new Dictionary <string, string> { { changedFieldsKey, GetChangedFieldsInfoForUser(model, r) } })) .Where(r => !string.IsNullOrEmpty(r.RecipientSpecificValues[changedFieldsKey])) //don't email if no changes are visible to user rights .ToList(); Func <bool, string> target = (forMessageBody) => model.IsCharacterMail ? $@"персонаж{(forMessageBody ? "a" : "")} {model.Character.CharacterName}" : $"заявк{(forMessageBody ? "и" : "a")} {model.Claim.Name} {(forMessageBody ? $", игрок {model.Claim.Player.DisplayName}" : "")}"; string linkString = model.IsCharacterMail ? _uriService.Get(model.Character) : _uriService.Get(model.Claim); if (recipients.Any()) { await SendEmail(recipients, $"{model.ProjectName}: {target(false)}", $@"Добрый день, {MailGunExts.MailGunRecipientName}, Данные {target(true)} были изменены. Новые значения: {MailGunExts.GetUserDependentValue(changedFieldsKey)} Для просмотра всех данных перейдите на страницу {(model.IsCharacterMail ? "персонажа" : "заявки")}: {linkString} {model.Initiator.DisplayName} ", model.Initiator.ToRecipient()); } }