Exemplo n.º 1
0
        /// <summary>
        /// Returns the planet membership of a user
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <TaskResult <List <Planet> > > GetPlanetMembership(ulong user_id, string token)
        {
            if (token == null)
            {
                return(new TaskResult <List <Planet> >(false, "Please supply an authentication token", null));
            }

            AuthToken authToken = await Context.AuthTokens.FindAsync(token);

            if (!Permission.HasPermission(authToken.Scope, UserPermissions.Membership))
            {
                return(new TaskResult <List <Planet> >(false, $"The given token does not have membership scope", null));
            }

            User user = await Context.Users.FindAsync(user_id);

            if (user == null)
            {
                return(new TaskResult <List <Planet> >(false, $"Could not find user {user_id}", null));
            }

            List <Planet> membership = new List <Planet>();

            ServerPlanet valourServer = await ServerPlanet.FindAsync(735703679107072);

            if (valourServer != null)
            {
                // Remove this after pre-pre-alpha
                if (!(await valourServer.IsMemberAsync(user_id, Context)))
                {
                    await valourServer.AddMemberAsync(user, Context);
                }
            }

            foreach (PlanetMember member in Context.PlanetMembers.Where(x => x.User_Id == user_id))
            {
                Planet planet = await Context.Planets.FindAsync(member.Planet_Id);

                if (planet != null)
                {
                    membership.Add(planet);
                }
            }

            return(new TaskResult <List <Planet> >(true, $"Retrieved {membership.Count} planets", membership));
        }
Exemplo n.º 2
0
        public async Task <TaskResult> Join(string code, string token)
        {
            AuthToken authToken = await Context.AuthTokens.FindAsync(token);

            if (authToken == null)
            {
                return(new TaskResult(false, $"Unable to authorize"));
            }

            ulong user_id = authToken.User_Id;

            PlanetInvite invite = await Context.PlanetInvites.FirstOrDefaultAsync(x => x.Code == code);

            if (invite == null)
            {
                return(new TaskResult(false, $"Invite code not found!"));
            }

            if (await Context.PlanetBans.AnyAsync(x => x.User_Id == user_id && x.Planet_Id == invite.Planet_Id))
            {
                return(new TaskResult(false, $"User is banned from this planet!"));
            }

            if (await Context.PlanetMembers.AnyAsync(x => x.User_Id == user_id && x.Planet_Id == invite.Planet_Id))
            {
                return(new TaskResult(false, $"User is already in this planet!"));
            }

            ServerPlanet planet = await ServerPlanet.FindAsync(invite.Planet_Id);

            if (!planet.Public)
            {
                return(new TaskResult(false, $"Planet is set to private!"));
            }

            User user = await Context.Users.FindAsync(user_id);

            await planet.AddMemberAsync(user, Context);

            return(new TaskResult(true, $"Successfully joined planet."));
        }
Exemplo n.º 3
0
        private static async Task Create(HttpContext ctx, ValourDB db,
                                         [FromHeader] string authorization, [Required] string name,
                                         [Required] string image_url)
        {
            ServerAuthToken auth = await ServerAuthToken.TryAuthorize(authorization, db);

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

                return;
            }

            TaskResult nameValid = ServerPlanet.ValidateName(name);

            if (!nameValid.Success)
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync(nameValid.Message);

                return;
            }

            ServerUser user = await db.Users.FindAsync(auth.User_Id);

            if (!user.Valour_Staff)
            {
                var owned_planets = await db.Planets.CountAsync(x => x.Owner_Id == user.Id);

                if (owned_planets > MAX_OWNED_PLANETS)
                {
                    ctx.Response.StatusCode = 400;
                    await ctx.Response.WriteAsync("Max owned planets reached");

                    return;
                }
            }

            // Image handling via proxy
            ProxyResponse proxyResponse = await MPSManager.GetProxy(image_url);

            bool is_media = MPSManager.Media_Types.Contains(proxyResponse.Item.Mime_Type);

            if (proxyResponse.Item == null || !is_media)
            {
                image_url = "https://valour.gg/image.png";
            }
            else
            {
                image_url = proxyResponse.Item.Url;
            }

            ulong planet_id = IdManager.Generate();

            // Create general category
            ServerPlanetCategory category = new ServerPlanetCategory()
            {
                Id          = IdManager.Generate(),
                Name        = "General",
                Parent_Id   = null,
                Planet_Id   = planet_id,
                Description = "General category",
                Position    = 0
            };

            // Create general channel
            ServerPlanetChatChannel channel = new ServerPlanetChatChannel()
            {
                Id            = IdManager.Generate(),
                Planet_Id     = planet_id,
                Name          = "General",
                Message_Count = 0,
                Description   = "General chat channel",
                Parent_Id     = category.Id
            };

            // Create default role
            ServerPlanetRole defaultRole = new ServerPlanetRole()
            {
                Id          = IdManager.Generate(),
                Planet_Id   = planet_id,
                Position    = uint.MaxValue,
                Color_Blue  = 255,
                Color_Green = 255,
                Color_Red   = 255,
                Name        = "@everyone"
            };

            ServerPlanet planet = new ServerPlanet()
            {
                Id              = planet_id,
                Name            = name,
                Member_Count    = 1,
                Description     = "A Valour server.",
                Image_Url       = image_url,
                Public          = true,
                Owner_Id        = user.Id,
                Default_Role_Id = defaultRole.Id,
                Main_Channel_Id = channel.Id
            };

            // Add planet to database
            await db.Planets.AddAsync(planet);

            await db.SaveChangesAsync(); // We must do this first to prevent foreign key errors

            // Add category to database
            await db.PlanetCategories.AddAsync(category);

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

            // Add default role to database
            await db.PlanetRoles.AddAsync(defaultRole);

            // Save changes
            await db.SaveChangesAsync();

            // Add owner to planet
            await planet.AddMemberAsync(user, db);

            ctx.Response.StatusCode = 200;
            await ctx.Response.WriteAsJsonAsync(planet.Id);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a server and if successful returns a task result with the created
        /// planet's id
        /// </summary>
        public async Task <TaskResult <ulong> > CreatePlanet(string name, string image_url, string token)
        {
            TaskResult nameValid = ValidateName(name);

            if (!nameValid.Success)
            {
                return(new TaskResult <ulong>(false, nameValid.Message, 0));
            }

            AuthToken authToken = await Context.AuthTokens.FindAsync(token);

            if (authToken == null)
            {
                return(new TaskResult <ulong>(false, "Failed to authorize user.", 0));
            }

            User user = await Context.Users.FindAsync(authToken.User_Id);

            if (await Context.Planets.CountAsync(x => x.Owner_Id == user.Id) > MAX_OWNED_PLANETS - 1)
            {
                return(new TaskResult <ulong>(false, "You have hit your maximum planets!", 0));
            }

            // User is verified and given planet info is valid by this point
            // We don't actually need the user object which is cool

            // Use MSP for proxying image

            MSPResponse proxyResponse = await MSPManager.GetProxy(image_url);

            if (string.IsNullOrWhiteSpace(proxyResponse.Url) || !proxyResponse.Is_Media)
            {
                image_url = "https://valour.gg/image.png";
            }
            else
            {
                image_url = proxyResponse.Url;
            }

            ulong planet_id = IdManager.Generate();

            // Create general category
            PlanetCategory category = new PlanetCategory()
            {
                Id        = IdManager.Generate(),
                Name      = "General",
                Parent_Id = null,
                Planet_Id = planet_id,
                Position  = 0
            };

            // Create general channel
            PlanetChatChannel channel = new PlanetChatChannel()
            {
                Id            = IdManager.Generate(),
                Planet_Id     = planet_id,
                Name          = "General",
                Message_Count = 0,
                Description   = "General chat channel",
                Parent_Id     = category.Id
            };

            // Create default role
            ServerPlanetRole defaultRole = new ServerPlanetRole()
            {
                Id          = IdManager.Generate(),
                Planet_Id   = planet_id,
                Position    = uint.MaxValue,
                Color_Blue  = 255,
                Color_Green = 255,
                Color_Red   = 255,
                Name        = "@everyone"
            };

            ServerPlanet planet = new ServerPlanet()
            {
                Id              = planet_id,
                Name            = name,
                Member_Count    = 1,
                Description     = "A Valour server.",
                Image_Url       = image_url,
                Public          = true,
                Owner_Id        = user.Id,
                Default_Role_Id = defaultRole.Id,
                Main_Channel_Id = channel.Id
            };

            // Add planet to database
            await Context.Planets.AddAsync(planet);

            await Context.SaveChangesAsync(); // We must do this first to prevent foreign key errors

            // Add category to database
            await Context.PlanetCategories.AddAsync(category);

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

            // Add default role to database
            await Context.PlanetRoles.AddAsync(defaultRole);

            // Save changes
            await Context.SaveChangesAsync();

            // Add owner to planet
            await planet.AddMemberAsync(user);

            // Return success
            return(new TaskResult <ulong>(true, "Successfully created planet.", planet.Id));
        }