Esempio n. 1
0
        /// <summary>
        /// Remove a 'Retweet' from the Tweet if it's already exists
        /// or add new if it's not
        /// </summary>
        /// <param name="tweetId"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <int> UpdateRetweets(int tweetId, TwitterCopyUser user)
        {
            var tweet = await _tweetRepository.GetTweetWithRetweetsAsync(tweetId);

            var retweet = new Retweet
            {
                Tweet = tweet,
                User  = user
            };

            var dupe = tweet.Retweets.FirstOrDefault(x => x.TweetId == tweet.Id && x.UserId == user.Id);

            if (dupe == null)
            {
                // If no duplicate was found
                // Add new retweet to the database
                _retweetRepository.Add(retweet);
            }
            else
            {
                // If duplicate was found
                // Delete dupe instead of retweer because
                // retweet doesn't have Id value
                _retweetRepository.Delete(dupe);
            }

            await _retweetRepository.SaveAsync();

            return(tweet.Retweets.Count);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates new Tweet entity from 'replyText' and
        /// populates RepliesTo property with 'replyTo',
        /// and with its previous replies.
        /// </summary>
        /// <param name="replyText"></param>
        /// <param name="user"></param>
        /// <param name="replyTo"></param>
        /// <returns>Created Tweet entity (reply)</returns>
        public async Task <Tweet> AddReplyAsync(string replyText, TwitterCopyUser user, Tweet replyTo)
        {
            var replyFrom = new Tweet
            {
                Text      = replyText,
                User      = user,
                RepliesTo = new List <TweetToTweet>()
            };

            // Add main replyTo
            replyFrom.RepliesTo.Add(new TweetToTweet
            {
                ReplyFrom = replyFrom,
                ReplyTo   = replyTo
            });
            // Add reply to the RepliesTo property of the replyTo tweet
            // so that you reply not to one tweet but
            // to the whole 'conversation'
            foreach (var tweet in replyTo.RepliesTo)
            {
                replyFrom.RepliesTo.Add(new TweetToTweet
                {
                    ReplyFrom = replyFrom,
                    ReplyTo   = tweet.ReplyTo
                });
            }

            _tweetRepository.Add(replyFrom);
            await _tweetRepository.SaveAsync();

            return(replyFrom);
        }
Esempio n. 3
0
        /// <summary>
        /// Remove a 'Like' from the Tweet if it's already exists
        /// or add new if it's not
        /// </summary>
        /// <param name="tweetId"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <int> UpdateLikes(int tweetId, TwitterCopyUser user)
        {
            var tweet = await _tweetRepository.GetTweetWithLikesAsync(tweetId);

            // Apply the user and tweet object from above to the new Like
            var like = new Like
            {
                Tweet = tweet,
                User  = user,
            };

            // Check if the user already has like on this tweet
            var dupe = tweet.Likes.FirstOrDefault(x => x.UserId.Equals(user.Id));

            if (dupe == null)
            {
                // If no duplicate was found
                // Add new like to the database
                _likeRepository.Add(like);
            }
            else
            {
                // If duplicate was found then
                // Delete dupe instead of like because
                // like doesn't have Id values
                _likeRepository.Delete(dupe);
            }

            await _likeRepository.SaveAsync();

            return(tweet.Likes.Count);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a new Tweet entity and saves it to the database
        /// </summary>
        /// <param name="text"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task AddTweet(string text, TwitterCopyUser user)
        {
            var tweet = new Tweet
            {
                Text = text,
                User = user
            };

            _tweetRepository.Add(tweet);
            await _tweetRepository.SaveAsync();
        }
Esempio n. 5
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                // TODO: Function for generating Slug if the UserName has white-spaces or other characters
                var user = new TwitterCopyUser
                {
                    UserName = Input.UserName,
                    Email    = Input.Email,
                    Slug     = Input.UserName.GenerateSlug(),
                    Avatar   = Globals.DefaultAvatar,
                    Banner   = Globals.DefaultBanner
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    // Prevent newly registered users from being automatically logged on
                    //await _signInManager.SignInAsync(user, isPersistent: false);
                    Message = "Check your email and confirm your account, you must be confirmed before you can log in.";
                    return(RedirectToPage("/Info"));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
        private async Task LoadSharedKeyAndQrCodeUriAsync(TwitterCopyUser user)
        {
            // Load the authenticator key & QR code URI to display on the form
            var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);

            if (string.IsNullOrEmpty(unformattedKey))
            {
                await _userManager.ResetAuthenticatorKeyAsync(user);

                unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
            }

            SharedKey = FormatKey(unformattedKey);

            var email = await _userManager.GetEmailAsync(user);

            AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey);
        }
Esempio n. 7
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                var user = new TwitterCopyUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            LoginProvider = info.LoginProvider;
            ReturnUrl     = returnUrl;
            return(Page());
        }
Esempio n. 8
0
 public bool CheckFollower(TwitterCopyUser user, Guid followerId)
 {
     return(user.Followers.Any(f => f.FollowerId.Equals(followerId)));
 }
Esempio n. 9
0
 public async Task UpdateUserAsync(TwitterCopyUser user)
 {
     _userRepository.Update(user);
     await _userRepository.SaveAsync();
 }