private static Question InitializeQuestion(string questionContent, ApplicationUser currentUser) { return new Question() { AskedBy = currentUser, Content = questionContent, TimeStamp = DateTime.Now }; }
public string GetAvatarUrl(ApplicationUser user) { if (user == null) return null; var url = "/Images/Avatars/"; if (user.Avatar != null) { return url + user.Avatar; } return url + "anonymous.jpg"; }
private void AddNotification(ApplicationUser user) { user.Notifications.Add(new Notification() { AllowNotificationAlert = true, EntityId = CommentId, User = user, TimeStamp = TimeStamp, Type = Notification.NotificationType.Comment, Seen = false }); }
public Comment AddComment(ApplicationUser user, string content) { var comment = new Comment() { Answer = this, AnswerId = AnswerId, User = user, UserId = user.Id, Content = content, Active = true, TimeStamp = DateTime.Now.AddDays((new Random()).Next(0, 15)) }; Comments.Add(comment); return comment; }
private QuestionDetail AddQuestionDetailToContext(ApplicationUser currentUser, ApplicationUser askedToUser, Question question) { var questionDetail = new QuestionDetail() { Answered = false, AskedBy = currentUser, AskedTo = askedToUser, Question = question, SeenByUser = false, Active = true, TimeStamp = question.TimeStamp }; _context.QuestionDetails.Add(questionDetail); return questionDetail; }
private ProfileViewModel GetProfileFor(ApplicationUser user) { return _notificationsService.GetUserProfile(user.UserName); }
public int GetFollowingCount(ApplicationUser user) { return GetFollowingCount(user.UserName); }
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return RedirectToAction("Index", "Manage"); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return RedirectToAction("Index", "Home"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
public string GetHeaderUrl(ApplicationUser user) { var url = "/Images/Headers/"; if (user.Header != null) { return url + user.Header; } return null; }
public int GetQuestionsAskedByUser(ApplicationUser user) { return _context.Answers.Count(answer => answer.Active && answer.QuestionDetail.AskedBy.Id == user.Id); }
private QuestionDetail AddQuestionToContext(string questionContent, ApplicationUser currentUser, ApplicationUser askedToUser) { Question question = InitializeQuestion(questionContent, currentUser); return AddQuestionDetailToContext(currentUser, askedToUser, question); }
private bool IsCurrentUser(ApplicationUser user) { return IsCurrentUser(user.UserName); }
private ProfileViewModel InitializeProfile(ApplicationUser currentUser, ApplicationUser profileUser) { var questionService = new QuestionsService(_context, _pageSize); var isCurrentUserFollowing = IsFollowing(currentUser, profileUser); return new ProfileViewModel() { ScreenName = profileUser.ScreenName, FollowsYou = IsFollowing(profileUser, currentUser), Following = IsFollowing(currentUser, profileUser), IsOwnProfile = currentUser != null && currentUser.UserName == profileUser.UserName, AvatarUrl = GetAvatarUrl(profileUser), HeaderUrl = GetHeaderUrl(profileUser), Bio = profileUser.Bio, Location = profileUser.Location, Homepage = GetHomepage(profileUser.Homepage), User = profileUser, QuestionsReplied = questionService.GetQuestionsAnsweredByUser(profileUser), QuestionsAsked = questionService.GetQuestionsAskedByUser(profileUser), FollowerCount = GetFollowerCount(profileUser), FollowingCount = GetFollowingCount(profileUser), FollowButton = GetFollowButtonViewModel(profileUser.UserName, isCurrentUserFollowing, currentUser != null), AnswerLikesCount = _context.AnswerLikes.Count(like => like.Active && like.ApplicationUserId == profileUser.Id), Age = currentUser.Birthday.HasValue ? Helpers.TimeHelper.GetAge(currentUser.Birthday.Value) : null }; }
private static string SaveImage(ApplicationUser currentUser, Image image, string imageFolder) { var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images"); path = Path.Combine(path, imageFolder); var fileName = currentUser.UserName + "-" + Guid.NewGuid().ToString() + ".jpg"; path = Path.Combine(path, fileName); image.Save(path, ImageFormat.Jpeg); return fileName; }
public ProfileSaveResult SaveProfile(ApplicationUser userModel) { var currentUser = GetCurrentUser(); if (currentUser == null) { return ProfileSaveResult.DatabaseError; } if (_context.Users.Any(user => user.Id != currentUser.Id && user.ScreenName.Equals(userModel.ScreenName, StringComparison.OrdinalIgnoreCase))) { return ProfileSaveResult.DuplicateScreenName; } currentUser.ScreenName = userModel.ScreenName; if (string.IsNullOrWhiteSpace(userModel.Avatar) == false) { var image = ExtractImage(userModel.Avatar); if (image != null) { try { currentUser.Avatar = SaveImage(currentUser, image, "Avatars"); } catch (Exception e) { return ProfileSaveResult.ImageRejected; } } } if (string.IsNullOrWhiteSpace(userModel.Header) == false) { var image = ExtractImage(userModel.Header); try { currentUser.Header = SaveImage(currentUser, image, "Headers"); } catch (Exception e) { return ProfileSaveResult.ImageRejected; } } currentUser.Bio = userModel.Bio; currentUser.Location = userModel.Location; currentUser.Homepage = userModel.Homepage; currentUser.Twitter = userModel.Twitter; currentUser.Birthday = userModel.Birthday; try { _context.SaveChanges(); return ProfileSaveResult.OK; } catch (Exception e) { return ProfileSaveResult.DatabaseError; } }
public bool IsFollowing(ApplicationUser followingUser, ApplicationUser followedUser) { if (followingUser == null || followedUser == null) { return false; } return _context.Relationships.Where(rel => rel.RelationshipType == RelationshipType.Friendship).Any(rel => rel.SourceUser.Id == followingUser.Id && rel.DestinationUser.Id == followedUser.Id); }