//添加用户或更新用户信息 public bool UpdateUserInfo(string loginType, string token) { if (loginType == "github") { GitHubUser githubUser = GitHub.GetGitHubUser(token); if (githubUser.id == 0) { return(false); } NullUser user = NullUserDataSvc.GetByCondition(u => u.GitHubID == githubUser.id).FirstOrDefault(); if (user == null) { user = new NullUser(); user.LoginType = "github"; user.GitHubAccessToken = token; user.Name = githubUser.name; user.AvatarUrl = githubUser.avatar_url; user.GitHubLogin = githubUser.login; user.GitHubID = githubUser.id; user.Role = (int)EnumUserRole.普通; user.Email = githubUser.email; user.FollowingCount = githubUser.following; user.FollowerCount = githubUser.followers; NullUserDataSvc.Add(user); //用关注人数小于500的机器人账号关注普通用户 if (user.Email == null || (user.Email != null && !user.Email.EndsWith("objnull.com"))) { List <NullUser> robots = NullUserDataSvc.GetByCondition(u => u.Email.EndsWith("objnull.com") && u.GitHubLogin.Contains("robot")).ToList(); //检查是否已关注 bool followed = false; GitHubClient github = new GitHubClient(new ProductHeaderValue("objnulldotcom")); foreach (NullUser robot in robots) { github.Credentials = new Credentials(robot.GitHubAccessToken); if (github.User.Followers.IsFollowing(robot.GitHubLogin, user.GitHubLogin).Result) { followed = true; break; } } //关注 if (!followed) { NullUser frobot = robots.Where(r => r.FollowingCount < 500).First(); github.Credentials = new Credentials(frobot.GitHubAccessToken); github.User.Get(frobot.GitHubLogin); github.User.Followers.Follow(user.GitHubLogin); frobot.FollowingCount += 1; NullUserDataSvc.Update(frobot); } } if (string.IsNullOrEmpty(user.Email)) { SysMsg msg = new SysMsg(); msg.Date = DateTime.Now; msg.Title = "您还未设置邮箱"; msg.Msg = "请到<a href=\"/Home/UserProfile\">我的主页</a>设置或修改<a href=\"https://github.com/settings/profile\" target=\"_blank\">GitHub</a>邮箱显示后更新账号。"; string key = MyRedisKeys.Pre_SysMsg + user.ID; MyRedisDB.SetAdd(key, msg); } } else { user.Name = githubUser.name; user.AvatarUrl = githubUser.avatar_url; user.GitHubID = githubUser.id; user.GitHubLogin = githubUser.login; user.GitHubAccessToken = token; user.Email = githubUser.email; user.FollowingCount = githubUser.following; user.FollowerCount = githubUser.followers; NullUserDataSvc.Update(user); } HttpContext.WriteCookie("UID", user.ID.ToString(), DateTime.Now.AddYears(3)); HttpContext.WriteCookie("UName", HttpUtility.UrlEncode(githubUser.name), DateTime.Now.AddYears(3)); HttpContext.WriteCookie("UAvatar", githubUser.avatar_url, DateTime.Now.AddYears(3)); HttpContext.WriteCookie("LoginType", user.LoginType, DateTime.Now.AddYears(3)); HttpContext.WriteCookie("GLogin", githubUser.login, DateTime.Now.AddYears(3)); HttpContext.WriteCookie("SKEY", Utils.RijndaelEncrypt(user.ID.ToString()), DateTime.Now.AddYears(3)); HttpContext.WriteCookie("Role", Utils.RijndaelEncrypt(user.ID.ToString() + ";" + user.Role.ToString()), DateTime.Now.AddYears(3)); } return(true); }
//添加用户或更新用户信息 public bool UpdateUserInfo(string loginType, string token) { if (loginType == "github") { GitHubUser githubUser = GitHub.GetGitHubUser(token); if (githubUser.id == 0) { return(false); } NullUser user = NullUserDataSvc.GetByCondition(u => u.GitHubID == githubUser.id).FirstOrDefault(); if (user == null) { user = new NullUser(); user.LoginType = "github"; user.GitHubAccessToken = token; user.Name = githubUser.name; user.AvatarUrl = githubUser.avatar_url; user.GitHubLogin = githubUser.login; user.GitHubID = githubUser.id; user.Role = (int)EnumUserRole.普通; user.Email = githubUser.email; NullUserDataSvc.Add(user); if (string.IsNullOrEmpty(user.Email)) { SysMsg msg = new SysMsg(); msg.Date = DateTime.Now; msg.Title = "您还未设置邮箱"; msg.Msg = "请到<a href=\"/Home/UserProfile\">我的主页</a>设置或修改<a href=\"https://github.com/settings/profile\" target=\"_blank\">GitHub</a>邮箱显示后更新账号。"; string key = MyRedisKeys.Pre_SysMsg + user.ID; MyRedisDB.SetAdd(key, msg); } //添加一篇newbee //NewBee nb = new NewBee(); //nb.OwnerID = user.ID; //nb.Title = "大家好,我是" + (string.IsNullOrEmpty(user.Name) ? user.GitHubLogin : user.Name) + ",很高兴加入象空。"; //nb.FloorCount = 1; //nb.LastFloorDate = DateTime.Now; //NewBeeDataSvc.Add(nb); //NewBeeFloor nbf = new NewBeeFloor(); //nbf.MDText = "我刚刚加入象空,点击查看更多关于我的信息,如果你有兴趣可以关注我的GitHub。"; //nbf.MDValue = "<p>我刚刚加入象空,点击查看更多关于我的信息,如果你有兴趣可以关注我的GitHub。</p>"; //nbf.NewBeeID = nb.ID; //nbf.Order = 1; //nbf.OwnerID = user.ID; //NewBeeFloorDataSvc.Add(nbf); } else { user.Name = githubUser.name; user.AvatarUrl = githubUser.avatar_url; user.GitHubID = githubUser.id; user.GitHubLogin = githubUser.login; user.GitHubAccessToken = token; user.Email = githubUser.email; NullUserDataSvc.Update(user); } HttpContext.WriteCookie("UID", user.ID.ToString(), DateTime.Now.AddYears(3)); HttpContext.WriteCookie("UName", githubUser.name, DateTime.Now.AddYears(3)); HttpContext.WriteCookie("UAvatar", githubUser.avatar_url, DateTime.Now.AddYears(3)); HttpContext.WriteCookie("LoginType", user.LoginType, DateTime.Now.AddYears(3)); HttpContext.WriteCookie("GLogin", githubUser.login, DateTime.Now.AddYears(3)); HttpContext.WriteCookie("SKEY", Utils.RijndaelEncrypt(user.ID.ToString()), DateTime.Now.AddYears(3)); HttpContext.WriteCookie("Role", Utils.RijndaelEncrypt(user.ID.ToString() + ";" + user.Role.ToString()), DateTime.Now.AddYears(3)); } return(true); }