Exemplo n.º 1
0
        public IHttpActionResult RecommendedPosts([FromUri] string AccessToken)
        {
            var UserId = TokenValidator.VerifyToken(AccessToken);

            var UserFollows = UserAuxiliar
                              .Follows(UserId, SQLContext)
                              .Select(x => x.Id);

            var UserTopics = UserAuxiliar.FollowedSubjects(UserId, SQLContext);

            var UserPosts        = PostAuxiliar.PostsByAuthor(UserId);
            var UserMentionPosts = PostAuxiliar.MentionsUser(UserId, SQLContext);

            var FollowsPosts        = PostAuxiliar.PostsByAuthors(UserFollows);
            var FollowsMentionPosts = PostAuxiliar.MentionsUsers(UserFollows);

            var TopicsPosts = PostAuxiliar.PostsBySubjects(UserTopics.Select(x => x.Id));

            var PostComparer = new PostComparer();

            var RecommendedPosts = UserPosts
                                   .Union(UserMentionPosts, PostComparer)
                                   .Union(FollowsPosts, PostComparer)
                                   .Union(FollowsMentionPosts, PostComparer)
                                   .Union(TopicsPosts, PostComparer)
                                   .ToList();

            RecommendedPosts = PostAuxiliar.UpdateOwner(RecommendedPosts, SQLContext);
            RecommendedPosts = GraphAuxiliar.OrderPostsByAffinity(UserId, RecommendedPosts, SQLContext);

            return(Ok(RecommendedPosts));
        }
Exemplo n.º 2
0
        public IHttpActionResult UserFollows([FromUri] string AccessToken)
        {
            var UserId = TokenValidator.VerifyToken(AccessToken);

            var Follows = UserAuxiliar.Follows(UserId, SQLContext);

            return(Ok(Follows));
        }
Exemplo n.º 3
0
        //TA MT FEIO HORROROSO DESCULPA
        private static List <dynamic> CreateAffinityProducts(int UserId, List <Product> Products, SQLContext SQLContext)
        {
            var ProductRecommendation = new List <dynamic>();
            var TaskArray             = new List <Task>();

            foreach (var Product in Products)
            {
                var NewTask = Task.Run
                              (
                    () =>
                {
                    double OwnerAffinity   = 0;
                    double SubjectAffinity = 0;
                    double FollowsAffinity = 0;

                    var OwnerId = Product.ProId;

                    IEnumerable <int> SubjectsFollowedByOwner;
                    IEnumerable <int> UsersFollowedByOwner;
                    IEnumerable <int> OwnerFollowers;

                    lock (SQLContext)
                    {
                        SubjectsFollowedByOwner = UserAuxiliar.FollowedSubjects((int)OwnerId, SQLContext).Select(x => x.Id);
                        UsersFollowedByOwner    = UserAuxiliar.Follows((int)OwnerId, SQLContext).Select(x => x.Id);
                        OwnerFollowers          = UserAuxiliar.Followers((int)OwnerId, SQLContext).Select(x => x.Id);
                    }

                    var Follows = UsersFollowedByOwner.Union(OwnerFollowers);

                    Follow OwnerFollow;

                    lock (SQLContext)
                    {
                        OwnerFollow = SQLContext.Follows.FirstOrDefault(x => x.FollowerId == UserId && x.FollowedId == OwnerId);
                    }

                    if (OwnerFollow != default)
                    {
                        OwnerAffinity += OwnerFollow.Affinity;
                    }

                    int RawSubjectAffinity = 0;

                    lock (SQLContext)
                    {
                        var SameSubjects = SQLContext.UserSubjects.Where(x => x.UserId == UserId && SubjectsFollowedByOwner.Contains(x.SubjectId));
                        if (SameSubjects.Any())
                        {
                            RawSubjectAffinity = SameSubjects.Sum(x => x.Affinity);
                        }
                    }

                    if (SubjectsFollowedByOwner.Any())
                    {
                        SubjectAffinity = RawSubjectAffinity / SubjectsFollowedByOwner.Count();
                    }

                    IQueryable <Follow> EqualFollows;
                    lock (SQLContext)
                    {
                        EqualFollows = SQLContext.Follows.Where(x => Follows.Contains(x.FollowedId) && x.FollowerId == UserId);
                        if (EqualFollows.Any())
                        {
                            FollowsAffinity = EqualFollows.Sum(x => x.Affinity) / EqualFollows.Count();
                        }
                    }

                    if (OwnerAffinity != 0 || FollowsAffinity != 0 || SubjectAffinity != 0)
                    {
                        ProductRecommendation.Add(new { Product, OwnerAffinity, SubjectAffinity, FollowsAffinity });
                    }
                }
                              );

                TaskArray.Add(NewTask);
            }

            Task.WaitAll(TaskArray.ToArray());

            return(ProductRecommendation);
        }