public Task <List <GetReportViewModel> > Handle(GetReportsQuery request, CancellationToken cancellationToken = default) { if (!RoleHelper.CanEdit(request.Role)) { throw new GetReportUnauthorizedUserException(); } var query = _dbContext.Reports.Include(e => e.Prompt).AsQueryable(); if (request.Role == RoleEnum.TagEdit) { query = query.Where(e => e.ReportReason == ReportReason.IncorrectTags || e.ReportReason == ReportReason.UntaggedNsfw); } return(query .AsNoTracking() .Select(report => new GetReportViewModel { Id = report.Id, ExtraDetails = report.ExtraDetails, PromptId = report.PromptId, PromptTitle = report.Prompt !.Title, ReportReason = report.ReportReason, Cleared = report.Cleared }).ToListAsync(cancellationToken));
public async Task <IActionResult> Edit(int id, CancellationToken cancellationToken) { if (!_currentUserService.TryGetCurrentUser(out GetUserViewModel? user)) { return(NotFound()); } GetPromptViewModel?prompt = await _mediator.Send(new GetPromptQuery(id), cancellationToken); if (prompt == null || (prompt.OwnerId != user !.Id && !RoleHelper.CanEdit(user.Role))) { return(NotFound()); } var command = new UpdatePromptCommand { AuthorsNote = prompt.AuthorsNote, Description = prompt.Description, Id = prompt.Id, Memory = prompt.Memory, Nsfw = prompt.Nsfw, PromptContent = prompt.PromptContent, PromptTags = string.Join(", ", prompt.PromptTags.Select(pt => pt.Name)), Quests = prompt.Quests, Title = prompt.Title, OwnerId = prompt.OwnerId, ParentId = prompt.ParentId, NovelAiScenario = prompt.NovelAiScenario, WorldInfos = prompt.WorldInfos.Any() ? prompt.WorldInfos .Select(wi => new UpdatePromptCommandWorldInfo { Entry = wi.Entry, Keys = wi.Keys }).ToList() : new List <UpdatePromptCommandWorldInfo> { new() } }; return(View(new UpdatePromptViewModel { Children = prompt.Children.ToList(), Command = command })); }
public static Task SignInUserAsync(this HttpContext context, GetUserViewModel user) { var claims = new List <Claim> { new(ClaimTypes.NameIdentifier, user.Id.ToString()), new(ClaimValueConstants.CanEdit, RoleHelper.CanEdit(user.Role).ToString()) }; var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties { AllowRefresh = true, ExpiresUtc = DateTimeOffset.UtcNow.AddDays(365), IsPersistent = true, IssuedUtc = DateTimeOffset.UtcNow }; return(context.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties )); }
public async Task <IActionResult> Edit(int?id, bool addWi, bool saveDraft, bool confirm, bool addChild, bool uploadWi, int?wiDelete, UpdatePromptViewModel model, IFormFile?scenarioFile, IFormFile?holoFile, CancellationToken cancellationToken) { model.Command.SaveDraft = saveDraft; if (id == null || !_currentUserService.TryGetCurrentUser(out GetUserViewModel? user)) { return(NotFound()); } GetPromptViewModel?prompt = await _mediator.Send(new GetPromptQuery(id.Value), cancellationToken); if (prompt == null || (prompt.OwnerId != user !.Id && !RoleHelper.CanEdit(user.Role))) { return(NotFound()); } model.Children = prompt.Children.ToList(); model.Command.Id = prompt.Id; model.Command.OwnerId = prompt.OwnerId; if (scenarioFile != null) { var novelAiScenarioString = await ReadFormFile(scenarioFile); try { NovelAiScenario?novelAiScenario = JsonSerializer.Deserialize <NovelAiScenario>(novelAiScenarioString); if (novelAiScenario != null) { model.Command.Description = novelAiScenario.Description; model.Command.Memory = novelAiScenario.Context.FirstOrDefault()?.Text; model.Command.AuthorsNote = novelAiScenario.Context.ElementAtOrDefault(1)?.Text; model.Command.Title = novelAiScenario.Title; model.Command.PromptContent = novelAiScenario.Prompt; model.Command.PromptTags = string.Join(", ", novelAiScenario.Tags); model.Command.WorldInfos = novelAiScenario.Lorebook.LorebookEntries.Any() ? novelAiScenario.Lorebook.LorebookEntries.Select(lore => new UpdatePromptCommandWorldInfo { Keys = string.Join(", ", lore.Keys), Entry = lore.Text }) .ToList() : new List <UpdatePromptCommandWorldInfo> { new() }; model.Command.NovelAiScenario = novelAiScenarioString; } } catch (JsonException e) { _logger.LogError(e, "Could not decode NAI Json data"); } ModelState.Clear(); return(View(model)); } if (holoFile != null) { var holoScenarioString = await ReadFormFile(holoFile); try { HoloAiScenario?holoScenario = JsonSerializer.Deserialize <HoloAiScenario>(holoScenarioString, new JsonSerializerOptions(JsonSerializerDefaults.Web)); if (holoScenario != null) { model.Command.Description = null; model.Command.Memory = holoScenario.Memory; model.Command.AuthorsNote = holoScenario.AuthorsNote; model.Command.Title = holoScenario.Title; model.Command.PromptContent = string.Join('\n', holoScenario.Content.Select(content => string.Join(' ', content.Children.Select(child => child.Text)))); model.Command.PromptTags = string.Join(", ", holoScenario.Tags); model.Command.WorldInfos = holoScenario.WorldInfo.ConvertAll(e => new UpdatePromptCommandWorldInfo { Entry = e.Value, Keys = string.Join(", ", e.Keys) }); model.Command.HoloAiScenario = holoScenarioString; } } catch (JsonException e) { _logger.LogError(e, "Could not decode Holo Json data"); } ModelState.Clear(); return(View(model)); } if (uploadWi) { ModelState.Clear(); if (model.WorldInfoFile != null) { List <WorldInfoJson> worldInfos = await ReadWorldInfoFromFileAsync(model.WorldInfoFile); if (worldInfos.Count > 0) { if (model.Command.WorldInfos.Count == 1 && string.IsNullOrWhiteSpace(model.Command.WorldInfos[0].Entry) && string.IsNullOrWhiteSpace(model.Command.WorldInfos[0].Keys)) { model.Command.WorldInfos = worldInfos.Select(wi => new UpdatePromptCommandWorldInfo { Keys = wi.Keys, Entry = wi.Entry }).ToList(); } else { model.Command.WorldInfos.AddRange(worldInfos.Select(wi => new UpdatePromptCommandWorldInfo { Keys = wi.Keys, Entry = wi.Entry })); } } } return(View(model)); } if (wiDelete.HasValue) { ModelState.Clear(); model.Command.WorldInfos.RemoveAt(wiDelete.Value); if (model.Command.WorldInfos.Count < 1) { model.Command.WorldInfos.Add(new UpdatePromptCommandWorldInfo()); } return(View(model)); } if (addWi) { ModelState.Clear(); model.Command.WorldInfos.Add(new UpdatePromptCommandWorldInfo()); return(View(model)); } if (model.ScriptZip != null) { using var stream = new MemoryStream(); await model.ScriptZip.CopyToAsync(stream, cancellationToken); model.Command.ScriptZip = stream.ToArray(); var validator = new UpdatePromptCommandValidator(); ValidationResult?results = await validator.ValidateAsync(model.Command, cancellationToken); results.AddToModelState(ModelState, nameof(model.Command)); } if (!ModelState.IsValid) { return(View(model)); } if (!model.Command.SaveDraft && !addChild && !model.Command.ParentId.HasValue) { SimilarPromptViewModel?duplicate = await _mediator.Send(new SimilarPromptQuery(model.Command.Title, model.Command.Id), cancellationToken); if (duplicate.Matched && !confirm) { model.SimilarPromptQuery = duplicate; return(View(model)); } } await _mediator.Send(model.Command, cancellationToken); if (addChild) { return(RedirectToAction("Create", new { parentId = id })); } if (model.Command.ParentId.HasValue) { return(RedirectToAction("Edit", new { id = model.Command.ParentId })); } return(RedirectToAction("View", new { id })); }