Exemplo n.º 1
0
        public ChatMessageListResult StartConversation(int SenderID)
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Cache-Control", "no-cache");
            ChatMessageListResult result = null;

            try
            {
                if (SPUtility.ValidateFormDigest())
                {
                    IChatMessageRepository repo = new ChatMessageRepository();
                    // get the users timezone
                    SPTimeZone zone = SPContext.Current.Web.RegionalSettings.TimeZone;
                    if (SPContext.Current.Web.CurrentUser.RegionalSettings != null)
                    {
                        SPRegionalSettings rs = SPContext.Current.Web.CurrentUser.RegionalSettings;
                        zone = rs.TimeZone;
                    }

                    var newItems = repo.GetUnReadByUserIDAndSenderID(SPContext.Current.Web.CurrentUser.ID, SenderID)
                                   .Select(m => new ChatMessageServiceView()
                    {
                        ID              = m.ID,
                        Created         = (zone.UTCToLocalTime(m.Created)).ToString(),
                        CreatedDateOnly = (zone.UTCToLocalTime(m.Created).Date).ToShortDateString(),
                        CreatedTimeOnly = (zone.UTCToLocalTime(m.Created)).ToString("HH:mm"),
                        CreatedBy       = m.CreatedBy.Name,
                        Message         = m.Message,
                        IsOld           = false,
                        Receivers       = m.Receivers.Select(r => new ChatContactServiceView()
                        {
                            ID       = r.ID,
                            Name     = r.Name,
                            Username = r.Username.Split('\\').Last()
                        }).ToArray()
                    }).ToArray();

                    var someOldItems = repo.GetLastByUserIDAndSenderID(SPContext.Current.Web.CurrentUser.ID, SenderID, 3)
                                       .Select(m => new ChatMessageServiceView()
                    {
                        ID              = m.ID,
                        Created         = (zone.UTCToLocalTime(m.Created)).ToString(),
                        CreatedDateOnly = (zone.UTCToLocalTime(m.Created).Date).ToShortDateString(),
                        CreatedTimeOnly = (zone.UTCToLocalTime(m.Created)).ToString("HH:mm"),
                        CreatedBy       = m.CreatedBy.Name,
                        Message         = m.Message,
                        IsOld           = true,
                        Receivers       = m.Receivers.Select(r => new ChatContactServiceView()
                        {
                            ID       = r.ID,
                            Name     = r.Name,
                            Username = r.Username.Split('\\').Last()
                        }).ToArray()
                    }).ToArray();

                    result = new ChatMessageListResult()
                    {
                        LastRequested = DateTime.Now.ToString(),
                    };
                    if (newItems.Length > 0)
                    {
                        repo.SetChatMessagesRead(newItems.Select(i => i.ID).ToArray());
                    }
                    result.ChatMessages = someOldItems.Concat(newItems).ToArray();
                }
                else
                {
                    WebOperationContext.Current.OutgoingResponse.StatusCode        = System.Net.HttpStatusCode.Unauthorized;
                    WebOperationContext.Current.OutgoingResponse.StatusDescription = Language.UserNotValidated;
                }
            }
            catch (Exception ex)
            {
                Config.WriteException(ex);
                WebOperationContext.Current.OutgoingResponse.StatusCode        = System.Net.HttpStatusCode.InternalServerError;
                WebOperationContext.Current.OutgoingResponse.StatusDescription = Language.GetMessagesError;
            }
            return(result);
        }