public async Task ShowNewUserNotification(string userId, int courseId)
        {
            //for user who created this course
            Course course = repository.Courses
                            .FirstOrDefault(c => c.CourseId == courseId);

            Notification notification = repository.Notifications
                                        .FirstOrDefault(n => n.Subject == "UserJoinCourse" &&
                                                        n.For == "Course" && n.ForId == courseId);
            NotificationCard notificationCard = notification.NotificationCards
                                                .LastOrDefault(c => c.CreatedBy == userId);

            AppUser notificationCardCreatedBy = userManager.Users
                                                .FirstOrDefault(u => u.Id == notificationCard.CreatedBy);
            string notificationCardCreatedByPhoto = (notificationCardCreatedBy.ProfilePhotoUrl != null) ?
                                                    $"/UsersData/{notificationCardCreatedBy.ProfilePhotoUrl}" : "/defaultAvatar.png";

            if (userId != course.CreatedBy.Id)
            {
                //for user who created this course
                await Clients.All.SendAsync("ShowNewUserOnCourseNotification", course.CreatedBy.Id,
                                            notificationCard.NotificationCardId, course.CourseId,
                                            notificationCard.Msg, notificationCardCreatedByPhoto);
            }
        }
예제 #2
0
        public RedirectToActionResult Redirect(string id, int notificationCardId,
                                               string For, int ForId)
        {
            if (For == "Video")
            {
                NotificationCard card = repository.NotificationCards
                                        .FirstOrDefault(c => c.NotificationCardId == notificationCardId);
                if (card.NotificationViews.FirstOrDefault(v => v.UserId == id) == null)
                {
                    repository.NewNotificationCardView(notificationCardId, id);
                }

                return(RedirectToAction("Index", "Recorder",
                                        new { id, videoId = ForId }));
            }
            else if (For == "Course")
            {
                NotificationCard card = repository.NotificationCards
                                        .FirstOrDefault(c => c.NotificationCardId == notificationCardId);
                if (card.NotificationViews.FirstOrDefault(v => v.UserId == id) == null)
                {
                    repository.NewNotificationCardView(notificationCardId, id);
                }

                return(RedirectToAction("CourseDetails", "Courses",
                                        new { id, courseId = ForId }));
            }

            else if (For == "Group")
            {
                NotificationCard card = repository.NotificationCards
                                        .FirstOrDefault(c => c.NotificationCardId == notificationCardId);
                if (card.NotificationViews.FirstOrDefault(v => v.UserId == id) == null)
                {
                    repository.NewNotificationCardView(notificationCardId, id);
                }

                return(RedirectToAction("GroupDetails", "Groups", new
                {
                    id,
                    groupId = ForId
                }));
            }

            return(RedirectToAction("Error", "Error",
                                    new { id, errMsg = "" }));
        }
예제 #3
0
        /// <summary>
        /// This method is used to send notifications to all channels of a group activity.
        /// </summary>
        /// <param name="request">notification request object.</param>
        /// <returns>Task.</returns>
        private async Task SendNotificationsAsync(NotificationRequest request)
        {
            if (request != null)
            {
                string serviceUrl = request.ServiceUrl;
                MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);
                foreach (GroupNotification channel in request.GroupNotificationChannels)
                {
                    string teamsChannelId        = channel.RowKey;
                    var    conversationReference = new ConversationReference()
                    {
                        ChannelId = Constants.Channel,
                        Bot       = new ChannelAccount()
                        {
                            Id = this.microsoftAppCredentials.MicrosoftAppId
                        },
                        ServiceUrl   = serviceUrl,
                        Conversation = new ConversationAccount()
                        {
                            ConversationType = Constants.ChannelConversationType, IsGroup = true, Id = teamsChannelId, TenantId = this.tenantId
                        },
                    };

                    this.logger.LogInformation($"sending notification to channelId- {teamsChannelId}");
                    var card = NotificationCard.GetNotificationCardAttachment(request, channel.ChannelName);
                    try
                    {
                        await retryPolicy.ExecuteAsync(async() =>
                        {
                            await((BotFrameworkAdapter)this.adapter).ContinueConversationAsync(
                                this.microsoftAppCredentials.MicrosoftAppId,
                                conversationReference,
                                async(conversationTurnContext, conversationCancellationToken) =>
                            {
                                await conversationTurnContext.SendActivityAsync(MessageFactory.Attachment(card));
                            },
                                CancellationToken.None);
                        });
                    }
                    catch (Exception ex)
                    {
                        this.logger.LogError(ex, "Error while sending notification to channel from background service.");
                    }
                }
            }
        }
예제 #4
0
        private async Task NotifyGroupMembersInChannel(IList <TeamsChannelAccount> groupMembers, string channelId, GroupDetail groupDetail, string groupActivityCreator, string groupingMessage, ITurnContext <IInvokeActivity> turnContext, CancellationToken cancellationToken)
        {
            var activity    = turnContext.Activity as Activity;
            var channelData = activity.GetChannelData <TeamsChannelData>();

            channelData.Channel.Id = channelId;

            // create card
            var notificationCard = MessageFactory.Attachment(NotificationCard.GetNewMemberNotificationCard(groupDetail, groupActivityCreator));

            // get mentions
            var mentionActivity = this.GetGroupMembersToMentionActivity(groupMembers);

            var conversationParameters = new ConversationParameters
            {
                Activity = (Activity)notificationCard,
                Bot      = new ChannelAccount {
                    Id = this.microsoftAppCredentials.MicrosoftAppId
                },
                ChannelData = channelData,
                TenantId    = channelData.Tenant.Id,
            };

            await((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(
                Constants.Channel,
                turnContext.Activity.ServiceUrl,
                this.microsoftAppCredentials,
                conversationParameters,
                async(newTurnContext, newCancellationToken) =>
            {
                await turnContext.Adapter.ContinueConversationAsync(
                    this.microsoftAppCredentials.MicrosoftAppId,
                    newTurnContext.Activity.GetConversationReference(),
                    async(conversationTurnContext, conversationCancellationToken) =>
                {
                    mentionActivity.ApplyConversationReference(conversationTurnContext.Activity.GetConversationReference());
                    await conversationTurnContext.SendActivityAsync(mentionActivity, conversationCancellationToken);
                },
                    newCancellationToken);
            },
                cancellationToken).ConfigureAwait(false);
        }
        //comment like/dislike notifications
        public async Task ShowNotification(string userId, int commentId)
        {
            Comment comment = repository.Comments
                              .FirstOrDefault(c => c.Id == commentId);
            Notification notification = repository.Notifications
                                        .FirstOrDefault(n => n.Subject == "LikeDislikeComment" &&
                                                        n.For == "Comment" && n.ForId == comment.Id);
            NotificationCard notificationCard = notification.NotificationCards
                                                .LastOrDefault(c => c.CreatedBy == userId);
            AppUser notificationCardCreatedBy = userManager.Users
                                                .FirstOrDefault(u => u.Id == notificationCard.CreatedBy);
            string notificationCardCreatedByPhoto = (notificationCardCreatedBy.ProfilePhotoUrl != null) ?
                                                    $"/UsersData/{notificationCardCreatedBy.ProfilePhotoUrl}" : "/defaultAvatar.png";

            //if NotificationCreatedBy == CommentCreatedBy
            //DO NOT SHOW notification
            if (userId != comment.CreatedBy)
            {
                await Clients.All.SendAsync("ShowNewNotification", comment.CreatedBy,
                                            notificationCard.NotificationCardId, comment.For, comment.ForId,
                                            notificationCard.Msg, notificationCardCreatedByPhoto);
            }
        }
        //comment for video
        public async Task ShowVideoCommentNotification(string userId, int videoId)
        {
            //for all users who commented this course
            //and user who created this course
            Video video = repository.Videos
                          .FirstOrDefault(v => v.Id == videoId);

            Notification notification = repository.Notifications
                                        .FirstOrDefault(n => n.Subject == "NewComment" &&
                                                        n.For == "Video" && n.ForId == videoId);
            NotificationCard notificationCard = notification.NotificationCards
                                                .LastOrDefault(c => c.CreatedBy == userId);
            AppUser notificationCardCreatedBy = userManager.Users
                                                .FirstOrDefault(u => u.Id == notificationCard.CreatedBy);
            string notificationCardCreatedByPhoto = (notificationCardCreatedBy.ProfilePhotoUrl != null) ?
                                                    $"/UsersData/{notificationCardCreatedBy.ProfilePhotoUrl}" : "/defaultAvatar.png";

            if (userId != video.CreatedBy)
            {
                //for user who created this course
                await Clients.All.SendAsync("ShowNewVideoCommentNotification", video.CreatedBy,
                                            notificationCard.NotificationCardId, videoId,
                                            notificationCard.Msg, notificationCardCreatedByPhoto);
            }

            //for all users who commented this course
            foreach (string commentCreatedBy in repository.Comments
                     .Where(c => c.For == "Video" && c.ForId == videoId &&
                            c.CreatedBy != video.CreatedBy &&
                            c.CreatedBy != notificationCardCreatedBy.Id)
                     .Select(c => c.CreatedBy).Distinct())
            {
                await Clients.All.SendAsync("ShowNewVideoCommentNotification", commentCreatedBy,
                                            notificationCard.NotificationCardId, videoId,
                                            notificationCard.Msg, notificationCardCreatedByPhoto);
            }
        }
        public async Task ShowRepresentationNewRating(string userId, int representationId)
        {
            //for user who created representation
            Representation representation = repository.Representations
                                            .FirstOrDefault(r => r.RepresentationId == representationId);
            Notification notification = repository.Notifications
                                        .FirstOrDefault(n => n.Subject == "RepresentationRating" &&
                                                        n.For == "Video" && n.ForId == representation.VideoId);
            NotificationCard notificationCard = notification.NotificationCards
                                                .LastOrDefault(c => c.CreatedBy == userId);

            AppUser notificationCardCreatedBy = userManager.Users
                                                .FirstOrDefault(u => u.Id == notificationCard.CreatedBy);
            string notificationCardCreatedByPhoto = (notificationCardCreatedBy.ProfilePhotoUrl != null) ?
                                                    $"/UsersData/{notificationCardCreatedBy.ProfilePhotoUrl}" : "/defaultAvatar.png";

            if (userId != representation.CreatedBy.Id)
            {
                //for user who created this course
                await Clients.All.SendAsync("ShowNewRatingNotification", representation.CreatedBy.Id,
                                            notificationCard.NotificationCardId, representation.VideoId,
                                            notificationCard.Msg, notificationCardCreatedByPhoto);
            }
        }
        public IViewComponentResult Invoke(string userId)
        {
            List <NotificationViewModel> notifications = new List <NotificationViewModel>();

            foreach (Notification notification in repository.Notifications
                     .OrderByDescending(n => n.NotificationDateAdded))
            {
                if (notification.For == "Video")
                {
                    //for LikeVideo, DislikeVideo, NewComment
                    if (userId == repository.Videos
                        .FirstOrDefault(v => v.Id == notification.ForId).CreatedBy &&
                        notification.Subject != "LikeComment" &&
                        notification.Subject != "DislikeComment")
                    {
                        NotificationCard notificationCard = notification.NotificationCards
                                                            .LastOrDefault(nc => nc.CreatedBy != userId);

                        if (notificationCard != null)
                        {
                            NotificationViewModel nModel = new NotificationViewModel
                            {
                                NotificationCard = notificationCard,
                                Views            = notificationCard.NotificationViews.AsQueryable(),
                                UserProfilePhoto = userManager.Users
                                                   .FirstOrDefault(u => u.Id == notificationCard.CreatedBy)
                                                   .ProfilePhotoUrl,
                                For   = notification.For,
                                ForId = notification.ForId
                            };

                            notifications.Add(nModel);
                        }
                    }
                    //For New Comment
                    else if (notification.Subject == "NewComment")
                    {
                        IQueryable <Comment> comments = repository.Comments
                                                        .Where(c => c.For == "Video" && c.ForId == notification.ForId);

                        if (comments.FirstOrDefault(c => c.CreatedBy == userId) != null)
                        {
                            NotificationCard notificationCard = notification.NotificationCards
                                                                .LastOrDefault(nc => nc.CreatedBy != userId);

                            if (notificationCard != null)
                            {
                                NotificationViewModel nModel = new NotificationViewModel
                                {
                                    NotificationCard = notificationCard,
                                    Views            = notificationCard.NotificationViews.AsQueryable(),
                                    UserProfilePhoto = userManager.Users
                                                       .FirstOrDefault(u => u.Id == notificationCard.CreatedBy)
                                                       .ProfilePhotoUrl,
                                    For   = notification.For,
                                    ForId = notification.ForId
                                };

                                notifications.Add(nModel);
                            }
                        }
                    }
                    //new representation
                    else if (notification.Subject == "NewRepresentation")
                    {
                        foreach (Course course in repository.Courses
                                 .Where(c => c.CreatedBy.Id == userId))
                        {
                            foreach (Presentation presentation in course.Presentations)
                            {
                                foreach (Representation repres in presentation.Representations)
                                {
                                    if (repres.VideoId == notification.ForId)
                                    {
                                        NotificationCard notificationCard = notification.NotificationCards
                                                                            .LastOrDefault(nc => nc.CreatedBy != userId);

                                        if (notificationCard != null)
                                        {
                                            NotificationViewModel nModel = new NotificationViewModel
                                            {
                                                NotificationCard = notificationCard,
                                                Views            = notificationCard.NotificationViews.AsQueryable(),
                                                UserProfilePhoto = userManager.Users
                                                                   .FirstOrDefault(u => u.Id == notificationCard.CreatedBy)
                                                                   .ProfilePhotoUrl,
                                                For   = notification.For,
                                                ForId = notification.ForId
                                            };

                                            notifications.Add(nModel);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else if (notification.For == "Course")
                {
                    Course course = repository.Courses
                                    .FirstOrDefault(c => c.CourseId == notification.ForId);

                    //if currentuser == courseCreatedBy
                    if (userId == course.CreatedBy.Id)
                    {
                        NotificationCard notificationCard = notification.NotificationCards
                                                            .LastOrDefault(nc => nc.CreatedBy != userId);

                        if (notificationCard != null)
                        {
                            NotificationViewModel nModel = new NotificationViewModel
                            {
                                NotificationCard = notificationCard,
                                Views            = notificationCard.NotificationViews.AsQueryable(),
                                UserProfilePhoto = userManager.Users
                                                   .FirstOrDefault(u => u.Id == notificationCard.CreatedBy)
                                                   .ProfilePhotoUrl,
                                For   = notification.For,
                                ForId = notification.ForId
                            };

                            notifications.Add(nModel);
                        }
                    }
                    //if currentUser commented on this course
                    else if (notification.Subject == "NewComment")
                    {
                        IQueryable <Comment> comments = repository.Comments
                                                        .Where(c => c.For == "Course" && c.ForId == notification.ForId);

                        if (comments.FirstOrDefault(c => c.CreatedBy == userId) != null)
                        {
                            NotificationCard notificationCard = notification.NotificationCards
                                                                .LastOrDefault(nc => nc.CreatedBy != userId);

                            if (notificationCard != null)
                            {
                                NotificationViewModel nModel = new NotificationViewModel
                                {
                                    NotificationCard = notificationCard,
                                    Views            = notificationCard.NotificationViews.AsQueryable(),
                                    UserProfilePhoto = userManager.Users
                                                       .FirstOrDefault(u => u.Id == notificationCard.CreatedBy)
                                                       .ProfilePhotoUrl,
                                    For   = notification.For,
                                    ForId = notification.ForId
                                };

                                notifications.Add(nModel);
                            }
                        }
                    }
                }
                else if (notification.For == "Comment")
                {
                    //For Like and Dislike Comment
                    if (notification.Subject == "LikeDislikeComment")
                    {
                        //notification.for = "Comment"
                        //notification.forID = CommentId
                        //get comment where commentId == notification.forID
                        Comment comment = repository.Comments
                                          .FirstOrDefault(c => c.Id == notification.ForId);

                        //if user with user.id == userId
                        //created this comment
                        if (comment.CreatedBy == userId)
                        {
                            NotificationCard notificationCard = notification.NotificationCards
                                                                .LastOrDefault(n => n.CreatedBy != userId);

                            if (notificationCard != null)
                            {
                                NotificationViewModel nModel = new NotificationViewModel
                                {
                                    NotificationCard = notificationCard,
                                    Views            = notificationCard.NotificationViews.AsQueryable(),
                                    UserProfilePhoto = userManager.Users
                                                       .FirstOrDefault(u => u.Id == notificationCard.CreatedBy)
                                                       .ProfilePhotoUrl,
                                    For   = comment.For,
                                    ForId = comment.ForId
                                };

                                notifications.Add(nModel);
                            }
                        }
                    }
                }
                if (notification.For == "Group")
                {
                    if (notification.Subject == "NewGroupUser" && notification.ForUserId != null)
                    {
                        if (notification.ForUserId == userId)
                        {
                            NotificationCard notificationCard = notification.NotificationCards
                                                                .LastOrDefault(n => n.CreatedBy != userId);

                            if (notificationCard != null)
                            {
                                NotificationViewModel nModel = new NotificationViewModel
                                {
                                    NotificationCard = notificationCard,
                                    Views            = notificationCard.NotificationViews.AsQueryable(),
                                    UserProfilePhoto = userManager.Users
                                                       .FirstOrDefault(u => u.Id == notificationCard.CreatedBy)
                                                       .ProfilePhotoUrl,
                                    For   = "Group",
                                    ForId = notification.ForId
                                };

                                notifications.Add(nModel);
                            }
                        }
                    }
                    else
                    {
                        if (repository.GroupUsers.Any(g => g.GroupId == notification.ForId &&
                                                      g.AppUserId == userId))
                        {
                            NotificationCard notificationCard = notification.NotificationCards
                                                                .LastOrDefault(n => n.CreatedBy != userId);

                            if (notificationCard != null)
                            {
                                NotificationViewModel nModel = new NotificationViewModel
                                {
                                    NotificationCard = notificationCard,
                                    Views            = notificationCard.NotificationViews.AsQueryable(),
                                    UserProfilePhoto = userManager.Users
                                                       .FirstOrDefault(u => u.Id == notificationCard.CreatedBy)
                                                       .ProfilePhotoUrl,
                                    For   = "Group",
                                    ForId = notification.ForId
                                };

                                notifications.Add(nModel);
                            }
                        }
                    }
                }
            }
            return(View(notifications.AsQueryable()));
        }