网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using Dpz.Core.Web.Library.Api;
using Dpz.Core.Web.Models;
using Microsoft.AspNetCore.Authorization;

namespace Dpz.Core.Web.Library.Hub;

public class ChatHub(
    IChatRecordService chatRecordService,
    IAccountService accountService,
    IChatCurrentUserService chatCurrentUserService
) : Microsoft.AspNetCore.SignalR.Hub
{
    [Authorize]
    public async Task SendMessageToUser(string user, string message)
    {
        if (Context.User == null)
        {
            return;
        }
        var sender = Context.User.GetIdentity();
        if (sender == null)
        {
            return;
        }
        await Clients
            .User(user)
            .SendCoreAsync(
                "ReceiveMessage",
                [ReceiveContent.ToReceiveContent(Context.User, message)]
            );

        #region save record

        var receiver = await accountService.GetOneUserAsync(user);
        await chatRecordService.SaveFriendRecordAsync(sender, message, receiver);
        #endregion
    }

    [Authorize]
    public async Task SendMessageToGroup(string groupId, string message)
    {
        if (Context.User == null)
        {
            return;
        }
        var sender = Context.User.GetIdentity();
        if (sender == null)
        {
            return;
        }
        var userInfo = Context.User.GetStrictIdentity();
        List<VmUserInfo> users;
        if (groupId == WebToolsExtensions.DefaultGroupId)
        {
            var list = await accountService.GetPageAsync(1, 100, userInfo.Id);
            users = [..list];
        }
        else
        {
            users = new List<VmUserInfo>();
        }
        await Clients
            .Users(users.Select(x => x.Id).ToList())
            .SendCoreAsync(
                "ReceiveMessage",
                [ReceiveContent.ToGroupContent(groupId, userInfo, message)]
            );

        #region save record

        await chatRecordService.SaveGroupRecordAsync(sender, groupId, message);
        #endregion
    }

    public override async Task OnConnectedAsync()
    {
        if (Context.User == null || !Context.User.IsLogin())
        {
            await Clients.Caller.SendCoreAsync(
                "System",
                [
                    new
                    {
                        code = -1,
                        system = true,
                        id = "00000",
                        content = "没有登录"
                    }
                ]
            );
            return;
        }

        var currentUser = Context.User.GetIdentity();
        if (currentUser == null)
        {
            return;
        }
        await chatCurrentUserService.AddAsync(Context.ConnectionId, currentUser);
        await Clients.Others.SendCoreAsync(
            "System",
            [
                new
                {
                    code = 1,
                    system = true,
                    isGuest = false,
                    user = currentUser
                }
            ]
        );
        await base.OnConnectedAsync();
    }

    public override async Task OnDisconnectedAsync(Exception? exception)
    {
        if (Context.User.IsLogin())
        {
            await chatCurrentUserService.RemoveAsync(Context.ConnectionId);
        }
        await Clients.Others.SendCoreAsync(
            "System",
            [
                new
                {
                    code = 0,
                    system = true,
                    isGuest = Context.User?.Identity?.IsAuthenticated != true,
                    user = Context.User?.Identity?.IsAuthenticated == true
                        ? Context.User.GetIdentity()
                        : null
                }
            ]
        );
        await base.OnDisconnectedAsync(exception);
    }
}

//public class DomainRestrictedRequirement :
//    AuthorizationHandler<DomainRestrictedRequirement, HubInvocationContext>,
//    IAuthorizationRequirement
//{
//    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
//        DomainRestrictedRequirement requirement,
//        HubInvocationContext resource)
//    {
//        if (IsUserAllowedToDoThis(resource.HubMethodName, context.User.Identity.Name) &&
//            context.User.Identity.Name.EndsWith("@microsoft.com"))
//        {
//            context.Succeed(requirement);
//        }
//        return Task.CompletedTask;
//    }

//    private bool IsUserAllowedToDoThis(string hubMethodName,
//        string currentUsername)
//    {
//        return !(currentUsername.Equals("asdf42@microsoft.com") &&
//                 hubMethodName.Equals("banUser", StringComparison.OrdinalIgnoreCase));
//    }
//}
loading