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.

154 lines
5.5 KiB

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