2016-11-03 14:52:17 +01:00
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Microsoft.Data.Entity;
|
2019-05-24 20:17:24 +01:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using Microsoft.AspNet.Mvc;
|
|
|
|
|
using Microsoft.AspNet.Identity;
|
|
|
|
|
using Yavsc.Models;
|
|
|
|
|
using Yavsc.ViewModels.Chat;
|
|
|
|
|
|
2016-11-03 14:52:17 +01:00
|
|
|
|
|
|
|
|
namespace Yavsc.Controllers
|
|
|
|
|
{
|
2019-05-24 20:17:24 +01:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2016-11-03 14:52:17 +01:00
|
|
|
[Route("api/chat")]
|
|
|
|
|
public class ChatApiController : Controller
|
|
|
|
|
{
|
2017-01-13 16:31:40 +01:00
|
|
|
ApplicationDbContext dbContext;
|
|
|
|
|
UserManager<ApplicationUser> userManager;
|
|
|
|
|
public ChatApiController(ApplicationDbContext dbContext,
|
|
|
|
|
UserManager<ApplicationUser> userManager)
|
|
|
|
|
{
|
|
|
|
|
this.dbContext = dbContext;
|
|
|
|
|
this.userManager = userManager;
|
|
|
|
|
}
|
2016-11-03 14:52:17 +01:00
|
|
|
|
|
|
|
|
[HttpGet("users")]
|
2017-01-13 16:31:40 +01:00
|
|
|
public IEnumerable<ChatUserInfo> GetUserList()
|
2016-11-03 14:52:17 +01:00
|
|
|
{
|
2017-01-13 16:31:40 +01:00
|
|
|
List<ChatUserInfo> result = new List<ChatUserInfo>();
|
2019-05-24 20:17:24 +01:00
|
|
|
var cxsQuery = dbContext.ChatConnection?.Include(c => c.Owner)
|
|
|
|
|
.Where(cx => cx.Connected).GroupBy(c => c.ApplicationUserId);
|
|
|
|
|
|
|
|
|
|
if (cxsQuery != null)
|
|
|
|
|
foreach (var g in cxsQuery)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var uid = g.Key;
|
|
|
|
|
var cxs = g.ToList();
|
|
|
|
|
if (cxs != null)
|
|
|
|
|
if (cxs.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var user = cxs.First().Owner;
|
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
|
|
|
|
result.Add(new ChatUserInfo
|
|
|
|
|
{
|
|
|
|
|
UserName = user.UserName,
|
|
|
|
|
UserId = user.Id,
|
|
|
|
|
Avatar = user.Avatar,
|
|
|
|
|
Connections = cxs,
|
|
|
|
|
Roles = (userManager.GetRolesAsync(user)).Result.ToArray()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result.Add(new ChatUserInfo { Connections = cxs });
|
|
|
|
|
}
|
2017-01-13 16:31:40 +01:00
|
|
|
}
|
2019-05-24 20:17:24 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-13 16:31:40 +01:00
|
|
|
return result;
|
2016-11-03 14:52:17 +01:00
|
|
|
}
|
2019-05-08 03:13:07 +01:00
|
|
|
|
2019-05-24 20:17:24 +01:00
|
|
|
// GET: api/chat/userName
|
|
|
|
|
[HttpGet("{userName}", Name = "uinfo")]
|
|
|
|
|
public IActionResult GetUserInfo([FromRoute] string userName)
|
|
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
// Miguel mech profiler
|
|
|
|
|
{
|
|
|
|
|
return HttpBadRequest(ModelState);
|
|
|
|
|
}
|
|
|
|
|
var uid = User.GetUserId();
|
|
|
|
|
var user = dbContext.ApplicationUser.Include(u => u.Connections).FirstOrDefault(u => u.UserName == userName);
|
|
|
|
|
|
|
|
|
|
if (user == null) return HttpNotFound();
|
|
|
|
|
|
|
|
|
|
return Ok(new ChatUserInfo
|
|
|
|
|
{
|
|
|
|
|
UserName = user.UserName,
|
|
|
|
|
UserId = user.Id,
|
|
|
|
|
Avatar = user.Avatar,
|
|
|
|
|
Connections = user.Connections,
|
|
|
|
|
Roles = (userManager.GetRolesAsync(user)).Result.ToArray()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 14:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|