private async Task NotifyOwnerAsync(Character character, RaidTeam team, bool approved, string?message)
    {
        if (character.OwnerId > 0)
        {
            var sb = new StringBuilder("Your application to ")
                     .Append(team.Name)
                     .Append(" for ")
                     .Append(character.Name)
                     .Append(" was ")
                     .Append(approved ? "approved!" : "rejected.");

            if (!string.IsNullOrWhiteSpace(message))
            {
                sb.AppendLine().Append("<@").Append(User.GetDiscordId()).Append("> said:");

                foreach (var line in message.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    sb.AppendLine().Append("> ").Append(line);
                }
            }

            if (approved)
            {
                await _discordClientProvider.AddRoleAsync(character.OwnerId.Value, team.Name, "Accepted onto the raid team.");
            }

            await _discordClientProvider.SendDmAsync(character.OwnerId.Value, sb.ToString());
        }
    }
Esempio n. 2
0
    public async Task <ActionResult <TeamDto> > Post([FromBody] TeamSubmissionDto dto, [FromServices] IdGen.IIdGenerator <long> idGenerator)
    {
        if (!ModelState.IsValid)
        {
            return(ValidationProblem());
        }

        Debug.Assert(dto.Name?.Length > 1);

        if (dto.Schedules.Count == 0)
        {
            ModelState.AddModelError(nameof(dto.Schedules), "At least one raid day schedule must be entered.");
        }

        var team = new RaidTeam(idGenerator.CreateId())
        {
            Name     = dto.Name,
            Inactive = dto.Inactive
        };

        foreach (var scheduleDto in dto.Schedules)
        {
            Debug.Assert(scheduleDto.Day.HasValue);
            Debug.Assert(scheduleDto.StartTime.HasValue);
            Debug.Assert(scheduleDto.Duration.HasValue);

            team.Schedules.Add(new RaidTeamSchedule(idGenerator.CreateId())
            {
                Day            = scheduleDto.Day.Value,
                Duration       = TimeSpan.FromHours(scheduleDto.Duration.Value),
                RealmTimeStart = scheduleDto.StartTime.Value,
                RaidTeam       = team
            });
        }

        _context.RaidTeams.Add(team);

        await _context.SaveChangesAsync();

        _telemetry.TrackEvent("TeamAdded", User, props =>
        {
            props["TeamId"]   = team.Id.ToString();
            props["TeamName"] = team.Name;
        });

        return(CreatedAtAction(nameof(Get), new { id = team.Id }, new TeamDto
        {
            Id = team.Id,
            Name = team.Name,
            Inactive = team.Inactive,
            Schedules = team.Schedules.Select(s => new ScheduleDto
            {
                Day = s.Day,
                RealmTimeStart = s.RealmTimeStart,
                Duration = s.Duration
            }).ToList()
        }));
    }
    private async Task <MemberDto?> TryGetMemberDtoAsync(Character character, RaidTeam team)
    {
        var scope = await _context.GetCurrentPriorityScopeAsync();

        var members = await HelperQueries.GetMembersAsync(
            _context,
            _serverTimeZoneInfo,
            _context.Characters.AsNoTracking().Where(c => c.Id == character.Id),
            scope,
            team.Id,
            team.Name,
            isLeader : true);

        return(members.Count == 1 ? members[0] : null);
    }
    protected override async ValueTask HandleRequirementAsync(AuthorizationHandlerContext context, TeamLeaderRequirement requirement, DiscordMember member)
    {
        if (requirement.AllowAdmin && DiscordClientProvider.HasAdminRole(member))
        {
            context.Succeed(requirement);
            return;
        }

        if ((requirement.AllowRaidLeader && DiscordClientProvider.HasRaidLeaderRole(member)) ||
            (requirement.AllowLootMaster && DiscordClientProvider.HasLootMasterRole(member)) ||
            (requirement.AllowRecruiter && DiscordClientProvider.HasRecruiterRole(member)))
        {
            long?teamId = context.Resource switch
            {
                long id => id,
                TeamDto team => team.Id,
                RaidTeam team => team.Id,
                _ => null
            };

            if (teamId.HasValue)
            {
                var discordId = (long)member.Id;

                if (await _context.RaidTeamLeaders.AsNoTracking().CountAsync(rtl => rtl.UserId == discordId && rtl.RaidTeamId == teamId) > 0)
                {
                    context.Succeed(requirement);
                }
            }
            else
            {
                context.Succeed(requirement);
            }
        }
    }
}
    private async Task <bool> AuthorizeTeamAsync(RaidTeam team, string policy)
    {
        var auth = await _authorizationService.AuthorizeAsync(User, team, policy);

        return(auth.Succeeded);
    }