Commit 8675f5bd authored by Liang Ding's avatar Liang Ding

#12514

parent 147746a2
...@@ -319,37 +319,47 @@ public final class Option { ...@@ -319,37 +319,47 @@ public final class Option {
*/ */
public static final String ID_C_OAUTH_GITHUB = "oauthGitHub"; public static final String ID_C_OAUTH_GITHUB = "oauthGitHub";
/**
* Key of GitHub repos.
*/
public static final String ID_C_GITHUB_REPOS = "githubRepos";
// Category constants // Category constants
/** /**
* Qiniu. * Category - Qiniu.
*/ */
public static final String CATEGORY_C_QINIU = "qiniu"; public static final String CATEGORY_C_QINIU = "qiniu";
/** /**
* Aliyun * Category - Aliyun
*/ */
public static final String CATEGORY_C_ALIYUN = "aliyun"; public static final String CATEGORY_C_ALIYUN = "aliyun";
/** /**
* Cloud object storage * Category - Cloud object storage
*/ */
public static final String CATEGORY_C_CLOU_STORAGE = "cloudStorage"; public static final String CATEGORY_C_CLOU_STORAGE = "cloudStorage";
/** /**
* Preference. * Category - Preference.
*/ */
public static final String CATEGORY_C_PREFERENCE = "preference"; public static final String CATEGORY_C_PREFERENCE = "preference";
/** /**
* Statistic. * Category - Statistic.
*/ */
public static final String CATEGORY_C_STATISTIC = "statistic"; public static final String CATEGORY_C_STATISTIC = "statistic";
/** /**
* OAuth. * Category - OAuth.
*/ */
public static final String CATEGORY_C_OAUTH = "oauth"; public static final String CATEGORY_C_OAUTH = "oauth";
/**
* Category - GitHub.
*/
public static final String CATEGORY_C_GITHUB = "github";
//// Transient //// //// Transient ////
/** /**
* Key of statistic blog published article count. * Key of statistic blog published article count.
......
...@@ -96,7 +96,7 @@ public class CronMgmtService { ...@@ -96,7 +96,7 @@ public class CronMgmtService {
} finally { } finally {
Stopwatchs.release(); Stopwatchs.release();
} }
}, delay, 1000 * 60 * 10, TimeUnit.MILLISECONDS); }, delay, 1000 * 60 * 60 * 24, TimeUnit.MILLISECONDS);
delay += 2000; delay += 2000;
} }
......
...@@ -104,6 +104,12 @@ public class PageMgmtService { ...@@ -104,6 +104,12 @@ public class PageMgmtService {
@Inject @Inject
private OptionQueryService optionQueryService; private OptionQueryService optionQueryService;
/**
* Option management service.
*/
@Inject
private OptionMgmtService optionMgmtService;
/** /**
* Refreshes GitHub repos. * Refreshes GitHub repos.
* 同步 GitHub 仓库 https://github.com/b3log/solo/issues/12514 * 同步 GitHub 仓库 https://github.com/b3log/solo/issues/12514
...@@ -122,13 +128,24 @@ public class PageMgmtService { ...@@ -122,13 +128,24 @@ public class PageMgmtService {
final JSONArray github = new JSONArray(value); final JSONArray github = new JSONArray(value);
final String githubPair = github.optString(0);// Just refresh the first account final String githubPair = github.optString(0);// Just refresh the first account
final String githubUserId = githubPair.split(OAuthGitHubProcessor.GITHUB_SPLIT)[0]; final String githubUserId = githubPair.split(OAuthGitHubProcessor.GITHUB_SPLIT)[0];
final List<JSONObject> gitHubRepos = Solos.getGitHubRepos(githubUserId); final JSONArray gitHubRepos = Solos.getGitHubRepos(githubUserId);
if (null == gitHubRepos || gitHubRepos.isEmpty()) { if (null == gitHubRepos || gitHubRepos.isEmpty()) {
return; return;
} }
final String userId = githubPair.split(OAuthGitHubProcessor.GITHUB_SPLIT)[1];
System.out.println(userId + ": " + gitHubRepos); JSONObject githubReposOpt = optionQueryService.getOptionById(Option.ID_C_GITHUB_REPOS);
if (null == githubReposOpt) {
githubReposOpt = new JSONObject();
githubReposOpt.put(Keys.OBJECT_ID, Option.ID_C_GITHUB_REPOS);
githubReposOpt.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_GITHUB);
}
githubReposOpt.put(Option.OPTION_VALUE, gitHubRepos.toString());
try {
optionMgmtService.addOrUpdateOption(githubReposOpt);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Updates github repos option failed", e);
}
} }
/** /**
......
...@@ -47,7 +47,10 @@ import javax.servlet.http.Cookie; ...@@ -47,7 +47,10 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import java.util.*; import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
/** /**
* Solo utilities. * Solo utilities.
...@@ -170,9 +173,9 @@ public final class Solos { ...@@ -170,9 +173,9 @@ public final class Solos {
* Gets GitHub repos. * Gets GitHub repos.
* *
* @param githubUserId the specified GitHub user id * @param githubUserId the specified GitHub user id
* @return GitHub repos, returns an empty list if not found * @return GitHub repos, returns {@code null} if not found
*/ */
public static List<JSONObject> getGitHubRepos(final String githubUserId) { public static JSONArray getGitHubRepos(final String githubUserId) {
try { try {
final HttpResponse res = HttpRequest.get("https://hacpai.com/github/repos?id=" + githubUserId). final HttpResponse res = HttpRequest.get("https://hacpai.com/github/repos?id=" + githubUserId).
connectionTimeout(3000).timeout(7000).header("User-Agent", Solos.USER_AGENT).send(); connectionTimeout(3000).timeout(7000).header("User-Agent", Solos.USER_AGENT).send();
...@@ -182,16 +185,16 @@ public final class Solos { ...@@ -182,16 +185,16 @@ public final class Solos {
res.charset("UTF-8"); res.charset("UTF-8");
final JSONObject result = new JSONObject(res.bodyText()); final JSONObject result = new JSONObject(res.bodyText());
if (0 != result.optInt(Keys.STATUS_CODE)) { if (0 != result.optInt(Keys.STATUS_CODE)) {
return Collections.emptyList(); return null;
} }
final JSONObject data = result.optJSONObject(Common.DATA); final JSONObject data = result.optJSONObject(Common.DATA);
final JSONArray repos = data.optJSONArray("githubrepos"); final JSONArray ret = data.optJSONArray("githubrepos");
return CollectionUtils.jsonArrayToList(repos); return ret;
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.ERROR, "Gets GitHub repos failed", e); LOGGER.log(Level.ERROR, "Gets GitHub repos failed", e);
return Collections.emptyList(); return null;
} }
} }
......
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