Пример #1
0
        /// <summary>
        /// Github第三方认证回调地址
        /// </summary>
        /// <returns></returns>
        public ActionResult GitHubOAuth()
        {
            //为了方便测试,这里不添加POST或者GET指示了
            //从Github返回的参数中获得code的值
            //这个code代表了授权用户的ID
            //由于是https,这里必须要转换为HttpWebRequest
            var userInfo = GithubAccount.GetUserInfo(Request.Params["code"]);

            if (userInfo != null)
            {
                //SN和UserInfoId在多种登陆方式的时候,不是同步的
                Session[ConstHelper.Session_USERID]    = userInfo.Sn;
                Session[ConstHelper.Session_NAME]      = userInfo.NickName;
                Session[ConstHelper.Session_AVATAR]    = userInfo.Avatar_url;
                Session[ConstHelper.Session_PRIVILEGE] = userInfo.Privilege;
                if (userInfo.Privilege == UserType.Admin)
                {
                    return(Redirect("/Admin/Index"));
                }
                else
                {
                    return(Redirect("/Home/PersonIndex"));
                }
            }
            return(Redirect("/"));
        }
        public IHttpActionResult Post(GithubAccount githubAccount)
        {
            GithubAccount repo = db.GithubAccounts.Where(gr => gr.UserName == githubAccount.UserName).FirstOrDefault();

            if (repo == null)
            {
                db.GithubAccounts.Add(githubAccount);
                db.SaveChanges();
                return(Created(githubAccount));
            }
            else
            {
                return(Conflict());
            }
        }
        public static List <GithubAccount> Load()
        {
            List <GithubAccount> githubAccounts = new List <GithubAccount>();

            using (var stream = new StreamReader("./accounts.txt"))
            {
                string accountLine = stream.ReadLine();
                while (accountLine != null)
                {
                    string[]      accountItem = accountLine.Trim().Split(' ');
                    GithubAccount account     = new GithubAccount();
                    account.UserName = accountItem[0];
                    account.Password = accountItem[1];
                    githubAccounts.Add(account);
                    accountLine = stream.ReadLine();
                }
            }
            return(githubAccounts);
        }
Пример #4
0
        /// <summary>
        /// 获得用户BODY
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public static UserBody GetUserBody(string accountId)
        {
            IMongoQuery x     = Query.EQ(nameof(UserBody.AccountId), accountId);
            var         cache = MongoDbRepository.GetFirstCacheRec <UserBody>(x);

            if (cache != null)
            {
                return(cache);
            }
            UserBody u = new UserBody();

            u.AccountId = accountId;
            u.UserInfo  = UserInfo.GetUserInfoBySn(accountId);

            switch (u.UserInfo.RegisterMethod)
            {
            case GithubAccount.Github:
                u.GitInfo = GithubAccount.GetGithubAccountBySn(u.UserInfo.RegisterAccountID);
                break;

            case QQAccount.QQ:
                u.QQInfo = QQAccount.GetQQAccountBySn(u.UserInfo.RegisterAccountID);
                break;

            default:
                break;
            }

            //获得用户文章列表
            u.ArticleList = new List <ArticleItemBody>();
            var alist = Article.GetListByOwnerId(accountId);

            foreach (var item in alist)
            {
                u.ArticleList.Add(ArticleListManager.GetArticleItemBodyById(item.Sn));
            }
            //关注的人
            u.FocusList = new List <UserInfo>();
            var focuslist = Focus.GetFocus(accountId);

            foreach (var item in focuslist)
            {
                u.FocusList.Add(UserInfo.GetUserInfoBySn(item));
            }
            //跟随的人
            u.FollowList = new List <UserInfo>();
            var followlist = Focus.GetFollow(accountId);

            foreach (var item in followlist)
            {
                u.FollowList.Add(UserInfo.GetUserInfoBySn(item));
            }
            //收藏
            u.StockList = new List <ArticleItemBody>();
            var slist = OwnerTableOperator.GetRecListByOwnerId <Stock>(accountId);

            foreach (var item in slist)
            {
                u.StockList.Add(ArticleListManager.GetArticleItemBodyById(item.ArticleID));
            }
            UserBody.InsertUserBody(u);
            return(u);
        }
Пример #5
0
        /// <summary>
        /// 模拟加载博客园标题
        /// </summary>
        /// <param name="cnblogFilename"></param>
        /// <param name="strMDContent"></param>
        /// <param name="strHTMLContent"></param>
        /// <param name="LimitCnt"></param>
        /// <param name="client"></param>
        public static void InsertCnblogs(string cnblogFilename, string strMDContent, string strHTMLContent, int LimitCnt, ElasticClient client, bool IsArticleRandom)
        {
            Random r = new Random();

            titles.Clear();
            userdic.Clear();
            userColdic.Clear();
            StreamReader FileReader  = new StreamReader(cnblogFilename);
            string       Line        = string.Empty;
            int          startlength = "the article title is :".Length;
            int          endlength   = " - 博客园".Length;

            MongoDbRepository.DrapCollection(Article.CollectionName);
            MongoDbRepository.DrapCollection(Collection.CollectionName);
            MongoDbRepository.DrapCollection(GithubAccount.CollectionName);
            MongoDbRepository.DrapCollection(QQAccount.CollectionName);
            MongoDbRepository.DrapCollection(UserInfo.CollectionName);
            MongoDbRepository.DrapCollection(ArticleContent.CollectionName);
            MongoDbRepository.DrapCollection(SiteConfig.CollectionName);


            var PublishStatusTypeValues = Enum.GetValues(typeof(ApproveStatus));
            var ArticleLevelValues      = Enum.GetValues(typeof(ArticleLevel));

            int GetCnt  = 0;
            int LineCnt = 0;

            while (!FileReader.EndOfStream)
            {
                Line = FileReader.ReadLine();
                LineCnt++;
                if (Line.StartsWith("the article title is :用户登录 - 博客园"))
                {
                    continue;
                }
                if (!Line.StartsWith("the article title is :"))
                {
                    System.Diagnostics.Debug.WriteLine(Line);
                    continue;
                }
                if (!Line.EndsWith(" - 博客园"))
                {
                    System.Diagnostics.Debug.WriteLine(Line);
                    continue;
                }
                try
                {
                    Line = Line.Substring(startlength, Line.Length - startlength - endlength);
                    int    pos   = Line.LastIndexOf(" - ");
                    string title = Line.Substring(0, pos);
                    string user  = Line.Substring(pos + 3);
                    if (!userdic.ContainsKey(user))
                    {
                        var           userinfo   = new UserInfo();
                        int           qqOrGit    = r.Next(100);
                        string        accountId  = string.Empty;
                        QQAccount     qqaccount  = new QQAccount();
                        GithubAccount gitaccount = new GithubAccount();

                        if (qqOrGit % 2 == 0)
                        {
                            //Github帐号
                            gitaccount = new GithubAccount()
                            {
                                Avatar_url = "https://avatars.githubusercontent.com/u/897796?v=3",
                                Login      = user,
                                Name       = user,
                                Email      = "*****@*****.**",
                                Location   = "Shanghai,China",
                                Blog       = "http://www.mywechatapp.com",
                                Company    = "Shanghai Chuwa software co.ltd",
                                Followers  = 50,
                                Following  = 2,
                            };

                            accountId = MongoDbRepository.InsertRec(gitaccount);

                            userinfo = new UserInfo()
                            {
                                RegisterAccountID = accountId,
                                Privilege         = UserType.Normal,
                                RegisterMethod    = GithubAccount.Github,
                                TopicList         = new List <string>(),
                                TagList           = new List <string>(),
                                NickName          = gitaccount.Name,
                                Avatar_url        = gitaccount.Avatar_url,
                                ContainTag        = string.Empty,
                                AntiTag           = string.Empty,
                                Catalog           = new List <string>(),
                                Level             = new List <ArticleLevel>()
                            };
                        }
                        else
                        {
                            //QQ
                            qqaccount = new QQAccount()
                            {
                                figureurl = "https://avatars.githubusercontent.com/u/19196306?v=3",
                                gender    = "男",
                                nickname  = user,
                                OpenID    = "1234567890"
                            };

                            accountId = MongoDbRepository.InsertRec(qqaccount);

                            userinfo = new UserInfo()
                            {
                                RegisterAccountID = accountId,
                                Privilege         = UserType.Normal,
                                RegisterMethod    = QQAccount.QQ,
                                TopicList         = new List <string>(),
                                TagList           = new List <string>(),
                                NickName          = qqaccount.nickname,
                                Avatar_url        = qqaccount.figureurl,
                                ContainTag        = string.Empty,
                                AntiTag           = string.Empty,
                                Catalog           = new List <string>(),
                                Level             = new List <ArticleLevel>()
                            };
                        }

                        var x = r.Next(100);
                        if (x % 10 == 0)
                        {
                            userinfo.Privilege = UserType.Author;
                        }
                        else
                        {
                            if (x == 51)
                            {
                                userinfo.Privilege = UserType.Editor;
                            }
                        }
                        var userId = MongoDbRepository.InsertRec(userinfo);
                        if (userId == 1.ToString(EntityBase.SnFormat))
                        {
                            userinfo.Privilege = UserType.Admin;
                            UserInfo.UpdateUserInfo(userinfo);
                        }
                        if (userId == 2.ToString(EntityBase.SnFormat))
                        {
                            userinfo.Privilege = UserType.Editor;
                            UserInfo.UpdateUserInfo(userinfo);
                        }

                        if (qqOrGit % 2 == 0)
                        {
                            MongoDbRepository.UpdateRec(gitaccount, nameof(GithubAccount.UserInfoID), (BsonString)userId);
                        }
                        else
                        {
                            MongoDbRepository.UpdateRec(qqaccount, nameof(QQAccount.UserInfoID), (BsonString)userId);
                        }
                        userdic.Add(user, userId);

                        //默认文集
                        Collection collection = new Collection()
                        {
                            Title       = user + " 的文集",
                            Description = user + " 的文集",
                            IsSerie     = (r.Next(100) % 2 == 1)
                        };
                        var CollectionId = Collection.InsertCollection(collection, userId);
                        userColdic.Add(user, CollectionId);
                    }

                    if (!titles.Contains(title))
                    {
                        string  ownerid  = userdic[user];
                        string  collecId = userColdic[user];
                        Article article  = new Article()
                        {
                            Title           = title,
                            IsFirstPage     = IsArticleRandom ? (r.Next(0, 100) % 2 == 1) : true,
                            IsPrivate       = IsArticleRandom ? (r.Next(0, 100) % 2 == 1) : false,
                            PublishStatus   = IsArticleRandom ? (ApproveStatus)(PublishStatusTypeValues.GetValue(r.Next(0, 100) % PublishStatusTypeValues.Length)) : ApproveStatus.Accept,
                            IsCloseComment  = (r.Next(0, 100) % 2 == 1),
                            IsOriginal      = (r.Next(0, 100) % 2 == 1),
                            CollectionID    = collecId,
                            PublishDateTime = DateTime.Now.AddMinutes(r.NextDouble() * -10000),
                            Level           = (ArticleLevel)(ArticleLevelValues.GetValue(r.Next(0, 100) % ArticleLevelValues.Length)),
                            Catalog         = Article.CatalogItem[r.Next(0, 100) % Article.CatalogItem.Length].Substring(0, 4)
                        };
                        if (article.IsPrivate)
                        {
                            article.IsFirstPage   = false;
                            article.PublishStatus = ApproveStatus.NotNeed;
                        }
                        else
                        {
                            article.IsTopicable         = (r.Next(0, 100) % 2 == 1);
                            article.IsNeedTopicApproval = (r.Next(0, 100) % 2 == 1);
                        }
                        if (article.IsFirstPage)
                        {
                            article.IsOriginal     = true;
                            article.IsCloseComment = false;
                        }
                        article.ConfirmDateTime = article.PublishDateTime.AddSeconds(r.Next(1000, 7200));
                        string ArticleId = Article.InsertArticle(article, ownerid);
                        if (client != null && (!article.IsPrivate) && (article.PublishStatus == ApproveStatus.Accept))
                        {
                            client.Index(article);
                        }
                        //插入MarkDown文档(保证首页只读,则只需要SaveMarkDownVersion:Current)
                        ArticleContent.SaveMarkDownVersion(ArticleId, strMDContent, ownerid, RevisionType.Current);
                        ArticleContent.SaveMarkDownVersion(ArticleId, strMDContent, ownerid, RevisionType.First);
                        ArticleContent.SaveMarkDownVersion(ArticleId, strMDContent, ownerid, RevisionType.Draft);
                        ArticleContent.SaveHTMLVersion(ArticleId, strHTMLContent, ownerid);
                        titles.Add(title);
                        GetCnt++;
                        if (GetCnt == LimitCnt)
                        {
                            break;
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Dulipt:" + Line);
                    }
                }
                catch (Exception)
                {
                    System.Diagnostics.Debug.WriteLine("Error:" + Line);
                }
            }
            FileReader.Close();
            System.Diagnostics.Debug.WriteLine("LineCnt:" + LineCnt);
        }