Ejemplo n.º 1
0
        private async Task <Follows.Models.Follow> FollowCreated(Follows.Models.Follow follow)
        {
            if (follow == null)
            {
                return(null);
            }

            // Is this a user follow?
            if (!follow.Name.Equals(FollowTypes.User.Name, StringComparison.OrdinalIgnoreCase))
            {
                return(follow);
            }

            // Get the user we are following
            var user = await _platoUserStore.GetByIdAsync(follow.ThingId);

            if (user == null)
            {
                return(follow);
            }

            // Award new follow reputation to the user following another user
            await _reputationAwarder.AwardAsync(Reputations.NewFollow, follow.CreatedUserId, $"Following user \"{user.DisplayName}");

            // Award new follower reputation to the user the current user is following
            await _reputationAwarder.AwardAsync(Reputations.NewFollower, follow.ThingId, $"{follow.CreatedBy.DisplayName} is following me");

            return(follow);
        }
Ejemplo n.º 2
0
        async Task <TEntityReply> EntityReplyCreated(TEntityReply reply)
        {
            if (reply == null)
            {
                throw new ArgumentNullException(nameof(reply));
            }

            if (reply.IsHidden)
            {
                return(reply);
            }

            if (reply.IsDeleted)
            {
                return(reply);
            }

            if (reply.IsSpam)
            {
                return(reply);
            }

            // Award reputation for new reply
            if (reply.CreatedUserId > 0)
            {
                await _reputationAwarder.AwardAsync(Reputations.NewComment, reply.CreatedUserId,
                                                    "Posted an article comment");
            }

            // Update entity details
            return(await EntityDetailsUpdater(reply));
        }
Ejemplo n.º 3
0
        private async Task <Follows.Models.Follow> FollowCreated(Follows.Models.Follow follow)
        {
            if (follow == null)
            {
                return(null);
            }

            // Is this a label follow?
            if (!follow.Name.Equals(FollowTypes.Label.Name, StringComparison.OrdinalIgnoreCase))
            {
                return(follow);
            }

            // Get the label we are following
            var label = await _labelStore.GetByIdAsync(follow.ThingId);

            if (label == null)
            {
                return(follow);
            }

            // Update follow count
            label.TotalFollows = label.TotalFollows + 1;

            // Persist changes
            var updatedLabel = await _labelStore.UpdateAsync(label);

            if (updatedLabel != null)
            {
                // Award reputation for following label
                await _reputationAwarder.AwardAsync(Reputations.NewFollow, follow.CreatedUserId, $"Followed label \"{label.Name}\"");
            }

            return(follow);
        }
Ejemplo n.º 4
0
        private async Task <Plato.Follows.Models.Follow> FollowCreated(Plato.Follows.Models.Follow follow)
        {
            if (follow == null)
            {
                return(null);
            }

            // Is this a tag follow?
            if (!follow.Name.Equals(FollowTypes.Topic.Name, StringComparison.OrdinalIgnoreCase))
            {
                return(follow);
            }

            // Ensure the topic we are following still exists
            var existingTopic = await _entityStore.GetByIdAsync(follow.ThingId);

            if (existingTopic == null)
            {
                return(follow);
            }

            // Update total follows
            existingTopic.TotalFollows = existingTopic.TotalFollows + 1;

            // Persist changes
            var updatedTopic = await _entityStore.UpdateAsync(existingTopic);

            if (updatedTopic != null)
            {
                // Award reputation for following topic
                await _reputationAwarder.AwardAsync(Reputations.NewFollow, follow.CreatedUserId, "Followed a topic");
            }

            return(follow);
        }
Ejemplo n.º 5
0
        async Task <TEntity> EntityCreated(TEntity entity)
        {
            if (entity.IsHidden())
            {
                return(entity);
            }

            // Award reputation
            if (entity.CreatedUserId > 0)
            {
                await _reputationAwarder.AwardAsync(Reputations.NewTopic, entity.CreatedUserId, "Topic posted");
            }

            // Return
            return(entity);
        }
Ejemplo n.º 6
0
        private async Task <Follows.Models.Follow> FollowCreated(Follows.Models.Follow follow)
        {
            if (follow == null)
            {
                return(null);
            }

            // Is this a tag follow?
            if (!follow.Name.Equals(FollowTypes.Tag.Name, StringComparison.OrdinalIgnoreCase))
            {
                return(follow);
            }

            // Ensure the tag we are following still exists
            var tag = await _tagStore.GetByIdAsync(follow.ThingId);

            if (tag == null)
            {
                return(follow);
            }

            // Update total follows
            tag.TotalFollows = tag.TotalFollows + 1;

            // Persist changes
            var updatedTag = await _tagStore.UpdateAsync(tag);

            if (updatedTag != null)
            {
                // Award reputation for following tag
                await _reputationAwarder.AwardAsync(Reputations.NewFollow, follow.CreatedUserId, $"Followed tag \"{tag.Name}\"");
            }

            return(follow);
        }
Ejemplo n.º 7
0
        async Task <TEntity> EntityCreated(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            if (entity.IsHidden())
            {
                return(entity);
            }

            // Award reputation
            if (entity.CreatedUserId > 0)
            {
                await _reputationAwarder.AwardAsync(Reputations.NewArticle, entity.CreatedUserId, "Created an article");
            }

            // Return
            return(entity);
        }
Ejemplo n.º 8
0
        private async Task <Stars.Models.Star> StarCreated(Stars.Models.Star star)
        {
            if (star == null)
            {
                return(null);
            }

            // Is this a article star?
            if (!star.Name.Equals(StarTypes.Article.Name, StringComparison.OrdinalIgnoreCase))
            {
                return(star);
            }

            // Ensure the entity we are starring exists
            var entity = await _entityStore.GetByIdAsync(star.ThingId);

            if (entity == null)
            {
                return(star);
            }

            // Update total stars
            entity.TotalStars = entity.TotalStars + 1;

            // Persist changes
            var updatedEntity = await _entityStore.UpdateAsync(entity);

            if (updatedEntity != null)
            {
                // Award reputation to user starring the entity
                await _reputationAwarder.AwardAsync(Reputations.StarArticle, star.CreatedUserId, "Starred an article");

                // Award reputation to entity author when there entity is starred
                await _reputationAwarder.AwardAsync(Reputations.StarredArticle, entity.CreatedUserId, "Someone starred my article");
            }

            return(star);
        }
Ejemplo n.º 9
0
        public async Task OnActionExecutingAsync(ResultExecutingContext context)
        {
            // Not a view result
            if (!(context.Result is ViewResult))
            {
                return;
            }

            // Tracking cookie already exists, simply execute the controller result
            if (_active)
            {
                return;
            }

            // Get authenticated user
            var user = await _contextFacade.GetAuthenticatedUserAsync();

            // Not authenticated, simply execute the controller result
            if (user == null)
            {
                return;
            }

            user.Visits           += 1;
            user.VisitsUpdatedDate = DateTimeOffset.UtcNow;
            user.LastLoginDate     = DateTimeOffset.UtcNow;

            var result = await _userStore.UpdateAsync(user);

            if (result != null)
            {
                // Award visit reputation
                await _userReputationAwarder.AwardAsync(new Reputation("Visit", 1), result.Id, "Unique Visit");

                // Set client cookie to ensure update does not
                // occur again for as long as the cookie exists
                _cookieBuilder
                .Contextulize(context.HttpContext)
                .Append(
                    _cookieName,
                    true.ToString(),
                    new CookieOptions
                {
                    HttpOnly = true,
                    Expires  = DateTime.Now.AddMinutes(_sessionLength)
                });
            }
        }
Ejemplo n.º 10
0
        private async Task <Follows.Models.Follow> FollowCreated(Follows.Models.Follow follow)
        {
            if (follow == null)
            {
                return(null);
            }

            // Is this a channel follow?
            if (!follow.Name.Equals(FollowTypes.Category.Name, StringComparison.OrdinalIgnoreCase))
            {
                return(follow);
            }

            // Award reputation for following channel
            await _reputationAwarder.AwardAsync(Reputations.NewFollow, follow.CreatedUserId, "Followed a discuss category");

            return(follow);
        }
Ejemplo n.º 11
0
        public async Task ExecuteAsync(object sender, SafeTimerEventArgs args)
        {
            var feature = await _featureFacade.GetFeatureByIdAsync("Plato.Questions");

            if (feature == null)
            {
                return;
            }

            var bot = await _userStore.GetPlatoBotAsync();

            foreach (var badge in this.Badges)
            {
                // Replacements for SQL script
                var replacements = new Dictionary <string, string>()
                {
                    ["{name}"]      = badge.Name,
                    ["{threshold}"] = badge.Threshold.ToString(),
                    ["{featureId}"] = feature.Id.ToString()
                };

                var userIds = await _dbHelper.ExecuteReaderAsync <IList <int> >(Sql, replacements, async reader =>
                {
                    var users = new List <int>();
                    while (await reader.ReadAsync())
                    {
                        if (reader.ColumnIsNotNull("UserId"))
                        {
                            users.Add(Convert.ToInt32(reader["UserId"]));
                        }
                    }

                    return(users);
                });

                if (userIds?.Count > 0)
                {
                    // Get all users awarded the badge
                    var users = await _userStore.QueryAsync()
                                .Take(1, userIds.Count)
                                .Select <UserQueryParams>(q => { q.Id.IsIn(userIds.ToArray()); })
                                .OrderBy("LastLoginDate", OrderBy.Desc)
                                .ToList();

                    // Send notifications
                    if (users != null)
                    {
                        foreach (var user in users.Data)
                        {
                            // ---------------
                            // Award reputation for new badges
                            // ---------------

                            var badgeReputation = badge.GetReputation();
                            if (badgeReputation.Points != 0)
                            {
                                await _userReputationAwarder.AwardAsync(badgeReputation, user.Id, $"{badge.Name} badge awarded");
                            }

                            // ---------------
                            // Trigger notifications
                            // ---------------

                            // Email notification
                            if (user.NotificationEnabled(_userNotificationTypeDefaults, EmailNotifications.NewBadge))
                            {
                                await _notificationManager.SendAsync(new Notification(EmailNotifications.NewBadge)
                                {
                                    To   = user,
                                    From = bot
                                }, badge);
                            }

                            // Web notification
                            if (user.NotificationEnabled(_userNotificationTypeDefaults, WebNotifications.NewBadge))
                            {
                                await _notificationManager.SendAsync(new Notification(WebNotifications.NewBadge)
                                {
                                    To   = user,
                                    From = bot
                                }, badge);
                            }
                        }
                    }

                    _cacheManager.CancelTokens(typeof(UserBadgeStore));
                }
            }
        }