Skip to content

Commit

Permalink
pandora modernization
Browse files Browse the repository at this point in the history
  • Loading branch information
sei-dupdyke committed Dec 18, 2023
1 parent 3fe3172 commit ee61597
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 138 deletions.
51 changes: 49 additions & 2 deletions src/ghosts.pandora.socializer/src/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using System.Net.Mime;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Hosting.Internal;
using Socializer.Hubs;
using Socializer.Infrastructure;

Expand All @@ -11,12 +13,14 @@ public class HomeController : Controller
private readonly ILogger<HomeController> _logger;
private readonly IHubContext<PostsHub> _hubContext;
private readonly DataContext _db;
private readonly IWebHostEnvironment _env;

public HomeController(ILogger<HomeController> logger, IHubContext<PostsHub> hubContext, DataContext dbContext)
public HomeController(ILogger<HomeController> logger, IHubContext<PostsHub> hubContext, DataContext dbContext, IWebHostEnvironment env)
{
_logger = logger;
_hubContext = hubContext;
_db = dbContext;
_env = env;
}

[HttpGet]
Expand All @@ -43,6 +47,49 @@ public IActionResult Index()
return View("Index", posts);
}

[HttpGet("/u/{userId}/avatar")]
public async Task<IActionResult> GetUserAvatar(string userId)
{
_logger.LogTrace("{RequestScheme}://{RequestHost}{RequestPath}{RequestQueryString}|{RequestMethod}|", Request.Scheme, Request.Host, Request.Path, Request.QueryString, Request.Method);

if (string.IsNullOrEmpty(userId))
{
return PhysicalFile(Path.Combine(_env.WebRootPath,"img", "avatar1.webp"), "image/webp");
}

var imageDir = Path.Combine(_env.WebRootPath, "images", "u", userId);
var imagePath = Path.Combine(imageDir, "avatar.webp");

if (!System.IO.File.Exists(imagePath))
{
Directory.CreateDirectory(imageDir);

var rnd = new Random();
var number = rnd.Next(1, 85);

var sourceImagePath = Path.Combine(_env.WebRootPath, "img", $"avatar{number}-sm.webp");
if (System.IO.File.Exists(sourceImagePath))
{
try
{
System.IO.File.Copy(sourceImagePath, imagePath);
}
catch (IOException ex)
{
_logger.LogError(ex, "Error copying file.");
return StatusCode(500, "Internal Server Error");
}
}
else
{
_logger.LogWarning($"Source avatar image not found: {sourceImagePath}");
return NotFound("Avatar image not available.");
}
}

return PhysicalFile(imagePath, "image/webp");
}

[HttpGet("/{id:guid}")]
public IActionResult Detail(Guid id)
{
Expand Down
16 changes: 1 addition & 15 deletions src/ghosts.pandora.socializer/src/Views/Home/Detail.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,6 @@

<div class="row row-cols-1 row-cols-md-12" id="itemList">
@{
var post = Model;
<div class="col" id="@post.Id">
<div class="card h-100">
<div class="card-body">
<p class="card-text">
@Html.Raw(post.Message)
</p>
<p class="card-text">
<a href="/u/@post.User" class="user">@post.User</a>
<br/>
<span class="created">@post.CreatedUtc</span>
</p>
</div>
</div>
</div>
await Html.RenderPartialAsync("_Post", Model);
}
</div>
117 changes: 1 addition & 116 deletions src/ghosts.pandora.socializer/src/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,128 +1,13 @@
@{
ViewData["Title"] = "Home Page";

var rnd = new Random();

Socializer.Infrastructure.User GetRandomUser()
{
var u = new Socializer.Infrastructure.User();
var number = rnd.Next(1, 85);
u.Avatar = $"/img/avatar{number}-sm.webp";
u.Name = Faker.Internet.UserName();
if(rnd.Next(1, 3) > 1)
u.Name = Faker.Name.FullName();
return u;
}

string GetAvatar()
{
var number = rnd.Next(1, 85);
return $"/img/avatar{number}-sm.webp";
}
}
@model List<Socializer.Infrastructure.Post>

<div id="newsfeed-items-grid">
@{
foreach (var post in Model)
{
<div class="ui-block">
<article class="hentry post">
<div class="post__author author vcard inline-items">
<img loading="lazy" src="@GetAvatar()" alt="author" width="42" height="42">
<div class="author-date">
<a class="h6 post__author-name fn" href="/u/@post.User">@post.User</a> shared a <a href="@post.Id">link</a>
<div class="post__date">
<time class="published" datetime="@post.CreatedUtc">
@post.CreatedUtc
</time>
</div>
</div>
<div class="more">
<svg class="olymp-three-dots-icon">
<use xlink:href="#olymp-three-dots-icon"></use>
</svg>
<ul class="more-dropdown">
<li>
<a href="#">Edit Post</a>
</li>
<li>
<a href="#">Delete Post</a>
</li>
<li>
<a href="#">Turn Off Notifications</a>
</li>
<li>
<a href="#">Select as Featured</a>
</li>
</ul>
</div>
</div>

@Html.Raw(post.Message)

<div class="post-additional-info inline-items">
<a href="#" class="post-add-icon inline-items">
<svg class="olymp-heart-icon">
<use xlink:href="#olymp-heart-icon"></use>
</svg>
<span>18</span>
</a>
<ul class="friends-harmonic">
@{
for (var i = 1; i < rnd.Next(2, 16); i++)
{
var img = $"/img/friend-harmonic{i}.webp";
<li>
<a href="#">
<img loading="lazy" src="@img" alt="friend" width="28" height="28">
</a>
</li>
}
}
</ul>
<div class="names-people-likes">
@{
var u = GetRandomUser();
<span><a href="/u/@u.Name">@u.Name</a> and <br/>@rnd.Next(1,900) more liked this</span>
}
</div>
<div class="comments-shared">
<a href="#" class="post-add-icon inline-items">
<svg class="olymp-speech-balloon-icon">
<use xlink:href="#olymp-speech-balloon-icon"></use>
</svg>

<span>0</span>
</a>
<a href="#" class="post-add-icon inline-items">
<svg class="olymp-share-icon">
<use xlink:href="#olymp-share-icon"></use>
</svg>

<span>16</span>
</a>
</div>
</div>
<div class="control-block-button post-control-button">
<a href="#" class="btn btn-control">
<svg class="olymp-like-post-icon">
<use xlink:href="#olymp-like-post-icon"></use>
</svg>
</a>
<a href="#" class="btn btn-control">
<svg class="olymp-comments-post-icon">
<use xlink:href="#olymp-comments-post-icon"></use>
</svg>
</a>
<a href="#" class="btn btn-control">
<svg class="olymp-share-icon">
<use xlink:href="#olymp-share-icon"></use>
</svg>
</a>
</div>
</article>
</div>
await Html.RenderPartialAsync("_Post", post);
}
}
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/ghosts.pandora.socializer/src/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
{
var u = new Socializer.Infrastructure.User();
var number = rnd.Next(1, 85);
u.Avatar = $"/img/avatar{number}-sm.webp";
u.Name = Faker.Internet.UserName();
u.Avatar = $"/u/{u.Name}/avatar";
if(rnd.Next(1, 3) > 1)
u.Name = Faker.Name.FullName();
u.Status = statusArray[rnd.Next(statusArray.Length)];
Expand Down Expand Up @@ -805,14 +805,14 @@
<script src="/js/main.js"></script>
<script src="/js/libs-init/libs-init.js"></script>

<script src="Bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="/Bootstrap/dist/js/bootstrap.bundle.min.js"></script>

<!-- SVG icons loader -->
<script src="/js/svg-loader.js"></script>
<!-- /SVG icons loader -->

<script src="~/js/signalr.min.js"></script>
<script src="~/js/posts.js" asp-append-version="true"></script>
<script src="~/js/posts.js[email protected]()" asp-append-version="true"></script>

@await RenderSectionAsync("Scripts", required: false)
</body>
Expand Down
116 changes: 116 additions & 0 deletions src/ghosts.pandora.socializer/src/Views/Shared/_Post.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
@model Socializer.Infrastructure.Post

@{
var rnd = new Random();

Socializer.Infrastructure.User GetRandomUser()
{
var u = new Socializer.Infrastructure.User();
var number = rnd.Next(1, 85);
u.Name = Faker.Internet.UserName();
u.Avatar = $"/u/{u.Name}/avatar";
if (rnd.Next(1, 3) > 1)
u.Name = Faker.Name.FullName();
return u;
}

var post = Model;

<div class="ui-block">
<article class="hentry post">
<div class="post__author author vcard inline-items">
<img loading="lazy" src="/u/@post.User/avatar" alt="author" width="42" height="42">
<div class="author-date">
<a class="h6 post__author-name fn" href="/u/@post.User">@post.User</a> shared a <a href="/@post.Id">link</a>
<div class="post__date">
<time class="published" datetime="@post.CreatedUtc">
@post.CreatedUtc
</time>
</div>
</div>
<div class="more">
<svg class="olymp-three-dots-icon">
<use xlink:href="#olymp-three-dots-icon"></use>
</svg>
<ul class="more-dropdown">
<li>
<a href="#">Edit Post</a>
</li>
<li>
<a href="#">Delete Post</a>
</li>
<li>
<a href="#">Turn Off Notifications</a>
</li>
<li>
<a href="#">Select as Featured</a>
</li>
</ul>
</div>
</div>

@Html.Raw(post.Message)

<div class="post-additional-info inline-items">
<a href="#" class="post-add-icon inline-items">
<svg class="olymp-heart-icon">
<use xlink:href="#olymp-heart-icon"></use>
</svg>
<span>18</span>
</a>
<ul class="friends-harmonic">
@{
for (var i = 1; i < rnd.Next(2, 16); i++)
{
var img = $"/img/friend-harmonic{i}.webp";
<li>
<a href="#">
<img loading="lazy" src="@img" alt="friend" width="28" height="28">
</a>
</li>
}
}
</ul>
<div class="names-people-likes">
@{
var u = GetRandomUser();
<span><a href="/u/@u.Name">@u.Name</a> and <br/>@rnd.Next(1, 900) more liked this</span>
}
</div>
<div class="comments-shared">
<a href="#" class="post-add-icon inline-items">
<svg class="olymp-speech-balloon-icon">
<use xlink:href="#olymp-speech-balloon-icon"></use>
</svg>

<span>0</span>
</a>
<a href="#" class="post-add-icon inline-items">
<svg class="olymp-share-icon">
<use xlink:href="#olymp-share-icon"></use>
</svg>

<span>16</span>
</a>
</div>
</div>
<div class="control-block-button post-control-button">
<a href="#" class="btn btn-control">
<svg class="olymp-like-post-icon">
<use xlink:href="#olymp-like-post-icon"></use>
</svg>
</a>
<a href="#" class="btn btn-control">
<svg class="olymp-comments-post-icon">
<use xlink:href="#olymp-comments-post-icon"></use>
</svg>
</a>
<a href="#" class="btn btn-control">
<svg class="olymp-share-icon">
<use xlink:href="#olymp-share-icon"></use>
</svg>
</a>
</div>
</article>
</div>
}
4 changes: 4 additions & 0 deletions src/ghosts.pandora.socializer/src/ghosts-socializer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\images\u\" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/ghosts.pandora.socializer/src/wwwroot/js/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ document.getElementById("sendButton").disabled;

connection.on("SendMessage", function (id, user, message, created) {
let d = document.createElement("div");
document.getElementById("itemList").prepend(d);
d.innerHTML = `<div class="col" id="${id}"><div class="card h-100"><div class="card-body"><p class="card-text">${message}</p><p class="card-text"><small class="text-muted">${user}</small></p><p class="created">${created}</p></div></div></div>`;
document.getElementById("newsfeed-items-grid").prepend(d);
d.innerHTML = `<div class="ui-block"><article class="hentry post"><div class="post__author author vcard inline-items"><img loading="lazy" src="/u/${user}/avatar" alt="author" width="42" height="42"><div class="author-date"><a class="h6 post__author-name fn" href="/u/${user}">${user}</a> shared a <a href="/${id}">link</a><div class="post__date"><time class="published" datetime="${created}">${created}</time></div></div></div>${message}</article></div>`;
});

connection.start().then(function () {
Expand Down

0 comments on commit ee61597

Please sign in to comment.