You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.5 KiB

1 year ago
  1. using CircleSDK.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using CircleViewer.Models;
  12. using Newtonsoft.Json;
  13. using Syncfusion.Windows.Forms;
  14. using System.Net.Http;
  15. using CDPShared;
  16. namespace CircleViewer.Dialogs
  17. {
  18. public partial class ActivityDlgDefault : Form
  19. {
  20. CDPWorker _cdp;
  21. List<CircleInfo> _CircleMeta;
  22. public ActivityDlgDefault(CDPWorker cdp)
  23. {
  24. _cdp = cdp;
  25. InitializeComponent();
  26. }
  27. private async void ActivityDlg_Load(object sender, EventArgs e)
  28. {
  29. LoadActivity();
  30. await Task.Run(async () =>
  31. {
  32. CircleInfo ci = await _cdp.GetCircle(Properties.Settings.Default.DefaultCircleId);
  33. this.BeginInvoke((MethodInvoker)delegate
  34. {
  35. this.Text = $"User Group Activity - {ci.CircleName}";
  36. });
  37. });
  38. }
  39. private async Task<List<ActivityRow>> LoadCircleActivity()
  40. {
  41. List<ActivityRow> lar = new List<ActivityRow>();
  42. GetMessagesReply gmReply = await _cdp.GetCircleViewwMessages();
  43. #if FALSE // I only want to display recent events. (making a video)
  44. foreach (MessageInfo mi in gmReply.Messages)
  45. {
  46. ActivityRow ar = new ActivityRow(mi);
  47. if ((DateTime.Now - ar.EventTime).TotalMinutes < 15)
  48. lar.Add(new ActivityRow(mi));
  49. }
  50. #else
  51. foreach (MessageInfo mi in gmReply.Messages)
  52. lar.Add(new ActivityRow(mi));
  53. #endif
  54. return lar;
  55. }
  56. private async Task<List<ActivityRow>> LoadCloudActivity()
  57. {
  58. List<ActivityRow> retList = new List<ActivityRow>();
  59. var url = "https://circlecloudfuncstaging.azurewebsites.net/api/GetCircleEvents?circleId=" + Properties.Settings.Default.DefaultCircleId;
  60. HttpClient client = new HttpClient();
  61. var response = await client.GetAsync(url);
  62. response.EnsureSuccessStatusCode();
  63. string result = await response.Content.ReadAsStringAsync();
  64. List<CircleViewEventRecord> lrecs = JsonConvert.DeserializeObject<List<CircleViewEventRecord>>(result);
  65. foreach (CircleViewEventRecord rec in lrecs)
  66. {
  67. retList.Add(new ActivityRow(rec));
  68. }
  69. return retList;
  70. }
  71. private async void LoadActivity()
  72. {
  73. GetMessagesReply gmReply = await _cdp.GetCircleViewwMessages();
  74. List<ActivityRow> lar = new List<ActivityRow>();
  75. List<ActivityRow> lCircleAR = await LoadCircleActivity();
  76. lar.AddRange(lCircleAR);
  77. Dictionary<string, string> _fileIdToName = new Dictionary<string, string>();
  78. Dictionary<string, string> _userIdToName = new Dictionary<string, string>();
  79. foreach (ActivityRow ar in lar)
  80. {
  81. switch ((CircleViewMessages)ar.MessageType)
  82. {
  83. case CircleViewMessages.FileEncrypted:
  84. {
  85. string fileId = ar.GetValue("File Id");
  86. if (!string.IsNullOrEmpty(fileId))
  87. {
  88. _fileIdToName[fileId] = ar.GetValue("Filename");
  89. }
  90. string viewerId = ar.GetValue("Sender Id");
  91. if (!string.IsNullOrEmpty(viewerId))
  92. {
  93. _userIdToName[viewerId] = ar.GetValue("Sender");
  94. }
  95. break;
  96. }
  97. case CircleViewMessages.FileViewed:
  98. {
  99. string fileId = ar.GetValue("File Id");
  100. if (!string.IsNullOrEmpty(fileId))
  101. {
  102. _fileIdToName[fileId] = ar.GetValue("Filename");
  103. }
  104. string viewerId = ar.GetValue("Viewer Id");
  105. if (!string.IsNullOrEmpty(viewerId))
  106. {
  107. _userIdToName[viewerId] = ar.GetValue("Viewer");
  108. }
  109. break;
  110. }
  111. default:
  112. break;
  113. }
  114. }
  115. List<ActivityRow> lCloudAR = await LoadCloudActivity();
  116. foreach (ActivityRow ar in lCloudAR)
  117. {
  118. string fileId = ar.GetValue("File Id");
  119. if (_fileIdToName.ContainsKey(fileId))
  120. {
  121. ar.AddValue("Filename*", _fileIdToName[fileId]);
  122. }
  123. string senderId = ar.GetValue("Sender Id");
  124. if (_userIdToName.ContainsKey(senderId))
  125. {
  126. ar.AddValue("Sender*", _userIdToName[senderId]);
  127. }
  128. }
  129. lar.AddRange(lCloudAR);
  130. sfgActivity.DataSource = lar.OrderBy(r => r.EventTime).ToList();
  131. }
  132. private void sfgActivity_CellDoubleClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs e)
  133. {
  134. ActivityRow ar = (ActivityRow)e.DataRow.RowData;
  135. ViewMetaDlg dlg = new ViewMetaDlg(ar);
  136. dlg.ShowDialog();
  137. }
  138. }
  139. }