Ejemplo n.º 1
0
 public string GetAllChannels()
 {
     ServiceData sd = new ServiceData() { ResultType = "getAllChannels" };
     try
     {
         GetSession();
         //_sessionState.DeleteMessage();
         //_sessionState.DeleteChannels();
         if (AppCache.ChannelList.Count == 0)
             AppCache.ChannelList = _sessionState.GetChannels().ToList();
         if(sd.Result == null)
             sd.Result = new List<string>();
         if(AppCache.ChannelList.Count > 0)
             sd.Result.AddRange(AppCache.ChannelList.Select(c => c.Name));
     }
     catch (Exception ex)
     {
         sd.IsError = true;
         sd.ErrorMessage = ex.Message + Environment.NewLine + ex.StackTrace;
     }
     finally
     {
         SaveSession();
     }
     return JsonConvert.SerializeObject(sd);
 }
Ejemplo n.º 2
0
        public string DeletePost(string name, string password, string messageId)
        {
            ServiceData sd = new ServiceData() { ResultType = "deletePost" };
            try
            {
                GetSession();
                if (_sessionState.CurrentUser != null && _sessionState.CurrentUser.ChannelName.Contains(name))
                {
                    Channel chan = _sessionState.GetChannel(name);
                    if (chan != null)
                    {
                        Message msg = AppCache.MessageList.Where(m => m.Id == messageId).FirstOrDefault();
                        if (msg == null)
                            msg = _sessionState.GetMessages().Where(m => m.Id == messageId).FirstOrDefault();
                        if (msg != null)
                        {
                            _sessionState.DeleteMessage(msg);
                            PublishToPubNub(name, sd, true);
                        }
                    }
                }
                else
                {
                    Channel chan = _sessionState.GetChannel(name, password, pubnub);
                    if (chan != null)
                    {

                    }
                }
            }
            catch (Exception ex)
            {
                sd.IsError = true;
                sd.ErrorMessage = ex.Message;
            }
            finally
            {
                SaveSession();
            }

            return JsonConvert.SerializeObject(sd);
        }
Ejemplo n.º 3
0
 private void PublishToPubNub(string name, ServiceData sd, bool isRemove)
 {
     Message msg = sd.Result as Message;
     if (msg != null)
     {
         List<dynamic> pubnubMessage = new List<dynamic>();
         Dictionary<string, string> actionDict = new Dictionary<string, string>();
         if(isRemove)
             actionDict.Add("Action", "del");
         else
             actionDict.Add("Action", "add");
         actionDict.Add("Channel", name);
         pubnubMessage.Add(actionDict);
         msg.PrivateData = null;
         pubnubMessage.Add(msg);
         List<object> publishResult = pubnub.Publish("NewMsgIn" + name, pubnubMessage);
     }
 }
Ejemplo n.º 4
0
 public string RestoreSession()
 {
     ServiceData sd = new ServiceData() { ResultType = "restoreSession" };
     try
     {
         GetSession();
         if (_sessionState.CurrentUser != null && _sessionState.CurrentUser.ChannelName.Count > 0)
             _restoreSession.GetUserChannels = _sessionState.CurrentUser.ChannelName;
         sd.Result = _restoreSession;
     }
     catch (Exception ex)
     {
         sd.IsError = true;
         sd.ErrorMessage = ex.Message;
     }
     finally
     {
         SaveSession();
     }
     return JsonConvert.SerializeObject(sd);
 }
Ejemplo n.º 5
0
 public string PostText(string name, string password, string message)
 {
     ServiceData sd = new ServiceData() { ResultType = "postText" };
     try
     {
         GetSession();
         sd.Result = PostContent(name, password, message, MessageTypes.Text);
     }
     catch (Exception ex)
     {
         sd.IsError = true;
         sd.ErrorMessage = ex.Message;
     }
     finally
     {
         SaveSession();
     }
     PublishToPubNub(name, sd, false);
     return JsonConvert.SerializeObject(sd);
 }
Ejemplo n.º 6
0
        public string JoinCreateChannel(string name, string password, int numberOfMessages)
        {
            ServiceData sd = new ServiceData() { ResultType = "joinCreateChannel" };
            try
            {
                GetSession();
                Channel chan = _sessionState.GetChannel(name, password, pubnub);
                if (chan != null)
                {
                    if (_sessionState.CurrentUser == null)
                        _sessionState.CurrentUser = new User() { Id = Guid.NewGuid(), ChannelName = new List<string>() };
                    if (!_sessionState.CurrentUser.ChannelName.Contains(name))
                    {
                        _sessionState.CurrentUser.ChannelName.Add(name);
                    }

                    _sessionState.GetChanMessages(chan, numberOfMessages);
                    sd.Result = new List<dynamic>();
                    sd.Result.Add(chan);
                    sd.Result.Add(AppCache.MessageList.Where(m => m.ChannelId == chan.Id).OrderBy(m => m.PrivateDateTime).Take(numberOfMessages).ToList());
                    if (_restoreSession.LastChannel == null)
                        _restoreSession.LastChannel = new List<dynamic>();
                    _restoreSession.LastChannel = sd.Result;
                }
                else
                {
                    sd.IsError = true;
                    sd.ErrorMessage = "If create a new channel: channel exist." + Environment.NewLine + "If login to a channel: password not match.";
                }
            }
            catch (Exception ex)
            {
                sd.IsError = true;
                sd.ErrorMessage = ex.Message;
            }
            finally
            {
                SaveSession();
            }
            return JsonConvert.SerializeObject(sd);
        }
Ejemplo n.º 7
0
 public string GetUserChannel(string name)
 {
     ServiceData sd = new ServiceData() { ResultType = "getUserChannel" };
     try
     {
         GetSession();
         if (_sessionState.CurrentUser != null && _sessionState.CurrentUser.ChannelName.Contains(name))
         {
             Channel chan = _sessionState.GetChannel(name);
             if (chan != null)
             {
                 _sessionState.GetChanMessages(chan, 10);
                 sd.Result = new List<dynamic>();
                 sd.Result.Add(chan);
                 sd.Result.Add(AppCache.MessageList.Where(m => m.ChannelId == chan.Id).Take(10).ToList());
             }
         }
         else
         {
             sd.IsError = true;
             if (_sessionState.CurrentUser == null)
                 sd.ErrorMessage = "You are not login";
             else if(!_sessionState.CurrentUser.ChannelName.Contains(name))
                 sd.ErrorMessage = "Login to the channel please";
         }
         if (_restoreSession.LastChannel == null)
             _restoreSession.LastChannel = new List<dynamic>();
         _restoreSession.LastChannel = sd.Result;
     }
     catch (Exception ex)
     {
         sd.IsError = true;
         sd.ErrorMessage = ex.Message;
     }
     finally
     {
         SaveSession();
     }
     return JsonConvert.SerializeObject(sd);
 }