示例#1
0
        public void TestRetrieveMessages()
        {
            Initialize();

            var logins = new string[, ] {
                { "One", "1" }, { "Two", "2" }, { "Three", "3" }, { "Four", "4" }
            };

            //Try for each login to check that group messaging works as intended
            for (int i = 0; i <= logins.GetUpperBound(0); i++)
            {
                var service = new ClientService();

                ListMessageResult messagesFull = service.ListMessages(new AuthenticatedData()
                {
                    Username = logins[i, 0],
                    Password = logins[i, 1]
                });

                Assert.AreEqual(3, messagesFull.Messages.Count);

                //Check again, should be no new messages
                ListMessageResult messagesEmpty = service.ListMessages(new AuthenticatedData()
                {
                    Username = logins[i, 0],
                    Password = logins[i, 1]
                });

                Assert.AreEqual(0, messagesEmpty.Messages.Count);
            }
        }
        public ListMessageResult ListMessages(AuthenticatedData data)
        {
            try
            {
                if (!data.Authenticate())
                {
                    return(null);
                }

                using (var db = new CSSDataContext())
                {
                    var messages = new List <IMessage>();
                    messages.AddRange(GroupMessage.GetGroupMessages(db, data.Username));
                    messages.AddRange(PersonalMessage.GetPrivateMessages(db, data.Username));
                    messages.AddRange(GroupMessage.GetGlobalMessages(db, data.Username));

                    //Re-insert messages into list since windows services
                    //don't seem to be able to handle objects wrapped in a baseclass.
                    //var result = messages
                    //    .OrderByDescending(p => p.DateToSend)
                    //    .Select(p => new BaseMessage()
                    //{
                    //    Id = p.Id,
                    //    Sender = p.Sender,
                    //    Subject = p.Subject,
                    //    Message = p.Message,
                    //    DateCreated = p.DateToSend,     //User doesn't actually have to know the date the message was created - only sent.
                    //    DateToSend = p.DateToSend,
                    //    DateExpires = p.DateExpires,
                    //}).ToList();

                    //return result;

                    ListMessageResult returnValue = new ListMessageResult();
                    returnValue.Messages = new List <ListMessageResult.ListMessage>();
                    messages.ForEach(p => returnValue.Messages.Add(new ListMessageResult.ListMessage()
                    {
                        DateCreated = p.DateCreated,
                        DateExpires = p.DateExpires,
                        DateToSend  = p.DateToSend,
                        Id          = p.Id,
                        Sender      = p.Sender,
                        Subject     = p.Subject,
                        Message     = p.Message
                    }));


                    return(returnValue);
                }
            }
            catch (Exception error)
            {
                Error.Write(error);
                throw;
            }
        }
        /// <summary>
        /// Retrieve messages from server if messages are not stored locally
        /// </summary>
        public static void RetrieveMessages(TaskDelegate onCompleteDelegate)
        {
            //Check the message datastore to see if messages have already been loaded
            var messages = Messages;

            if (messages == null)
            {
                messages = new List <BaseMessage>();
            }

            //If not, retrieve the message list from the server
            TaskHandler.RunTask(delegate(object data)
            {
                var parameters = data as object[];
                var signal     = parameters[0] as TaskDelegate;
                var dataStore  = parameters[1] as DataStore;
                var list       = parameters[2] as List <BaseMessage>;
                var newItems   = 0;

                try
                {
                    ListMessageResult listMessageResult = ServiceHandler.Service.ListMessages(new AuthenticatedData());
                    var insertionList = new List <BaseMessage>();
                    newItems          = listMessageResult.Messages.Length;

                    foreach (var message in listMessageResult.Messages)
                    {
                        insertionList.Add(new BaseMessage()
                        {
                            Id          = message.Id,
                            Subject     = message.Subject,
                            Message     = message.Message,
                            Sender      = message.Sender,
                            DateToSend  = message.DateToSend,
                            DateExpires = message.DateExpires,
                            DateCreated = message.DateCreated
                        });
                    }

                    list.InsertRange(0, insertionList);

                    //Store callsign list to datastore
                    dataStore[MessageKey] = list;
                    dataStore.Save();
                }
                catch (Exception error)
                {
                    Log.Write(error);
                }

                //Signal to the calling thread that the operation is complete
                signal(new object[] { list, newItems });
            }, onCompleteDelegate, MessageStore, messages);
        }