use of org.stepik.api.objects.users.User in project intellij-plugins by StepicOrg.
the class StepikAuthManager method showDialog.
@NotNull
private static StepikAuthState showDialog() {
Map<String, String> map = AuthDialog.showAuthForm();
StepikAuthState newState = NOT_AUTH;
TokenInfo tokenInfo = new TokenInfo();
if (!map.isEmpty() && !map.containsKey("error")) {
newState = AUTH;
tokenInfo.setAccessToken(map.get("access_token"));
tokenInfo.setExpiresIn(Integer.valueOf(map.getOrDefault("expires_in", "0")));
tokenInfo.setScope(map.get("scope"));
tokenInfo.setTokenType(map.get("token_type"));
tokenInfo.setRefreshToken(map.get("refresh_token"));
}
stepikApiClient.setTokenInfo(tokenInfo);
if (newState == AUTH && tokenInfo.getAccessToken() != null) {
User user = getCurrentUser(true);
if (!user.isGuest()) {
setTokenInfo(user.getId(), tokenInfo);
} else {
newState = NOT_AUTH;
}
}
logger.info("Show the authentication dialog with result: " + newState);
return newState;
}
use of org.stepik.api.objects.users.User in project intellij-plugins by StepicOrg.
the class QuizHelper method getSubmissionsCount.
public int getSubmissionsCount() {
if (submissionsCount == -1) {
StepikApiClient stepikApiClient = authAndGetStepikApiClient();
User user = getCurrentUser();
if (user.isGuest()) {
action = NEED_LOGIN;
return 0;
}
long userId = user.getId();
submissionsCount = 0;
int page = 1;
Submissions submissions;
do {
try {
submissions = stepikApiClient.submissions().get().step(getStepNode().getId()).user(userId).page(page).execute();
} catch (StepikClientException e) {
logger.warn("Failed get submissions count", e);
return 0;
}
submissionsCount += submissions.getCount();
page++;
} while (submissions.getMeta().getHasNext());
}
return submissionsCount;
}
use of org.stepik.api.objects.users.User in project intellij-plugins by StepicOrg.
the class QuizHelper method initStepOptions.
void initStepOptions() {
if (initialized) {
return;
}
initialized = true;
status = UNCHECKED;
action = GET_FIRST_ATTEMPT;
StepikApiClient stepikApiClient = authAndGetStepikApiClient();
User user = getCurrentUser();
if (user.isGuest()) {
action = NEED_LOGIN;
fail();
initialized = false;
return;
}
long userId = user.getId();
try {
if (!loadAttempt(stepikApiClient, userId)) {
fail();
initialized = false;
return;
}
loadSubmission(stepikApiClient, userId);
done();
initialized = true;
} catch (StepikClientException e) {
logger.warn("Failed init test-step options", e);
fail();
}
}
use of org.stepik.api.objects.users.User in project intellij-plugins by StepicOrg.
the class StepikAuthManager method setState.
private static void setState(@NotNull StepikAuthState value) {
StepikAuthState oldState = state;
state = value;
if (state == NOT_AUTH) {
stepikApiClient.setTokenInfo(null);
user = null;
long userId = getLastUser();
setTokenInfo(userId, new TokenInfo());
setLastUser(0);
}
if (oldState != state) {
if (state == AUTH) {
Metrics.authenticate(SUCCESSFUL);
}
executor.execute(() -> listeners.forEach(listener -> listener.stateChanged(oldState, state)));
}
}
use of org.stepik.api.objects.users.User in project intellij-plugins by StepicOrg.
the class ProgrammingLanguageUtils method getOrCreateMainFile.
@Nullable
private static PsiFile getOrCreateMainFile(@NotNull Project project, @NotNull VirtualFile parent, @NotNull SupportedLanguages language, @NotNull StepNode stepNode) {
String fileName = language.getMainFileName();
final VirtualFile[] file = { parent.findChild(fileName) };
Application application = ApplicationManager.getApplication();
if (file[0] == null) {
application.invokeAndWait(() -> application.runWriteAction(() -> {
try {
file[0] = parent.createChildData(null, fileName);
String template = null;
StepikApiClient stepikApiClient = authAndGetStepikApiClient();
User user = getCurrentUser();
if (!user.isGuest()) {
try {
Submissions submissions = stepikApiClient.submissions().get().user(user.getId()).order(Order.DESC).step(stepNode.getId()).execute();
if (!submissions.isEmpty()) {
Optional<Submission> lastSubmission = submissions.getItems().stream().filter(submission -> SupportedLanguages.langOfName(submission.getReply().getLanguage()) == language).limit(1).findFirst();
if (lastSubmission.isPresent()) {
template = lastSubmission.get().getReply().getCode();
}
}
} catch (StepikClientException e) {
logger.warn(e);
}
}
if (template == null) {
template = stepNode.getTemplate(language);
}
file[0].setBinaryContent(template.getBytes());
} catch (IOException e) {
file[0] = null;
}
}));
}
return application.runReadAction((Computable<PsiFile>) () -> PsiManager.getInstance(project).findFile(file[0]));
}
Aggregations