Commit 147746a2 authored by Liang Ding's avatar Liang Ding

#12514

parent 4b4d3752
......@@ -17,8 +17,6 @@
*/
package org.b3log.solo.processor;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.b3log.latke.Keys;
......@@ -35,7 +33,6 @@ import org.b3log.latke.servlet.annotation.RequestProcessor;
import org.b3log.latke.util.CollectionUtils;
import org.b3log.latke.util.Requests;
import org.b3log.latke.util.URLs;
import org.b3log.solo.model.Common;
import org.b3log.solo.model.Option;
import org.b3log.solo.model.UserExt;
import org.b3log.solo.service.*;
......@@ -114,6 +111,11 @@ public class OAuthGitHubProcessor {
@Inject
private InitService initService;
/**
* GitHub split.
*/
public static final String GITHUB_SPLIT = ":@:";
/**
* Redirects to GitHub auth page.
*
......@@ -148,7 +150,7 @@ public class OAuthGitHubProcessor {
STATES.remove(state);
final String accessToken = context.param("ak");
final JSONObject userInfo = getGitHubUserInfo(accessToken);
final JSONObject userInfo = Solos.getGitHubUserInfo(accessToken);
if (null == userInfo) {
context.sendError(HttpServletResponse.SC_FORBIDDEN);
......@@ -175,7 +177,6 @@ public class OAuthGitHubProcessor {
}
final JSONArray github = new JSONArray(value);
final Set<String> githubAuths = CollectionUtils.jsonArrayToSet(github);
final String splitChar = ":@:";
final String oAuthPair = Option.getOAuthPair(githubAuths, openId); // openId:@:userId
if (StringUtils.isBlank(oAuthPair)) {
if (!initService.isInited()) {
......@@ -222,7 +223,7 @@ public class OAuthGitHubProcessor {
user = userQueryService.getUserByEmailOrUserName(userEmail);
}
final String userId = user.optString(Keys.OBJECT_ID);
githubAuths.add(openId + splitChar + userId);
githubAuths.add(openId + GITHUB_SPLIT + userId);
value = new JSONArray(githubAuths).toString();
oauthGitHubOpt.put(Option.OPTION_VALUE, value);
try {
......@@ -238,7 +239,7 @@ public class OAuthGitHubProcessor {
return;
}
final String[] openIdUserId = oAuthPair.split(splitChar);
final String[] openIdUserId = oAuthPair.split(GITHUB_SPLIT);
final String userId = openIdUserId[1];
final JSONObject userResult = userQueryService.getUser(userId);
if (null == userResult) {
......@@ -252,49 +253,4 @@ public class OAuthGitHubProcessor {
context.sendRedirect(Latkes.getServePath());
LOGGER.log(Level.INFO, "Logged in [email={0}, remoteAddr={1}] with GitHub oauth", userEmail, Requests.getRemoteAddr(request));
}
/**
* Gets GitHub user info.
*
* @param accessToken the specified access token
* @return GitHub user info, for example, <pre>
* {
* "openId": "",
* "userName": "D",
* "userEmail": "d@b3log.org", // may be empty
* "userAvatar": "https://avatars3.githubusercontent.com/u/873584?v=4"
* }
* </pre>, returns {@code null} if not found QQ user info
*/
private JSONObject getGitHubUserInfo(final String accessToken) {
try {
final HttpResponse res = HttpRequest.get("https://hacpai.com/github/user?ak=" + accessToken).
connectionTimeout(3000).timeout(7000).header("User-Agent", Solos.USER_AGENT).send();
if (HttpServletResponse.SC_OK != res.statusCode()) {
return null;
}
res.charset("UTF-8");
final JSONObject result = new JSONObject(res.bodyText());
if (0 != result.optInt(Keys.STATUS_CODE)) {
return null;
}
final JSONObject data = result.optJSONObject(Common.DATA);
final String userName = StringUtils.trim(data.optString("userName"));
final String email = data.optString("userEmail");
final String openId = data.optString("userId");
final String avatarUrl = data.optString("userAvatarURL");
final JSONObject ret = new JSONObject();
ret.put("openId", openId);
ret.put(User.USER_NAME, userName);
ret.put(User.USER_EMAIL, email);
ret.put(UserExt.USER_AVATAR, avatarUrl);
return ret;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Gets GitHub user info failed", e);
return null;
}
}
}
......@@ -31,7 +31,7 @@ import java.util.concurrent.TimeUnit;
* Cron management service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.1, Jan 24, 2019
* @version 1.0.0.2, Jan 29, 2019
* @since 2.9.7
*/
@Service
......@@ -59,6 +59,18 @@ public class CronMgmtService {
@Inject
private UserQueryService userQueryService;
/**
* Page management service.
*/
@Inject
private PageMgmtService pageMgmtService;
/**
* Option query service.
*/
@Inject
private OptionQueryService optionQueryService;
/**
* Start all cron tasks.
*/
......@@ -75,6 +87,17 @@ public class CronMgmtService {
}
}, delay, 1000 * 60 * 10, TimeUnit.MILLISECONDS);
delay += 2000;
SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(() -> {
try {
pageMgmtService.refreshGitHub();
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Executes cron failed", e);
} finally {
Stopwatchs.release();
}
}, delay, 1000 * 60 * 10, TimeUnit.MILLISECONDS);
delay += 2000;
}
/**
......
......@@ -31,8 +31,11 @@ import org.b3log.latke.util.Ids;
import org.b3log.solo.model.Comment;
import org.b3log.solo.model.Option;
import org.b3log.solo.model.Page;
import org.b3log.solo.processor.OAuthGitHubProcessor;
import org.b3log.solo.repository.CommentRepository;
import org.b3log.solo.repository.PageRepository;
import org.b3log.solo.util.Solos;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -95,6 +98,39 @@ public class PageMgmtService {
@Inject
private StatisticQueryService statisticQueryService;
/**
* Option query service.
*/
@Inject
private OptionQueryService optionQueryService;
/**
* Refreshes GitHub repos.
* 同步 GitHub 仓库 https://github.com/b3log/solo/issues/12514
*/
public void refreshGitHub() {
final JSONObject oauthGitHubOpt = optionQueryService.getOptionById(Option.ID_C_OAUTH_GITHUB);
if (null == oauthGitHubOpt) {
return;
}
String value = oauthGitHubOpt.optString(Option.OPTION_VALUE);
if (StringUtils.isBlank(value)) {
return;
}
final JSONArray github = new JSONArray(value);
final String githubPair = github.optString(0);// Just refresh the first account
final String githubUserId = githubPair.split(OAuthGitHubProcessor.GITHUB_SPLIT)[0];
final List<JSONObject> gitHubRepos = Solos.getGitHubRepos(githubUserId);
if (null == gitHubRepos || gitHubRepos.isEmpty()) {
return;
}
final String userId = githubPair.split(OAuthGitHubProcessor.GITHUB_SPLIT)[1];
System.out.println(userId + ": " + gitHubRepos);
}
/**
* Updates a page by the specified request json object.
*
......
......@@ -17,6 +17,8 @@
*/
package org.b3log.solo.util;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
......@@ -36,23 +38,22 @@ import org.b3log.solo.SoloServletListener;
import org.b3log.solo.model.Article;
import org.b3log.solo.model.Common;
import org.b3log.solo.model.Option;
import org.b3log.solo.model.UserExt;
import org.b3log.solo.repository.UserRepository;
import org.json.JSONArray;
import org.json.JSONObject;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.*;
/**
* Solo utilities.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.6.0.0, Jan 25, 2019
* @version 1.6.0.1, Jan 29, 2019
* @since 2.8.0
*/
public final class Solos {
......@@ -165,6 +166,80 @@ public final class Solos {
COOKIE_HTTP_ONLY = Boolean.valueOf(Latkes.getLocalProperty("cookieHttpOnly"));
}
/**
* Gets GitHub repos.
*
* @param githubUserId the specified GitHub user id
* @return GitHub repos, returns an empty list if not found
*/
public static List<JSONObject> getGitHubRepos(final String githubUserId) {
try {
final HttpResponse res = HttpRequest.get("https://hacpai.com/github/repos?id=" + githubUserId).
connectionTimeout(3000).timeout(7000).header("User-Agent", Solos.USER_AGENT).send();
if (HttpServletResponse.SC_OK != res.statusCode()) {
return null;
}
res.charset("UTF-8");
final JSONObject result = new JSONObject(res.bodyText());
if (0 != result.optInt(Keys.STATUS_CODE)) {
return Collections.emptyList();
}
final JSONObject data = result.optJSONObject(Common.DATA);
final JSONArray repos = data.optJSONArray("githubrepos");
return CollectionUtils.jsonArrayToList(repos);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Gets GitHub repos failed", e);
return Collections.emptyList();
}
}
/**
* Gets GitHub user info.
*
* @param accessToken the specified access token
* @return GitHub user info, for example, <pre>
* {
* "openId": "",
* "userName": "D",
* "userEmail": "d@b3log.org", // may be empty
* "userAvatar": "https://avatars3.githubusercontent.com/u/873584?v=4"
* }
* </pre>, returns {@code null} if not found QQ user info
*/
public static JSONObject getGitHubUserInfo(final String accessToken) {
try {
final HttpResponse res = HttpRequest.get("https://hacpai.com/github/user?ak=" + accessToken).
connectionTimeout(3000).timeout(7000).header("User-Agent", Solos.USER_AGENT).send();
if (HttpServletResponse.SC_OK != res.statusCode()) {
return null;
}
res.charset("UTF-8");
final JSONObject result = new JSONObject(res.bodyText());
if (0 != result.optInt(Keys.STATUS_CODE)) {
return null;
}
final JSONObject data = result.optJSONObject(Common.DATA);
final String userName = StringUtils.trim(data.optString("userName"));
final String email = data.optString("userEmail");
final String openId = data.optString("userId");
final String avatarUrl = data.optString("userAvatarURL");
final JSONObject ret = new JSONObject();
ret.put("openId", openId);
ret.put(User.USER_NAME, userName);
ret.put(User.USER_EMAIL, email);
ret.put(UserExt.USER_AVATAR, avatarUrl);
return ret;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Gets GitHub user info failed", e);
return null;
}
}
/**
* Adds noindex header for Google. https://github.com/b3log/solo/issues/12631
* <p>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment