protected void BtnSearch_Click(object sender, EventArgs e) { string searchTerm = TBSearch.Text; if (string.IsNullOrWhiteSpace(searchTerm)) { LSearchTitle.Text = "Search results for \"" + searchTerm + "\""; LSearchEmpty.Text = "0"; return; } searchTerm = searchTerm.ToLower(); WebRequest request = WebRequest.Create(path + "SearchForFriends/" + userId + "/" + searchTerm); WebResponse response = request.GetResponse(); Stream theDataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(theDataStream); String data = reader.ReadToEnd(); reader.Close(); response.Close(); JavaScriptSerializer js = new JavaScriptSerializer(); User[] friends = js.Deserialize <User[]>(data); for (int i = 0; i < friends.Length; i++) { // exclude if self if (friends[i].UserId != userId) { FriendCard ctrl = (FriendCard)LoadControl("FriendCard.ascx"); ctrl.FirstName = friends[i].FirstName.ToString(); ctrl.LastName = friends[i].LastName.ToString(); int friendId = int.Parse(friends[i].UserId.ToString()); ctrl.ImageUrl = util.ProfPicArrayToImage(friendId); //ctrl.Bio = friends[i].Bio.ToString(); ctrl.UserId = friendId; // bind data to ctrl ctrl.DataBind(); // add to panel SearchPanel.Controls.Add(ctrl); } } SearchPanel.Visible = true; LSearchTitle.Text = "Search results for \"" + searchTerm + "\""; LSearchEmpty.Text = friends.Length.ToString(); }
protected void LoadFriends() { WebRequest request = WebRequest.Create(path + "GetFriendsByUserId/" + userId); WebResponse response = request.GetResponse(); Stream theDataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(theDataStream); String data = reader.ReadToEnd(); reader.Close(); response.Close(); JavaScriptSerializer js = new JavaScriptSerializer(); User[] friends = js.Deserialize <User[]>(data); for (int i = 0; i < friends.Length; i++) { FriendCard ctrl = (FriendCard)LoadControl("FriendCard.ascx"); ctrl.FirstName = friends[i].FirstName.ToString(); ctrl.LastName = friends[i].LastName.ToString(); int friendId = int.Parse(friends[i].UserId.ToString()); ctrl.UserId = friendId; // load default pic if there is no profile pic if (util.ProfPicArrayToImage((int)Session["userId"]) != "") { ctrl.ImageUrl = util.ProfPicArrayToImage(friendId); } else { ctrl.ImageUrl = "https://www.telegraph.co.uk/content/dam/technology/2021/01/28/Screenshot-2021-01-28-at-13-20-35_trans_NvBQzQNjv4BqEGKV9LrAqQtLUTT1Z0gJNRFI0o2dlzyIcL3Nvd0Rwgc.png"; } ctrl.DataBind(); // Add the control object to the WebForm's Controls collection FriendPanel.Controls.Add(ctrl); } // update the badge to show how many LFriendsNumber.Text = friends.Length.ToString(); }
protected List <int> LoadFriends() { string extension = "GetFriends/"; WebRequest request = WebRequest.Create(path + extension + userId); WebResponse response = request.GetResponse(); Stream theDataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(theDataStream); String data = reader.ReadToEnd(); reader.Close(); response.Close(); JavaScriptSerializer js = new JavaScriptSerializer(); User[] friends = js.Deserialize <User[]>(data); List <int> UserIds = new List <int>(); for (int i = 0; i < friends.Length; i++) { // create card and add data FriendCard ctrl = (FriendCard)LoadControl("FriendCard.ascx"); ctrl.FirstName = friends[i].FirstName.ToString(); ctrl.LastName = friends[i].LastName.ToString(); // get friend id int friendId = int.Parse(friends[i].UserId.ToString()); ctrl.UserId = friendId; // load default pic if there is no profile pic string profilePicture = GetProfilePicture(friendId); ctrl.ImageUrl = profilePicture; ctrl.Description = "Friend"; // bind data to ctrl ctrl.DataBind(); // add ctrl to list of all ctrls UserIds.Add(friends[i].UserId); // add to panel FriendsPanel.Controls.Add(ctrl); } return(UserIds); }
protected void LoadFriends() { // load other person's friends WebRequest request = WebRequest.Create(path + "GetFriendsByUserId/" + otherPersonId); WebResponse response = request.GetResponse(); Stream theDataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(theDataStream); String data = reader.ReadToEnd(); reader.Close(); response.Close(); JavaScriptSerializer js = new JavaScriptSerializer(); User[] friends = js.Deserialize <User[]>(data); for (int i = 0; i < friends.Length; i++) { if (friends[i].UserId == userId) // if friend is self { SelfCard ctrl = (SelfCard)LoadControl("SelfCard.ascx"); ctrl.FirstName = friends[i].FirstName.ToString(); ctrl.LastName = friends[i].LastName.ToString(); int friendId = int.Parse(friends[i].UserId.ToString()); ctrl.UserId = friendId; // load default pic if there is no profile pic string profilePicture = GetProfilePicture(friendId); ctrl.ImageUrl = profilePicture; // bind data to ctrl ctrl.DataBind(); // add to panel FriendPanel.Controls.Add(ctrl); } else { // check if friend and load friend card if theyre friends bool areFriends = AreFriends(friends[i].UserId); if (!areFriends) // load non friend card if this person isn't a friend of current user { NonFriendCard ctrl = (NonFriendCard)LoadControl("NonFriendCard.ascx"); ctrl.FirstName = friends[i].FirstName.ToString(); ctrl.LastName = friends[i].LastName.ToString(); int friendId = int.Parse(friends[i].UserId.ToString()); ctrl.UserId = friendId; // if no profile pic, load default string profilePicture = GetProfilePicture(friendId); ctrl.ImageUrl = profilePicture; ctrl.Description = friends[i].Bio.ToString(); ctrl.UserId = int.Parse(friends[i].UserId.ToString()); // bind data to ctrl ctrl.DataBind(); // add to panel FriendPanel.Controls.Add(ctrl); } else // else if person is also a friend of current user { FriendCard ctrl = (FriendCard)LoadControl("FriendCard.ascx"); ctrl.FirstName = friends[i].FirstName.ToString(); ctrl.LastName = friends[i].LastName.ToString(); int friendId = int.Parse(friends[i].UserId.ToString()); ctrl.UserId = friendId; // load default pic if there is no profile pic string profilePicture = GetProfilePicture(friendId); ctrl.ImageUrl = profilePicture; // bind data to ctrl ctrl.DataBind(); // add to panel FriendPanel.Controls.Add(ctrl); } } } // update the badge to show how many LFriendsNumber.Text = friends.Length.ToString(); }
protected void BtnSearch_Click(object sender, EventArgs e) { string searchTerm = TBSearch.Text; if (string.IsNullOrWhiteSpace(searchTerm)) { LSearchTitle.Text = "Search results for \"" + searchTerm + "\""; LSearchEmpty.Text = "0"; return; } SearchPanel.Visible = true; searchTerm = searchTerm.ToLower(); WebRequest request = WebRequest.Create(path + "SearchForFriends/" + otherPersonId + "/" + searchTerm); WebResponse response = request.GetResponse(); Stream theDataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(theDataStream); String data = reader.ReadToEnd(); reader.Close(); response.Close(); JavaScriptSerializer js = new JavaScriptSerializer(); User[] friends = js.Deserialize <User[]>(data); for (int i = 0; i < friends.Length; i++) { // if self if (friends[i].UserId == userId) { SelfCard ctrl = (SelfCard)LoadControl("SelfCard.ascx"); ctrl.FirstName = friends[i].FirstName.ToString(); ctrl.LastName = friends[i].LastName.ToString(); int friendId = int.Parse(friends[i].UserId.ToString()); ctrl.UserId = friendId; // if no profile pic, load default string profilePicture = GetProfilePicture(friendId); ctrl.ImageUrl = profilePicture; ctrl.UserId = int.Parse(friends[i].UserId.ToString()); // bind data to ctrl ctrl.DataBind(); // add to panel SearchPanel.Controls.Add(ctrl); } else // if not self // check if friend and load friend card if theyre friends { bool areFriends = AreFriends(friends[i].UserId); if (!areFriends) { NonFriendCard ctrl = (NonFriendCard)LoadControl("NonFriendCard.ascx"); ctrl.FirstName = friends[i].FirstName.ToString(); ctrl.LastName = friends[i].LastName.ToString(); int friendId = int.Parse(friends[i].UserId.ToString()); ctrl.UserId = friendId; // if no profile pic, load default string profilePicture = GetProfilePicture(friendId); ctrl.ImageUrl = profilePicture; ctrl.Description = friends[i].Bio.ToString(); ctrl.UserId = int.Parse(friends[i].UserId.ToString()); // bind data to ctrl ctrl.DataBind(); // add to panel SearchPanel.Controls.Add(ctrl); } else { FriendCard ctrl = (FriendCard)LoadControl("FriendCard.ascx"); ctrl.FirstName = friends[i].FirstName.ToString(); ctrl.LastName = friends[i].LastName.ToString(); int friendId = int.Parse(friends[i].UserId.ToString()); ctrl.UserId = friendId; // load default pic if there is no profile pic string profilePicture = GetProfilePicture(friendId); ctrl.ImageUrl = profilePicture; // bind data to ctrl ctrl.DataBind(); // add to panel SearchPanel.Controls.Add(ctrl); } } } LSearchTitle.Text = "Search results for \"" + searchTerm + "\""; LSearchEmpty.Text = friends.Length.ToString(); }
protected List <int> LoadUsers(string extension, Panel panel) { WebRequest request = WebRequest.Create(path + extension + userId); WebResponse response = request.GetResponse(); Stream theDataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(theDataStream); String data = reader.ReadToEnd(); reader.Close(); response.Close(); JavaScriptSerializer js = new JavaScriptSerializer(); User[] friends = js.Deserialize <User[]>(data); List <int> UserIds = new List <int>(); for (int i = 0; i < friends.Length; i++) { // skip loading the currently logged in user if (friends[i].UserId != userId) { // skip if this user is already loaded if (!UserIds.Contains(friends[i].UserId)) { // check if friend and load friend card if theyre friends bool areFriends = AreFriends(friends[i].UserId); // if not friends, load the not friends card if (!areFriends) { NonFriendCard ctrl = (NonFriendCard)LoadControl("NonFriendCard.ascx"); ctrl.FirstName = friends[i].FirstName.ToString(); ctrl.LastName = friends[i].LastName.ToString(); int friendId = int.Parse(friends[i].UserId.ToString()); ctrl.UserId = friendId; // if no profile pic, load default string profilePicture = GetProfilePicture(friendId); ctrl.ImageUrl = profilePicture; ctrl.Description = friends[i].Bio.ToString(); ctrl.UserId = int.Parse(friends[i].UserId.ToString()); // bind data to ctrl ctrl.DataBind(); UserIds.Add(friends[i].UserId); // add to panel panel.Controls.Add(ctrl); } else { FriendCard ctrl = (FriendCard)LoadControl("FriendCard.ascx"); ctrl.FirstName = friends[i].FirstName.ToString(); ctrl.LastName = friends[i].LastName.ToString(); int friendId = int.Parse(friends[i].UserId.ToString()); ctrl.UserId = friendId; // load default pic if there is no profile pic string profilePicture = GetProfilePicture(friendId); ctrl.ImageUrl = profilePicture; ctrl.Description = friends[i].Bio.ToString() + " and you"; // bind data to ctrl ctrl.DataBind(); UserIds.Add(friends[i].UserId); // add to panel panel.Controls.Add(ctrl); } } } } return(UserIds); }