Exemplo n.º 1
0
        private static async Task CreateChannel(HttpContext ctx, ValourDB db,
                                                [FromHeader] string authorization)
        {
            ServerPlanetChatChannel channel_data =
                await JsonSerializer.DeserializeAsync <ServerPlanetChatChannel>(ctx.Request.Body);

            if (channel_data == null)
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync("Please include channel in body");

                return;
            }

            if (string.IsNullOrWhiteSpace(channel_data.Name))
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync("Please include a channel name");

                return;
            }

            // Request parameter validation //

            TaskResult name_valid = ServerPlanetChatChannel.ValidateName(channel_data.Name);

            if (!name_valid.Success)
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync($"Name is not valid [name: {channel_data.Name}]");

                return;
            }

            // Request authorization //

            AuthToken auth = await ServerAuthToken.TryAuthorize(authorization, db);

            if (!auth.HasScope(UserPermissions.PlanetManagement))
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync("Token lacks UserPermissions.PlanetManagement scope");

                return;
            }

            ServerPlanet planet = await db.Planets.Include(x => x.Members.Where(x => x.User_Id == auth.User_Id))
                                  .FirstOrDefaultAsync(x => x.Id == channel_data.Planet_Id);

            var member = planet.Members.FirstOrDefault();

            if (!await planet.HasPermissionAsync(member, PlanetPermissions.ManageChannels, db))
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync("Member lacks PlanetPermissions.ManageChannels node");

                return;
            }

            // Ensure parent category exists

            ServerPlanetCategory parent = await db.PlanetCategories.FindAsync(channel_data.Parent_Id);

            if (parent == null)
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync("Could not find parent");

                return;
            }

            if (parent.Planet_Id != planet.Id)
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync("Parent id does not match planet");

                return;
            }

            // Request action //

            // Creates the channel

            ushort child_count = 0;

            child_count += (ushort)await db.PlanetChatChannels.CountAsync(x => x.Parent_Id == channel_data.Parent_Id);

            child_count += (ushort)await db.PlanetCategories.CountAsync(x => x.Parent_Id == channel_data.Parent_Id);

            ServerPlanetChatChannel channel = new ServerPlanetChatChannel()
            {
                Id            = IdManager.Generate(),
                Name          = channel_data.Name,
                Planet_Id     = channel_data.Planet_Id,
                Parent_Id     = channel_data.Parent_Id,
                Message_Count = 0,
                Description   = channel_data.Description,
                Position      = child_count
            };

            // Add channel to database
            await db.PlanetChatChannels.AddAsync(channel);

            // Save changes to DB
            await db.SaveChangesAsync();

            // Send channel refresh
            PlanetHub.NotifyChatChannelChange(channel);

            ctx.Response.StatusCode = 201;
            await ctx.Response.WriteAsync(channel.Id.ToString());
        }