Calculate and enforce configurable resource consumption, expose balances in PostIt and document the rule in CGU. Add confirmed monthly PayPal credits and disk quotas of twice the free default for Discovery and five times for Premium, with payment reconciliation, migrations and regression tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
235 lines
8.3 KiB
C#
235 lines
8.3 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Yavsc.Models;
|
|
using Yavsc.Helpers;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc.Localization;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.AspNetCore.Diagnostics;
|
|
using Microsoft.Extensions.Options;
|
|
using Yavsc.Server.Helpers;
|
|
|
|
namespace Yavsc.Controllers
|
|
{
|
|
|
|
[AllowAnonymous]
|
|
public class HomeController : Controller
|
|
{
|
|
readonly ApplicationDbContext _dbContext;
|
|
readonly ILogger<HomeController> _logger;
|
|
private readonly bool _isDevelopment;
|
|
readonly IHtmlLocalizer _localizer;
|
|
|
|
private SiteSettings siteSettings;
|
|
public HomeController(ILogger<HomeController> logger,
|
|
IHtmlLocalizer<HomeController> localizer,
|
|
ApplicationDbContext context,
|
|
IOptions<SiteSettings> settingsOptions,
|
|
IWebHostEnvironment env
|
|
)
|
|
{
|
|
_localizer = localizer;
|
|
_dbContext = context;
|
|
siteSettings = settingsOptions.Value;
|
|
_logger = logger;
|
|
_isDevelopment = env.IsDevelopment();
|
|
|
|
}
|
|
|
|
public async Task<IActionResult> Index(string id)
|
|
{
|
|
ViewBag.IsFromSecureProx = Request.Headers.ContainsKey(Constants.SshHeaderKey) && Request.Headers[Constants.SshHeaderKey] == "on";
|
|
ViewBag.SecureHomeUrl = "https://" + Request.Headers["X-Forwarded-Host"];
|
|
ViewBag.SshHeaderKey = Request.Headers[Constants.SshHeaderKey];
|
|
var uid = User.GetUserId();
|
|
long[] clicked = null;
|
|
if (uid == null)
|
|
{
|
|
// await HttpContext.Session.LoadAsync();
|
|
var strclicked = HttpContext.Session.GetString("clicked");
|
|
if (strclicked != null) clicked = strclicked.Split(':').Select(c => long.Parse(c)).ToArray();
|
|
if (clicked == null) clicked = new long[0];
|
|
}
|
|
else clicked = _dbContext.DismissClicked.Where(d => d.UserId == uid).Select(d => d.NotificationId).ToArray();
|
|
var isAdmin = User.IsInRole(Constants.AdminName);
|
|
var notes = _dbContext.Notification.Where(
|
|
n => !clicked.Contains(n.Id)
|
|
).Where(n =>
|
|
string.IsNullOrWhiteSpace(n.Target)
|
|
|| (n.Target != null && n.Target.StartsWith("pub/"))
|
|
|| (!string.IsNullOrWhiteSpace(uid) && n.Target == $"user/{uid}")
|
|
|| (isAdmin && n.Target == Constants.Administration));
|
|
if (notes.Any()) this.Notify(notes);
|
|
|
|
var toShow = _dbContext.Activities
|
|
.Include(a => a.Forms)
|
|
.Include(a => a.Parent)
|
|
.Include(a => a.Children)
|
|
.Where(a => !a.Hidden)
|
|
.Where(a => a.ParentCode == id)
|
|
.OrderByDescending(a => a.Rate).ToList();
|
|
|
|
var candidateCodes = toShow
|
|
.Select(a => a.Code)
|
|
.Concat(toShow.SelectMany(a => (a.Children ?? new List<Yavsc.Models.Workflow.Activity>())
|
|
.Where(c => !c.Hidden)
|
|
.Select(c => c.Code)))
|
|
.Where(c => !string.IsNullOrWhiteSpace(c))
|
|
.Distinct()
|
|
.ToArray();
|
|
|
|
var performerCounts = _dbContext.UserActivities
|
|
.Where(ua => candidateCodes.Contains(ua.DoesCode))
|
|
.GroupBy(ua => ua.DoesCode)
|
|
.Select(g => new { Code = g.Key, Count = g.Select(x => x.UserId).Distinct().Count() })
|
|
.ToDictionary(x => x.Code, x => x.Count);
|
|
|
|
toShow = toShow
|
|
.Where(a =>
|
|
(performerCounts.TryGetValue(a.Code, out var ownCount) && ownCount > 0)
|
|
|| (a.Children ?? new List<Yavsc.Models.Workflow.Activity>())
|
|
.Where(c => !c.Hidden)
|
|
.Any(c => performerCounts.TryGetValue(c.Code, out var childCount) && childCount > 0))
|
|
.ToList();
|
|
|
|
foreach (var a in toShow)
|
|
{
|
|
a.Children = (a.Children ?? new List<Yavsc.Models.Workflow.Activity>())
|
|
.Where(c => !c.Hidden)
|
|
.Where(c => performerCounts.TryGetValue(c.Code, out var childCount) && childCount > 0)
|
|
.ToList();
|
|
}
|
|
return View(toShow);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Web-site dismissal of a notification: persisted for signed-in users,
|
|
/// kept in the session for anonymous visitors.
|
|
/// API clients use <c>POST api/v1/dismiss/click/{id}</c> on Yavsc.Api.
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<IActionResult> DismissNotification(long id)
|
|
{
|
|
if (!await _dbContext.Notification.AnyAsync(n => n.Id == id))
|
|
return NotFound();
|
|
|
|
var uid = User.GetUserId();
|
|
if (uid != null)
|
|
{
|
|
if (!await _dbContext.DismissClicked.AnyAsync(d => d.UserId == uid && d.NotificationId == id))
|
|
{
|
|
_dbContext.DismissClicked.Add(new Yavsc.Models.Messaging.DismissClicked { UserId = uid, NotificationId = id });
|
|
await _dbContext.SaveChangesAsync(uid);
|
|
}
|
|
return NoContent();
|
|
}
|
|
|
|
await HttpContext.Session.LoadAsync();
|
|
var clicked = HttpContext.Session.GetString("clicked");
|
|
HttpContext.Session.SetString("clicked", clicked == null ? id.ToString() : $"{clicked}:{id}");
|
|
await HttpContext.Session.CommitAsync();
|
|
return NoContent();
|
|
}
|
|
|
|
public async Task<IActionResult> About()
|
|
{
|
|
return View("About");
|
|
}
|
|
public IActionResult Privacy()
|
|
{
|
|
return View();
|
|
}
|
|
public IActionResult CGU()
|
|
{
|
|
return View();
|
|
}
|
|
public IActionResult AboutMarkdown()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
public IActionResult Contact()
|
|
{
|
|
return View(siteSettings);
|
|
}
|
|
public IActionResult Dash()
|
|
{
|
|
return View();
|
|
}
|
|
public ActionResult Chat()
|
|
{
|
|
if (User.Identity.IsAuthenticated)
|
|
{
|
|
ViewBag.IsAuthenticated = true;
|
|
string uid = User.GetUserId();
|
|
ViewBag.Contacts = _dbContext.Contact.Where(c => c.OwnerId == uid)
|
|
;
|
|
}
|
|
else ViewBag.IsAuthenticated = false;
|
|
return View();
|
|
}
|
|
|
|
public IActionResult Error()
|
|
{
|
|
if (_isDevelopment)
|
|
{
|
|
_logger.LogInformation(
|
|
"Home/Error requested in Development. This endpoint is disabled because DeveloperExceptionPage should handle unhandled exceptions.");
|
|
|
|
return NotFound(
|
|
"In Development, /Home/Error is disabled. Unhandled exceptions are rendered by DeveloperExceptionPage.");
|
|
}
|
|
|
|
var errorViewModel = new ErrorViewModel
|
|
{
|
|
RequestId = HttpContext.TraceIdentifier
|
|
};
|
|
|
|
var exceptionHandlerPathFeature =
|
|
HttpContext.Features.Get<IExceptionHandlerPathFeature>();
|
|
|
|
if (exceptionHandlerPathFeature is null)
|
|
{
|
|
_logger.LogWarning(
|
|
"Home/Error called without IExceptionHandlerPathFeature in non-development environment.");
|
|
|
|
return View("~/Views/Shared/Error.cshtml", errorViewModel);
|
|
}
|
|
|
|
if (exceptionHandlerPathFeature?.Error is FileNotFoundException)
|
|
{
|
|
errorViewModel.Description = "The file was not found.";
|
|
}
|
|
|
|
if (exceptionHandlerPathFeature?.Path == "/")
|
|
{
|
|
errorViewModel.Description ??= string.Empty;
|
|
errorViewModel.Description += " Page: Home.";
|
|
}
|
|
|
|
|
|
return View("~/Views/Shared/Error.cshtml", errorViewModel);
|
|
}
|
|
public IActionResult Status(int id)
|
|
{
|
|
ViewBag.StatusCode = id;
|
|
return View("~/Views/Shared/Status.cshtml");
|
|
}
|
|
public IActionResult Todo()
|
|
{
|
|
User.GetUserId();
|
|
|
|
return View();
|
|
}
|
|
|
|
public IActionResult VideoChat()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
public IActionResult Audio()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
}
|
|
}
|