Esempio n. 1
0
 public MessageRepository(
     ApplicationDbContext dbContext,
     UserContext userContext,
     AccountRepository accountRepository,
     BoardRepository boardRepository,
     SmileyRepository smileyRepository,
     IImageStore imageStore,
     BBCodeParser bbcParser,
     GzipWebClient webClient,
     ImgurClient imgurClient,
     YouTubeClient youTubeClient,
     ILogger <MessageRepository> log
     )
 {
     DbContext         = dbContext;
     CurrentUser       = userContext;
     AccountRepository = accountRepository;
     BoardRepository   = boardRepository;
     SmileyRepository  = smileyRepository;
     ImageStore        = imageStore;
     BBCParser         = bbcParser;
     WebClient         = webClient;
     ImgurClient       = imgurClient;
     YouTubeClient     = youTubeClient;
     Log = log;
 }
Esempio n. 2
0
		public MessageRepository(
			ApplicationDbContext dbContext,
			UserContext userContext,
			AccountRepository accountRepository,
			BoardRepository boardRepository,
			RoleRepository roleRepository,
			SmileyRepository smileyRepository,
			IHubContext<ForumHub> forumHub,
			IActionContextAccessor actionContextAccessor,
			IUrlHelperFactory urlHelperFactory,
			IImageStore imageStore,
			BBCodeParser bbcParser,
			GzipWebClient webClient,
			ImgurClient imgurClient,
			YouTubeClient youTubeClient,
			ILogger<MessageRepository> log
		) {
			DbContext = dbContext;
			UserContext = userContext;
			AccountRepository = accountRepository;
			BoardRepository = boardRepository;
			RoleRepository = roleRepository;
			SmileyRepository = smileyRepository;
			ForumHub = forumHub;
			UrlHelper = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
			ImageStore = imageStore;
			BBCParser = bbcParser;
			WebClient = webClient;
			ImgurClient = imgurClient;
			YouTubeClient = youTubeClient;
			Log = log;
		}
Esempio n. 3
0
 public TopicRepository(
     ApplicationDbContext dbContext,
     UserContext userContext,
     BoardRepository boardRepository,
     BookmarkRepository bookmarkRepository,
     MessageRepository messageRepository,
     NotificationRepository notificationRepository,
     RoleRepository roleRepository,
     SmileyRepository smileyRepository,
     AccountRepository accountRepository,
     IActionContextAccessor actionContextAccessor,
     IUrlHelperFactory urlHelperFactory
     )
 {
     DbContext              = dbContext;
     UserContext            = userContext;
     AccountRepository      = accountRepository;
     BoardRepository        = boardRepository;
     BookmarkRepository     = bookmarkRepository;
     MessageRepository      = messageRepository;
     NotificationRepository = notificationRepository;
     RoleRepository         = roleRepository;
     SmileyRepository       = smileyRepository;
     UrlHelper              = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
 }
Esempio n. 4
0
		public async Task ProcessSmileys(InputModels.ProcessedMessageInput processedMessageInput) {
			var smileys = await SmileyRepository.Records();

			for (var i = 0; i < smileys.Count(); i++) {
				var pattern = $@"SMILEY_{i}_INDEX";
				var replacement = $"<img src='{smileys[i].Path}' />";
				processedMessageInput.DisplayBody = Regex.Replace(processedMessageInput.DisplayBody, pattern, replacement);
			}
		}
Esempio n. 5
0
        /// <summary>
        /// Some minor housekeeping on the message before we get into the heavy lifting.
        /// </summary>
        public async Task <InputModels.ProcessedMessageInput> PreProcess(InputModels.ProcessedMessageInput processedMessage)
        {
            processedMessage.DisplayBody = processedMessage.DisplayBody.Trim();

            if (string.IsNullOrEmpty(processedMessage.DisplayBody))
            {
                throw new ArgumentException("Message body is empty.");
            }

            var smileys = await SmileyRepository.Records();

            // Ensures the smileys are safe from other HTML processing.
            for (var i = 0; i < smileys.Count(); i++)
            {
                var pattern     = $@"(^|[\r\n\s]){Regex.Escape(smileys[i].Code)}(?=$|[\r\n\s])";
                var replacement = $"$1SMILEY_{i}_INDEX";
                processedMessage.DisplayBody = Regex.Replace(processedMessage.DisplayBody, pattern, replacement, RegexOptions.Singleline);
            }

            processedMessage.DisplayBody = BBCParser.ToHtml(processedMessage.DisplayBody);

            return(processedMessage);
        }
Esempio n. 6
0
		/// <summary>
		/// Some minor housekeeping on the message before we get into the heavy lifting.
		/// </summary>
		public async Task<InputModels.ProcessedMessageInput> PreProcessMessageInput(string messageBody) {
			var processedMessageInput = new InputModels.ProcessedMessageInput {
				OriginalBody = messageBody ?? string.Empty,
				DisplayBody = messageBody ?? string.Empty,
				MentionedUsers = new List<string>()
			};

			processedMessageInput.DisplayBody = processedMessageInput.DisplayBody.Trim();

			if (string.IsNullOrEmpty(processedMessageInput.DisplayBody)) {
				throw new ArgumentException("Message body is empty.");
			}

			var smileys = await SmileyRepository.Records();

			// Ensures the smileys are safe from other HTML processing.
			for (var i = 0; i < smileys.Count(); i++) {
				var pattern = $@"(^|[\r\n\s]){Regex.Escape(smileys[i].Code)}(?=$|[\r\n\s])";
				var replacement = $"$1SMILEY_{i}_INDEX";
				processedMessageInput.DisplayBody = Regex.Replace(processedMessageInput.DisplayBody, pattern, replacement, RegexOptions.Singleline);
			}

			return processedMessageInput;
		}
Esempio n. 7
0
        /// <summary>
        /// Builds a collection of Message objects. The message ids should already have been filtered by permissions.
        /// </summary>
        public async Task <List <ViewModels.Messages.DisplayMessage> > GetMessages(List <int> messageIds)
        {
            var thoughtQuery = from mt in DbContext.MessageThoughts
                               where messageIds.Contains(mt.MessageId)
                               select new ViewModels.Messages.MessageThought {
                MessageId = mt.MessageId.ToString(),
                UserId    = mt.UserId,
                SmileyId  = mt.SmileyId,
            };

            var thoughts = thoughtQuery.ToList();
            var smileys  = await SmileyRepository.Records();

            var users = await AccountRepository.Records();

            foreach (var item in thoughts)
            {
                var smiley = smileys.FirstOrDefault(r => r.Id == item.SmileyId);
                var user   = users.FirstOrDefault(r => r.Id == item.UserId);

                item.Path    = smiley.Path;
                item.Thought = smiley.Thought.Replace("{user}", user.DecoratedName);
            }

            var messageQuery = from message in DbContext.Messages
                               where messageIds.Contains(message.Id)
                               select new ViewModels.Messages.DisplayMessage {
                Id           = message.Id.ToString(),
                TopicId      = message.TopicId,
                ReplyId      = message.ReplyId,
                Body         = message.DisplayBody,
                Cards        = message.Cards,
                OriginalBody = message.OriginalBody,
                PostedById   = message.PostedById,
                TimePosted   = message.TimePosted,
                TimeEdited   = message.TimeEdited,
                RecordTime   = message.TimeEdited
            };

            var messages = await messageQuery.ToListAsync();

            foreach (var message in messages)
            {
                message.ShowControls = true;

                if (message.ReplyId > 0)
                {
                    var reply = DbContext.Messages.FirstOrDefault(item => item.Id == message.ReplyId && !item.Deleted);

                    if (!(reply is null))
                    {
                        if (string.IsNullOrEmpty(reply.ShortPreview))
                        {
                            reply.ShortPreview = "No preview";
                        }

                        message.ReplyBody    = reply.DisplayBody;
                        message.ReplyPreview = reply.ShortPreview;

                        var replyPostedBy = users.FirstOrDefault(item => item.Id == reply.PostedById);
                        message.ReplyPostedBy = replyPostedBy?.DecoratedName ?? "A user";
                    }
                }

                var topic = DbContext.Topics.Find(message.TopicId);

                message.IsFirstMessage = topic.FirstMessageId.ToString() == message.Id;
                message.CanEdit        = CurrentUser.IsAdmin || CurrentUser.Id == message.PostedById;
                message.CanDelete      = CurrentUser.IsAdmin || CurrentUser.Id == message.PostedById;
                message.CanReply       = CurrentUser.IsAuthenticated;
                message.CanThought     = CurrentUser.IsAuthenticated;
                message.CanQuote       = CurrentUser.IsAuthenticated;
                message.Thoughts       = thoughts.Where(item => item.MessageId == message.Id).ToList();

                var postedBy = users.FirstOrDefault(item => item.Id == message.PostedById);

                if (!(postedBy is null))
                {
                    message.PostedByAvatarPath = postedBy.AvatarPath;
                    message.PostedByName       = postedBy.DecoratedName;
                    message.Poseys             = postedBy.Poseys;

                    if (DateTime.Now.Date == new DateTime(DateTime.Now.Year, postedBy.Birthday.Month, postedBy.Birthday.Day).Date)
                    {
                        message.Birthday = true;
                    }
                }
            }

            return(messages);
        }