/// <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));
        }
Exemple #2
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, ulong userid, string token)
        {
            TaskResult nameValid = ValidateName(name);

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

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

            // Return the same if the token is for the wrong user to prevent someone
            // from knowing if they cracked another user's token. This is basically
            // impossible to happen by chance but better safe than sorry in the case that
            // the literal impossible odds occur, more likely someone gets a stolen token
            // but is not aware of the owner but I'll shut up now - Spike
            if (authToken == null || authToken.User_Id != userid)
            {
                return(new TaskResult <ulong>(false, "Failed to authorize user.", 0));
            }

            if (await Context.Planets.CountAsync(x => x.Owner_Id == userid) > 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;
            }


            Planet planet = new Planet()
            {
                Name         = name,
                Member_Count = 1,
                Description  = "A Valour server.",
                Image_Url    = image_url,
                Public       = true,
                Owner_Id     = userid
            };

            await Context.Planets.AddAsync(planet);

            // Have to do this first for auto-incremented ID
            await Context.SaveChangesAsync();

            PlanetMember member = new PlanetMember()
            {
                User_Id   = userid,
                Planet_Id = planet.Id
            };

            // Add the owner to the planet as a member
            await Context.PlanetMembers.AddAsync(member);


            // Create general channel
            PlanetChatChannel channel = new PlanetChatChannel()
            {
                Name          = "General",
                Planet_Id     = planet.Id,
                Message_Count = 0,
                Description   = "General chat channel"
            };

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

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

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