Example #1
0
        public async Task <IActionResult> Index()
        {
            DiscordUserInfo currentUser = null;

            try
            {
                currentUser = await GetAuthenticatedUserInfoAsync();
            }
            catch (HttpException httpEx) when(httpEx.HttpCode == HttpStatusCode.Unauthorized)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                return(Unauthorized("Something broke, your login may be unauthorized."));
            }
            catch (HttpException httpEx) when(httpEx.HttpCode == HttpStatusCode.NotFound)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                return(NotFound("Something wasn't found."));
            }
            catch (HttpException httpEx)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                throw;
            }

            if (currentUser == null)
            {
                logger.LogError("Current User is null, this indicates some issue with logging in.");
                return(Unauthorized("Unable to log in."));
            }
            if (!currentUser.InGuild || currentUser.IsMuted)
            {
                return(Unauthorized("You are either muted or not in this guild; so you cannot join any channels."));
            }
            var currentUserRoles = await GetRolesForUserAsync(currentUser.CurrentUser.Id);

            var model   = new RoleModel();
            var storage = GetStorageUtil();

            model.CurrentUser = currentUser;

            // get all channels
            var courses = await storage.GetCoursesAsync(GuildId);

            foreach (var course in courses)
            {
                if (currentUserRoles.Contains(course.RoleId))
                {
                    // user is in this course
                    model.LeaveableCourses.Add(course);
                }
                else
                {
                    model.JoinableCourses.Add(course);
                }
            }

            return(View(nameof(Index), model));
        }
Example #2
0
        public async Task <IActionResult> Join(string id)
        {
            DiscordUserInfo currentUser = null;

            try
            {
                currentUser = await GetAuthenticatedUserInfoAsync();
            }
            catch (HttpException httpEx) when(httpEx.HttpCode == HttpStatusCode.Unauthorized)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                return(Unauthorized("Something broke, your login may be unauthorized."));
            }
            catch (HttpException httpEx) when(httpEx.HttpCode == HttpStatusCode.NotFound)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                return(NotFound("Something wasn't found."));
            }
            catch (HttpException httpEx)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                throw;
            }

            if (currentUser == null)
            {
                logger.LogError("Current User is null, this indicates some issue with logging in.");
                return(Unauthorized("Unable to log in."));
            }
            if (!currentUser.InGuild || currentUser.IsMuted)
            {
                return(Unauthorized("You are either muted or not in this guild; so you cannot join any channels."));
            }

            var storage = GetStorageUtil();
            var course  = await storage.GetCourseAsync(GuildId, id);

            if (course == null)
            {
                return(NotFound($"Couldn't find a course with the id: {id}"));
            }

            var roleId = ulong.Parse(course.RoleId);

            await AddRoleForUserAsync(currentUser.CurrentUser.Id, roleId);

            return(Redirect("/Courses"));
        }
Example #3
0
        public async Task <IActionResult> Index()
        {
            DiscordUserInfo currentUser = null;

            try
            {
                currentUser = await GetAuthenticatedUserInfoAsync();
            }
            catch (HttpException httpEx) when(httpEx.HttpCode == HttpStatusCode.Unauthorized)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                return(Unauthorized("Something broke, your login may be unauthorized."));
            }
            catch (HttpException httpEx) when(httpEx.HttpCode == HttpStatusCode.NotFound)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                return(NotFound("Something wasn't found."));
            }
            catch (HttpException httpEx)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                throw;
            }

            if (currentUser == null)
            {
                logger.LogError("Current User is null, this indicates some issue with logging in.");
                return(Unauthorized("Unable to log in."));
            }

            if (!currentUser.InGuild)
            {
                return(Unauthorized("You are not a member of this server."));
            }
            if (!currentUser.IsAdmin)
            {
                return(Unauthorized("You must be an admin to view this page."));
            }

            var model   = new CourseChannelList();
            var storage = GetStorageUtil();

            var courses = await storage.GetCoursesAsync(GuildId);

            model.Courses = courses;

            return(View(nameof(Index), model));
        }
Example #4
0
        public async Task <IActionResult> CreateNew(string courseName)
        {
            DiscordUserInfo currentUser = null;

            try
            {
                currentUser = await GetAuthenticatedUserInfoAsync();
            }
            catch (HttpException httpEx) when(httpEx.HttpCode == HttpStatusCode.Unauthorized)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                return(Unauthorized("Something broke, your login may be unauthorized."));
            }
            catch (HttpException httpEx) when(httpEx.HttpCode == HttpStatusCode.NotFound)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                return(NotFound("Something wasn't found."));
            }
            catch (HttpException httpEx)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                throw;
            }

            if (currentUser == null)
            {
                logger.LogError("Current User is null, this indicates some issue with logging in.");
                return(Unauthorized("Unable to log in."));
            }
            if (!currentUser.InGuild)
            {
                return(Unauthorized("You are not a member of this server."));
            }
            if (!currentUser.IsAdmin)
            {
                return(Unauthorized("You must be an admin to view this page."));
            }

            var appClient = await GetAppClientAsync();

            var results = await CreateCourseChannelAsync(appClient, courseName,
                                                         $"Created by {currentUser.CurrentUser.Username}#{currentUser.CurrentUser.Discriminator} {currentUser.CurrentUser.Id} online for course {courseName}.");

            return(await CreateExisting(results.role.Id, results.channel.Id, courseName));
        }
        // this has to be unique per user
        public async Task <DiscordUserInfo> GetAuthenticatedUserInfoAsync()
        {
            var token = await HttpContext.GetTokenAsync("Discord", "access_token");

            if (token == null)
            {
                return(null);
            }

            using var client = new DiscordRestClient();
            await client.LoginAsync(TokenType.Bearer, token, true);

            var guildSummaries = await client.GetGuildSummariesAsync().FlattenAsync();

            var targetGuild = guildSummaries.FirstOrDefault(x => x.Id == GuildId);

            var result = new DiscordUserInfo()
            {
                Guild       = targetGuild,
                CurrentUser = client.CurrentUser,
            };

            return(result);
        }
Example #6
0
        public async Task <IActionResult> CreateExisting(ulong roleId, ulong channelId, string courseName)
        {
            DiscordUserInfo currentUser = null;

            try
            {
                currentUser = await GetAuthenticatedUserInfoAsync();
            }
            catch (HttpException httpEx) when(httpEx.HttpCode == HttpStatusCode.Unauthorized)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                return(Unauthorized("Something broke, your login may be unauthorized."));
            }
            catch (HttpException httpEx) when(httpEx.HttpCode == HttpStatusCode.NotFound)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                return(NotFound("Something wasn't found."));
            }
            catch (HttpException httpEx)
            {
                logger.LogError(httpEx, "Unhandled Discord HTTP Exception");
                throw;
            }

            if (currentUser == null)
            {
                logger.LogError("Current User is null, this indicates some issue with logging in.");
                return(Unauthorized("Unable to log in."));
            }
            if (!currentUser.InGuild)
            {
                return(Unauthorized("You are not a member of this server."));
            }
            if (!currentUser.IsAdmin)
            {
                return(Unauthorized("You must be an admin to view this page."));
            }

            var exists = await CheckExistsAndValidAsync(roleId, channelId);

            if (!exists)
            {
                return(BadRequest("Either the role or channel Id did not exist in this guild, or the role had permissions that could not be granted."));
            }

            courseName = NormalizeCourseChannelName(courseName);

            var courseEntity = new CourseEntity()
            {
                GuildId   = GuildId.ToString(),
                CourseId  = courseName,
                ChannelId = channelId.ToString(),
                RoleId    = roleId.ToString(),
            };

            var storage = GetStorageUtil();
            var table   = await storage.GetCourseTableAsync();

            var operation = TableOperation.InsertOrMerge(courseEntity);
            await table.ExecuteAsync(operation);

            // show the list
            return(await Index());
        }