Beispiel #1
0
        public async void Send(string name, string message, string groupKey, string toKey)
        {

            // send email about it

            MailMessage Message = new MailMessage();
            SmtpClient Smtp = new SmtpClient();

            string password = System.Web.Configuration.WebConfigurationManager.AppSettings["MailPassword"];

            System.Net.NetworkCredential SmtpUser = new System.Net.NetworkCredential("*****@*****.**", password);

            string chat;
            byte[] bytes = Encoding.Unicode.GetBytes(message);
            chat = Encoding.Unicode.GetString(bytes);

            //string email = "chat: \n\n";
            //email = email + "bytes:\n";
            //email = email + bytes[0].ToString() + "\n";
            //email = email + name + "\n";
            //email = email + chat + "\n";

            //Message.From = new MailAddress("*****@*****.**");
            //Message.To.Add(new MailAddress("*****@*****.**"));
            //Message.IsBodyHtml = false;
            //Message.Subject = "new handle";
            //Message.Body = email;
            //Message.Priority = MailPriority.Normal;
            //Smtp.EnableSsl = false;

            //Smtp.Credentials = SmtpUser;
            //Smtp.Host = "198.57.199.92";
            //Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            //Smtp.Port = 26;

            //Smtp.Send(Message);

            string connect_id = Context.ConnectionId;
            lilAuthie chatter = (lilAuthie)handles[connect_id];



            if (chatter == null)
            {
                // pull from database
                // insert into handles
                string user_id = Context.User.Identity.Name;

                chatter = new lilAuthie();
                chatter.connectionId = connect_id;
                chatter.handle = (from handle r in db.handles where r.userGuid.Equals(user_id) select r).FirstOrDefault();
                handles.Add(connect_id, chatter);

                // add to group for easier sending, plus multi-devicers
                await Groups.Add(connect_id, chatter.handle.publicKey);
            }

            // now loop through all of the clients,
            // and only send the message to the appropriate one...
            thread selected_thread = (from thread m in db.threads where m.groupKey == groupKey select m).FirstOrDefault();

            // how to determine who this should be sent to:
            // -- find the thread with the group key
            // -- check to see if fromHandle is same as chatter,
                  // if it is, then we are to the other person

            string notify_public_key = "";
            if (selected_thread.fromHandle.id == chatter.handle.id)
            {
                notify_public_key = selected_thread.toHandle.publicKey;
            }
            else
            {
                notify_public_key = selected_thread.fromHandle.publicKey;
            }


            // toId refers to the opposite of the thread starter.
            // the thread starter can have several convos open,
            // and toId is how we distinguish which one is which.


            //int toId = 1;
            //if (chatter.handle.id == selected_thread.fromHandle.id)
            //{
            //    // chat is from the user who started the thread,
            //    // so how can i get the other person's id...

            //    // it's either the toHandleId...
            //    toId = selected_thread.toHandle.id;
            //}
            //else
            //{
            //    // chat is from user who did not start the thread,
            //    // so the toId is their chatter.handle.id,
            //    toId = chatter.handle.id;
            //}

            handle toHandle = (from m in db.handles where m.publicKey == toKey select m).FirstOrDefault();


            // make sure that the user i not blocked
            handle msg_sent_to_user = (from m in db.handles where m.publicKey == notify_public_key select m).FirstOrDefault();
            block blocked = (from m in db.blocks
                             where m.blockedByHandleId == msg_sent_to_user.id
                                 && m.blockedHandleId == chatter.handle.id && m.active == 1
                             select m).FirstOrDefault();
            if (blocked != null)
            {
                // bail out, don't send the message
                return;
            }


            Clients.Group(notify_public_key).addMessage(name, message, groupKey);

            foreach (lilAuthie a in handles.Values)
            {
                if (a.handle.publicKey == notify_public_key)
                {
                    Clients.Client(a.connectionId).addMessage(name, message, groupKey);
                }
            }

            message clean_message = new message();
            clean_message.fromHandleId = chatter.handle.id;
            clean_message.sentDate = DateTime.UtcNow;
            clean_message.messageText = chat;
            clean_message.threadId = selected_thread.id;
            clean_message.toHandleId = toHandle.id;

            db.messages.Add(clean_message);
            db.SaveChanges();

            // send a notification
            //string alert_message = chatter.handle.name + " said: " + message;
            //AirshipChatNotificationRESTService service = new AirshipChatNotificationRESTService();
            //AirshipResponse arg = await service.SendChat(notify_public_key, alert_message, selected_thread.groupKey, chatter.handle.publicKey, clean_message.id);

        }
        public async Task<RODResponseMessage> Post(Chat msg)
        {
            RODResponseMessage response = new RODResponseMessage();

            string user_id = User.Identity.Name;
            handle logged_in = (from handle r in db.handles where r.userGuid.Equals(User.Identity.Name) select r).FirstOrDefault();

            // now loop through all of the clients,
            // and only send the message to the appropriate one...
            thread selected_thread = (from thread m in db.threads where m.groupKey == msg.groupKey select m).FirstOrDefault();

            // how to determine who this should be sent to:
            // -- find the thread with the group key
            // -- check to see if fromHandle is same as chatter,
                  // if it is, then we are to the other person

            string notify_public_key = "";
            if (selected_thread.fromHandle.id == logged_in.id)
            {
                notify_public_key = selected_thread.toHandle.publicKey;
            }
            else
            {
                notify_public_key = selected_thread.fromHandle.publicKey;
            }


            // toId refers to the opposite of the thread starter.
            // the thread starter can have several convos open,
            // and toId is how we distinguish which one is which.


            //int toId = 1;
            //if (chatter.handle.id == selected_thread.fromHandle.id)
            //{
            //    // chat is from the user who started the thread,
            //    // so how can i get the other person's id...

            //    // it's either the toHandleId...
            //    toId = selected_thread.toHandle.id;
            //}
            //else
            //{
            //    // chat is from user who did not start the thread,
            //    // so the toId is their chatter.handle.id,
            //    toId = chatter.handle.id;
            //}

            handle toHandle = (from m in db.handles where m.publicKey == msg.toKey select m).FirstOrDefault();


            // make sure that the user i not blocked
            handle msg_sent_to_user = (from m in db.handles where m.publicKey == notify_public_key select m).FirstOrDefault();
            block blocked = (from m in db.blocks
                             where m.blockedByHandleId == msg_sent_to_user.id
                                 && m.blockedHandleId == logged_in.id && m.active == 1
                             select m).FirstOrDefault();
            if (blocked != null)
            {
                // bail out, don't send the message
                return new RODResponseMessage() { message = "Blocked.", result = 0 };
            }

            // i really should get a reference to the hub from here,
            // and use that to send out the push message... but since the hub
            // isn't even working yet, we can put that off for now
            // Clients.Client(a.connectionId).addMessage(name, message, groupKey);

            message clean_message = new message();
            clean_message.fromHandleId = logged_in.id;
            clean_message.sentDate = DateTime.UtcNow;
            clean_message.messageText = msg.message;
            clean_message.threadId = selected_thread.id;
            clean_message.toHandleId = toHandle.id;

            db.messages.Add(clean_message);
            db.SaveChanges();

            // send a notification
            string alert_message = logged_in.name + " said, '" + msg.message +"'";
            AirshipChatNotificationRESTService service = new AirshipChatNotificationRESTService();
            AirshipResponse arg = await service.SendChat(notify_public_key, alert_message, msg.message, selected_thread.groupKey, logged_in.publicKey, clean_message.id);

            response = new RODResponseMessage() { message = "Success.", result = clean_message.id };

            return response;
        }
        public async Task<RODResponseMessage> Post(Snap _snap)
        {

            RODResponseMessage msg = new RODResponseMessage();

            if (_snap == null)
            {
                msg.result = -1;
                msg.message = "Snap was null.";
                return msg;
            }

            string user_id = User.Identity.Name;
            handle logged_in = (from handle r in db.handles where r.userGuid.Equals(User.Identity.Name) select r).FirstOrDefault();
            handle to_handle = (from handle r in db.handles where r.publicKey == _snap.toGuid select r).FirstOrDefault();


            // check to make sure that the thread sender 
            // is not blocked by the recipent
            block blocked = (from m in db.blocks
                             where m.blockedByHandleId == to_handle.id
                                 && m.blockedHandleId == logged_in.id && m.active == 1
                             select m).FirstOrDefault();
            if (blocked != null)
            {
                // bail out, don't send the message
                msg.result = -1;
                msg.message = "Unable to send message.";
                return msg;
            }


            thread clean_thread = new thread();
            clean_thread.active = 1;
            clean_thread.startDate = DateTime.UtcNow;
            clean_thread.fromHandleId = logged_in.id;
            clean_thread.caption = _snap.caption;
            clean_thread.hearts = 0;
            clean_thread.toHandleId = to_handle.id;
            clean_thread.location = _snap.location;
            clean_thread.font = _snap.font;
            clean_thread.textColor = _snap.textColor;

            //
            // check to see if the to_handle is authorized
            // to make the send, if they are not, then this is
            // an authorization request. 
            //
            clean_thread.authorizeRequest = 0;

            //
            // we don't need to do this check if this is for
            // toHandleId = 1 or toHandleId = 2 (snaps sent
            // to profile and the daily)
            //

            if (clean_thread.toHandleId != 1 && clean_thread.toHandleId != 2)
            {

                follower confirmed_to_handle_follower = (from m in db.followers
                                                         where m.followeeId == to_handle.id &&
                                                         m.followerId == logged_in.id &&
                                                         m.active == 1
                                                         select m).FirstOrDefault();

                if (confirmed_to_handle_follower == null)
                {
                    clean_thread.authorizeRequest = 1;
                }

            }

            // should it all be organized around this group key then?
            // add new threads and messages at the same with the same
            // group key generated on the client (like a guid)...

            clean_thread.groupKey = _snap.groupKey;
            db.threads.Add(clean_thread);
            db.SaveChanges();

            if (_snap.caption != null)
            {

                if (_snap.caption.Length > 0)
                {
                    message chat_message = new message();
                    chat_message.threadId = clean_thread.id;
                    chat_message.messageText = clean_thread.caption;
                    chat_message.fromHandleId = clean_thread.fromHandleId;
                    chat_message.sentDate = DateTime.UtcNow;
                    chat_message.toHandleId = to_handle.id;
                    db.messages.Add(chat_message);
                    db.SaveChanges();
                }
            }

            msg.message = clean_thread.id.ToString();
            msg.result = 1;


            return msg;
        }