Ejemplo n.º 1
0
        /// <summary>
        /// API to get the list of messages from ids
        /// </summary>
        /// <param name="Array of message ids"></param>
        /// <returns>Array of messages.</returns>
        public MessageLayer.Message GetFullMessageForMessageId(Guid conversationId, Guid messageId)
        {
            DAL.User user = this.GetAuthenticatedUser();

            if (user == null)
            {
                throw new Exception("User not authenticated");
            }

            MessageLayer.Message message = MessageStore.MessageStore.Instance.GetMessage(conversationId, messageId);

            return(message);
        }
Ejemplo n.º 2
0
        public DAL.Group CreateGroup(DAL.Group newGroup)
        {
            DAL.User sender = this.GetAuthenticatedUser();

            if (newGroup.Members == null)
            {
                throw new Exception("Invalid member list");
            }

            Dictionary <int, DAL.User> uniqueUsers = new Dictionary <int, DAL.User>();

            for (int i = 0; i < newGroup.Members.Count; i++)
            {
                if (string.Compare(DAL.UserService.Instance.GetUserFromId(newGroup.Members[i].Id).PhoneNumber, newGroup.Members[i].PhoneNumber, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    throw new Exception("Invalid user specified");
                }

                uniqueUsers.Add(newGroup.Members[i].Id, newGroup.Members[i]);
            }

            if (!uniqueUsers.ContainsKey(sender.Id))
            {
                uniqueUsers.Add(sender.Id, sender);
            }

            DAL.Group createdGroup = DAL.Group.CreateGroup(sender, newGroup.Name, uniqueUsers.Values.ToList());

            MessageLayer.Message newGroupMessage = new MessageLayer.Message()
            {
                SenderId = sender.Id, RecipientId = createdGroup.Id, MessageFlags = MessageLayer.MessageFlags.GroupCreatedMessage, TextMessage = sender.Name + " has created the group"
            };

            MessageStore.MessageStore.Instance.SaveMessage(sender, newGroupMessage);

            if (newGroupMessage.RecipientId != sender.Id)
            {
                this.SendPushNotifications(
                    DAL.Subscription.GetSubscriptions(newGroupMessage.RecipientId, sender),
                    newGroupMessage.MessageId,
                    newGroupMessage.ConversationId,
                    sender,
                    newGroupMessage.PostDateTimeUtcTicks,
                    newGroupMessage.TextMessage,
                    createdGroup,
                    newGroupMessage.IsEncrypted);
            }
            return(createdGroup);
        }
Ejemplo n.º 3
0
        public static Message ConvertToMessage(ITable table, ITableRow tableRow)
        {
            Message message = null;
            if (table.BlobValueColumn != null)
            {
                byte[] value = (byte[])tableRow.ColumnValues[table.BlobValueColumn];
                message = MessageSerializer.DeSerializeFromProtocolBuffer(value);
            }
            else
            {
                message = new Message();
            }

            DataContractToTableRowConverter.SetYapperColumnValues(message, tableRow);

            return message;
        }
Ejemplo n.º 4
0
        public void TestQueryRows()
        {
            Message message1 = new Message();
            message1.SenderId = 1;
            message1.Sender = new DataAccessLayer.User() { Id = message1.SenderId, Name = "TestSender" + DateTime.UtcNow.Ticks.ToString(), PhoneNumber = "+1 425-111-2222" };
            message1.RecipientId = 7;
            message1.Recipient = new DataAccessLayer.User() { Id = message1.RecipientId, Name = "TestRecipient" + DateTime.UtcNow.Ticks.ToString(), PhoneNumber = "+1 425-111-2222" }; message1.ConversationId = GetGuid(1, 7);
            message1.MessageId = Guid.NewGuid();
            message1.LastUpdateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message1.PostDateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message1.TextMessage = "This is a test Msg" + DateTime.UtcNow.Ticks.ToString();
            message1.MessageBlobName = Guid.NewGuid();
            message1.MessageFlags = 0;

            Message message2 = new Message();
            message2.SenderId = 1;
            message2.RecipientId = 8;
            message2.Sender = new DataAccessLayer.User() { Id = message2.SenderId, Name = "TestSender" + DateTime.UtcNow.Ticks.ToString(), PhoneNumber = "+1 425-111-2222" };
            message2.Recipient = new DataAccessLayer.User() { Id = message2.RecipientId, Name = "TestRecipient" + DateTime.UtcNow.Ticks.ToString(), PhoneNumber = "+1 425-111-2222" }; message1.ConversationId = GetGuid(1, 7);
            message2.ConversationId = GetGuid(1, 7);
            message2.MessageId = Guid.NewGuid();
            message2.LastUpdateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message2.PostDateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message2.TextMessage = "This is a test Msg" + DateTime.UtcNow.Ticks.ToString();
            message2.MessageBlobName = Guid.NewGuid();
            message2.MessageFlags = 0;

            Message message3 = new Message();
            message3.SenderId = 1;
            message3.RecipientId = 8;
            message3.Sender = new DataAccessLayer.User() { Id = message3.SenderId, Name = "TestSender" + DateTime.UtcNow.Ticks.ToString(), PhoneNumber = "+1 425-111-2222" };
            message3.Recipient = new DataAccessLayer.User() { Id = message3.RecipientId, Name = "TestRecipient" + DateTime.UtcNow.Ticks.ToString(), PhoneNumber = "+1 425-111-2222" }; message1.ConversationId = GetGuid(1, 7);
            message3.ConversationId = GetGuid(1, 7);
            message3.MessageId = Guid.NewGuid();
            message3.LastUpdateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message3.PostDateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message3.TextMessage = "This is a test Msg" + DateTime.UtcNow.Ticks.ToString();
            message3.MessageBlobName = Guid.NewGuid();
            message3.MessageFlags = 0;

            MessageStore.MessageStore.Instance.SaveMessage(message1.Sender, message1);
            MessageStore.MessageStore.Instance.SaveMessage(message2.Sender, message2);
            MessageStore.MessageStore.Instance.SaveMessage(message3.Sender, message3);

            List<Message> queriedMessages = MessageStore.MessageStore.Instance.GetAllMessagesForUser(new DataAccessLayer.User(1, "+1 425-111-9999", "Test User", null), null);
        }
Ejemplo n.º 5
0
        public void TestMessageSave()
        {
            Message message = new Message();
            message.SenderId = 1;
            message.Sender = new DataAccessLayer.User() { Id = message.SenderId, Name = "TestSender" + DateTime.UtcNow.Ticks.ToString(), PhoneNumber = "+1 425-111-2222" };
            message.RecipientId = 7;
            message.Recipient = new DataAccessLayer.User() { Id = message.RecipientId, Name = "TestRecipient" + DateTime.UtcNow.Ticks.ToString(), PhoneNumber = "+1 425-111-2222" };
            message.ConversationId = GetGuid(1, 7);
            message.MessageId = Guid.NewGuid();
            message.LastUpdateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message.PostDateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message.TextMessage = "This is a test Msg" + DateTime.UtcNow.Ticks.ToString();
            message.MessageBlobName = Guid.NewGuid();
            message.MessageFlags = 0;

            MessageStore.MessageStore.Instance.SaveMessage(message.Sender, message);
        }
Ejemplo n.º 6
0
        public bool AddUserToGroup(int groupId, string userPhone)
        {
            DAL.User sender = this.GetAuthenticatedUser();

            DAL.Group group = (DAL.Group)DAL.UserService.Instance.GetUserFromId(groupId);

            DAL.User member = DAL.UserService.Instance.GetUserFromPhone(userPhone);

            bool success = DAL.Group.AddGroupMember(sender, group, member);

            if (!success)
            {
                return(false);
            }

            // Read the group again
            group = (DAL.Group)DAL.UserService.Instance.GetUserFromId(groupId);

            MessageLayer.Message newGroupMessage = new MessageLayer.Message()
            {
                SenderId = sender.Id, RecipientId = group.Id, MessageFlags = MessageLayer.MessageFlags.GroupJoinMessage, TextMessage = sender.Name + " has added " + member.Name + " to the group"
            };

            MessageStore.MessageStore.Instance.SaveMessage(sender, newGroupMessage);

            if (newGroupMessage.RecipientId != sender.Id)
            {
                this.SendPushNotifications(
                    DAL.Subscription.GetSubscriptions(newGroupMessage.RecipientId, sender),
                    newGroupMessage.MessageId,
                    newGroupMessage.ConversationId,
                    sender,
                    newGroupMessage.PostDateTimeUtcTicks,
                    newGroupMessage.TextMessage,
                    group,
                    newGroupMessage.IsEncrypted);
            }

            return(success);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Save a message in the database
 /// </summary>
 /// <param name="message"></param>
 public void SaveMessage(DataAccessLayer.User sender, Message message)
 {
     message.Validate(sender);
     ITableRow tableRow = message.ConvertToTableRow();
     MessageTable.Instance.InsertRow(tableRow);
 }
Ejemplo n.º 8
0
        private static void SetYapperColumnValues(Message message, ITableRow row)
        {
            PropertyInfo[] propertyInfoArray = message.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo propInfo in propertyInfoArray)
            {
                IEnumerable<Attribute> attrs = propInfo.GetCustomAttributes();
                if (attrs == null)
                {
                    continue;
                }

                IColumn column = null;
                bool isDbColumn = false;
                bool isInBlob = false; ;
                foreach (Attribute a in attrs)
                {
                    if (a is YapperColumnAttribute && ((YapperColumnAttribute)a).ColumnLocation == ColumnLocation.Database)
                    {
                        isDbColumn = true;
                        YapperColumnAttribute yapperColumn = a as YapperColumnAttribute;
                        column = MessageTable.GetColumnFromName(yapperColumn.Name);
                    }

                    if (a is ProtoBuf.ProtoMemberAttribute)
                    {
                        isInBlob = true;
                    }
                }

                if (column != null && isDbColumn && !isInBlob)
                {
                    propInfo.SetValue(message, row.ColumnValues[column]);
                }
            }
        }
Ejemplo n.º 9
0
        public bool AddUserToGroup(int groupId, string userPhone)
        {
            DAL.User sender = this.GetAuthenticatedUser();

            DAL.Group group = (DAL.Group)DAL.UserService.Instance.GetUserFromId(groupId);

            DAL.User member = DAL.UserService.Instance.GetUserFromPhone(userPhone);

            bool success = DAL.Group.AddGroupMember(sender, group, member);

            if (!success)
            {
                return false;
            }

            // Read the group again
            group = (DAL.Group)DAL.UserService.Instance.GetUserFromId(groupId);

            MessageLayer.Message newGroupMessage = new MessageLayer.Message() { SenderId = sender.Id, RecipientId = group.Id, MessageFlags = MessageLayer.MessageFlags.GroupJoinMessage, TextMessage = sender.Name + " has added " + member.Name + " to the group" };

            MessageStore.MessageStore.Instance.SaveMessage(sender, newGroupMessage);

            if (newGroupMessage.RecipientId != sender.Id)
            {
                this.SendPushNotifications(
                    DAL.Subscription.GetSubscriptions(newGroupMessage.RecipientId, sender),
                    newGroupMessage.MessageId,
                    newGroupMessage.ConversationId,
                    sender,
                    newGroupMessage.PostDateTimeUtcTicks,
                    newGroupMessage.TextMessage,
                    group,
                    newGroupMessage.IsEncrypted);
            }

            return success;
        }
Ejemplo n.º 10
0
        public DAL.Group CreateGroup(DAL.Group newGroup)
        {
            DAL.User sender = this.GetAuthenticatedUser();

            if (newGroup.Members == null)
            {
                throw new Exception("Invalid member list");
            }

            Dictionary<int, DAL.User> uniqueUsers = new Dictionary<int, DAL.User>();

            for (int i = 0; i < newGroup.Members.Count; i++)
            {
                if (string.Compare(DAL.UserService.Instance.GetUserFromId(newGroup.Members[i].Id).PhoneNumber, newGroup.Members[i].PhoneNumber, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    throw new Exception("Invalid user specified");
                }

                uniqueUsers.Add(newGroup.Members[i].Id, newGroup.Members[i]);
            }

            if (!uniqueUsers.ContainsKey(sender.Id))
            {
                uniqueUsers.Add(sender.Id, sender);
            }

            DAL.Group createdGroup = DAL.Group.CreateGroup(sender, newGroup.Name, uniqueUsers.Values.ToList());

            MessageLayer.Message newGroupMessage = new MessageLayer.Message() { SenderId = sender.Id, RecipientId = createdGroup.Id, MessageFlags = MessageLayer.MessageFlags.GroupCreatedMessage, TextMessage = sender.Name + " has created the group" };

            MessageStore.MessageStore.Instance.SaveMessage(sender, newGroupMessage);

            if (newGroupMessage.RecipientId != sender.Id)
            {
                this.SendPushNotifications(
                    DAL.Subscription.GetSubscriptions(newGroupMessage.RecipientId, sender),
                    newGroupMessage.MessageId,
                    newGroupMessage.ConversationId,
                    sender,
                    newGroupMessage.PostDateTimeUtcTicks,
                    newGroupMessage.TextMessage,
                    createdGroup,
                    newGroupMessage.IsEncrypted);
            }
            return createdGroup;
        }
Ejemplo n.º 11
0
        /// <summary>
        /// API to send a new message
        /// </summary>
        /// <param name="recipientPhoneNumber"></param>
        /// <param name="message"></param>
        /// <param name="UtcPostTime"></param>
        /// <returns></returns>
        public MessageLayer.Message SendMessage(MessageLayer.Message message)
        {
            Stopwatch watch               = new Stopwatch();
            long      authTime            = 0;
            long      messageCreationTime = 0;
            long      pushNotification    = 0;

            watch.Start();
            DAL.User sender = this.GetAuthenticatedUser();

            authTime = watch.ElapsedMilliseconds;

            if (sender == null)
            {
                throw new Exception("User not authenticated");
            }

            try
            {
                if (sender.Id != message.SenderId)
                {
                    throw new Exception("Invalid senderId");
                }

                if (sender.Id == message.RecipientId)
                {
                    throw new Exception("Invalid recipientId");
                }

                DAL.User recipient = DAL.UserService.Instance.GetUserFromId(message.RecipientId);
                if (recipient == null)
                {
                    throw new Exception("recipient not found");
                }

                if (!recipient.CanSendMessage(sender))
                {
                    throw new Exception("Cannot send message to recipient");
                }

                message.Recipient = recipient;

                MessageStore.MessageStore.Instance.SaveMessage(sender, message);
                messageCreationTime = watch.ElapsedMilliseconds - authTime;

                if (message != null)
                {
                    if (message.RecipientId != sender.Id)
                    {
                        this.SendPushNotifications(
                            DAL.Subscription.GetSubscriptions(message.RecipientId, sender),
                            message.MessageId,
                            message.ConversationId,
                            sender,
                            message.PostDateTimeUtcTicks,
                            message.TextMessage,
                            message.Recipient.UserType == DAL.UserType.Group ? (DAL.Group)message.Recipient : null,
                            message.IsEncrypted);

                        pushNotification = watch.ElapsedMilliseconds - messageCreationTime - authTime;
                    }
                }

                return(message);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Ejemplo n.º 12
0
        public void TestUnseenCount()
        {
            Message message1 = new Message();
            message1.SenderId = 1;
            message1.RecipientId = 7;
            message1.ConversationId = GetGuid(1, 7);
            message1.MessageId = Guid.NewGuid();
            message1.LastUpdateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message1.PostDateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message1.TextMessage = "This is a test Msg" + DateTime.UtcNow.Ticks.ToString();
            message1.MessageBlobName = Guid.NewGuid();
            message1.MessageFlags = 0;

            Message message2 = new Message();
            message2.SenderId = 1;
            message2.RecipientId = 8;
            message2.ConversationId = GetGuid(1, 7);
            message2.MessageId = Guid.NewGuid();
            message2.LastUpdateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message2.PostDateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message2.TextMessage = "This is a test Msg" + DateTime.UtcNow.Ticks.ToString();
            message2.MessageBlobName = Guid.NewGuid();
            message2.MessageFlags = 0;

            Message message3 = new Message();
            message3.SenderId = 1;
            message3.RecipientId = 8;
            message3.ConversationId = GetGuid(1, 7);
            message3.MessageId = Guid.NewGuid();
            message3.LastUpdateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message3.PostDateTimeUtcTicks = DateTime.UtcNow.Ticks;
            message3.TextMessage = "This is a test Msg" + DateTime.UtcNow.Ticks.ToString();
            message3.MessageBlobName = Guid.NewGuid();
            message3.MessageFlags = 0;

            MessageStore.MessageStore.Instance.SaveMessage(message1.Sender, message1);
            MessageStore.MessageStore.Instance.SaveMessage(message2.Sender, message2);
            MessageStore.MessageStore.Instance.SaveMessage(message3.Sender, message3);

            int count = MessageStore.MessageStore.Instance.GetUnseenMessageCount(new DataAccessLayer.User(1, "+1 425-111-9999", "Test User", null), DateTime.UtcNow - new TimeSpan(7, 0, 0, 0));
        }