From 3602f54fa1da17e44a46be19d42b3b6a26b62a88 Mon Sep 17 00:00:00 2001 From: Anna Date: Wed, 3 Mar 2021 23:34:52 -0500 Subject: [PATCH] refactor: break ui code into parts --- NoSoliciting/Commands.cs | 4 +- NoSoliciting/Interface/PluginUi.cs | 29 + NoSoliciting/Interface/Report.cs | 305 +++++++++++ NoSoliciting/Interface/ReportStatus.cs | 8 + .../{PluginUi.cs => Interface/Settings.cs} | 494 ++++-------------- NoSoliciting/Plugin.cs | 1 + 6 files changed, 459 insertions(+), 382 deletions(-) create mode 100755 NoSoliciting/Interface/PluginUi.cs create mode 100755 NoSoliciting/Interface/Report.cs create mode 100755 NoSoliciting/Interface/ReportStatus.cs rename NoSoliciting/{PluginUi.cs => Interface/Settings.cs} (57%) mode change 100644 => 100755 diff --git a/NoSoliciting/Commands.cs b/NoSoliciting/Commands.cs index 90bf3ed..459f866 100755 --- a/NoSoliciting/Commands.cs +++ b/NoSoliciting/Commands.cs @@ -28,11 +28,11 @@ namespace NoSoliciting { } if (args == "report") { - this.Plugin.Ui.ToggleReporting(); + this.Plugin.Ui.Report.Toggle(); return; } - this.Plugin.Ui.ToggleSettings(); + this.Plugin.Ui.Settings.Toggle(); } } } diff --git a/NoSoliciting/Interface/PluginUi.cs b/NoSoliciting/Interface/PluginUi.cs new file mode 100755 index 0000000..6f94a82 --- /dev/null +++ b/NoSoliciting/Interface/PluginUi.cs @@ -0,0 +1,29 @@ +using System; + +namespace NoSoliciting.Interface { + public class PluginUi : IDisposable { + private Plugin Plugin { get; } + + public Settings Settings { get; } + public Report Report { get; } + + public PluginUi(Plugin plugin) { + this.Plugin = plugin; + + this.Settings = new Settings(plugin, this); + this.Report = new Report(plugin); + + this.Plugin.Interface.UiBuilder.OnBuildUi += this.Draw; + } + + public void Dispose() { + this.Plugin.Interface.UiBuilder.OnBuildUi -= this.Draw; + this.Settings.Dispose(); + } + + private void Draw() { + this.Settings.Draw(); + this.Report.Draw(); + } + } +} diff --git a/NoSoliciting/Interface/Report.cs b/NoSoliciting/Interface/Report.cs new file mode 100755 index 0000000..a3f6c58 --- /dev/null +++ b/NoSoliciting/Interface/Report.cs @@ -0,0 +1,305 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Net; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; +using Dalamud.Game.Chat.SeStringHandling; +using Dalamud.Game.Chat.SeStringHandling.Payloads; +using Dalamud.Plugin; +using ImGuiNET; + +namespace NoSoliciting.Interface { + public class Report { + private Plugin Plugin { get; } + + private ReportStatus LastReportStatus { get; set; } = ReportStatus.None; + + private bool _showReporting; + + private bool ShowReporting { + get => this._showReporting; + set => this._showReporting = value; + } + + public Report(Plugin plugin) { + this.Plugin = plugin; + } + + public void Open() { + this.ShowReporting = true; + } + + public void Toggle() { + this.ShowReporting = !this.ShowReporting; + } + + public void Draw() { + if (!this.ShowReporting) { + return; + } + + ImGui.SetNextWindowSize(new Vector2(1_000, 350), ImGuiCond.FirstUseEver); + + if (!ImGui.Begin("NoSoliciting reporting", ref this._showReporting)) { + return; + } + + ImGui.TextUnformatted("Click on one of the entries below to report it to the developer as miscategorised."); + + if (this.LastReportStatus != ReportStatus.None) { + var status = this.LastReportStatus switch { + ReportStatus.Failure => "failed to send", + ReportStatus.Successful => "sent successfully", + ReportStatus.InProgress => "sending", + _ => "unknown", + }; + ImGui.TextUnformatted($"Last report status: {status}"); + } + + ImGui.Separator(); + ImGui.Spacing(); + + if (ImGui.BeginTabBar("##report-tabs")) { + this.ChatTab(); + this.PartyFinderTab(); + + ImGui.EndTabBar(); + } + + ImGui.End(); + } + + private void ChatTab() { + if (!ImGui.BeginTabItem("Chat##chat-report")) { + return; + } + + float[] maxSizes = {0f, 0f, 0f, 0f}; + + if (ImGui.BeginChild("##chat-messages", new Vector2(-1, -1))) { + ImGui.Columns(5); + + AddRow(maxSizes, "Timestamp", "Channel", "Reason", "Sender", "Message"); + ImGui.Separator(); + + foreach (var message in this.Plugin.MessageHistory) { + if (message.FilterReason != null) { + ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(238f / 255f, 71f / 255f, 71f / 255f, 1f)); + } + + var sender = message.Sender.Payloads + .Where(payload => payload.Type == PayloadType.RawText) + .Cast() + .Select(payload => payload.Text) + .FirstOrDefault() ?? ""; + + if (AddRow(maxSizes, message.Timestamp.ToString(CultureInfo.CurrentCulture), message.ChatType.Name(this.Plugin.Interface.Data), message.FilterReason ?? "", sender, message.Content.TextValue)) { + ImGui.OpenPopup($"###modal-message-{message.Id}"); + } + + if (message.FilterReason != null) { + ImGui.PopStyleColor(); + } + + this.SetUpReportModal(message); + } + + for (var idx = 0; idx < maxSizes.Length; idx++) { + ImGui.SetColumnWidth(idx, maxSizes[idx] + ImGui.GetStyle().ItemSpacing.X * 2); + } + + ImGui.Columns(1); + + ImGui.EndChild(); + } + + ImGui.EndTabItem(); + } + + private void PartyFinderTab() { + if (!ImGui.BeginTabItem("Party Finder##pf-report")) { + return; + } + + #if DEBUG + if (ImGui.Button("Copy CSV")) { + var builder = new StringBuilder(); + + foreach (var message in this.Plugin.PartyFinderHistory) { + if (message.FilterReason == null) { + continue; + } + + message.ToCsv(builder).Append('\n'); + } + + ImGui.SetClipboardText(builder.ToString()); + } + #endif + + float[] maxSizes = {0f, 0f, 0f}; + + if (ImGui.BeginChild("##pf-messages", new Vector2(-1, -1))) { + ImGui.Columns(4); + + AddRow(maxSizes, "Timestamp", "Reason", "Host", "Description"); + ImGui.Separator(); + + foreach (var message in this.Plugin.PartyFinderHistory) { + if (message.FilterReason != null) { + ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(238f / 255f, 71f / 255f, 71f / 255f, 1f)); + } + + var sender = message.Sender.Payloads + .Where(payload => payload.Type == PayloadType.RawText) + .Cast() + .Select(payload => payload.Text) + .FirstOrDefault() ?? ""; + + if (AddRow(maxSizes, message.Timestamp.ToString(CultureInfo.CurrentCulture), message.FilterReason ?? "", sender, message.Content.TextValue)) { + ImGui.OpenPopup($"###modal-message-{message.Id}"); + } + + if (message.FilterReason != null) { + ImGui.PopStyleColor(); + } + + this.SetUpReportModal(message); + } + + for (var idx = 0; idx < maxSizes.Length; idx++) { + ImGui.SetColumnWidth(idx, maxSizes[idx] + ImGui.GetStyle().ItemSpacing.X * 2); + } + + ImGui.Columns(1); + + ImGui.EndChild(); + } + + ImGui.EndTabItem(); + } + + #region Modal + + private void SetUpReportModal(Message message) { + ImGui.SetNextWindowSize(new Vector2(350, -1)); + if (!ImGui.BeginPopupModal($"Report to NoSoliciting###modal-message-{message.Id}")) { + return; + } + + ImGui.PushTextWrapPos(); + + if (!message.Ml) { + ImGui.TextUnformatted("You cannot report messages filtered by definitions. Please switch to machine learning mode."); + + goto EndPopup; + } + + ImGui.TextUnformatted("Reporting this message will let the developer know that you think this message was incorrectly classified."); + + ImGui.TextUnformatted(message.FilterReason != null + ? "Specifically, this message WAS filtered but shouldn't have been." + : "Specifically, this message WAS NOT filtered but should have been."); + + ImGui.Separator(); + + ImGui.TextUnformatted(message.Content.TextValue); + + ImGui.Separator(); + + ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(1f, 0f, 0f, 1f)); + ImGui.TextUnformatted("NoSoliciting only works for English messages. Do not report non-English messages."); + ImGui.PopStyleColor(); + + ImGui.Separator(); + + if (message.FilterReason == "custom") { + ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(1f, 0f, 0f, 1f)); + ImGui.TextUnformatted("You cannot report messages filtered because of a custom filter."); + ImGui.PopStyleColor(); + } else { + if (ImGui.Button("Report")) { + Task.Run(async () => { + string? resp = null; + try { + using var client = new WebClient(); + this.LastReportStatus = ReportStatus.InProgress; + var reportUrl = this.Plugin.MlFilter?.ReportUrl; + if (reportUrl != null) { + resp = await client.UploadStringTaskAsync(reportUrl, message.ToJson()).ConfigureAwait(true); + } + } catch (Exception) { + // ignored + } + + this.LastReportStatus = resp == "{\"message\":\"ok\"}" ? ReportStatus.Successful : ReportStatus.Failure; + PluginLog.Log(resp == null + ? "Report not sent. ML model not set." + : $"Report sent. Response: {resp}"); + }); + ImGui.CloseCurrentPopup(); + } + + ImGui.SameLine(); + } + + if (ImGui.Button("Copy to clipboard")) { + ImGui.SetClipboardText(message.Content.TextValue); + } + + #if DEBUG + ImGui.SameLine(); + if (ImGui.Button("Copy CSV")) { + ImGui.SetClipboardText(message.ToCsv().ToString()); + } + #endif + + ImGui.SameLine(); + + EndPopup: + if (ImGui.Button("Cancel")) { + ImGui.CloseCurrentPopup(); + } + + ImGui.PopTextWrapPos(); + + ImGui.EndPopup(); + } + + #endregion + + #region Utility + + private static bool AddRow(IList maxSizes, params string[] args) { + var clicked = false; + + for (var i = 0; i < args.Length; i++) { + var arg = args[i]; + var last = i == args.Length - 1; + + if (last) { + ImGui.PushTextWrapPos(); + } + + ImGui.TextUnformatted(arg); + if (last) { + ImGui.PopTextWrapPos(); + } + + clicked = clicked || ImGui.IsItemClicked(); + if (!last) { + maxSizes[i] = Math.Max(maxSizes[i], ImGui.CalcTextSize(arg).X); + } + + ImGui.NextColumn(); + } + + return clicked; + } + + #endregion + } +} diff --git a/NoSoliciting/Interface/ReportStatus.cs b/NoSoliciting/Interface/ReportStatus.cs new file mode 100755 index 0000000..9a46576 --- /dev/null +++ b/NoSoliciting/Interface/ReportStatus.cs @@ -0,0 +1,8 @@ +namespace NoSoliciting.Interface { + public enum ReportStatus { + None = -1, + Failure = 0, + Successful = 1, + InProgress = 2, + } +} diff --git a/NoSoliciting/PluginUi.cs b/NoSoliciting/Interface/Settings.cs old mode 100644 new mode 100755 similarity index 57% rename from NoSoliciting/PluginUi.cs rename to NoSoliciting/Interface/Settings.cs index 422ca26..b2ecb21 --- a/NoSoliciting/PluginUi.cs +++ b/NoSoliciting/Interface/Settings.cs @@ -1,25 +1,15 @@ -using Dalamud.Game.Chat.SeStringHandling; -using Dalamud.Game.Chat.SeStringHandling.Payloads; -using Dalamud.Interface; -using Dalamud.Plugin; -using ImGuiNET; -using System; +using System; using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Net; using System.Numerics; -#if DEBUG -using System.Text; -#endif using System.Text.RegularExpressions; -using System.Threading.Tasks; +using Dalamud.Interface; +using ImGuiNET; using NoSoliciting.Ml; -namespace NoSoliciting { - public class PluginUi : IDisposable { +namespace NoSoliciting.Interface { + public class Settings : IDisposable { private Plugin Plugin { get; } - private ReportStatus LastReportStatus { get; set; } = ReportStatus.None; + private PluginUi Ui { get; } private bool _showSettings; @@ -28,49 +18,27 @@ namespace NoSoliciting { set => this._showSettings = value; } - private bool _showReporting; - - private bool ShowReporting { - get => this._showReporting; - set => this._showReporting = value; - } - - public PluginUi(Plugin plugin) { + public Settings(Plugin plugin, PluginUi ui) { this.Plugin = plugin; + this.Ui = ui; - this.Plugin.Interface.UiBuilder.OnBuildUi += this.Draw; - this.Plugin.Interface.UiBuilder.OnOpenConfigUi += this.OpenSettings; + this.Plugin.Interface.UiBuilder.OnOpenConfigUi += this.Open; } public void Dispose() { - this.Plugin.Interface.UiBuilder.OnOpenConfigUi -= this.OpenSettings; - this.Plugin.Interface.UiBuilder.OnBuildUi -= this.Draw; + this.Plugin.Interface.UiBuilder.OnOpenConfigUi -= this.Open; } - private void OpenSettings(object? sender, EventArgs? e) { + private void Open(object? sender, EventArgs? e) { this.ShowSettings = true; } - public void ToggleSettings() { + public void Toggle() { this.ShowSettings = !this.ShowSettings; } - public void ToggleReporting() { - this.ShowReporting = !this.ShowReporting; - } - - private void Draw() { - if (this.ShowSettings) { - this.DrawSettings(); - } - - if (this.ShowReporting) { - this.DrawReportWindow(); - } - } - - private void DrawSettings() { - if (!ImGui.Begin($"{this.Plugin.Name} settings", ref this._showSettings)) { + public void Draw() { + if (!this.ShowSettings || !ImGui.Begin($"{this.Plugin.Name} settings", ref this._showSettings)) { return; } @@ -129,96 +97,13 @@ namespace NoSoliciting { ImGui.Separator(); if (ImGui.Button("Show reporting window")) { - this.ShowReporting = true; + this.Ui.Report.Open(); } ImGui.End(); } - private void DrawOtherFilters() { - if (!ImGui.BeginTabItem("Other filters")) { - return; - } - - if (ImGui.CollapsingHeader("Chat filters")) { - var customChat = this.Plugin.Config.CustomChatFilter; - if (ImGui.Checkbox("Enable custom chat filters", ref customChat)) { - this.Plugin.Config.CustomChatFilter = customChat; - this.Plugin.Config.Save(); - } - - if (this.Plugin.Config.CustomChatFilter) { - var substrings = this.Plugin.Config.ChatSubstrings; - var regexes = this.Plugin.Config.ChatRegexes; - this.DrawCustom("chat", ref substrings, ref regexes); - } - } - - if (ImGui.CollapsingHeader("Party Finder filters")) { - var filterHugeItemLevelPFs = this.Plugin.Config.FilterHugeItemLevelPFs; - // ReSharper disable once InvertIf - if (ImGui.Checkbox("Filter PFs with item level above maximum", ref filterHugeItemLevelPFs)) { - this.Plugin.Config.FilterHugeItemLevelPFs = filterHugeItemLevelPFs; - this.Plugin.Config.Save(); - } - - var considerPrivate = this.Plugin.Config.ConsiderPrivatePfs; - if (ImGui.Checkbox("Apply filters to private Party Finder listings", ref considerPrivate)) { - this.Plugin.Config.ConsiderPrivatePfs = considerPrivate; - this.Plugin.Config.Save(); - } - - var customPf = this.Plugin.Config.CustomPFFilter; - if (ImGui.Checkbox("Enable custom Party Finder filters", ref customPf)) { - this.Plugin.Config.CustomPFFilter = customPf; - this.Plugin.Config.Save(); - } - - if (this.Plugin.Config.CustomPFFilter) { - var substrings = this.Plugin.Config.PFSubstrings; - var regexes = this.Plugin.Config.PFRegexes; - this.DrawCustom("pf", ref substrings, ref regexes); - } - } - - ImGui.EndTabItem(); - } - - private void DrawDefsBasicSettings() { - if (this.Plugin.Definitions == null) { - return; - } - - if (!ImGui.BeginTabItem("Filters")) { - return; - } - - this.DrawCheckboxes(this.Plugin.Definitions.Chat.Values, true, "chat"); - - ImGui.Separator(); - - this.DrawCheckboxes(this.Plugin.Definitions.PartyFinder.Values, true, "Party Finder"); - - ImGui.EndTabItem(); - } - - private void DrawDefsAdvancedSettings() { - if (this.Plugin.Definitions == null) { - return; - } - - if (ImGui.BeginTabItem("Chat")) { - this.DrawCheckboxes(this.Plugin.Definitions.Chat.Values, false, "chat"); - - ImGui.EndTabItem(); - } - - if (ImGui.BeginTabItem("Party Finder")) { - this.DrawCheckboxes(this.Plugin.Definitions.PartyFinder.Values, false, "Party Finder"); - - ImGui.EndTabItem(); - } - } + #region ML config private void DrawMachineLearningConfig() { if (this.Plugin.Config.AdvancedMode) { @@ -338,6 +223,10 @@ namespace NoSoliciting { ImGui.EndTabItem(); } + #endregion + + #region Definitions config + private void DrawDefinitionsConfig() { if (this.Plugin.Config.AdvancedMode) { this.DrawDefsAdvancedSettings(); @@ -373,6 +262,95 @@ namespace NoSoliciting { ImGui.EndTabItem(); } + private void DrawDefsBasicSettings() { + if (this.Plugin.Definitions == null) { + return; + } + + if (!ImGui.BeginTabItem("Filters")) { + return; + } + + this.DrawCheckboxes(this.Plugin.Definitions.Chat.Values, true, "chat"); + + ImGui.Separator(); + + this.DrawCheckboxes(this.Plugin.Definitions.PartyFinder.Values, true, "Party Finder"); + + ImGui.EndTabItem(); + } + + private void DrawDefsAdvancedSettings() { + if (this.Plugin.Definitions == null) { + return; + } + + if (ImGui.BeginTabItem("Chat")) { + this.DrawCheckboxes(this.Plugin.Definitions.Chat.Values, false, "chat"); + + ImGui.EndTabItem(); + } + + if (ImGui.BeginTabItem("Party Finder")) { + this.DrawCheckboxes(this.Plugin.Definitions.PartyFinder.Values, false, "Party Finder"); + + ImGui.EndTabItem(); + } + } + + #endregion + + #region Other config + + private void DrawOtherFilters() { + if (!ImGui.BeginTabItem("Other filters")) { + return; + } + + if (ImGui.CollapsingHeader("Chat filters")) { + var customChat = this.Plugin.Config.CustomChatFilter; + if (ImGui.Checkbox("Enable custom chat filters", ref customChat)) { + this.Plugin.Config.CustomChatFilter = customChat; + this.Plugin.Config.Save(); + } + + if (this.Plugin.Config.CustomChatFilter) { + var substrings = this.Plugin.Config.ChatSubstrings; + var regexes = this.Plugin.Config.ChatRegexes; + this.DrawCustom("chat", ref substrings, ref regexes); + } + } + + if (ImGui.CollapsingHeader("Party Finder filters")) { + var filterHugeItemLevelPFs = this.Plugin.Config.FilterHugeItemLevelPFs; + // ReSharper disable once InvertIf + if (ImGui.Checkbox("Filter PFs with item level above maximum", ref filterHugeItemLevelPFs)) { + this.Plugin.Config.FilterHugeItemLevelPFs = filterHugeItemLevelPFs; + this.Plugin.Config.Save(); + } + + var considerPrivate = this.Plugin.Config.ConsiderPrivatePfs; + if (ImGui.Checkbox("Apply filters to private Party Finder listings", ref considerPrivate)) { + this.Plugin.Config.ConsiderPrivatePfs = considerPrivate; + this.Plugin.Config.Save(); + } + + var customPf = this.Plugin.Config.CustomPFFilter; + if (ImGui.Checkbox("Enable custom Party Finder filters", ref customPf)) { + this.Plugin.Config.CustomPFFilter = customPf; + this.Plugin.Config.Save(); + } + + if (this.Plugin.Config.CustomPFFilter) { + var substrings = this.Plugin.Config.PFSubstrings; + var regexes = this.Plugin.Config.PFRegexes; + this.DrawCustom("pf", ref substrings, ref regexes); + } + } + + ImGui.EndTabItem(); + } + private void DrawCustom(string name, ref List substrings, ref List regexes) { ImGui.Columns(2); @@ -452,6 +430,10 @@ namespace NoSoliciting { } } + #endregion + + #region Utility + private void DrawCheckboxes(IEnumerable defs, bool basic, string labelFillIn) { foreach (var def in defs) { this.Plugin.Config.FilterStatus.TryGetValue(def.Id, out var enabled); @@ -465,254 +447,6 @@ namespace NoSoliciting { } } - private void DrawReportWindow() { - ImGui.SetNextWindowSize(new Vector2(1_000, 350), ImGuiCond.FirstUseEver); - - if (!ImGui.Begin("NoSoliciting reporting", ref this._showReporting)) { - return; - } - - ImGui.TextUnformatted("Click on one of the entries below to report it to the developer as miscategorised."); - - if (this.LastReportStatus != ReportStatus.None) { - var status = this.LastReportStatus switch { - ReportStatus.Failure => "failed to send", - ReportStatus.Successful => "sent successfully", - ReportStatus.InProgress => "sending", - _ => "unknown", - }; - ImGui.TextUnformatted($"Last report status: {status}"); - } - - ImGui.Separator(); - ImGui.Spacing(); - - if (ImGui.BeginTabBar("##report-tabs")) { - if (ImGui.BeginTabItem("Chat##chat-report")) { - float[] maxSizes = {0f, 0f, 0f, 0f}; - - if (ImGui.BeginChild("##chat-messages", new Vector2(-1, -1))) { - ImGui.Columns(5); - - AddRow(maxSizes, "Timestamp", "Channel", "Reason", "Sender", "Message"); - ImGui.Separator(); - - foreach (var message in this.Plugin.MessageHistory) { - if (message.FilterReason != null) { - ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(238f / 255f, 71f / 255f, 71f / 255f, 1f)); - } - - var sender = message.Sender.Payloads - .Where(payload => payload.Type == PayloadType.RawText) - .Cast() - .Select(payload => payload.Text) - .FirstOrDefault() ?? ""; - - if (AddRow(maxSizes, message.Timestamp.ToString(CultureInfo.CurrentCulture), message.ChatType.Name(this.Plugin.Interface.Data), message.FilterReason ?? "", sender, message.Content.TextValue)) { - ImGui.OpenPopup($"###modal-message-{message.Id}"); - } - - if (message.FilterReason != null) { - ImGui.PopStyleColor(); - } - - this.SetUpReportModal(message); - } - - for (var idx = 0; idx < maxSizes.Length; idx++) { - ImGui.SetColumnWidth(idx, maxSizes[idx] + ImGui.GetStyle().ItemSpacing.X * 2); - } - - ImGui.Columns(1); - - ImGui.EndChild(); - } - - ImGui.EndTabItem(); - } - - if (ImGui.BeginTabItem("Party Finder##pf-report")) { - #if DEBUG - if (ImGui.Button("Copy CSV")) { - var builder = new StringBuilder(); - - foreach (var message in this.Plugin.PartyFinderHistory) { - if (message.FilterReason == null) { - continue; - } - - message.ToCsv(builder).Append('\n'); - } - - ImGui.SetClipboardText(builder.ToString()); - } - #endif - - float[] maxSizes = {0f, 0f, 0f}; - - if (ImGui.BeginChild("##pf-messages", new Vector2(-1, -1))) { - ImGui.Columns(4); - - AddRow(maxSizes, "Timestamp", "Reason", "Host", "Description"); - ImGui.Separator(); - - foreach (var message in this.Plugin.PartyFinderHistory) { - if (message.FilterReason != null) { - ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(238f / 255f, 71f / 255f, 71f / 255f, 1f)); - } - - var sender = message.Sender.Payloads - .Where(payload => payload.Type == PayloadType.RawText) - .Cast() - .Select(payload => payload.Text) - .FirstOrDefault() ?? ""; - - if (AddRow(maxSizes, message.Timestamp.ToString(CultureInfo.CurrentCulture), message.FilterReason ?? "", sender, message.Content.TextValue)) { - ImGui.OpenPopup($"###modal-message-{message.Id}"); - } - - if (message.FilterReason != null) { - ImGui.PopStyleColor(); - } - - this.SetUpReportModal(message); - } - - for (var idx = 0; idx < maxSizes.Length; idx++) { - ImGui.SetColumnWidth(idx, maxSizes[idx] + ImGui.GetStyle().ItemSpacing.X * 2); - } - - ImGui.Columns(1); - - ImGui.EndChild(); - } - - ImGui.EndTabItem(); - } - - ImGui.EndTabBar(); - } - - ImGui.End(); - } - - private void SetUpReportModal(Message message) { - ImGui.SetNextWindowSize(new Vector2(350, -1)); - if (!ImGui.BeginPopupModal($"Report to NoSoliciting###modal-message-{message.Id}")) { - return; - } - - ImGui.PushTextWrapPos(); - - if (!message.Ml) { - ImGui.TextUnformatted("You cannot report messages filtered by definitions. Please switch to machine learning mode."); - - goto EndPopup; - } - - ImGui.TextUnformatted("Reporting this message will let the developer know that you think this message was incorrectly classified."); - - ImGui.TextUnformatted(message.FilterReason != null - ? "Specifically, this message WAS filtered but shouldn't have been." - : "Specifically, this message WAS NOT filtered but should have been."); - - ImGui.Separator(); - - ImGui.TextUnformatted(message.Content.TextValue); - - ImGui.Separator(); - - ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(1f, 0f, 0f, 1f)); - ImGui.TextUnformatted("NoSoliciting only works for English messages. Do not report non-English messages."); - ImGui.PopStyleColor(); - - ImGui.Separator(); - - if (message.FilterReason == "custom") { - ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(1f, 0f, 0f, 1f)); - ImGui.TextUnformatted("You cannot report messages filtered because of a custom filter."); - ImGui.PopStyleColor(); - } else { - if (ImGui.Button("Report")) { - Task.Run(async () => { - string? resp = null; - try { - using var client = new WebClient(); - this.LastReportStatus = ReportStatus.InProgress; - var reportUrl = this.Plugin.MlFilter?.ReportUrl; - if (reportUrl != null) { - resp = await client.UploadStringTaskAsync(reportUrl, message.ToJson()).ConfigureAwait(true); - } - } catch (Exception) { - // ignored - } - - this.LastReportStatus = resp == "{\"message\":\"ok\"}" ? ReportStatus.Successful : ReportStatus.Failure; - PluginLog.Log(resp == null - ? "Report not sent. ML model not set." - : $"Report sent. Response: {resp}"); - }); - ImGui.CloseCurrentPopup(); - } - - ImGui.SameLine(); - } - - if (ImGui.Button("Copy to clipboard")) { - ImGui.SetClipboardText(message.Content.TextValue); - } - - #if DEBUG - ImGui.SameLine(); - if (ImGui.Button("Copy CSV")) { - ImGui.SetClipboardText(message.ToCsv().ToString()); - } - #endif - - ImGui.SameLine(); - - EndPopup: - if (ImGui.Button("Cancel")) { - ImGui.CloseCurrentPopup(); - } - - ImGui.PopTextWrapPos(); - - ImGui.EndPopup(); - } - - private enum ReportStatus { - None = -1, - Failure = 0, - Successful = 1, - InProgress = 2, - } - - private static bool AddRow(IList maxSizes, params string[] args) { - var clicked = false; - - for (var i = 0; i < args.Length; i++) { - var arg = args[i]; - var last = i == args.Length - 1; - - if (last) { - ImGui.PushTextWrapPos(); - } - - ImGui.TextUnformatted(arg); - if (last) { - ImGui.PopTextWrapPos(); - } - - clicked = clicked || ImGui.IsItemClicked(); - if (!last) { - maxSizes[i] = Math.Max(maxSizes[i], ImGui.CalcTextSize(arg).X); - } - - ImGui.NextColumn(); - } - - return clicked; - } + #endregion } } diff --git a/NoSoliciting/Plugin.cs b/NoSoliciting/Plugin.cs index 4479bb6..1051937 100644 --- a/NoSoliciting/Plugin.cs +++ b/NoSoliciting/Plugin.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Reflection; using System.Threading.Tasks; +using NoSoliciting.Interface; using NoSoliciting.Ml; namespace NoSoliciting {