using Dpz.Core.Web.Library.Hub;
using Dpz.Core.Web.Models;
using Microsoft.AspNetCore.SignalR;
using Dpz.Core.EnumLibrary;
using Dpz.Core.Public.ViewModel.V4;
using Dpz.Core.Service.ObjectStorage.Services;
using MongoDB.Bson;
using IMumbleService = Dpz.Core.Service.V4.Services.IMumbleService;
namespace Dpz.Core.Web.Controllers;
public class TalkController(
IMumbleService mumbleService,
IHubContext<Notification> hubContext,
IObjectStorageOperation objectStorageService)
: Controller
{
public async Task<IActionResult> Index(int pageIndex = 1, int pageSize = 10)
{
this.SetTitle("日常碎碎念");
ViewBag.Menu = MenuItem.Mumble;
var list = await mumbleService.GetPagesAsync(pageIndex, pageSize);
if (Request.Headers["X-PJAX"] == "true" && Request.Query["_pjax"] == ".mumble-list")
return View("_TalkPagePartial", list);
return View(list);
}
[CheckAuthorize]
[HttpPost]
public async Task<IActionResult> Publish(string markdown, string html, string id = "")
{
var viewModel = new VmMumble
{
Id = id,
Author = this.GetIdentity(),
CreateTime = DateTime.Now,
HtmlContent = html,
LastUpdateTime = DateTime.Now,
Markdown = markdown
};
if (!string.IsNullOrEmpty(id))
{
var userInfo = User.GetStrictIdentity();
var talk = await mumbleService.FindAsync(id);
if (userInfo.Id != talk?.Author.Id)
{
return Json(new ResultInfo("没有权限修改碎碎念!"));
}
}
if (string.IsNullOrEmpty(id))
{
await mumbleService.CreateAsync(viewModel);
await hubContext.Clients.All.SendCoreAsync("pushMessage", [viewModel]);
}
else
await mumbleService.EditContentAsync(viewModel);
return Json(new ResultInfo(true));
}
//[CheckAuthorize]
[HttpPost]
public async Task<IActionResult> Like(string id)
{
var talk = await mumbleService.LikeAsync(id);
return Json(new ResultInfo(talk?.Like));
}
[CheckAuthorize]
[HttpPost]
public async Task<IActionResult> Upload()
{
var file = Request.Form.Files.FirstOrDefault();
if (file is { Length: > 0 } && file.ContentType.Contains("image"))
{
//var userInfo = User.GetIdentity();
var path = new List<string> { "images", "mumble", DateTime.Now.ToString("yyyy-MM-dd") };
#if DEBUG
path.Insert(0,"Test");
#endif
var fileName = ObjectId.GenerateNewId() + file.FileName[file.FileName.LastIndexOf('.')..];
var result = await objectStorageService.UploadAsync(file.OpenReadStream(), path, fileName);
return Json(new { success = 1, message = "", url = result.AccessUrl });
}
return Json(new { success = 0, message = "请选择一张图片!", url = "" });
}
[CheckAuthorize, HttpGet]
public async Task<IActionResult> Publish(string id = "")
{
VmMumble? model = null;
if (!string.IsNullOrEmpty(id))
{
model = await mumbleService.FindAsync(id);
}
return View(model);
}
[HttpGet]
public async Task<IActionResult> Preview(string id = "")
{
if (string.IsNullOrEmpty(id))
{
return NotFound();
}
var talk = await mumbleService.FindAsync(id);
if (talk == null) return NotFound();
return View(talk);
}
[HttpPost, CheckAuthorize]
public async Task<IActionResult> Delete(string id)
{
var userInfo = User.GetStrictIdentity();
var talk = await mumbleService.FindAsync(id);
if (talk == null || talk.Author.Id != userInfo.Id)
{
return Json(new ResultInfo("碎碎念删除失败,没有权限删除或者不存在!"));
}
await mumbleService.DeleteAsync(id);
return Json(new ResultInfo(true));
}
[HttpGet]
public async Task<IActionResult> Comment(string id = "")
{
var talk = await mumbleService.FindAsync(id);
if (talk == null) return NotFound();
this.SetTitle("碎碎念回复");
ViewBag.Menu = MenuItem.Mumble;
return View(talk);
}
#region memble mobile
[HttpGet,CheckAuthorize]
public async Task<IActionResult> MyTalk(int pageIndex = 1, int pageSize = 10, string content = "")
{
var userInfo = User.GetStrictIdentity();
var source = await mumbleService.GetPagesAsync(pageIndex, pageSize, content, userInfo.Id);
var data = Pagination<VmMumble>.Create(source);
return Json(new ResultInfo(data));
}
[HttpGet]
public async Task<IActionResult> Detail(string id = "")
{
if (string.IsNullOrEmpty(id))
{
return Json(new ResultInfo("缺失参数"));
}
var mumble = await mumbleService.FindAsync(id);
if (mumble == null)
{
return Json(new ResultInfo("碎碎念不存在"));
}
return Json(new ResultInfo(mumble));
}
#endregion
}