<commit_before>package controllers;

import models.Project;
import models.User;
import models.UserProjectNotification;
import models.Watch;
import models.enumeration.EventType;
import models.enumeration.ResourceType;
import org.codehaus.jackson.node.ObjectNode;
import play.i18n.Messages;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.Result;
import utils.Constants;
import utils.ErrorViews;
import utils.WatchService;

public class WatchProjectApp extends Controller {

    public static Result watch(String userName, String projectName) {
        Project project = Project.findByOwnerAndProjectName(userName, projectName);
        if(project == null) {
            return badProject(userName, projectName);
        }

        User user = UserApp.currentUser();
        if(user.isAnonymous()) {
            flash(Constants.WARNING, "user.login.alert");
            return redirect(routes.UserApp.loginForm());
        }

        WatchService.watch(project.asResource());

        return redirect(request().getHeader(Http.HeaderNames.REFERER));
    }

    public static Result unwatch(String userName, String projectName) {
        Project project = Project.findByOwnerAndProjectName(userName, projectName);
        if(project == null) {
            return badProject(userName, projectName);
        }

        User user = UserApp.currentUser();
        if(user.isAnonymous()) {
            flash(Constants.WARNING, "user.login.alert");
            return redirect(routes.UserApp.loginForm());
        }

        WatchService.unwatch(project.asResource());

        return redirect(request().getHeader(Http.HeaderNames.REFERER));
    }

    public static Result toggle(Long projectId, String notificationType) {
        EventType notiType = EventType.valueOf(notificationType);
        Project project = Project.find.byId(projectId);
        if(project == null) {
            return notFound(ErrorViews.NotFound.render("No project matches given id '" + projectId + "'"));
        }

        User user = UserApp.currentUser();

        if(!WatchService.isWatching(user, project.asResource())) {
            return badRequest(Messages.get("error.notfound.watch"));
        }

        UserProjectNotification upn = UserProjectNotification.findOne(user, project, notiType);
        if(upn == null) { // make the EventType OFF, because default is ON.
            UserProjectNotification.unwatchExplictly(user, project, notiType);
        } else {
            upn.toggle();
        }

        return ok();
    }

    private static Result badProject(String userName, String projectName) {
        return badRequest(ErrorViews.BadRequest.render("No project matches given user name '" + userName + "' and project name '" + projectName + "'"));
    }
}<commit_msg>watching project: fixed to check auth<commit_after>package controllers;

import actions.AnonymousCheckAction;
import controllers.annotation.ProjectAccess;
import models.Project;
import models.User;
import models.UserProjectNotification;
import models.Watch;
import models.enumeration.EventType;
import models.enumeration.Operation;
import models.enumeration.ResourceType;
import org.codehaus.jackson.node.ObjectNode;
import play.i18n.Messages;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.Result;
import play.mvc.With;
import utils.Constants;
import utils.ErrorViews;
import utils.WatchService;

public class WatchProjectApp extends Controller {

    @ProjectAccess(Operation.READ)
    @With(AnonymousCheckAction.class)
    public static Result watch(String userName, String projectName) {
        Project project = Project.findByOwnerAndProjectName(userName, projectName);
        WatchService.watch(project.asResource());
        return redirect(request().getHeader(Http.HeaderNames.REFERER));
    }

    @ProjectAccess(Operation.READ)
    @With(AnonymousCheckAction.class)
    public static Result unwatch(String userName, String projectName) {
        Project project = Project.findByOwnerAndProjectName(userName, projectName);
        WatchService.unwatch(project.asResource());
        return redirect(request().getHeader(Http.HeaderNames.REFERER));
    }

    @ProjectAccess(Operation.READ)
    @With(AnonymousCheckAction.class)
    public static Result toggle(Long projectId, String notificationType) {
        EventType notiType = EventType.valueOf(notificationType);
        Project project = Project.find.byId(projectId);

        User user = UserApp.currentUser();
        if(!WatchService.isWatching(user, project.asResource())) {
            return badRequest(Messages.get("error.notfound.watch"));
        }

        UserProjectNotification upn = UserProjectNotification.findOne(user, project, notiType);
        if(upn == null) { // make the EventType OFF, because default is ON.
            UserProjectNotification.unwatchExplictly(user, project, notiType);
        } else {
            upn.toggle();
        }

        return ok();
    }
}
{'fraction_non_alphanumeric': 0.10262052410482096, 'fraction_numerical': 0.0008001600320064013, 'mean_word_length': 3.488330341113106, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4211086', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1309, 'n_words': 287}