public async Task <TaskResult> SetName(string name, 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.")); } PlanetChatChannel channel = await Context.PlanetChatChannels.Where(x => x.Id == id).FirstOrDefaultAsync(); ServerPlanet planet = await ServerPlanet.FindAsync(channel.Planet_Id, Mapper); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageChannels))) { return(new TaskResult(false, "You are not authorized to do this.")); } channel.Name = name; await Context.SaveChangesAsync(); return(new TaskResult(true, "Successfully set name.")); }
public async Task <TaskResult <ulong> > PostMessage(PlanetMessage msg) { //ClientMessage msg = JsonConvert.DeserializeObject<ClientMessage>(json); if (msg == null) { return(new TaskResult <ulong>(false, "Malformed message.", 0)); } ulong channel_id = msg.ChannelId; PlanetChatChannel channel = await Context.PlanetChatChannels.FindAsync(channel_id); // Get index for message ulong index = channel.Message_Count; // Update message count. May have to queue this in the future to prevent concurrency issues. channel.Message_Count += 1; await Context.SaveChangesAsync(); msg.Index = index; string json = JsonConvert.SerializeObject(msg); await MessageHub.Current.Clients.Group(channel_id.ToString()).SendAsync("Relay", json); return(new TaskResult <ulong>(true, $"Posted message {msg.Index}.", index)); }
public async Task <TaskResult> SetParentId(ulong id, ulong parentId, 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.")); } PlanetChatChannel channel = await Context.PlanetChatChannels.FindAsync(id); ServerPlanet planet = await ServerPlanet.FindAsync(channel.Planet_Id); if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageChannels))) { return(new TaskResult(false, "You are not authorized to do this.")); } channel.Parent_Id = parentId; await Context.SaveChangesAsync(); await PlanetHub.Current.Clients.Group($"p-{channel.Planet_Id}").SendAsync("RefreshChannelList", ""); return(new TaskResult(true, "Successfully set parentid.")); }
public async Task <ChannelPermissionsNode> GetChannelNodeAsync(PlanetChatChannel channel) { using (ValourDB Context = new ValourDB(ValourDB.DBOptions)) { return(await Context.ChannelPermissionsNodes.FirstOrDefaultAsync(x => x.Channel_Id == channel.Id && x.Role_Id == Id)); } }
/// <summary> /// Retrieves a ServerPlanetChatChannel for the given id /// </summary> public static async Task <ServerPlanetChatChannel> FindAsync(ulong id) { using (ValourDB db = new ValourDB(ValourDB.DBOptions)) { PlanetChatChannel channel = await db.PlanetChatChannels.FindAsync(id); return(ServerPlanetChatChannel.FromBase(channel)); } }
/// <summary> /// Creates a server and if successful returns a task result with the created /// planet's id /// </summary> public async Task <TaskResult <ulong> > CreateChannel(string name, ulong planet_id, ulong user_id, ulong parentid, 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.ManageChannels))) { 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 PlanetChatChannel channel = new PlanetChatChannel() { Id = IdManager.Generate(), Name = name, Planet_Id = planet_id, Parent_Id = parentid, Message_Count = 0, Description = "A chat channel", Position = Convert.ToUInt16(Context.PlanetChatChannels.Count()) }; // Add channel to database await Context.PlanetChatChannels.AddAsync(channel); // 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 channel.", channel.Id)); }
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!")); }
/// <summary> /// Creates a server and if successful returns a task result with the created /// planet's id /// </summary> public async Task <TaskResult <ulong> > CreateChannel(string name, ulong userid, ulong planetid, 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)); } // User is verified and given channel info is valid by this point // Creates the channel channel PlanetChatChannel channel = new PlanetChatChannel() { Name = name, Planet_Id = planetid, Message_Count = 0 }; // 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 channel.", channel.Id)); }
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.")); } PlanetChatChannel channel = await Context.PlanetChatChannels.FindAsync(id); if (channel == null) { return(new TaskResult(false, "That channel does not exist.")); } ServerPlanet planet = await ServerPlanet.FindAsync(channel.Planet_Id, Mapper); if (planet == null) { return(new TaskResult(false, "Could not find the planet.")); } if (!(await planet.AuthorizedAsync(authToken, PlanetPermissions.ManageChannels))) { return(new TaskResult(false, "You are not authorized to do this.")); } Context.PlanetChatChannels.Remove(channel); await Context.SaveChangesAsync(); return(new TaskResult(true, "Successfully deleted.")); }
public async Task <PermissionState> GetPermissionStateAsync(Permission permission, PlanetChatChannel channel) { return(await GetPermissionStateAsync(permission, channel.Id)); }
/// <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)); }
/// <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)); } if (image_url.Length > 255) { return(new TaskResult <ulong>(false, "Image url must be under 255 characters.", 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)); } // User is verified and given planet info is valid by this point // We don't actually need the user object which is cool 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 }; // 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)); }
/// <summary> /// Converts to a client version of planet chat channel /// </summary> public static ClientPlanetChatChannel FromBase(PlanetChatChannel channel, IMapper mapper) { return(mapper.Map <ClientPlanetChatChannel>(channel)); }
public ChatChannelWindow(int index, PlanetChatChannel channel) : base(index) { this.Channel = channel; }
/// <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)); }
/// <summary> /// Returns a ServerPlanetChatChannel using a PlanetChatChannel as a base /// </summary> public static ServerPlanetChatChannel FromBase(PlanetChatChannel channel) { return(MappingManager.Mapper.Map <ServerPlanetChatChannel>(channel)); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { Task task = Task.Run(async() => { //try //{ Context = new ValourDB(ValourDB.DBOptions); // This is a stream and will run forever foreach (PlanetMessage Message in MessageQueue.GetConsumingEnumerable()) { ulong channel_id = Message.Channel_Id; PlanetChatChannel channel = await Context.PlanetChatChannels.FindAsync(channel_id); // Get index for message ulong index = channel.Message_Count; // Update message count. May have to queue this in the future to prevent concurrency issues (done). channel.Message_Count += 1; Message.Message_Index = index; string json = JsonConvert.SerializeObject(Message); // This is not awaited on purpose PlanetHub.Current.Clients.Group($"c-{channel_id}").SendAsync("Relay", json); StagedMessages.Add(Message); } //} //catch (System.Exception e) //{ // Console.WriteLine("FATAL MESSAGE WORKER ERROR:"); // Console.WriteLine(e.Message); //} }); while (!task.IsCompleted) { _logger.LogInformation($"Planet Message Worker running at: {DateTimeOffset.Now}"); _logger.LogInformation($"Queue size: {MessageQueue.Count}"); _logger.LogInformation($"Saving {StagedMessages.Count} messages to DB."); if (Context != null) { await Context.PlanetMessages.AddRangeAsync(StagedMessages); StagedMessages.Clear(); await Context.SaveChangesAsync(); _logger.LogInformation($"Saved successfully."); } // Save to DB await Task.Delay(30000, stoppingToken); } _logger.LogInformation("Planet Message Worker task stopped at: {time}", DateTimeOffset.Now); _logger.LogInformation("Restarting.", DateTimeOffset.Now); } }