Ejemplo n.º 1
0
        private static async Task Public(HttpContext ctx, ValourDB db, ulong planet_id,
                                         [FromHeader] string authorization)
        {
            AuthToken auth = await ServerAuthToken.TryAuthorize(authorization, db);

            if (auth == null)
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync($"Token is invalid [token: {authorization}]");

                return;
            }

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

            if (planet == null)
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync($"Planet not found [id: {planet_id}]");

                return;
            }

            ServerPlanetMember member = planet.Members.FirstOrDefault();

            if (member == null)
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync($"Member not found");

                return;
            }

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

                return;
            }

            switch (ctx.Request.Method)
            {
            case "GET":
            {
                ctx.Response.StatusCode = 200;
                await ctx.Response.WriteAsJsonAsync(planet.Public);

                return;
            }

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

                    return;
                }

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

                    return;
                }

                string body = await ctx.Request.ReadBodyStringAsync();

                bool parsed = false;
                parsed = bool.TryParse(body, out var in_public);

                if (!parsed)
                {
                    ctx.Response.StatusCode = 400;
                    await ctx.Response.WriteAsync("Failed to parse body");

                    return;
                }

                var result = await planet.TrySetPublicAsync(in_public, db);

                if (!result.Success)
                {
                    ctx.Response.StatusCode = 400;
                }
                else
                {
                    ctx.Response.StatusCode = 200;
                }

                await ctx.Response.WriteAsync(result.Message);

                return;
            }
            }
        }