Search in sources :

Example 1 with User

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;
}
Also used : User(org.stepik.api.objects.users.User) TokenInfo(org.stepik.api.objects.auth.TokenInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with User

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;
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) User(org.stepik.api.objects.users.User) StepikAuthManager.getCurrentUser(org.stepik.core.stepik.StepikAuthManager.getCurrentUser) Submissions(org.stepik.api.objects.submissions.Submissions) StepikClientException(org.stepik.api.exceptions.StepikClientException)

Example 3 with User

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();
    }
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) User(org.stepik.api.objects.users.User) StepikAuthManager.getCurrentUser(org.stepik.core.stepik.StepikAuthManager.getCurrentUser) StepikClientException(org.stepik.api.exceptions.StepikClientException)

Example 4 with User

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)));
    }
}
Also used : AUTH(org.stepik.core.stepik.StepikAuthState.AUTH) Application(com.intellij.openapi.application.Application) CredentialAttributes(com.intellij.credentialStore.CredentialAttributes) PLUGIN_ID(org.stepik.core.utils.PluginUtils.PLUGIN_ID) ModalityState(com.intellij.openapi.application.ModalityState) StepikClientException(org.stepik.api.exceptions.StepikClientException) CompletableFuture(java.util.concurrent.CompletableFuture) ProductGroup(org.stepik.core.utils.ProductGroup) PluginUtils.getCurrentProduct(org.stepik.core.utils.PluginUtils.getCurrentProduct) User(org.stepik.api.objects.users.User) ArrayList(java.util.ArrayList) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) Credentials(com.intellij.credentialStore.Credentials) Metrics(org.stepik.core.metrics.Metrics) Map(java.util.Map) Logger(com.intellij.openapi.diagnostic.Logger) PluginUtils.getVersion(org.stepik.core.utils.PluginUtils.getVersion) SUCCESSFUL(org.stepik.core.metrics.MetricsStatus.SUCCESSFUL) HttpConfigurable(com.intellij.util.net.HttpConfigurable) ExecutorService(java.util.concurrent.ExecutorService) PropertiesComponent(com.intellij.ide.util.PropertiesComponent) SHOW_DIALOG(org.stepik.core.stepik.StepikAuthState.SHOW_DIALOG) PluginUtils.getCurrentProductVersion(org.stepik.core.utils.PluginUtils.getCurrentProductVersion) NOT_AUTH(org.stepik.core.stepik.StepikAuthState.NOT_AUTH) Executors(java.util.concurrent.Executors) UNKNOWN(org.stepik.core.stepik.StepikAuthState.UNKNOWN) InvocationTargetException(java.lang.reflect.InvocationTargetException) PluginUtils(org.stepik.core.utils.PluginUtils) AuthDialog(org.stepik.plugin.auth.ui.AuthDialog) HttpTransportClient(org.stepik.api.client.HttpTransportClient) TokenInfo(org.stepik.api.objects.auth.TokenInfo) List(java.util.List) PasswordSafe(com.intellij.ide.passwordSafe.PasswordSafe) ApplicationManager(com.intellij.openapi.application.ApplicationManager) StepikProjectManager(org.stepik.core.StepikProjectManager) StepikApiClient(org.stepik.api.client.StepikApiClient) NotNull(org.jetbrains.annotations.NotNull) javax.swing(javax.swing) TokenInfo(org.stepik.api.objects.auth.TokenInfo)

Example 5 with User

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]));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) Application(com.intellij.openapi.application.Application) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) ProjectFilesUtils.getOrCreateSrcPsiDirectory(org.stepik.core.utils.ProjectFilesUtils.getOrCreateSrcPsiDirectory) Computable(com.intellij.openapi.util.Computable) StepikClientException(org.stepik.api.exceptions.StepikClientException) User(org.stepik.api.objects.users.User) PsiManager(com.intellij.psi.PsiManager) ArrayList(java.util.ArrayList) ProjectView(com.intellij.ide.projectView.ProjectView) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) ProjectFilesUtils.getOrCreatePsiDirectory(org.stepik.core.utils.ProjectFilesUtils.getOrCreatePsiDirectory) Metrics(org.stepik.core.metrics.Metrics) StepikAuthManager.getCurrentUser(org.stepik.core.stepik.StepikAuthManager.getCurrentUser) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Logger(com.intellij.openapi.diagnostic.Logger) SUCCESSFUL(org.stepik.core.metrics.MetricsStatus.SUCCESSFUL) Submission(org.stepik.api.objects.submissions.Submission) SupportedLanguages(org.stepik.core.SupportedLanguages) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) StepNode(org.stepik.core.courseFormat.StepNode) IOException(java.io.IOException) StudyUtils(org.stepik.core.StudyUtils) MoveFilesOrDirectoriesUtil(com.intellij.refactoring.move.moveFilesOrDirectories.MoveFilesOrDirectoriesUtil) Nullable(org.jetbrains.annotations.Nullable) EduNames(org.stepik.core.core.EduNames) Submissions(org.stepik.api.objects.submissions.Submissions) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Optional(java.util.Optional) PsiDirectory(com.intellij.psi.PsiDirectory) StepikApiClient(org.stepik.api.client.StepikApiClient) NotNull(org.jetbrains.annotations.NotNull) Order(org.stepik.api.queries.Order) StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) User(org.stepik.api.objects.users.User) StepikAuthManager.getCurrentUser(org.stepik.core.stepik.StepikAuthManager.getCurrentUser) Optional(java.util.Optional) PsiFile(com.intellij.psi.PsiFile) IOException(java.io.IOException) Application(com.intellij.openapi.application.Application) Submissions(org.stepik.api.objects.submissions.Submissions) StepikClientException(org.stepik.api.exceptions.StepikClientException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

User (org.stepik.api.objects.users.User)5 StepikApiClient (org.stepik.api.client.StepikApiClient)4 StepikClientException (org.stepik.api.exceptions.StepikClientException)4 NotNull (org.jetbrains.annotations.NotNull)3 StepikAuthManager.authAndGetStepikApiClient (org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient)3 StepikAuthManager.getCurrentUser (org.stepik.core.stepik.StepikAuthManager.getCurrentUser)3 Application (com.intellij.openapi.application.Application)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 ArrayList (java.util.ArrayList)2 Submissions (org.stepik.api.objects.submissions.Submissions)2 Metrics (org.stepik.core.metrics.Metrics)2 SUCCESSFUL (org.stepik.core.metrics.MetricsStatus.SUCCESSFUL)2 CredentialAttributes (com.intellij.credentialStore.CredentialAttributes)1 Credentials (com.intellij.credentialStore.Credentials)1 PasswordSafe (com.intellij.ide.passwordSafe.PasswordSafe)1 ProjectView (com.intellij.ide.projectView.ProjectView)1 PropertiesComponent (com.intellij.ide.util.PropertiesComponent)1 ModalityState (com.intellij.openapi.application.ModalityState)1 Document (com.intellij.openapi.editor.Document)1