Exemplo n.º 1
0
        public async Task <TaskResult> SetName(string name, ulong id, ulong user_id, string token)
        {
            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 != user_id)
            {
                return(new TaskResult(false, "Failed to authorize user."));
            }

            PlanetCategory category = await Context.PlanetCategories.Where(x => x.Id == id).FirstOrDefaultAsync();

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

            if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageCategories)))
            {
                return(new TaskResult(false, "You are not authorized to do this."));
            }

            category.Name = name;

            await Context.SaveChangesAsync();

            await PlanetHub.Current.Clients.Group($"p-{category.Planet_Id}").SendAsync("RefreshChannelList", "");

            return(new TaskResult(true, "Successfully set name."));
        }
Exemplo n.º 2
0
        public async Task <TaskResult> UpdateOrder([FromBody] Dictionary <ushort, List <ulong> > json, ulong user_id, string token, ulong Planet_Id)
        {
            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 != user_id)
            {
                return(new TaskResult(false, "Failed to authorize user."));
            }

            ServerPlanet planet = await ServerPlanet.FindAsync(Planet_Id);

            if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageChannels)))
            {
                return(new TaskResult(false, "You are not authorized to do this."));
            }

            Console.WriteLine(json);
            var values = json;//JsonConvert.DeserializeObject<Dictionary<ulong, ulong>>(data);

            foreach (var value in values)
            {
                ushort position = value.Key;
                ulong  id       = value.Value[0];

                //checks if item is a channel
                Console.WriteLine(value.Value[0]);
                if (value.Value[1] == 0)
                {
                    PlanetChatChannel channel = await Context.PlanetChatChannels.Where(x => x.Id == id).FirstOrDefaultAsync();

                    channel.Position = position;
                }
                if (value.Value[1] == 1)
                {
                    PlanetCategory category = await Context.PlanetCategories.Where(x => x.Id == id).FirstOrDefaultAsync();

                    category.Position = position;
                }

                Console.WriteLine(value);
            }
            await Context.SaveChangesAsync();

            await PlanetHub.Current.Clients.Group($"p-{Planet_Id}").SendAsync("RefreshChannelList", "");

            return(new TaskResult(true, "Updated Order!"));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a server and if successful returns a task result with the created
        /// planet's id
        /// </summary>
        public async Task <TaskResult <ulong> > CreateCategory(string name, ulong user_id, ulong parentid, ulong planet_id, 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 != user_id)
            {
                return(new TaskResult <ulong>(false, "Failed to authorize user.", 0));
            }

            ServerPlanet planet = await ServerPlanet.FindAsync(planet_id);

            if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageCategories)))
            {
                return(new TaskResult <ulong>(false, "You are not authorized to do this.", 0));
            }

            // User is verified and given channel info is valid by this point

            // Creates the channel channel

            PlanetCategory category = new PlanetCategory()
            {
                Id        = IdManager.Generate(),
                Name      = name,
                Planet_Id = planet_id,
                Parent_Id = parentid
            };

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

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

            await PlanetHub.Current.Clients.Group($"p-{planet_id}").SendAsync("RefreshChannelList", "");

            // Return success
            return(new TaskResult <ulong>(true, "Successfully created category.", category.Id));
        }
Exemplo n.º 4
0
        public async Task <TaskResult <IEnumerable <PlanetCategory> > > GetPlanetCategoriesAsync(ulong planet_id)
        {
            IEnumerable <PlanetCategory> categories = await Task.Run(() => Context.PlanetCategories.Where(c => c.Planet_Id == planet_id).ToList());

            // in case theres 0 categories or "General" does not exist
            if (categories.Count() == 0 || !(categories.Any(x => x.Name == "General")))
            {
                PlanetCategory category = new PlanetCategory();
                category.Name      = "General";
                category.Planet_Id = planet_id;
                await Context.PlanetCategories.AddAsync(category);

                await Context.SaveChangesAsync();

                categories = await Task.Run(() => Context.PlanetCategories.Where(c => c.Planet_Id == planet_id).ToList());
            }

            return(new TaskResult <IEnumerable <PlanetCategory> >(true, "Successfully retrieved Categories.", categories));
        }
Exemplo n.º 5
0
        public async Task <TaskResult> Delete(ulong id, ulong userid, string token)
        {
            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(false, "Failed to authorize user."));
            }

            PlanetCategory category = await Context.PlanetCategories.FindAsync(id);

            ServerPlanet planet = await ServerPlanet.FindAsync(category.Planet_Id, Mapper);

            if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageCategories)))
            {
                return(new TaskResult(false, "You are not authorized to do this."));
            }

            List <PlanetCategory> categories = await Task.Run(() => Context.PlanetCategories.Where(x => x.Parent_Id == id).ToList());

            foreach (PlanetCategory Category in categories)
            {
                Category.Parent_Id = null;
            }
            List <PlanetChatChannel> channels = await Task.Run(() => Context.PlanetChatChannels.Where(x => x.Parent_Id == id).ToList());

            foreach (PlanetChatChannel channel in channels)
            {
                Context.PlanetChatChannels.Remove(channel);
            }

            Context.PlanetCategories.Remove(category);

            Context.PlanetCategories.Remove(category);
            await Context.SaveChangesAsync();

            return(new TaskResult(true, "Successfully deleted."));
        }
Exemplo n.º 6
0
        public async Task <TaskResult> SetParentId(ulong id, ushort parentId, ulong userid, string token)
        {
            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(false, "Failed to authorize user."));
            }

            PlanetCategory category = await Context.PlanetCategories.Where(x => x.Id == id).FirstOrDefaultAsync();

            ServerPlanet planet = await ServerPlanet.FindAsync(category.Planet_Id, Mapper);

            if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageCategories)))
            {
                return(new TaskResult(false, "You are not authorized to do this."));
            }

            if (parentId == 0)
            {
                category.Parent_Id = null;
            }
            else
            {
                category.Parent_Id = parentId;
            }

            await Context.SaveChangesAsync();

            return(new TaskResult(true, "Successfully set parent id."));
        }
Exemplo n.º 7
0
 /// <summary>
 /// Converts to a client version of planet category
 /// </summary>
 public static ClientPlanetCategory FromBase(PlanetCategory channel, IMapper mapper)
 {
     return(mapper.Map <ClientPlanetCategory>(channel));
 }
Exemplo n.º 8
0
        public async Task <TaskResult> Delete(ulong id, ulong user_id, string token)
        {
            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 != user_id)
            {
                return(new TaskResult(false, "Failed to authorize user."));
            }

            PlanetCategory category = await Context.PlanetCategories.FindAsync(id);

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

            if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageCategories)))
            {
                return(new TaskResult(false, "You are not authorized to do this."));
            }

            List <PlanetCategory> cate = await Task.Run(() => Context.PlanetCategories.Where(x => x.Planet_Id == category.Planet_Id).ToList());

            if (cate.Count == 1)
            {
                return(new TaskResult(false, "You can not delete your last category!"));
            }

            List <PlanetCategory> categories = await Task.Run(() => Context.PlanetCategories.Where(x => x.Parent_Id == id).ToList());

            List <PlanetChatChannel> channels = await Task.Run(() => Context.PlanetChatChannels.Where(x => x.Parent_Id == id).ToList());

            //Check if any channels in this category are the main channel
            foreach (PlanetChatChannel channel in channels)
            {
                if (channel.Id == planet.Main_Channel_Id)
                {
                    return(new TaskResult(false, "You can not delete your main channel!"));
                }
            }
            //If not, then delete the channels
            foreach (PlanetChatChannel channel in channels)
            {
                Context.PlanetChatChannels.Remove(channel);
            }

            foreach (PlanetCategory Category in categories)
            {
                Category.Parent_Id = null;
            }

            ulong parentId = category.Planet_Id;

            Context.PlanetCategories.Remove(category);

            await Context.SaveChangesAsync();

            await PlanetHub.Current.Clients.Group($"p-{parentId}").SendAsync("RefreshChannelList", "");

            return(new TaskResult(true, "Successfully deleted."));
        }
Exemplo n.º 9
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));
        }
Exemplo n.º 10
0
 public void SetOpen(PlanetCategory category, bool value)
 {
     SetOpen(category.Id, value);
 }
Exemplo n.º 11
0
 public bool IsOpen(PlanetCategory category)
 {
     return(IsOpen(category.Id));
 }
Exemplo n.º 12
0
 public Planet(PolarPosition position, PlanetSize size, PlanetCategory category)
 {
     Position = position;
     Size     = size;
     Category = category;
 }