-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMsgChannelUtilities.cs
94 lines (87 loc) · 3.28 KB
/
MsgChannelUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using Common.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terrasoft.Core;
using Terrasoft.Messaging.Common;
namespace DevTraining.Files.cs
{
public static class MsgChannelUtilities
{
#region Constants
public const string ProcessEngineSenderName = "ProcessEngine";
[Obsolete("7.15.1 | Constant is not in use and will be removed in upcoming releases")]
public const string ProcessEngineBackHistoryStateSenderName = "ProcessEngineBackHistoryState";
#endregion
#region Fields
private static readonly ILog _log = LogManager.GetLogger("DevelopmentTrainingLogger");
#endregion
#region Methods : Private
private static bool CheckCanPostMessage(string userName, string senderName)
{
if (!MsgChannelManager.IsRunning)
{
_log.WarnFormat("Can't post message to {0} from {1} while MsgChannelManager is not running",
userName ?? "All", senderName);
return false;
}
return true;
}
private static void PostMessageInternal(IMsgChannel channel, string sender, string msg)
{
IMsg simpleMessage = new SimpleMessage()
{
Id = Guid.NewGuid(),
Body = msg
};
simpleMessage.Header.Sender = sender;
_log.Debug($"Channel {channel.Name} post for {sender} msg: {msg}");
channel.PostMessage(simpleMessage);
}
#endregion
#region Methods : Public
/// <summary>
/// Sends message to a specific user
/// </summary>
/// <param name="userConnection">userConnection</param>
/// <param name="senderName">Sender Name</param>
/// <param name="messageText">Message body</param>
public static void PostMessage(UserConnection userConnection, string senderName, string messageText)
{
if (!CheckCanPostMessage(userConnection.CurrentUser.Name, senderName))
{
return;
}
MsgChannelManager channelManager = MsgChannelManager.Instance;
IMsgChannel userChannel = channelManager.FindItemByUId(userConnection.CurrentUser.Id);
if (userChannel != null)
{
PostMessageInternal(userChannel, senderName, messageText);
}
else
{
_log.Info($"Channel not found for user {userConnection.CurrentUser.Name} from {senderName}");
}
}
/// <summary>
/// Sends message to all users
/// </summary>
/// <param name="senderName">Sender Name</param>
/// <param name="messageText">Message Body</param>
public static void PostMessageToAll(string senderName, string messageText)
{
if (!CheckCanPostMessage(null, senderName))
{
return;
}
MsgChannelManager channelManager = MsgChannelManager.Instance;
foreach (IMsgChannel userChannel in channelManager.Channels.Values)
{
PostMessageInternal(userChannel, senderName, messageText);
}
}
#endregion
}
}