Пример #1
0
		public void NotifyMentionedUsers(List<string> mentionedUsers, int messageId) {
			foreach (var user in mentionedUsers) {
				var notification = new DataModels.Notification {
					MessageId = messageId,
					TargetUserId = UserContext.ApplicationUser.Id,
					UserId = user,
					Time = DateTime.Now,
					Type = ENotificationType.Mention,
					Unread = true
				};

				DbContext.Notifications.Add(notification);
			}
		}
Пример #2
0
        public async Task NotifyMentionedUsers(List <string> mentionedUsers, int messageId)
        {
            foreach (var user in mentionedUsers)
            {
                var notification = new DataModels.Notification {
                    MessageId    = messageId,
                    TargetUserId = CurrentUser.Id,
                    UserId       = user,
                    Time         = DateTime.Now,
                    Type         = ENotificationType.Mention,
                    Unread       = true
                };

                DbContext.Notifications.Add(notification);
            }

            await DbContext.SaveChangesAsync();
        }
Пример #3
0
        public DataModels.Message CreateMessageRecord(InputModels.ProcessedMessageInput processedMessage, DataModels.Message replyRecord)
        {
            var parentId = 0;
            var replyId  = 0;

            DataModels.Message parentMessage = null;

            if (replyRecord != null)
            {
                if (replyRecord.ParentId == 0)
                {
                    parentId = replyRecord.Id;
                    replyId  = 0;

                    parentMessage = replyRecord;
                }
                else
                {
                    parentId = replyRecord.ParentId;
                    replyId  = replyRecord.Id;

                    parentMessage = DbContext.Messages.First(item => item.Id == replyRecord.ParentId);
                }
            }

            var currentTime = DateTime.Now;

            var record = new DataModels.Message {
                OriginalBody = processedMessage.OriginalBody,
                DisplayBody  = processedMessage.DisplayBody,
                ShortPreview = processedMessage.ShortPreview,
                LongPreview  = processedMessage.LongPreview,
                Cards        = processedMessage.Cards,

                TimePosted      = currentTime,
                TimeEdited      = currentTime,
                LastReplyPosted = currentTime,

                PostedById    = UserContext.ApplicationUser.Id,
                EditedById    = UserContext.ApplicationUser.Id,
                LastReplyById = UserContext.ApplicationUser.Id,

                ParentId = parentId,
                ReplyId  = replyId,

                Processed = true
            };

            DbContext.Messages.Add(record);
            DbContext.SaveChanges();

            if (replyId > 0)
            {
                replyRecord.LastReplyId     = record.Id;
                replyRecord.LastReplyById   = UserContext.ApplicationUser.Id;
                replyRecord.LastReplyPosted = currentTime;

                DbContext.Update(replyRecord);

                if (replyRecord.PostedById != UserContext.ApplicationUser.Id)
                {
                    var notification = new DataModels.Notification {
                        MessageId    = record.Id,
                        UserId       = replyRecord.PostedById,
                        TargetUserId = UserContext.ApplicationUser.Id,
                        Time         = DateTime.Now,
                        Type         = Enums.ENotificationType.Quote,
                        Unread       = true,
                    };

                    DbContext.Notifications.Add(notification);
                }
            }

            if (parentMessage != null && parentId != replyId)
            {
                parentMessage.ReplyCount++;
                parentMessage.LastReplyId     = record.Id;
                parentMessage.LastReplyById   = UserContext.ApplicationUser.Id;
                parentMessage.LastReplyPosted = currentTime;

                DbContext.Update(parentMessage);

                if (parentMessage.PostedById != UserContext.ApplicationUser.Id)
                {
                    var notification = new DataModels.Notification {
                        MessageId    = record.Id,
                        UserId       = parentMessage.PostedById,
                        TargetUserId = UserContext.ApplicationUser.Id,
                        Time         = DateTime.Now,
                        Type         = Enums.ENotificationType.Reply,
                        Unread       = true,
                    };

                    DbContext.Notifications.Add(notification);
                }
            }

            NotifyMentionedUsers(processedMessage.MentionedUsers, record.Id);

            var topicId = parentId == 0 ? record.Id : parentId;

            UpdateTopicParticipation(topicId, UserContext.ApplicationUser.Id, DateTime.Now);

            DbContext.SaveChanges();

            return(record);
        }
Пример #4
0
        public async Task <ServiceModels.ServiceResponse> AddThought(InputModels.ThoughtInput input)
        {
            var serviceResponse = new ServiceModels.ServiceResponse();

            var messageRecord = DbContext.Messages.Find(input.MessageId);

            if (messageRecord is null)
            {
                serviceResponse.Error($@"No message was found with the id '{input.MessageId}'");
            }

            var smileyRecord = await DbContext.Smileys.FindAsync(input.SmileyId);

            if (messageRecord is null)
            {
                serviceResponse.Error($@"No smiley was found with the id '{input.SmileyId}'");
            }

            if (!serviceResponse.Success)
            {
                return(serviceResponse);
            }

            var existingRecord = await DbContext.MessageThoughts
                                 .SingleOrDefaultAsync(mt =>
                                                       mt.MessageId == messageRecord.Id &&
                                                       mt.SmileyId == smileyRecord.Id &&
                                                       mt.UserId == UserContext.ApplicationUser.Id);

            if (existingRecord is null)
            {
                var messageThought = new DataModels.MessageThought {
                    MessageId = messageRecord.Id,
                    SmileyId  = smileyRecord.Id,
                    UserId    = UserContext.ApplicationUser.Id
                };

                DbContext.MessageThoughts.Add(messageThought);

                if (messageRecord.PostedById != UserContext.ApplicationUser.Id)
                {
                    var notification = new DataModels.Notification {
                        MessageId    = messageRecord.Id,
                        UserId       = messageRecord.PostedById,
                        TargetUserId = UserContext.ApplicationUser.Id,
                        Time         = DateTime.Now,
                        Type         = Enums.ENotificationType.Thought,
                        Unread       = true,
                    };

                    DbContext.Notifications.Add(notification);
                }
            }
            else
            {
                DbContext.Remove(existingRecord);

                var notification = DbContext.Notifications.FirstOrDefault(item => item.MessageId == existingRecord.MessageId && item.TargetUserId == existingRecord.UserId && item.Type == Enums.ENotificationType.Thought);

                if (notification != null)
                {
                    DbContext.Remove(notification);
                }
            }

            DbContext.SaveChanges();

            serviceResponse.RedirectPath = UrlHelper.DirectMessage(input.MessageId);
            return(serviceResponse);
        }
Пример #5
0
        public async Task <ControllerModels.Messages.AddThoughtResult> AddThought(int messageId, int smileyId)
        {
            var result = new ControllerModels.Messages.AddThoughtResult();

            var messageRecord = DbContext.Messages.Find(messageId);

            if (messageRecord is null || messageRecord.Deleted)
            {
                result.Errors.Add(string.Empty, $@"No message was found with the id '{messageId}'");
            }

            var smileyRecord = await DbContext.Smileys.FindAsync(smileyId);

            if (messageRecord is null)
            {
                result.Errors.Add(string.Empty, $@"No smiley was found with the id '{smileyId}'");
            }

            if (result.Errors.Any())
            {
                return(result);
            }

            var existingRecord = await DbContext.MessageThoughts
                                 .FirstOrDefaultAsync(mt =>
                                                      mt.MessageId == messageRecord.Id &&
                                                      mt.SmileyId == smileyRecord.Id &&
                                                      mt.UserId == CurrentUser.Id);

            if (existingRecord is null)
            {
                var messageThought = new DataModels.MessageThought {
                    MessageId = messageRecord.Id,
                    SmileyId  = smileyRecord.Id,
                    UserId    = CurrentUser.Id
                };

                DbContext.MessageThoughts.Add(messageThought);

                if (messageRecord.PostedById != CurrentUser.Id)
                {
                    var notification = new DataModels.Notification {
                        MessageId    = messageRecord.Id,
                        UserId       = messageRecord.PostedById,
                        TargetUserId = CurrentUser.Id,
                        Time         = DateTime.Now,
                        Type         = ENotificationType.Thought,
                        Unread       = true,
                    };

                    DbContext.Notifications.Add(notification);
                }
            }
            else
            {
                DbContext.Remove(existingRecord);

                var notification = await DbContext.Notifications.FirstOrDefaultAsync(item => item.MessageId == existingRecord.MessageId && item.TargetUserId == existingRecord.UserId && item.Type == ENotificationType.Thought);

                if (notification != null)
                {
                    DbContext.Remove(notification);
                }
            }

            await DbContext.SaveChangesAsync();

            result.TopicId   = messageRecord.TopicId;
            result.MessageId = messageRecord.Id;

            return(result);
        }
Пример #6
0
        public async Task <ControllerModels.Messages.CreateReplyResult> CreateReply(ControllerModels.Messages.CreateReplyInput input)
        {
            var result = new ControllerModels.Messages.CreateReplyResult();

            var topic = await DbContext.Topics.FirstOrDefaultAsync(m => m.Id == input.TopicId);

            if (topic is null || topic.Deleted)
            {
                result.Errors.Add(nameof(input.TopicId), $"A record does not exist with ID '{input.TopicId}'");
                return(result);
            }

            var replyTargetMessage = await DbContext.Messages.FirstOrDefaultAsync(m => m.Id == input.Id);

            var previousMessage = await DbContext.Messages.FirstOrDefaultAsync(m =>
                                                                               m.Id == topic.LastMessageId &&
                                                                               m.PostedById == CurrentUser.Id);

            var now         = DateTime.Now;
            var recentReply = (now - topic.LastMessageTimePosted) < (now - now.AddSeconds(-300));

            if (recentReply && !(previousMessage is null) && input.Id == previousMessage.ReplyId)
            {
                return(await CreateMergedReply(topic.LastMessageId, input));
            }

            if (result.Errors.Any())
            {
                return(result);
            }

            var processedMessage = await ProcessMessageInput(input.Body);

            foreach (var error in processedMessage.Errors)
            {
                result.Errors.Add(error.Key, error.Value);
            }

            if (!result.Errors.Any())
            {
                var record = await CreateMessageRecord(processedMessage);

                record.TopicId = topic.Id;
                record.ReplyId = replyTargetMessage?.Id ?? 0;

                DbContext.Update(record);

                topic.ReplyCount++;
                topic.LastMessageId           = record.Id;
                topic.LastMessagePostedById   = CurrentUser.Id;
                topic.LastMessageTimePosted   = now;
                topic.LastMessageShortPreview = record.ShortPreview;

                DbContext.Update(topic);

                if (!(replyTargetMessage is null) && replyTargetMessage.PostedById != CurrentUser.Id)
                {
                    var notification = new DataModels.Notification {
                        MessageId    = record.Id,
                        UserId       = replyTargetMessage.PostedById,
                        TargetUserId = CurrentUser.Id,
                        Time         = DateTime.Now,
                        Type         = ENotificationType.Quote,
                        Unread       = true,
                    };

                    DbContext.Notifications.Add(notification);
                }

                await DbContext.SaveChangesAsync();

                UpdateTopicParticipation(topic.Id, CurrentUser.Id, DateTime.Now);

                result.TopicId   = record.TopicId;
                result.MessageId = record.Id;
            }

            return(result);
        }
Пример #7
0
		public async Task<ServiceModels.ServiceResponse> AddThought(int messageId, int smileyId) {
			var serviceResponse = new ServiceModels.ServiceResponse();

			var messageRecord = DbContext.Messages.Find(messageId);

			if (messageRecord is null) {
				serviceResponse.Error($@"No message was found with the id '{messageId}'");
			}

			var smileyRecord = await DbContext.Smileys.FindAsync(smileyId);

			if (messageRecord is null) {
				serviceResponse.Error($@"No smiley was found with the id '{smileyId}'");
			}

			if (!serviceResponse.Success) {
				return serviceResponse;
			}

			var existingRecord = await DbContext.MessageThoughts
				.FirstOrDefaultAsync(mt =>
					mt.MessageId == messageRecord.Id
					&& mt.SmileyId == smileyRecord.Id
					&& mt.UserId == UserContext.ApplicationUser.Id);

			if (existingRecord is null) {
				var messageThought = new DataModels.MessageThought {
					MessageId = messageRecord.Id,
					SmileyId = smileyRecord.Id,
					UserId = UserContext.ApplicationUser.Id
				};

				DbContext.MessageThoughts.Add(messageThought);

				if (messageRecord.PostedById != UserContext.ApplicationUser.Id) {
					var notification = new DataModels.Notification {
						MessageId = messageRecord.Id,
						UserId = messageRecord.PostedById,
						TargetUserId = UserContext.ApplicationUser.Id,
						Time = DateTime.Now,
						Type = ENotificationType.Thought,
						Unread = true,
					};

					DbContext.Notifications.Add(notification);
				}
			}
			else {
				DbContext.Remove(existingRecord);

				var notification = DbContext.Notifications.FirstOrDefault(item => item.MessageId == existingRecord.MessageId && item.TargetUserId == existingRecord.UserId && item.Type == ENotificationType.Thought);

				if (notification != null) {
					DbContext.Remove(notification);
				}
			}

			DbContext.SaveChanges();

			await ForumHub.Clients.All.SendAsync("updated-message", new HubModels.Message {
				TopicId = messageRecord.ParentId > 0 ? messageRecord.ParentId : messageRecord.Id,
				MessageId = messageRecord.Id
			});

			serviceResponse.RedirectPath = UrlHelper.DisplayMessage(messageId);
			return serviceResponse;
		}