Exemple #1
0
        public static async Task <string> LoadMessageDetails(string noteHubId, string groupId, string userId, string msgId)
        {
            var cntx = Cntx;
            var gsvc = new UserGroupServiceProxy();
            var svc  = new ShortMessageServiceProxy();
            var g    = await gsvc.LoadEntityByKeyAsync(cntx, groupId);

            var rmsg = await svc.LoadEntityByKeyAsync(cntx, msgId);

            while (rmsg.ReplyToID != null)
            {
                rmsg = await svc.LoadEntityByKeyAsync(cntx, rmsg.ReplyToID);
            }
            EntitySetType[] excludes = new EntitySetType[]
            {
                EntitySetType.UserGroup,
                EntitySetType.ShortMessageAudience
            };
            EntitySetRelation[] drills = new EntitySetRelation[]
            {
                new EntitySetRelation
                {
                    SetType     = EntitySetType.User,
                    RelatedSets = new EntitySetType[]
                    {
                        EntitySetType.UserAppMember,
                    }
                }
            };
            var msg = await svc.LoadEntityGraphRecursAsync(cntx, rmsg.ID, excludes, drills);

            var msgstr = GetJsonMessage(msg, userId, g, true, msgId);

            return(msgstr);
        }
        public static async Task <string[]> LoadMessages(string peerId, string userId, int maxMessages, bool dialog)
        {
            var svc    = new MembershipPlusServiceProxy();
            var usvc   = new UserServiceProxy();
            var msgsvc = new ShortMessageServiceProxy();
            var cntx   = Cntx;
            var peer   = await usvc.LoadEntityByKeyAsync(cntx, peerId);

            DateTime dt   = DateTime.UtcNow.AddMinutes(-InitMsgTimeWindow);
            var      cond = new ShortMessageSetConstraints
            {
                ApplicationIDWrap = new ForeignKeyData <string> {
                    KeyValue = AppId
                },
                TypeIDWrap = new ForeignKeyData <int> {
                    KeyValue = 1
                },
                GroupIDWrap = new ForeignKeyData <string> {
                    KeyValue = null
                }
            };
            var qexpr = new QueryExpresion();

            qexpr.OrderTks = new List <QToken>(new QToken[] {
                new QToken {
                    TkName = "CreatedDate"
                },
                new QToken {
                    TkName = "desc"
                }
            });
            //                ToIDWrap = new ForeignKeyData<string> { KeyValue = peerId },

            qexpr.FilterTks = new List <QToken>(new QToken[] {
                new QToken {
                    TkName = "( FromID ==  \"" + peerId + "\" && ToID == \"" + userId + "\" || FromID ==  \"" + userId + "\" && ToID == \"" + peerId + "\" ) && CreatedDate > " + svc.FormatRepoDateTime(dt)
                }
            });
            if (dialog)
            {
                qexpr.FilterTks.Add(new QToken {
                    TkName = " && ReplyToID is null"
                });
            }
            var           msgs     = (await msgsvc.ConstraintQueryLimitedAsync(cntx, new ShortMessageSet(), cond, qexpr, maxMessages)).ToArray();
            List <string> jsonMsgs = new List <string>();

            if (msgs.Length > 0)
            {
                for (int i = msgs.Length - 1; i >= 0; i--)
                {
                    EntitySetType[] excludes;
                    if (dialog)
                    {
                        excludes = new EntitySetType[]
                        {
                            EntitySetType.UserGroup,
                            //EntitySetType.ShortMessageAudience,
                            EntitySetType.ShortMessageAttachment
                        };
                    }
                    else
                    {
                        excludes = new EntitySetType[]
                        {
                            EntitySetType.UserGroup,
                            //EntitySetType.ShortMessageAudience,
                            EntitySetType.ShortMessageAttachment,
                            EntitySetType.ShortMessage
                        };
                    }
                    msgs[i] = await msgsvc.LoadEntityGraphRecursAsync(cntx, msgs[i].ID, excludes, null);

                    jsonMsgs.Add(GetJsonMessage(msgs[i], userId, peer, dialog));
                }
            }
            return(jsonMsgs.ToArray());
        }
 public static async Task<string[]> GetMessages(string noteHubId, string userId, string set, string qexpr, string prevlast)
 {
     JavaScriptSerializer jser = new JavaScriptSerializer();
     dynamic sobj = jser.DeserializeObject(set) as dynamic;
     DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(QueryExpresion));
     DataContractJsonSerializer ser2 = new DataContractJsonSerializer(typeof(ShortMessage));
     var ser3 = new JavaScriptSerializer();
     System.IO.MemoryStream strm = new System.IO.MemoryStream();
     byte[] sbf = System.Text.Encoding.UTF8.GetBytes(qexpr);
     strm.Write(sbf, 0, sbf.Length);
     strm.Position = 0;
     var _qexpr = ser1.ReadObject(strm) as QueryExpresion;
     var svc = new ShortMessageServiceProxy();
     var _set = new ShortMessageSet();
     _set.PageBlockSize = int.Parse(sobj["pageBlockSize"]);
     _set.PageSize_ = int.Parse(sobj["pageSize"]);
     if (!sobj.ContainsKey("setFilter"))
         throw new Exception("The page is not properly parameterized!");
     else
     {
         Func<string, string, int> count = (s, p) =>
         {
             int _cnt = 0;
             int i = 0;
             while ((i = s.IndexOf(p, i)) != -1)
             {
                 _cnt++;
                 i += p.Length;
             }
             return _cnt;
         };
         string filter = sobj["setFilter"];
         if (filter.EndsWith("&& ToID is not null && GroupID is null && ( ToID == \"{0}\" || FromID == \"{0}\" )") && count(filter, "||") == 1)
         {
             filter = string.Format(filter, userId);
             _set.SetFilter = filter;
         }
         else
             throw new Exception("The page is not properly parameterized!");
     }
     ShortMessage _prevlast = null;
     if (!string.IsNullOrEmpty(prevlast))
     {
         strm = new System.IO.MemoryStream();
         sbf = System.Text.Encoding.UTF8.GetBytes(prevlast);
         strm.Write(sbf, 0, sbf.Length);
         strm.Position = 0;
         _prevlast = ser2.ReadObject(strm) as ShortMessage;
     }
     var cntx = Cntx;
     var result = await svc.GetPageItemsAsync(cntx, _set, _qexpr, _prevlast);
     var ar = new List<string>();
     var udic = new Dictionary<string, User>();
     foreach (var e in result)
     {
         User u, p;
         ShortMessage msg;
         if (!udic.TryGetValue(e.FromID, out u) || !udic.TryGetValue(e.ToID, out p))
         {
             EntitySetType[] excludes = new EntitySetType[]
             {
                 EntitySetType.UserGroup,
                 EntitySetType.ShortMessageAudience,
                 EntitySetType.ShortMessageAttachment,
                 EntitySetType.ShortMessage
             };
             EntitySetRelation[] drills = new EntitySetRelation[]
             {
                 new EntitySetRelation
                 {
                     SetType = EntitySetType.User,
                     RelatedSets = new EntitySetType[] 
                     {
                         EntitySetType.UserAppMember,
                     }
                 }
             };
             msg = await svc.LoadEntityGraphRecursAsync(cntx, e.ID, excludes, drills);
             u = msg.User_FromID;
             p = msg.User_ToID;
             {
                 var member = (from d in u.ChangedUserAppMembers where d.ApplicationID == ApplicationContext.App.ID select d).SingleOrDefault();
                 u.ChangedUserAppMembers = new UserAppMember[] { member };
                 if (member.ChangedMemberCallbacks != null && member.ChangedMemberCallbacks.Length > 0)
                 {
                     var cbk = (from d in member.ChangedMemberCallbacks where d.HubID == noteHubId && d.ChannelID == "System" select d).SingleOrDefault();
                     if (cbk != null)
                         member.ChangedMemberCallbacks = new MemberCallback[] { cbk };
                     else
                         member.ChangedMemberCallbacks = new MemberCallback[] { };
                 }
             }
             {
                 var member = (from d in p.ChangedUserAppMembers where d.ApplicationID == ApplicationContext.App.ID select d).SingleOrDefault();
                 p.ChangedUserAppMembers = new UserAppMember[] { member };
                 if (member.ChangedMemberCallbacks != null && member.ChangedMemberCallbacks.Length > 0)
                 {
                     var cbk = (from d in member.ChangedMemberCallbacks where d.HubID == noteHubId && d.ChannelID == "System" select d).SingleOrDefault();
                     if (cbk != null)
                         member.ChangedMemberCallbacks = new MemberCallback[] { cbk };
                     else
                         member.ChangedMemberCallbacks = new MemberCallback[] { };
                 }
             }
             if (!udic.ContainsKey(e.FromID))
                 udic.Add(e.FromID, u);
             if (!udic.ContainsKey(e.ToID))
                 udic.Add(e.ToID, p);
         }
         else
         {
             msg = e;
             msg.User_FromID = u;
             msg.User_ToID = p;
         }
         ar.Add(GetJsonMessage(msg, userId, false));
     }
     return ar.ToArray();
 }
 public static async Task<string> LoadMessageDetails(string noteHubId, string userId, string msgId)
 {
     var cntx = Cntx;
     var gsvc = new UserGroupServiceProxy();
     var svc = new ShortMessageServiceProxy();
     var rmsg = await svc.LoadEntityByKeyAsync(cntx, msgId);
     var msg0 = rmsg;
     while (rmsg.ReplyToID != null)
         rmsg = await svc.LoadEntityByKeyAsync(cntx, rmsg.ReplyToID);
     EntitySetType[] excludes = new EntitySetType[]
             {
                 EntitySetType.UserGroup,
                 EntitySetType.ShortMessageAudience
             };
     EntitySetRelation[] drills = new EntitySetRelation[]
             {
                 new EntitySetRelation
                 {
                     SetType = EntitySetType.User,
                     RelatedSets = new EntitySetType[] 
                     {
                         EntitySetType.UserAppMember,
                     }
                 }
             };
     var msg = await svc.LoadEntityGraphRecursAsync(cntx, rmsg.ID, excludes, drills);
     var msgstr = GetJsonMessage(msg, userId, true, msgId);
     if (msg0.IsNotReceived == false)
     {
         msg0.IsNotReceived = false;
         await svc.AddOrUpdateEntitiesAsync(cntx, new ShortMessageSet(), new ShortMessage[] { msg0 });
     }
     return msgstr;
 }
 public static async Task<string[]> GetMessages(string noteHubId, string userId, string set, string qexpr, string prevlast)
 {
     JavaScriptSerializer jser = new JavaScriptSerializer();
     dynamic sobj = jser.DeserializeObject(set) as dynamic;
     DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(QueryExpresion));
     DataContractJsonSerializer ser2 = new DataContractJsonSerializer(typeof(ShortMessage));
     var ser3 = new JavaScriptSerializer();
     System.IO.MemoryStream strm = new System.IO.MemoryStream();
     byte[] sbf = System.Text.Encoding.UTF8.GetBytes(qexpr);
     strm.Write(sbf, 0, sbf.Length);
     strm.Position = 0;
     var _qexpr = ser1.ReadObject(strm) as QueryExpresion;
     var svc = new ShortMessageServiceProxy();
     var _set = new ShortMessageSet();
     _set.PageBlockSize = int.Parse(sobj["pageBlockSize"]);
     _set.PageSize_ = int.Parse(sobj["pageSize"]);
     Dictionary<string, UserGroup> groupDic = new Dictionary<string, UserGroup>();
     if (!sobj.ContainsKey("setFilter"))
         throw new Exception("The page is not properly parameterized!");
     else
     {
         Func<string, string, int> count = (s, p) =>
         {
             int _cnt = 0;
             int i = 0;
             while ((i = s.IndexOf(p, i)) != -1)
             {
                 _cnt++;
                 i += p.Length;
             }
             return _cnt;
         };
         string filter = sobj["setFilter"];
         if (filter.Contains("ToID is null") && filter.Contains("___usergroups___") && count(filter, "||") == 0)
         {
             var mbgrps = await UserGroupChatGroups(userId);
             if (mbgrps == null || mbgrps.Length == 0)
                 throw new Exception(ResourceUtils.GetString("234038e6185f013e25d0213c06f5a0e9", "You are not a member of any chat group."));
             string groupexpr = "";
             foreach (var g in mbgrps)
             {
                 groupexpr += (groupexpr != "" ? " || " : "") + "GroupID == \"" + g.ID + "\"";
                 groupDic.Add(g.ID, g);
             }
             _set.SetFilter = filter.Replace("___usergroups___", groupexpr);
         }
         else
             throw new Exception("The page is not properly parameterized!");
     }
     ShortMessage _prevlast = null;
     if (!string.IsNullOrEmpty(prevlast))
     {
         strm = new System.IO.MemoryStream();
         sbf = System.Text.Encoding.UTF8.GetBytes(prevlast);
         strm.Write(sbf, 0, sbf.Length);
         strm.Position = 0;
         _prevlast = ser2.ReadObject(strm) as ShortMessage;
     }
     var cntx = Cntx;
     var result = await svc.GetPageItemsAsync(cntx, _set, _qexpr, _prevlast);
     var ar = new List<string>();
     var udic = new Dictionary<string, User>();
     foreach (var e in result)
     {
         User u;
         ShortMessage msg;
         if (!udic.TryGetValue(e.FromID, out u))
         {
             EntitySetType[] excludes = new EntitySetType[]
             {
                 EntitySetType.UserGroup,
                 EntitySetType.ShortMessageAudience,
                 EntitySetType.ShortMessageAttachment,
                 EntitySetType.ShortMessage
             };
             EntitySetRelation[] drills = new EntitySetRelation[]
             {
                 new EntitySetRelation
                 {
                     SetType = EntitySetType.User,
                     RelatedSets = new EntitySetType[] 
                     {
                         EntitySetType.UserAppMember,
                     }
                 }
             };
             msg = await svc.LoadEntityGraphRecursAsync(cntx, e.ID, excludes, drills);
             u = msg.User_FromID;
             var member = (from d in u.ChangedUserAppMembers where d.ApplicationID == ApplicationContext.App.ID select d).SingleOrDefault();
             u.ChangedUserAppMembers = new UserAppMember[] { member };
             if (member.ChangedMemberCallbacks != null && member.ChangedMemberCallbacks.Length > 0)
             {
                 var cbk = (from d in member.ChangedMemberCallbacks where d.HubID == noteHubId && d.ChannelID == "System" select d).SingleOrDefault();
                 if (cbk != null)
                     member.ChangedMemberCallbacks = new MemberCallback[] { cbk };
                 else
                     member.ChangedMemberCallbacks = new MemberCallback[] { };
             }
             udic.Add(e.FromID, u);
         }
         else
         {
             msg = e;
             msg.User_FromID = u;
         }
         ar.Add(GetJsonMessage(msg, userId, groupDic[e.GroupID], false));
     }
     return ar.ToArray();
 }
        public static async Task<string[]> LoadMessages(string peerId, string userId, int maxMessages, bool dialog)
        {
            var svc = new MembershipPlusServiceProxy();
            var usvc = new UserServiceProxy();
            var msgsvc = new ShortMessageServiceProxy();
            var cntx = Cntx;
            var peer = await usvc.LoadEntityByKeyAsync(cntx, peerId);
            DateTime dt = DateTime.UtcNow.AddMinutes(-InitMsgTimeWindow);
            var cond = new ShortMessageSetConstraints
            {
                ApplicationIDWrap = new ForeignKeyData<string> { KeyValue = AppId },
                TypeIDWrap = new ForeignKeyData<int> { KeyValue = 1 },
                GroupIDWrap = new ForeignKeyData<string> { KeyValue = null }
            };
            var qexpr = new QueryExpresion();
            qexpr.OrderTks = new List<QToken>(new QToken[] { 
                new QToken { TkName = "CreatedDate" },
                new QToken { TkName = "desc" }
            });
            //                ToIDWrap = new ForeignKeyData<string> { KeyValue = peerId },

            qexpr.FilterTks = new List<QToken>(new QToken[] {
                new QToken { TkName = "( FromID ==  \"" + peerId + "\" && ToID == \"" + userId + "\" || FromID ==  \"" + userId + "\" && ToID == \"" + peerId + "\" ) && CreatedDate > " + svc.FormatRepoDateTime(dt) }
            });
            if (dialog)
            {
                qexpr.FilterTks.Add(new QToken { TkName = " && ReplyToID is null" });
            }
            var msgs = (await msgsvc.ConstraintQueryLimitedAsync(cntx, new ShortMessageSet(), cond, qexpr, maxMessages)).ToArray();
            List<string> jsonMsgs = new List<string>();
            if (msgs.Length > 0)
            {
                for (int i = msgs.Length - 1; i >= 0; i--)
                {
                    EntitySetType[] excludes;
                    if (dialog)
                    {
                        excludes = new EntitySetType[]
                                {
                                    EntitySetType.UserGroup,
                                    //EntitySetType.ShortMessageAudience,
                                    EntitySetType.ShortMessageAttachment
                                };
                    }
                    else
                    {
                        excludes = new EntitySetType[]
                                {
                                    EntitySetType.UserGroup,
                                    //EntitySetType.ShortMessageAudience,
                                    EntitySetType.ShortMessageAttachment,
                                    EntitySetType.ShortMessage
                                };
                    }
                    msgs[i] = await msgsvc.LoadEntityGraphRecursAsync(cntx, msgs[i].ID, excludes, null);
                    jsonMsgs.Add(GetJsonMessage(msgs[i], userId, peer, dialog));
                }
            }
            return jsonMsgs.ToArray();
        }
        public static async Task <ChatRoomVM> LoadChatRoom(string hubId, string groupId, string userId, int maxMessages, bool dialog = false)
        {
            UserGroupServiceProxy gsvc = new UserGroupServiceProxy();
            var        cntx            = Cntx;
            ChatRoomVM m = new ChatRoomVM()
            {
                RoomExists = false, DialogMode = dialog
            };

            if (!string.IsNullOrEmpty(groupId))
            {
                var g = await gsvc.LoadEntityByKeyAsync(cntx, groupId);

                if (g != null)
                {
                    m.RoomExists = true;
                    m.ID         = groupId;
                    m.RoomPath   = g.DistinctString.Split('/');
                    m.RoomInfo   = await gsvc.LoadEntityGroupDescriptionAsync(cntx, g.ID);

                    UserGroupMemberServiceProxy gmsvc = new UserGroupMemberServiceProxy();
                    UserGroupMember             uig   = await gmsvc.LoadEntityByKeyAsync(cntx, groupId, userId);

                    if (uig == null)
                    {
                        uig = new UserGroupMember
                        {
                            UserID               = userId,
                            UserGroupID          = groupId,
                            SubscribedTo         = false,
                            ActivityNotification = false
                        };
                        await gmsvc.AddOrUpdateEntitiesAsync(cntx, new UserGroupMemberSet(), new UserGroupMember[] { uig });
                    }
                    cntx.DirectDataAccess = true;
                    MemberCallbackServiceProxy cbsv = new MemberCallbackServiceProxy();
                    var           qexpr             = getConnectedGroupMemberFilter(hubId, g.ID);
                    var           peers             = (await cbsv.QueryDatabaseAsync(cntx, new MemberCallbackSet(), qexpr)).ToArray();
                    List <string> jsonPeers         = new List <string>();
                    if (peers.Length > 0)
                    {
                        for (int i = 0; i < peers.Length; i++)
                        {
                            // retrieve the related entity graph
                            peers[i] = await cbsv.LoadEntityGraphRecursAsync(cntx, groupId, hubId, AppId, peers[i].UserID, null, null);

                            jsonPeers.Add(GetJsonPeer(peers[i]));
                        }
                    }
                    m.ActivePeers         = jsonPeers.ToArray();
                    cntx.DirectDataAccess = false;
                    ShortMessageServiceProxy msgsvc = new ShortMessageServiceProxy();
                    var cond = new ShortMessageSetConstraints
                    {
                        ApplicationIDWrap = new ForeignKeyData <string> {
                            KeyValue = AppId
                        },
                        TypeIDWrap = new ForeignKeyData <int> {
                            KeyValue = ApplicationContext.ChatShortMsgTypeId
                        },
                        GroupIDWrap = new ForeignKeyData <string> {
                            KeyValue = groupId
                        }
                    };
                    UserGroupMemberServiceProxy uigsvc = new UserGroupMemberServiceProxy();
                    qexpr          = new QueryExpresion();
                    qexpr.OrderTks = new List <QToken>(new QToken[] {
                        new QToken {
                            TkName = "UserID"
                        }
                    });
                    qexpr.FilterTks = new List <QToken>(new QToken[] {
                        new QToken {
                            TkName = "UserGroupID == \"" + g.ID + "\" && SubscribedTo is not null && SubscribedTo == true"
                        }
                    });
                    m.Subscribers = (int)(await uigsvc.QueryEntityCountAsync(cntx, new UserGroupMemberSet(), qexpr));
                    var      svc = new MembershipPlusServiceProxy();
                    DateTime dt  = DateTime.UtcNow.AddMinutes(-InitMsgTimeWindow);
                    qexpr          = new QueryExpresion();
                    qexpr.OrderTks = new List <QToken>(new QToken[] {
                        new QToken {
                            TkName = "CreatedDate"
                        },
                        new QToken {
                            TkName = "desc"
                        }
                    });
                    qexpr.FilterTks = new List <QToken>(new QToken[] {
                        new QToken {
                            TkName = "ToID is null && CreatedDate > " + svc.FormatRepoDateTime(dt)
                        }
                    });
                    if (dialog)
                    {
                        qexpr.FilterTks.Add(new QToken {
                            TkName = " && ReplyToID is null"
                        });
                    }
                    var           msgs     = (await msgsvc.ConstraintQueryLimitedAsync(cntx, new ShortMessageSet(), cond, qexpr, maxMessages)).ToArray();
                    List <string> jsonMsgs = new List <string>();
                    if (msgs.Length > 0)
                    {
                        for (int i = msgs.Length - 1; i >= 0; i--)
                        {
                            EntitySetType[] excludes;
                            if (dialog)
                            {
                                excludes = new EntitySetType[]
                                {
                                    EntitySetType.UserGroup,
                                    //EntitySetType.ShortMessageAudience,
                                    EntitySetType.ShortMessageAttachment
                                };
                            }
                            else
                            {
                                excludes = new EntitySetType[]
                                {
                                    EntitySetType.UserGroup,
                                    //EntitySetType.ShortMessageAudience,
                                    EntitySetType.ShortMessageAttachment,
                                    EntitySetType.ShortMessage
                                };
                            }
                            msgs[i] = await msgsvc.LoadEntityGraphRecursAsync(cntx, msgs[i].ID, excludes, null);

                            jsonMsgs.Add(GetJsonMessage(msgs[i], userId, g, dialog));
                        }
                    }
                    m.RecentMsgs = jsonMsgs.ToArray();
                }
            }
            return(m);
        }
Exemple #8
0
        public static async Task <string[]> GetMessages(string noteHubId, string userId, string set, string qexpr, string prevlast)
        {
            JavaScriptSerializer jser = new JavaScriptSerializer();
            dynamic sobj = jser.DeserializeObject(set) as dynamic;
            DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(QueryExpresion));
            DataContractJsonSerializer ser2 = new DataContractJsonSerializer(typeof(ShortMessage));
            var ser3 = new JavaScriptSerializer();

            System.IO.MemoryStream strm = new System.IO.MemoryStream();
            byte[] sbf = System.Text.Encoding.UTF8.GetBytes(qexpr);
            strm.Write(sbf, 0, sbf.Length);
            strm.Position = 0;
            var _qexpr = ser1.ReadObject(strm) as QueryExpresion;
            var svc    = new ShortMessageServiceProxy();
            var _set   = new ShortMessageSet();

            _set.PageBlockSize = int.Parse(sobj["pageBlockSize"]);
            _set.PageSize_     = int.Parse(sobj["pageSize"]);
            if (!sobj.ContainsKey("setFilter"))
            {
                throw new Exception("The page is not properly parameterized!");
            }
            else
            {
                Func <string, string, int> count = (s, p) =>
                {
                    int _cnt = 0;
                    int i    = 0;
                    while ((i = s.IndexOf(p, i)) != -1)
                    {
                        _cnt++;
                        i += p.Length;
                    }
                    return(_cnt);
                };
                string filter = sobj["setFilter"];
                if (filter.EndsWith("&& ToID is not null && GroupID is null && ( ToID == \"{0}\" || FromID == \"{0}\" )") && count(filter, "||") == 1)
                {
                    filter         = string.Format(filter, userId);
                    _set.SetFilter = filter;
                }
                else
                {
                    throw new Exception("The page is not properly parameterized!");
                }
            }
            ShortMessage _prevlast = null;

            if (!string.IsNullOrEmpty(prevlast))
            {
                strm = new System.IO.MemoryStream();
                sbf  = System.Text.Encoding.UTF8.GetBytes(prevlast);
                strm.Write(sbf, 0, sbf.Length);
                strm.Position = 0;
                _prevlast     = ser2.ReadObject(strm) as ShortMessage;
            }
            var cntx   = Cntx;
            var result = await svc.GetPageItemsAsync(cntx, _set, _qexpr, _prevlast);

            var ar   = new List <string>();
            var udic = new Dictionary <string, User>();

            foreach (var e in result)
            {
                User         u, p;
                ShortMessage msg;
                if (!udic.TryGetValue(e.FromID, out u) || !udic.TryGetValue(e.ToID, out p))
                {
                    EntitySetType[] excludes = new EntitySetType[]
                    {
                        EntitySetType.UserGroup,
                        EntitySetType.ShortMessageAudience,
                        EntitySetType.ShortMessageAttachment,
                        EntitySetType.ShortMessage
                    };
                    EntitySetRelation[] drills = new EntitySetRelation[]
                    {
                        new EntitySetRelation
                        {
                            SetType     = EntitySetType.User,
                            RelatedSets = new EntitySetType[]
                            {
                                EntitySetType.UserAppMember,
                            }
                        }
                    };
                    msg = await svc.LoadEntityGraphRecursAsync(cntx, e.ID, excludes, drills);

                    u = msg.User_FromID;
                    p = msg.User_ToID;
                    {
                        var member = (from d in u.ChangedUserAppMembers where d.ApplicationID == ApplicationContext.App.ID select d).SingleOrDefault();
                        u.ChangedUserAppMembers = new UserAppMember[] { member };
                        if (member.ChangedMemberCallbacks != null && member.ChangedMemberCallbacks.Length > 0)
                        {
                            var cbk = (from d in member.ChangedMemberCallbacks where d.HubID == noteHubId && d.ChannelID == "System" select d).SingleOrDefault();
                            if (cbk != null)
                            {
                                member.ChangedMemberCallbacks = new MemberCallback[] { cbk }
                            }
                            ;
                            else
                            {
                                member.ChangedMemberCallbacks = new MemberCallback[] { }
                            };
                        }
                    }
                    {
                        var member = (from d in p.ChangedUserAppMembers where d.ApplicationID == ApplicationContext.App.ID select d).SingleOrDefault();
                        p.ChangedUserAppMembers = new UserAppMember[] { member };
                        if (member.ChangedMemberCallbacks != null && member.ChangedMemberCallbacks.Length > 0)
                        {
                            var cbk = (from d in member.ChangedMemberCallbacks where d.HubID == noteHubId && d.ChannelID == "System" select d).SingleOrDefault();
                            if (cbk != null)
                            {
                                member.ChangedMemberCallbacks = new MemberCallback[] { cbk }
                            }
                            ;
                            else
                            {
                                member.ChangedMemberCallbacks = new MemberCallback[] { }
                            };
                        }
                    }
                    if (!udic.ContainsKey(e.FromID))
                    {
                        udic.Add(e.FromID, u);
                    }
                    if (!udic.ContainsKey(e.ToID))
                    {
                        udic.Add(e.ToID, p);
                    }
                }
                else
                {
                    msg             = e;
                    msg.User_FromID = u;
                    msg.User_ToID   = p;
                }
                ar.Add(GetJsonMessage(msg, userId, false));
            }
            return(ar.ToArray());
        }
Exemple #9
0
        public static async Task <string[]> GetMessages(string noteHubId, string userId, string set, string qexpr, string prevlast)
        {
            JavaScriptSerializer jser = new JavaScriptSerializer();
            dynamic sobj = jser.DeserializeObject(set) as dynamic;
            DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(QueryExpresion));
            DataContractJsonSerializer ser2 = new DataContractJsonSerializer(typeof(ShortMessage));
            var ser3 = new JavaScriptSerializer();

            System.IO.MemoryStream strm = new System.IO.MemoryStream();
            byte[] sbf = System.Text.Encoding.UTF8.GetBytes(qexpr);
            strm.Write(sbf, 0, sbf.Length);
            strm.Position = 0;
            var _qexpr = ser1.ReadObject(strm) as QueryExpresion;
            var svc    = new ShortMessageServiceProxy();
            var _set   = new ShortMessageSet();

            _set.PageBlockSize = int.Parse(sobj["pageBlockSize"]);
            _set.PageSize_     = int.Parse(sobj["pageSize"]);
            Dictionary <string, UserGroup> groupDic = new Dictionary <string, UserGroup>();

            if (!sobj.ContainsKey("setFilter"))
            {
                throw new Exception("The page is not properly parameterized!");
            }
            else
            {
                Func <string, string, int> count = (s, p) =>
                {
                    int _cnt = 0;
                    int i    = 0;
                    while ((i = s.IndexOf(p, i)) != -1)
                    {
                        _cnt++;
                        i += p.Length;
                    }
                    return(_cnt);
                };
                string filter = sobj["setFilter"];
                if (filter.Contains("ToID is null") && filter.Contains("___usergroups___") && count(filter, "||") == 0)
                {
                    var mbgrps = await UserGroupChatGroups(userId);

                    if (mbgrps == null || mbgrps.Length == 0)
                    {
                        throw new Exception(ResourceUtils.GetString("234038e6185f013e25d0213c06f5a0e9", "You are not a member of any chat group."));
                    }
                    string groupexpr = "";
                    foreach (var g in mbgrps)
                    {
                        groupexpr += (groupexpr != "" ? " || " : "") + "GroupID == \"" + g.ID + "\"";
                        groupDic.Add(g.ID, g);
                    }
                    _set.SetFilter = filter.Replace("___usergroups___", groupexpr);
                }
                else
                {
                    throw new Exception("The page is not properly parameterized!");
                }
            }
            ShortMessage _prevlast = null;

            if (!string.IsNullOrEmpty(prevlast))
            {
                strm = new System.IO.MemoryStream();
                sbf  = System.Text.Encoding.UTF8.GetBytes(prevlast);
                strm.Write(sbf, 0, sbf.Length);
                strm.Position = 0;
                _prevlast     = ser2.ReadObject(strm) as ShortMessage;
            }
            var cntx   = Cntx;
            var result = await svc.GetPageItemsAsync(cntx, _set, _qexpr, _prevlast);

            var ar   = new List <string>();
            var udic = new Dictionary <string, User>();

            foreach (var e in result)
            {
                User         u;
                ShortMessage msg;
                if (!udic.TryGetValue(e.FromID, out u))
                {
                    EntitySetType[] excludes = new EntitySetType[]
                    {
                        EntitySetType.UserGroup,
                        EntitySetType.ShortMessageAudience,
                        EntitySetType.ShortMessageAttachment,
                        EntitySetType.ShortMessage
                    };
                    EntitySetRelation[] drills = new EntitySetRelation[]
                    {
                        new EntitySetRelation
                        {
                            SetType     = EntitySetType.User,
                            RelatedSets = new EntitySetType[]
                            {
                                EntitySetType.UserAppMember,
                            }
                        }
                    };
                    msg = await svc.LoadEntityGraphRecursAsync(cntx, e.ID, excludes, drills);

                    u = msg.User_FromID;
                    var member = (from d in u.ChangedUserAppMembers where d.ApplicationID == ApplicationContext.App.ID select d).SingleOrDefault();
                    u.ChangedUserAppMembers = new UserAppMember[] { member };
                    if (member.ChangedMemberCallbacks != null && member.ChangedMemberCallbacks.Length > 0)
                    {
                        var cbk = (from d in member.ChangedMemberCallbacks where d.HubID == noteHubId && d.ChannelID == "System" select d).SingleOrDefault();
                        if (cbk != null)
                        {
                            member.ChangedMemberCallbacks = new MemberCallback[] { cbk }
                        }
                        ;
                        else
                        {
                            member.ChangedMemberCallbacks = new MemberCallback[] { }
                        };
                    }
                    udic.Add(e.FromID, u);
                }
                else
                {
                    msg             = e;
                    msg.User_FromID = u;
                }
                ar.Add(GetJsonMessage(msg, userId, groupDic[e.GroupID], false));
            }
            return(ar.ToArray());
        }
 public static async Task<ChatRoomVM> LoadChatRoom(string hubId, string groupId, string userId, int maxMessages, bool dialog = false)
 {
     UserGroupServiceProxy gsvc = new UserGroupServiceProxy();
     var cntx = Cntx;
     ChatRoomVM m = new ChatRoomVM() { RoomExists = false, DialogMode = dialog };
     if (!string.IsNullOrEmpty(groupId))
     {
         var g = await gsvc.LoadEntityByKeyAsync(cntx, groupId);
         if (g != null)
         {
             m.RoomExists = true;
             m.ID = groupId;
             m.RoomPath = g.DistinctString.Split('/');
             m.RoomInfo = await gsvc.LoadEntityGroupDescriptionAsync(cntx, g.ID);
             UserGroupMemberServiceProxy gmsvc = new UserGroupMemberServiceProxy();
             UserGroupMember uig = await gmsvc.LoadEntityByKeyAsync(cntx, groupId, userId);
             if (uig == null)
             {
                 uig = new UserGroupMember
                 {
                     UserID = userId,
                     UserGroupID = groupId,
                     SubscribedTo = false,
                     ActivityNotification = false
                 };
                 await gmsvc.AddOrUpdateEntitiesAsync(cntx, new UserGroupMemberSet(), new UserGroupMember[] { uig });
             }
             cntx.DirectDataAccess = true;
             MemberCallbackServiceProxy cbsv = new MemberCallbackServiceProxy();
             var qexpr = getConnectedGroupMemberFilter(hubId, g.ID);
             var peers = (await cbsv.QueryDatabaseAsync(cntx, new MemberCallbackSet(), qexpr)).ToArray();
             List<string> jsonPeers = new List<string>();
             if (peers.Length > 0)
             {
                 for (int i = 0; i < peers.Length; i++)
                 {
                     // retrieve the related entity graph
                     peers[i] = await cbsv.LoadEntityGraphRecursAsync(cntx, groupId, hubId, AppId, peers[i].UserID, null, null);
                     jsonPeers.Add(GetJsonPeer(peers[i]));
                 }
             }
             m.ActivePeers = jsonPeers.ToArray();
             cntx.DirectDataAccess = false;
             ShortMessageServiceProxy msgsvc = new ShortMessageServiceProxy();
             var cond = new ShortMessageSetConstraints
             {
                 ApplicationIDWrap = new ForeignKeyData<string> { KeyValue = AppId },
                 TypeIDWrap = new ForeignKeyData<int> { KeyValue = ApplicationContext.ChatShortMsgTypeId },
                 GroupIDWrap = new ForeignKeyData<string> { KeyValue = groupId }
             };
             UserGroupMemberServiceProxy uigsvc = new UserGroupMemberServiceProxy();
             qexpr = new QueryExpresion();
             qexpr.OrderTks = new List<QToken>(new QToken[] { 
                 new QToken { TkName = "UserID" }
             });
             qexpr.FilterTks = new List<QToken>(new QToken[] { 
                 new QToken { TkName = "UserGroupID == \"" + g.ID + "\" && SubscribedTo is not null && SubscribedTo == true"  }
             });
             m.Subscribers = (int)(await uigsvc.QueryEntityCountAsync(cntx, new UserGroupMemberSet(), qexpr));
             var svc = new MembershipPlusServiceProxy();
             DateTime dt = DateTime.UtcNow.AddMinutes(-InitMsgTimeWindow);
             qexpr = new QueryExpresion();
             qexpr.OrderTks = new List<QToken>(new QToken[] { 
                 new QToken { TkName = "CreatedDate" },
                 new QToken { TkName = "desc" }
             });
             qexpr.FilterTks = new List<QToken>(new QToken[] {
                 new QToken { TkName = "ToID is null && CreatedDate > " + svc.FormatRepoDateTime(dt) }
             });
             if (dialog)
             {
                 qexpr.FilterTks.Add(new QToken { TkName = " && ReplyToID is null" });
             }
             var msgs = (await msgsvc.ConstraintQueryLimitedAsync(cntx, new ShortMessageSet(), cond, qexpr, maxMessages)).ToArray();
             List<string> jsonMsgs = new List<string>();
             if (msgs.Length > 0)
             {
                 for (int i = msgs.Length - 1; i >= 0; i--)
                 {
                     EntitySetType[] excludes;
                     if (dialog)
                     {
                         excludes = new EntitySetType[]
                         {
                             EntitySetType.UserGroup,
                             //EntitySetType.ShortMessageAudience,
                             EntitySetType.ShortMessageAttachment
                         };
                     }
                     else
                     {
                         excludes = new EntitySetType[]
                         {
                             EntitySetType.UserGroup,
                             //EntitySetType.ShortMessageAudience,
                             EntitySetType.ShortMessageAttachment,
                             EntitySetType.ShortMessage
                         };
                     }
                     msgs[i] = await msgsvc.LoadEntityGraphRecursAsync(cntx, msgs[i].ID, excludes, null);
                     jsonMsgs.Add(GetJsonMessage(msgs[i], userId, g, dialog));
                 }
             }
             m.RecentMsgs = jsonMsgs.ToArray();
         }
     }
     return m;
 }