2023-07-03 12:51:23 -04:00
using System.Diagnostics ;
2024-05-26 19:43:17 -04:00
using System.Text ;
using System.Web ;
2023-07-03 12:51:23 -04:00
using Microsoft.AspNetCore.Mvc ;
2024-06-10 16:24:30 -04:00
using Microsoft.EntityFrameworkCore ;
2024-05-26 19:43:17 -04:00
using Microsoft.Extensions.FileSystemGlobbing.Internal.PathSegments ;
2023-07-03 12:51:23 -04:00
using vassago.Models ;
namespace vassago.Controllers ;
public class HomeController : Controller
{
private readonly ILogger < HomeController > _logger ;
2024-05-26 19:43:17 -04:00
private readonly ChattingContext _db ;
2023-07-03 12:51:23 -04:00
2024-05-26 19:43:17 -04:00
public HomeController ( ILogger < HomeController > logger , ChattingContext db )
2023-07-03 12:51:23 -04:00
{
_logger = logger ;
2024-05-26 19:43:17 -04:00
_db = db ;
2023-07-03 12:51:23 -04:00
}
public IActionResult Index ( )
{
2024-05-26 19:43:17 -04:00
var allAccounts = _db . Accounts . ToList ( ) ;
2024-06-10 16:24:30 -04:00
var allChannels = _db . Channels . Include ( c = > c . Users ) . ToList ( ) ;
2024-05-26 19:43:17 -04:00
var sb = new StringBuilder ( ) ;
sb . Append ( "[" ) ;
sb . Append ( "{text: \"channels\", nodes: [" ) ;
var first = true ;
2024-05-26 19:54:51 -04:00
var topLevelChannels = _db . Channels . Where ( x = > x . ParentChannel = = null ) ;
foreach ( var topLevelChannel in topLevelChannels )
2024-05-26 19:43:17 -04:00
{
if ( first )
{
first = false ;
}
else
{
sb . Append ( ',' ) ;
}
serializeChannel ( ref sb , ref allChannels , ref allAccounts , topLevelChannel ) ;
}
2024-06-10 16:24:30 -04:00
sb . Append ( "]}" ) ;
2024-05-26 19:43:17 -04:00
if ( allChannels . Any ( ) )
{
2024-06-10 16:24:30 -04:00
sb . Append ( ",{text: \"orphaned channels\", nodes: [" ) ;
2024-05-26 19:43:17 -04:00
first = true ;
while ( true )
{
if ( first )
{
first = false ;
}
else
{
sb . Append ( ',' ) ;
}
serializeChannel ( ref sb , ref allChannels , ref allAccounts , allChannels . First ( ) ) ;
if ( ! allChannels . Any ( ) )
{
break ;
}
}
2024-06-10 16:24:30 -04:00
sb . Append ( "]}" ) ;
2024-05-26 19:43:17 -04:00
}
if ( allAccounts . Any ( ) )
{
2024-06-10 16:24:30 -04:00
sb . Append ( ",{text: \"channelless accounts\", nodes: [" ) ;
2024-05-26 19:43:17 -04:00
first = true ;
foreach ( var acc in allAccounts )
{
if ( first )
{
first = false ;
}
else
{
sb . Append ( ',' ) ;
}
serializeAccount ( ref sb , acc ) ;
}
sb . Append ( "]}" ) ;
}
2024-05-26 19:54:51 -04:00
var users = _db . Users . ToList ( ) ;
if ( users . Any ( ) )
{
2024-06-10 16:24:30 -04:00
sb . Append ( ",{text: \"users\", nodes: [" ) ;
2024-05-26 19:54:51 -04:00
first = true ;
//refresh list; we'll be knocking them out again in serializeUser
allAccounts = _db . Accounts . ToList ( ) ;
foreach ( var user in users )
{
if ( first )
{
first = false ;
}
else
{
sb . Append ( ',' ) ;
}
serializeUser ( ref sb , ref allAccounts , user ) ;
}
sb . Append ( "]}" ) ;
}
2024-05-26 19:43:17 -04:00
sb . Append ( "]" ) ;
ViewData . Add ( "treeString" , sb . ToString ( ) ) ;
return View ( "Index" ) ;
}
private void serializeChannel ( ref StringBuilder sb , ref List < Channel > allChannels , ref List < Account > allAccounts , Channel currentChannel )
{
allChannels . Remove ( currentChannel ) ;
2024-06-02 17:13:15 -04:00
//"but adam", you say, "there's an href attribute, why make a link?" because that makes the entire bar a link, and trying to expand the node will probably click the link
sb . Append ( $"{{\" text \ ": \"<a href=\\\"{Url.ActionLink(action: " Details ", controller: " Channels ", values: new {id = currentChannel.Id})}\\\">{currentChannel.DisplayName}</a>\"" ) ;
2024-06-10 16:24:30 -04:00
var theseAccounts = allAccounts . Where ( a = > a . SeenInChannel ? . Id = = currentChannel . Id ) . ToList ( ) ;
2024-05-26 19:43:17 -04:00
allAccounts . RemoveAll ( a = > a . SeenInChannel ? . Id = = currentChannel . Id ) ;
var first = true ;
if ( currentChannel . SubChannels ! = null | | theseAccounts ! = null )
{
sb . Append ( ", \"nodes\": [" ) ;
}
if ( currentChannel . SubChannels ! = null )
{
foreach ( var subChannel in currentChannel . SubChannels ? ? new List < Channel > ( ) )
{
if ( first )
{
first = false ;
}
else
{
sb . Append ( ',' ) ;
}
serializeChannel ( ref sb , ref allChannels , ref allAccounts , subChannel ) ;
}
if ( theseAccounts ! = null )
{
sb . Append ( ',' ) ;
}
}
if ( theseAccounts ! = null )
{
first = true ;
sb . Append ( $"{{\" text \ ": \"(accounts: {theseAccounts.Count()})\", \"expanded\":true, nodes:[" ) ;
foreach ( var account in theseAccounts )
{
if ( first )
{
first = false ;
}
else
{
sb . Append ( ',' ) ;
}
serializeAccount ( ref sb , account ) ;
}
sb . Append ( "]}" ) ;
}
sb . Append ( "]}" ) ;
}
private void serializeAccount ( ref StringBuilder sb , Account currentAccount )
{
sb . Append ( $"{{\" text \ ": \"{currentAccount.DisplayName}\"}}" ) ;
}
private void serializeUser ( ref StringBuilder sb , ref List < Account > allAccounts , User currentUser )
{
2024-06-10 16:24:30 -04:00
sb . Append ( $"{{\" text \ ": " +
$"\" < a href = \ \ \ "{Url.ActionLink(action: " Details ", controller: " Users ", values: new {id = currentUser.Id})}\\\">"
+ currentUser . DisplayName +
"</a>\", " ) ;
// \"{currentUser.DisplayName}\", ");
2024-05-26 19:43:17 -04:00
var ownedAccounts = allAccounts . Where ( a = > a . IsUser = = currentUser ) ;
sb . Append ( "nodes: [" ) ;
2024-05-26 19:54:51 -04:00
sb . Append ( $"{{\" text \ ": \"owned accounts:\", \"expanded\":true, \"nodes\": [" ) ;
2024-05-26 19:43:17 -04:00
if ( ownedAccounts ! = null )
{
foreach ( var acc in ownedAccounts )
{
serializeAccount ( ref sb , acc ) ;
sb . Append ( ',' ) ;
}
}
sb . Append ( "]}]}" ) ;
2023-07-03 12:51:23 -04:00
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error ( )
{
return View ( new ErrorPageViewModel { RequestId = Activity . Current ? . Id ? ? HttpContext . TraceIdentifier } ) ;
}
}