public static List <IndividualChatRoom> getAllChatby2ID(string sender, string receiver)
    {
        List <IndividualChatRoom> chats = new List <IndividualChatRoom>();

        try
        {
            SqlCommand command = new SqlCommand("select * from IndividualChat where (sender=@sender or sender=@receiver) and (receiver=@sender  or receiver=@receiver) order by time");
            command.Parameters.AddWithValue("@sender", sender);
            command.Parameters.AddWithValue("@receiver", receiver);
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                IndividualChatRoom m = new IndividualChatRoom();
                m.Sender   = reader["sender"].ToString();
                m.Receiver = reader["receiver"].ToString();
                m.ChatTime = Convert.ToDateTime(reader["time"]);
                m.Messages = reader["message"].ToString();
                chats.Add(m);
            }
            reader.Close();
        }
        finally
        {
            connection.Close();
        }
        return(chats);
    }
Exemple #2
0
        //Create
        public Task <IndividualChatRoom> AddIndividualChatRoom(User user)
        {
            TaskCompletionSource <IndividualChatRoom> ResultCompletionSource = new TaskCompletionSource <IndividualChatRoom>();

            Android.Runtime.JavaDictionary <string, Java.Lang.Object> chatRoomUser = new Android.Runtime.JavaDictionary <string, Java.Lang.Object>
            {
                { user.Email.Replace(".", ":"), true },
                { UserSetting.UserEmail.Replace(".", ":"), true }
            };

            var chatRoom = new Dictionary <string, Java.Lang.Object>
            {
                { "image", null },
                { "roomTitle", null },
                { "users", chatRoomUser },
                { "isGroup", false },
                { "isDestruct", false }
            };

            Conn.Collection("roomList").Add(chatRoom).AddOnCompleteListener(new OnCompleteEventHandleListener((Android.Gms.Tasks.Task obj) => {
                if (obj.IsSuccessful)
                {
                    DocumentReference documentReference = (DocumentReference)obj.Result;
                    IndividualChatRoom temp             = new IndividualChatRoom(documentReference.Id, null, user, false);
                    ResultCompletionSource.SetResult(temp);
                }
                else
                {
                    ResultCompletionSource.SetResult(null);
                }
            }));
            return(ResultCompletionSource.Task);
        }
    public static List <IndividualChatRoom> getAllChat()
    {
        List <IndividualChatRoom> chats = new List <IndividualChatRoom>();

        try
        {
            SqlCommand command = new SqlCommand("select * from [dbo].[IndividualChat] order by time");
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                IndividualChatRoom m = new IndividualChatRoom();
                m.Sender   = reader["sender"].ToString();
                m.Receiver = reader["receiver"].ToString();
                m.ChatTime = Convert.ToDateTime(reader["time"]);
                m.Messages = reader["message"].ToString();

                chats.Add(m);
            }
            reader.Close();
        }
        finally
        {
            connection.Close();
        }
        return(chats);
    }
Exemple #4
0
 protected void BtnSentMessage_Click1(object sender, EventArgs e)
 {
     if (Session["echat"] != null)
     {
         string             eID = Session["echat"].ToString();
         Establishment      r   = EstablishmentDB.getEstablishmentByID(eID);
         Establishment      s   = (Establishment)Session["establishment"];
         IndividualChatRoom icr = new IndividualChatRoom(s.ID, r.ID, System.DateTime.Now, TextBox1.Text);
         IndividualChatRoomDB.insertIndChat(icr);
     }
     else if (Session["rwEst"] != null)
     {
         string             eID = Session["rwEst"].ToString();
         Establishment      r   = EstablishmentDB.getEstablishmentByID(eID);
         Establishment      s   = (Establishment)Session["establishment"];
         IndividualChatRoom icr = new IndividualChatRoom(s.ID, r.ID, System.DateTime.Now, TextBox1.Text);
         IndividualChatRoomDB.insertIndChat(icr);
     }
     else
     {
         Users              r   = UsersDB.getUserbyID(Session["ldID"].ToString());
         Establishment      s   = (Establishment)Session["establishment"];
         IndividualChatRoom icr = new IndividualChatRoom(s.ID, r.userId, System.DateTime.Now, TextBox1.Text);
         IndividualChatRoomDB.insertIndChat(icr);
     }
     TextBox1.Text = "";
     UpdatePanel1.Update();
 }
Exemple #5
0
        public Task <object> SearchChatRoomById(string roomId)
        {
            ChatRoom chatRoom = null;
            TaskCompletionSource <object> ResultCompletionSource = new TaskCompletionSource <object>();

            Conn.Collection("roomList").Document(roomId).Get().AddOnCompleteListener(new OnCompleteEventHandleListener(async(Android.Gms.Tasks.Task obj) => {
                if (obj.IsSuccessful)
                {
                    DocumentSnapshot documentSnapshot = (DocumentSnapshot)obj.Result;

                    var temp       = documentSnapshot.Data;
                    var Id         = documentSnapshot.Id;
                    var image      = (string)temp["image"];
                    var isDestruct = (bool)temp["isDestruct"];
                    var roomTitle  = (string)temp["roomTitle"];
                    var users      = (Android.Runtime.JavaDictionary)temp["users"];

                    string admin;
                    var isGroup = (bool)temp["isGroup"];

                    if (isGroup)
                    {
                        admin = (string)temp["admin"];

                        ObservableCollection <User> GroupMembers = new ObservableCollection <User>();

                        foreach (string key in users.Keys)
                        {
                            GroupMembers.Add(new User(key.Replace(":", ".")));
                        }
                        chatRoom = new GroupChatRoom(Id, roomTitle, admin, GroupMembers, isDestruct);
                    }
                    else
                    {
                        string RecipientEmail = null;
                        if (users.Keys.Count == 2)
                        {
                            foreach (string key in users.Keys)
                            {
                                if (!key.Replace(":", ".").Equals(UserSetting.UserEmail))
                                {
                                    RecipientEmail = key.Replace(":", ".");
                                }
                            }

                            string RecipientName = await GetUsername(RecipientEmail);
                            chatRoom             = new IndividualChatRoom(Id, RecipientName, new User(RecipientEmail, RecipientName), isDestruct);
                        }
                    }
                }
            }));
            ResultCompletionSource.SetResult(chatRoom);
            return(ResultCompletionSource.Task);
        }
Exemple #6
0
        //Retrieve
        public Task <IndividualChatRoom> SearchIndividualChatRoom(string userEmail)
        {
            TaskCompletionSource <IndividualChatRoom> ResultCompletionSource = new TaskCompletionSource <IndividualChatRoom>();
            IndividualChatRoom individualChat = null;
            Query query = Conn.Collection("roomList")
                          .WhereEqualTo("users." + userEmail.Replace(".", ":"), true)
                          .WhereEqualTo("users." + UserSetting.UserEmail.Replace(".", ":"), true)
                          .WhereEqualTo("isGroup", false);

            query.Get().AddOnCompleteListener(new OnCompleteEventHandleListener(async(Android.Gms.Tasks.Task obj) =>
            {
                if (obj.IsSuccessful)
                {
                    QuerySnapshot TempDocSnapshot = (QuerySnapshot)obj.Result;
                    if (!TempDocSnapshot.IsEmpty)
                    {
                        foreach (DocumentSnapshot documentSnapshot in TempDocSnapshot.Documents)
                        {
                            var tempUser = (Android.Runtime.JavaDictionary)documentSnapshot.Data["users"];
                            {
                                var id = documentSnapshot.Id;

                                User user      = new User();
                                var isDestruct = (bool)documentSnapshot.Data["isDestruct"];

                                foreach (string useremail in tempUser.Keys)
                                {
                                    if (string.Compare(useremail, UserSetting.UserEmail.Replace(".", ":")) != 0)
                                    {
                                        user.Email = useremail;
                                    }
                                }
                                user.UserName  = await GetUsername(user.Email.Replace(":", "."));
                                individualChat = new IndividualChatRoom(id, null, user, isDestruct);
                            }
                        }
                        ResultCompletionSource.SetResult(individualChat);
                    }
                    else
                    {
                        ResultCompletionSource.SetResult(null);
                    }
                }
            }));
            return(ResultCompletionSource.Task);
        }
 protected void BtnSentMessage_Click1(object sender, EventArgs e)
 {
     if (Session["chat"] != null)
     {
         Users s = UsersDB.getUserbyEmail(Session["email"].ToString());
         Users r = UsersDB.getUserbyUsername(lblName.Text);
         IndividualChatRoom icr = new IndividualChatRoom(s.UserId, r.UserId, System.DateTime.Now, TextBox1.Text);
         IndividualChatRoomDB.insertIndChat(icr);
     }
     else
     {
         string             eID = Session["echat"].ToString();
         Users              s   = UsersDB.getUserbyEmail(Session["email"].ToString());
         Establishment      r   = EstablishmentDB.getEstablishmentByID(eID);
         IndividualChatRoom icr = new IndividualChatRoom(s.UserId, r.ID, System.DateTime.Now, TextBox1.Text);
         IndividualChatRoomDB.insertIndChat(icr);
     }
     TextBox1.Text = "";
     UpdatePanel1.Update();
 }
    public static int insertIndChat(IndividualChatRoom ld)
    {
        int num = -1;

        try
        {
            SqlCommand command = new SqlCommand("insert into IndividualChat values(@sender, @receiver, @time, @message)");
            command.Parameters.AddWithValue("@sender", ld.Sender);
            command.Parameters.AddWithValue("@receiver", ld.Receiver);
            command.Parameters.AddWithValue("@time", ld.ChatTime);
            command.Parameters.AddWithValue("@message", ld.Messages);
            command.Connection = connection;
            connection.Open();
            if (command.ExecuteNonQuery() > 0)
            {
                num = 1;
            }
        }
        finally
        {
            connection.Close();
        }
        return(num);
    }
Exemple #9
0
 public ChatContents(IndividualChatRoom SelectedUser)
 {
     InitializeComponent();
     this.BindingContext = _chatRoom = new ChatContentsViewModel(SelectedUser);
     this.Title          = SelectedUser._User.UserName;
 }