public static TweetVM MapTweetToViewModel(Tweet tweet, ApplicationUser user) { return new TweetVM() { TweetId = tweet.Id.ToString(), Content = tweet.Content, ImageUrl = (tweet.FilePath != null) ? tweet.FilePath : string.Empty, ImageMimeType = (tweet.FileMimeType != null) ? tweet.FileMimeType : string.Empty, TweetedByUserId = user.Id, TweetedByFullName = user.FullName, TweetedByUserName = user.UserName, TweetedOn = tweet.CreatedOn, RetweetedFromUserName = tweet.RetweetedFromUserName, Latitude = tweet.Latitude, Longitude = tweet.Longitude }; }
public async Task<JsonResult> PostTweet(PostTweetVM tweet) { var result = new JsonServiceResult<TweetVM>(); try { var user = await GetCurrentUserAsync(); var now = DateTime.Now; var file = tweet.File; var newTweet = new Tweet() { Content = tweet.Content, UserId = user.Id.ToObjectId(), CreatedOn = now }; if (tweet.LocationEnabled) { double latitude; double longitude; if (Double.TryParse(tweet.Latitude, NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo, out latitude) && Double.TryParse(tweet.Longitude, NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo, out longitude)) { newTweet.Latitude = latitude; newTweet.Longitude = longitude; } } if (file != null) { var allowedExtensions = new string[] { ".jpg", ".png", ".gif" }; if (file.Length > 0) { var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition); var fileName = parsedContentDisposition.FileName.Trim('"'); string ext = Path.GetExtension(fileName).ToLower(); if (allowedExtensions.Contains(ext)) { var userAsciiName = user.NormalizedUserName.ToAsciiString(); var fileNameHash = fileName.GetHashCode(); if (fileNameHash < 0) fileNameHash = fileNameHash * -1; var mediaDir = Path.Combine(_environment.WebRootPath, "media"); var userDir = Path.Combine(mediaDir, userAsciiName); Directory.CreateDirectory(userDir); var filePath = Path.Combine(userDir, fileNameHash.ToString()); await file.SaveAsAsync(string.Format("{0}{1}", filePath, ext)); newTweet.FileMimeType = file.ContentType; newTweet.FilePath = string.Format("/media/{0}/{1}{2}", userAsciiName, fileNameHash, ext); } } } await _tweetManager.CreateTweet(newTweet); var mentionedUserNames = TweetUtils.ExtractUserMentions(tweet.Content); foreach (var mentionedUserName in mentionedUserNames) { var mentionedUser = await _userManager.FindByNameAsync(mentionedUserName.ToUpper()); if (mentionedUser != null) { await _notificationManager.CreateNewNotification(mentionedUser.Id.ToObjectId(), NotificationType.Mention, user.Id.ToObjectId(), newTweet.Id); } } var viewModel = new TweetVM(); viewModel.Content = tweet.Content; viewModel.TweetedByFullName = user.FullName; viewModel.TweetedByUserName = user.UserName; viewModel.TweetedOn = now; result.Value = viewModel; result.IsValid = true; } catch (Exception ex) { result.Message = ex.Message; } return Json(result); }
public async Task CreateTweet(Tweet tweet) { await _repository.Tweets.InsertOneAsync(tweet); }