public bool SendContact(string sessionKey, Shared.Dto.Contact contact)
        {
            var dbContext = DbContextFactory.GetContext();

            var sessionService = new SessionService();
            var user = sessionService.GetUser(sessionKey);
            if (user == null) return false;

            User recipient;
            try
            {
                recipient =
                    dbContext.Users.Single(u => u.Id == contact.ReceiverId);
            }
            catch (Exception)
            {
                return false;
            }

            var newContact = new Contact
            {
                PublicKey = contact.PublicKey,
                Name = contact.Name,
                Sender = user,
                Recipient = recipient
            };

            dbContext.Add(newContact);
            dbContext.SaveChanges();

            var userService = new PushService();
            userService.Push(recipient, "Recieved new contact!");

            return true;
        }
        public IHttpActionResult IsPushAllowed([FromUri]string sessionKey, SetPushAllowedRequest request)
        {
            var pushService = new PushService();
            var ok = pushService.SetPushAllowed(sessionKey, request.IsAllowed);

            if (ok) return Ok();
            return Unauthorized();
        }
        public bool SendMessage(string sessionKey, Shared.Dto.Message msg)
        {
            var dbContext = DbContextFactory.GetContext();

            var sessionService = new SessionService();
            var user = sessionService.GetUser(sessionKey);
            if (user == null)
            {
                return false;
            }

            User recipient;
            try
            {
                recipient =
                    dbContext.Users.Single(u => u.Id == msg.ReceiverId);
            }
            catch (Exception)
            {
                return false;
            }

            var newMsg = new Message
            {
                Content = msg.Body,
                Sender = user,
                Recipient = recipient,
                SentTime = msg.DateSent,
                Signature = msg.DigitalSignature,
                SessionKey = msg.SymmetricKey,
                InitVector = msg.Iv

            };

            var userService = new PushService();
            userService.Push(recipient, "You have new message");

            dbContext.Add(newMsg);
            dbContext.SaveChanges();

            return true;
        }
        public IHttpActionResult RegisterPushToken([FromUri]string sessionKey, [FromBody]string pushToken)
        {
            var pushService = new PushService();
            var ok = pushService.RegisterPushToken(sessionKey, pushToken);

            if (ok) return Ok();
            return Unauthorized();
        }
 public IHttpActionResult SetPushAllowed([FromUri]string sessionKey)
 {
     if (!new SessionService().ValidateSession(sessionKey)) return Unauthorized();
     var pushService = new PushService();
     var isAllowed = pushService.IsPushAllowed(sessionKey);
     return Ok(isAllowed);
 }