From f5ef64e87d4ec61f630ab28235c4906bfb08a2e1 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 12 Feb 2017 14:03:46 -0500 Subject: Admin panel for chat frontend. --- ShiftOS.Server.WebAdmin/Program.cs | 202 +++++++++++++++++++++++++++++++++++-- 1 file changed, 196 insertions(+), 6 deletions(-) (limited to 'ShiftOS.Server.WebAdmin/Program.cs') diff --git a/ShiftOS.Server.WebAdmin/Program.cs b/ShiftOS.Server.WebAdmin/Program.cs index 6e3661d..91e4831 100644 --- a/ShiftOS.Server.WebAdmin/Program.cs +++ b/ShiftOS.Server.WebAdmin/Program.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -122,6 +123,100 @@ namespace ShiftOS.Server.WebAdmin return false; } + public static string BuildFormFromObject(object obj) + { + StringBuilder sb = new StringBuilder(); + sb.AppendLine("
"); + foreach(var prop in obj.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)) + { + string name = ""; + string description = "No description."; + foreach(var attrib in prop.GetCustomAttributes(false)) + { + if(attrib is FriendlyNameAttribute) + { + name = (attrib as FriendlyNameAttribute).Name; + } + if(attrib is FriendlyDescriptionAttribute) + { + description = (attrib as FriendlyDescriptionAttribute).Description; + } + } + if (name != "") + { + sb.AppendLine(""); + + sb.AppendLine($@" +"); + } + else + { + sb.AppendLine($""); + } + } + sb.AppendLine(""); + sb.AppendLine("
+

{name}

+

{description}

+
"); + if (prop.PropertyType == typeof(bool)) + { + string isChecked = ((bool)prop.GetValue(obj) == true) ? "checked" : ""; + sb.AppendLine($""); + } + else if (prop.PropertyType == typeof(string)) + { + sb.AppendLine($""); + } + + sb.AppendLine("
"); + return sb.ToString(); + } + + public static Channel GetChat(string id) + { + if (File.Exists("chats.json")) + foreach (var channel in JsonConvert.DeserializeObject>(File.ReadAllText("chats.json"))) + { + if (channel.ID == id) + return channel; + } + return new Channel(); + } + + public static string GetAllChats() + { + StringBuilder sb = new StringBuilder(); + sb.AppendLine(""); + sb.AppendLine($@" + + + + + + "); + if (File.Exists("chats.json")) + { + foreach(var chat in JsonConvert.DeserializeObject>(File.ReadAllText("chats.json"))) + { + sb.AppendLine($@" + + + + + + + +"); + } + } + sb.AppendLine("
IDNameTopicIs Discord RelayDiscord channel IDDiscord Bot TokenActions
{chat.ID}{chat.Name}{chat.Topic}{chat.IsDiscordProxy}{chat.DiscordChannelID}{chat.DiscordBotToken} + Edit + Delete +
"); + return sb.ToString(); + } + public static string GetCPWorth() { if (System.IO.Directory.Exists("saves")) @@ -222,7 +317,7 @@ namespace ShiftOS.Server.WebAdmin Get["/logout"] = parameters => { - return this.Logout("/"); + return this.Logout("~/"); }; Post["/login"] = parameters => @@ -269,16 +364,111 @@ namespace ShiftOS.Server.WebAdmin this.RequiresClaims("Admin"); Get["/"] = _ => { - return PageBuilder.Build("status", new Dictionary{ + return statusBuilder(); + }; + Get["/status"] = _ => + { + return statusBuilder(); + }; + Get["/chats"] = _ => + { + return chatsListBuilder(); + }; + + Get["/createchat"] = _ => + { + return PageBuilder.Build("editchat", new Dictionary + { + {"{body}", Properties.Resources.ChatEditTemplate }, + {"{form}", SystemManager.BuildFormFromObject(new Channel()) } + }); + }; + + Post["/createchat"] = parameters => + { + var chat = this.Bind(); + chat.ID = chat.Name.ToLower().Replace(" ", "_"); + List chats = new List(); + if (File.Exists("chats.json")) + chats = JsonConvert.DeserializeObject>(File.ReadAllText("chats.json")); + + bool chatExists = false; + + for (int i = 0; i < chats.Count; i++) + { + if (chats[i].ID == chat.ID) + { + chats[i] = chat; + chatExists = true; + } + } + + if (!chatExists) + { + chats.Add(chat); + } + + File.WriteAllText("chats.json", JsonConvert.SerializeObject(chats, Formatting.Indented)); + + return chatsListBuilder(); + }; + + Get["/editchat/{id}"] = parameters => + { + return PageBuilder.Build("editchat", new Dictionary + { + {"{body}", Properties.Resources.ChatEditTemplate }, + {"{form}", SystemManager.BuildFormFromObject(SystemManager.GetChat(parameters.id)) } + }); + }; + + Post["/editchat/{id}"] = parameters => + { + var chat = this.Bind(); + chat.ID = chat.Name.ToLower().Replace(" ", "_"); + List chats = new List(); + if (File.Exists("chats.json")) + chats = JsonConvert.DeserializeObject>(File.ReadAllText("chats.json")); + + bool chatExists = false; + + for (int i = 0; i < chats.Count; i++) + { + if (chats[i].ID == chat.ID) + { + chats[i] = chat; + chatExists = true; + } + } + + if (!chatExists) + { + chats.Add(chat); + } + + File.WriteAllText("chats.json", JsonConvert.SerializeObject(chats, Formatting.Indented)); + return chatsListBuilder(); + }; + + } + + private string statusBuilder() + { + return PageBuilder.Build("status", new Dictionary{ { "{cp_worth}", SystemManager.GetCPWorth() }, { "{user_count}", SystemManager.GetUserCount() }, { "{system_time}", DateTime.Now.ToString() }, }); - }; - Get["/status"] = _ => + + } + + private string chatsListBuilder() + { + return PageBuilder.Build("bla", new Dictionary { - return PageBuilder.Build("status"); - }; + { "{body}", Properties.Resources.ChatListView }, + { "{chat_table}", SystemManager.GetAllChats() } + }); } } -- cgit v1.2.3