public static async Task LeaveUserMessage(string chatHubId, string userId, string peerId, string replyId, string message)
 {
     var cntx = Cntx;
     UserServiceProxy usvc = new UserServiceProxy();
     var u = await usvc.LoadEntityByKeyAsync(cntx, userId);
     var peer = await usvc.LoadEntityByKeyAsync(cntx, peerId);
     ShortMessageServiceProxy msvc = new ShortMessageServiceProxy();
     PeerShotMessage m = new PeerShotMessage();
     var now = DateTime.UtcNow;
     ShortMessage msg = new ShortMessage
     {
         ID = Guid.NewGuid().ToString(),
         ApplicationID = AppId,
         TypeID = 1,
         GroupID = null,
         FromID = userId,
         ToID = peerId,
         ReplyToID = string.IsNullOrEmpty(replyId) ? null : replyId,
         CreatedDate = now,
         LastModified = now,
         MsgText = message,
         IsNotReceived = true
     };
     await msvc.AddOrUpdateEntitiesAsync(cntx, new ShortMessageSet(), new ShortMessage[] { msg });
     UserAssociationServiceProxy uasvc = new UserAssociationServiceProxy();
     DateTime dt = DateTime.UtcNow;
     List<UserAssociation> lass = new List<UserAssociation>();
     UserAssociation utop = await uasvc.LoadEntityByKeyAsync(cntx, userId, peerId, ApplicationContext.ChatAssocTypeId);
     if (utop == null)
     {
         utop = new UserAssociation
         {
             TypeID = ApplicationContext.ChatAssocTypeId,
             FromUserID = userId,
             ToUserID = peerId,
             CreateDate = dt,
             AssocCount = 0,
             LastAssoc = dt,
             InteractCount = 1,
             Votes = 0
         };
     }
     else
         utop.InteractCount++;
     lass.Add(utop);
     if (!string.IsNullOrEmpty(replyId))
     {
         UserAssociation ptou = await uasvc.LoadEntityByKeyAsync(cntx, peerId, userId, ApplicationContext.ChatAssocTypeId);
         if (ptou == null)
         {
             ptou = new UserAssociation
             {
                 TypeID = ApplicationContext.ChatAssocTypeId,
                 FromUserID = peerId,
                 ToUserID = userId,
                 CreateDate = dt,
                 AssocCount = 0,
                 LastAssoc = dt,
                 InteractCount = 0,
                 Votes = 0
             };
         }
         else
             ptou.InteractCount++;
         lass.Add(ptou);
     }
     await uasvc.AddOrUpdateEntitiesAsync(cntx, new UserAssociationSet(), lass.ToArray());
 }
 public static async Task<PeerShotMessage> UpdateUserMessage(string chatHubId, string userId, string peerId, string msgId, string message)
 {
     var cntx = Cntx;
     UserServiceProxy usvc = new UserServiceProxy();
     var u = await usvc.LoadEntityByKeyAsync(cntx, userId);
     var peer = await usvc.LoadEntityByKeyAsync(cntx, peerId);
     ShortMessageServiceProxy msvc = new ShortMessageServiceProxy();
     PeerShotMessage m = new PeerShotMessage();
     ShortMessage msg = await msvc.LoadEntityByKeyAsync(cntx, msgId);
     if (msg == null || msg.FromID != userId || msg.ToID != peerId)
         return null;
     var now = DateTime.UtcNow;
     msg.MsgText = message;
     msg.LastModified = now;
     var r = await msvc.AddOrUpdateEntitiesAsync(cntx, new ShortMessageSet(), new ShortMessage[] { msg });
     var _msg = r.ChangedEntities[0].UpdatedItem;
     _msg.User_FromID = u;
     m.msg = GetJsonMessage(_msg, userId, peer, false);
     UserAssociationServiceProxy uasvc = new UserAssociationServiceProxy();
     var utop = await uasvc.LoadEntityByKeyAsync(cntx, userId, peerId, ApplicationContext.ChatAssocTypeId);
     if (utop != null)
     {
         utop.InteractCount = utop.InteractCount == null ? 1 : utop.InteractCount + 1;
         utop.LastInteract = DateTime.UtcNow;
         await uasvc.AddOrUpdateEntitiesAsync(cntx, new UserAssociationSet(), new UserAssociation[] { utop });
     }
     MembershipPlusServiceProxy svc = new MembershipPlusServiceProxy();
     DateTime dt = DateTime.UtcNow.AddMinutes(-ApplicationContext.OnlineUserInactiveTime);
     MemberCallbackServiceProxy mcbsvc = new MemberCallbackServiceProxy();
     var qexpr = new QueryExpresion();
     qexpr.OrderTks = new List<QToken>(new QToken[] { 
         new QToken { TkName = "UserID" },
         new QToken { TkName = "asc" }
     });
     qexpr.FilterTks = new List<QToken>(new QToken[] { 
         new QToken { TkName = "HubID == \"" + chatHubId + "\" && ChannelID == \"" + userId + "\" && ConnectionID is not null && IsDisconnected == false" },
         new QToken { TkName = "&&" },
         new QToken { TkName = "ApplicationID == \"" + AppId + "\" && UserID == \"" + peerId + "\"" },
         new QToken { TkName = "&&" },
         new QToken { TkName = "UserAppMemberRef.LastActivityDate > " + svc.FormatRepoDateTime(dt) }
     });
     MemberCallbackServiceProxy cbsv = new MemberCallbackServiceProxy();
     m.peer = (await cbsv.QueryDatabaseAsync(cntx, new MemberCallbackSet(), qexpr)).SingleOrDefault();
     return m;
 }