| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 4 | import edu.ucsb.cs156.frontiers.entities.CourseOption; | |
| 5 | import edu.ucsb.cs156.frontiers.entities.CourseStaff; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
| 7 | import edu.ucsb.cs156.frontiers.enums.CourseOptions; | |
| 8 | import edu.ucsb.cs156.frontiers.enums.RosterStatus; | |
| 9 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 10 | import edu.ucsb.cs156.frontiers.errors.SlackApiException; | |
| 11 | import edu.ucsb.cs156.frontiers.jobs.SetupPrivateSlackChannelsJob; | |
| 12 | import edu.ucsb.cs156.frontiers.jobs.SetupSectionSlackChannelsJob; | |
| 13 | import edu.ucsb.cs156.frontiers.jobs.SetupTeamSlackChannelsJob; | |
| 14 | import edu.ucsb.cs156.frontiers.models.SlackAuthTestResponse; | |
| 15 | import edu.ucsb.cs156.frontiers.models.SlackUser; | |
| 16 | import edu.ucsb.cs156.frontiers.repositories.CourseOptionRepository; | |
| 17 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 18 | import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository; | |
| 19 | import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository; | |
| 20 | import edu.ucsb.cs156.frontiers.repositories.SectionRepository; | |
| 21 | import edu.ucsb.cs156.frontiers.repositories.TeamRepository; | |
| 22 | import edu.ucsb.cs156.frontiers.services.CanvasApiTokenSecurityService; | |
| 23 | import edu.ucsb.cs156.frontiers.services.SlackService; | |
| 24 | import edu.ucsb.cs156.frontiers.utilities.CanonicalFormConverter; | |
| 25 | import edu.ucsb.cs156.jobs.entities.Job; | |
| 26 | import edu.ucsb.cs156.jobs.services.JobService; | |
| 27 | import io.swagger.v3.oas.annotations.Operation; | |
| 28 | import io.swagger.v3.oas.annotations.Parameter; | |
| 29 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 30 | import java.util.ArrayList; | |
| 31 | import java.util.HashMap; | |
| 32 | import java.util.LinkedHashMap; | |
| 33 | import java.util.List; | |
| 34 | import java.util.Map; | |
| 35 | import org.springframework.http.HttpStatus; | |
| 36 | import org.springframework.http.ResponseEntity; | |
| 37 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 38 | import org.springframework.web.bind.annotation.ExceptionHandler; | |
| 39 | import org.springframework.web.bind.annotation.GetMapping; | |
| 40 | import org.springframework.web.bind.annotation.PostMapping; | |
| 41 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 42 | import org.springframework.web.bind.annotation.RequestParam; | |
| 43 | import org.springframework.web.bind.annotation.RestController; | |
| 44 | ||
| 45 | /** | |
| 46 | * Endpoints for the Slack integration of a course. | |
| 47 | * | |
| 48 | * <p>The Slack bot token is a secret: it is encrypted at rest (using the same infrastructure as | |
| 49 | * Canvas API tokens), never logged, and only ever returned to the frontend in masked form. | |
| 50 | */ | |
| 51 | @RestController | |
| 52 | @RequestMapping("/api/courses/slack") | |
| 53 | @Tag(name = "Slack") | |
| 54 | public class SlackController extends ApiController { | |
| 55 | ||
| 56 | private static final int VISIBLE_TOKEN_CHARS = 4; | |
| 57 | ||
| 58 | /** Error code used when a valid token could not be encrypted, and so was not stored. */ | |
| 59 | public static final String ENCRYPTION_FAILED = "encryption_failed"; | |
| 60 | ||
| 61 | /** Where a workspace can be opened when its own URL is not known. */ | |
| 62 | public static final String SLACK_CLIENT_URL = "https://app.slack.com/client/"; | |
| 63 | ||
| 64 | private final CourseRepository courseRepository; | |
| 65 | private final RosterStudentRepository rosterStudentRepository; | |
| 66 | private final CourseStaffRepository courseStaffRepository; | |
| 67 | private final SectionRepository sectionRepository; | |
| 68 | private final TeamRepository teamRepository; | |
| 69 | private final CourseOptionRepository courseOptionRepository; | |
| 70 | private final SlackService slackService; | |
| 71 | private final CanvasApiTokenSecurityService tokenSecurityService; | |
| 72 | private final JobService jobService; | |
| 73 | ||
| 74 | public SlackController( | |
| 75 | CourseRepository courseRepository, | |
| 76 | RosterStudentRepository rosterStudentRepository, | |
| 77 | CourseStaffRepository courseStaffRepository, | |
| 78 | SectionRepository sectionRepository, | |
| 79 | TeamRepository teamRepository, | |
| 80 | CourseOptionRepository courseOptionRepository, | |
| 81 | SlackService slackService, | |
| 82 | CanvasApiTokenSecurityService tokenSecurityService, | |
| 83 | JobService jobService) { | |
| 84 | this.courseRepository = courseRepository; | |
| 85 | this.rosterStudentRepository = rosterStudentRepository; | |
| 86 | this.courseStaffRepository = courseStaffRepository; | |
| 87 | this.sectionRepository = sectionRepository; | |
| 88 | this.teamRepository = teamRepository; | |
| 89 | this.courseOptionRepository = courseOptionRepository; | |
| 90 | this.slackService = slackService; | |
| 91 | this.tokenSecurityService = tokenSecurityService; | |
| 92 | this.jobService = jobService; | |
| 93 | } | |
| 94 | ||
| 95 | /** A person with an active account in the Slack workspace, and their role in the course. */ | |
| 96 | public static record SlackUserView( | |
| 97 | String slackUserId, | |
| 98 | String name, | |
| 99 | String realName, | |
| 100 | String displayName, | |
| 101 | String email, | |
| 102 | String courseRole) {} | |
| 103 | ||
| 104 | /** A roster student or staff member who does not have an active account in the workspace. */ | |
| 105 | public static record SlackMissingMemberView( | |
| 106 | String courseRole, String firstName, String lastName, String email, String slackStatus) {} | |
| 107 | ||
| 108 | public static final String ROLE_INSTRUCTOR = "INSTRUCTOR"; | |
| 109 | public static final String ROLE_STAFF = "STAFF"; | |
| 110 | public static final String ROLE_STUDENT = "STUDENT"; | |
| 111 | public static final String ROLE_NONE = "NONE"; | |
| 112 | ||
| 113 | /** Invited to the workspace, but has not yet signed in. */ | |
| 114 | public static final String STATUS_INVITED = "INVITED"; | |
| 115 | ||
| 116 | /** Has an account in the workspace, but it has been deactivated. */ | |
| 117 | public static final String STATUS_DEACTIVATED = "DEACTIVATED"; | |
| 118 | ||
| 119 | /** Slack has no record of this email address in the workspace. */ | |
| 120 | public static final String STATUS_NOT_IN_SLACK = "NOT_IN_SLACK"; | |
| 121 | ||
| 122 | private static final String STATUS_ACTIVE = "ACTIVE"; | |
| 123 | ||
| 124 | /** | |
| 125 | * Returns the Slack workspace (team) associated with the course, along with a masked version of | |
| 126 | * the Slack bot token. | |
| 127 | * | |
| 128 | * @param courseId the id of the course | |
| 129 | * @return a map with courseId, slackBotToken (masked), slackTeamId, slackTeamName and | |
| 130 | * slackTeamUrl; values are empty strings when no token has been stored | |
| 131 | */ | |
| 132 | @Operation(summary = "Get Slack workspace info and Slack bot token (masked) for a course") | |
| 133 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 134 | @GetMapping("/info") | |
| 135 | public Map<String, String> getSlackInfo( | |
| 136 | @Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 137 | Course course = | |
| 138 | courseRepository | |
| 139 | .findById(courseId) | |
| 140 |
1
1. lambda$getSlackInfo$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::lambda$getSlackInfo$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 141 | course = backfillSlackTeamUrl(course); | |
| 142 |
1
1. getSlackInfo : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/SlackController::getSlackInfo → KILLED |
return slackInfo(course); |
| 143 | } | |
| 144 | ||
| 145 | /** | |
| 146 | * Tokens saved before the workspace URL was recorded (see {@link Course#getSlackTeamUrl()}) have | |
| 147 | * no way to show a real "Admin" link; this calls Slack's <code>auth.test</code> again, using the | |
| 148 | * already-stored (and validated) token, to fetch and save the workspace's own URL. Any failure to | |
| 149 | * reach Slack is ignored here: the rest of the page still works from the cached data, and the | |
| 150 | * <code>app.slack.com</code> fallback URL is used instead. | |
| 151 | * | |
| 152 | * @param course the course | |
| 153 | * @return the course, with slackTeamUrl backfilled and saved if it was missing and could be | |
| 154 | * determined | |
| 155 | */ | |
| 156 | private Course backfillSlackTeamUrl(Course course) { | |
| 157 |
2
1. backfillSlackTeamUrl : negated conditional → KILLED 2. backfillSlackTeamUrl : negated conditional → KILLED |
boolean hasUrl = course.getSlackTeamUrl() != null && !course.getSlackTeamUrl().isEmpty(); |
| 158 |
2
1. backfillSlackTeamUrl : negated conditional → KILLED 2. backfillSlackTeamUrl : negated conditional → KILLED |
boolean hasToken = course.getSlackBotToken() != null && !course.getSlackBotToken().isEmpty(); |
| 159 |
2
1. backfillSlackTeamUrl : negated conditional → KILLED 2. backfillSlackTeamUrl : negated conditional → KILLED |
if (hasUrl || !hasToken) { |
| 160 |
1
1. backfillSlackTeamUrl : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::backfillSlackTeamUrl → KILLED |
return course; |
| 161 | } | |
| 162 | String token = tokenSecurityService.decrypt(course.getSlackBotToken()); | |
| 163 | SlackAuthTestResponse authTest = slackService.authTest(token); | |
| 164 |
3
1. backfillSlackTeamUrl : negated conditional → KILLED 2. backfillSlackTeamUrl : negated conditional → KILLED 3. backfillSlackTeamUrl : negated conditional → KILLED |
if (!authTest.getOk() || authTest.getUrl() == null || authTest.getUrl().isEmpty()) { |
| 165 |
1
1. backfillSlackTeamUrl : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::backfillSlackTeamUrl → KILLED |
return course; |
| 166 | } | |
| 167 |
1
1. backfillSlackTeamUrl : removed call to edu/ucsb/cs156/frontiers/entities/Course::setSlackTeamUrl → KILLED |
course.setSlackTeamUrl(authTest.getUrl()); |
| 168 |
1
1. backfillSlackTeamUrl : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::backfillSlackTeamUrl → KILLED |
return courseRepository.save(course); |
| 169 | } | |
| 170 | ||
| 171 | /** | |
| 172 | * Validates a Slack bot token by calling the Slack <code>auth.test</code> method. If the token is | |
| 173 | * valid, it is encrypted and stored, along with the id and name of the workspace (team) it | |
| 174 | * belongs to. If it is invalid, nothing is stored, and a 400 response describes the problem. | |
| 175 | * | |
| 176 | * <p>The token should be sent in the (form encoded) body of the request rather than in the query | |
| 177 | * string, so that it does not end up in access logs. | |
| 178 | * | |
| 179 | * @param courseId the id of the course | |
| 180 | * @param slackBotToken the Slack bot token (<code>xoxb-...</code>) | |
| 181 | * @return on success, a map with ok=true, courseId, slackBotToken (masked), slackTeamId and | |
| 182 | * slackTeamName; on failure, a map with ok=false, error (Slack error code) and message. If | |
| 183 | * the token is valid but cannot be encrypted (e.g. the encryption key is not configured), it | |
| 184 | * is not stored, and a 500 response with error {@link #ENCRYPTION_FAILED} is returned. | |
| 185 | */ | |
| 186 | @Operation(summary = "Validate (via Slack auth.test) and store the Slack bot token for a course") | |
| 187 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 188 | @PostMapping("/token") | |
| 189 | public ResponseEntity<Map<String, Object>> updateSlackToken( | |
| 190 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 191 | @Parameter(name = "slackBotToken") @RequestParam String slackBotToken) { | |
| 192 | Course course = | |
| 193 | courseRepository | |
| 194 | .findById(courseId) | |
| 195 |
1
1. lambda$updateSlackToken$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::lambda$updateSlackToken$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 196 | ||
| 197 | String token = slackBotToken.strip(); | |
| 198 | SlackAuthTestResponse authTest = slackService.authTest(token); | |
| 199 | ||
| 200 |
1
1. updateSlackToken : negated conditional → KILLED |
if (!authTest.getOk()) { |
| 201 | Map<String, Object> body = new LinkedHashMap<>(); | |
| 202 | body.put("ok", false); | |
| 203 | body.put("error", String.valueOf(authTest.getError())); | |
| 204 | body.put("message", errorMessage(authTest.getError())); | |
| 205 |
1
1. updateSlackToken : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::updateSlackToken → KILLED |
return ResponseEntity.badRequest().body(body); |
| 206 | } | |
| 207 | ||
| 208 | String encryptedToken; | |
| 209 | try { | |
| 210 | encryptedToken = tokenSecurityService.encrypt(token); | |
| 211 | } catch (IllegalStateException e) { | |
| 212 | // Typically a missing or malformed encryption key. The exception message describes the key | |
| 213 | // problem and never contains the token. | |
| 214 | Map<String, Object> body = new LinkedHashMap<>(); | |
| 215 | body.put("ok", false); | |
| 216 | body.put("error", ENCRYPTION_FAILED); | |
| 217 | body.put("message", encryptionFailedMessage(e.getMessage())); | |
| 218 |
1
1. updateSlackToken : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::updateSlackToken → KILLED |
return ResponseEntity.internalServerError().body(body); |
| 219 | } | |
| 220 | ||
| 221 |
1
1. updateSlackToken : removed call to edu/ucsb/cs156/frontiers/entities/Course::setSlackBotToken → KILLED |
course.setSlackBotToken(encryptedToken); |
| 222 |
1
1. updateSlackToken : removed call to edu/ucsb/cs156/frontiers/entities/Course::setSlackTeamId → KILLED |
course.setSlackTeamId(authTest.getTeamId()); |
| 223 |
1
1. updateSlackToken : removed call to edu/ucsb/cs156/frontiers/entities/Course::setSlackTeamName → KILLED |
course.setSlackTeamName(authTest.getTeam()); |
| 224 |
1
1. updateSlackToken : removed call to edu/ucsb/cs156/frontiers/entities/Course::setSlackTeamUrl → KILLED |
course.setSlackTeamUrl(authTest.getUrl()); |
| 225 | Course savedCourse = courseRepository.save(course); | |
| 226 | ||
| 227 | Map<String, Object> body = new LinkedHashMap<>(); | |
| 228 | body.put("ok", true); | |
| 229 |
1
1. updateSlackToken : removed call to java/util/Map::putAll → KILLED |
body.putAll(slackInfo(savedCourse)); |
| 230 |
1
1. updateSlackToken : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::updateSlackToken → KILLED |
return ResponseEntity.ok(body); |
| 231 | } | |
| 232 | ||
| 233 | private Map<String, String> slackInfo(Course course) { | |
| 234 | Map<String, String> info = new LinkedHashMap<>(); | |
| 235 | info.put("courseId", course.getId().toString()); | |
| 236 | info.put("slackBotToken", maskToken(tokenSecurityService.decrypt(course.getSlackBotToken()))); | |
| 237 |
1
1. slackInfo : negated conditional → KILLED |
info.put("slackTeamId", course.getSlackTeamId() != null ? course.getSlackTeamId() : ""); |
| 238 |
1
1. slackInfo : negated conditional → KILLED |
info.put("slackTeamName", course.getSlackTeamName() != null ? course.getSlackTeamName() : ""); |
| 239 | info.put("slackTeamUrl", slackTeamUrl(course)); | |
| 240 |
1
1. slackInfo : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/SlackController::slackInfo → KILLED |
return info; |
| 241 | } | |
| 242 | ||
| 243 | /** | |
| 244 | * The URL of the course's Slack workspace, e.g. <code>https://ucsb-cs156-f26.slack.com/</code>. | |
| 245 | * Tokens stored before the URL was being recorded fall back to a URL based on the team id. | |
| 246 | * | |
| 247 | * @param course the course | |
| 248 | * @return the URL, or an empty string if the course is not connected to a workspace | |
| 249 | */ | |
| 250 | public static String slackTeamUrl(Course course) { | |
| 251 |
2
1. slackTeamUrl : negated conditional → KILLED 2. slackTeamUrl : negated conditional → KILLED |
if (course.getSlackTeamUrl() != null && !course.getSlackTeamUrl().isEmpty()) { |
| 252 |
1
1. slackTeamUrl : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SlackController::slackTeamUrl → KILLED |
return course.getSlackTeamUrl(); |
| 253 | } | |
| 254 |
2
1. slackTeamUrl : negated conditional → KILLED 2. slackTeamUrl : negated conditional → KILLED |
if (course.getSlackTeamId() != null && !course.getSlackTeamId().isEmpty()) { |
| 255 |
1
1. slackTeamUrl : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SlackController::slackTeamUrl → KILLED |
return SLACK_CLIENT_URL + course.getSlackTeamId(); |
| 256 | } | |
| 257 | return ""; | |
| 258 | } | |
| 259 | ||
| 260 | /** | |
| 261 | * Lists the people with active accounts in the course's Slack workspace (no bots, no deactivated | |
| 262 | * accounts, no invitations that have not been accepted yet). Each is matched by email against the | |
| 263 | * course to determine whether they are the instructor, a staff member, a roster student (only | |
| 264 | * those with status ROSTER or MANUAL; not dropped students), or none of these. | |
| 265 | * | |
| 266 | * @param courseId the id of the course | |
| 267 | * @return the active users of the workspace | |
| 268 | */ | |
| 269 | @Operation(summary = "List active users of the course's Slack workspace, with their course role") | |
| 270 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 271 | @GetMapping("/users") | |
| 272 | public List<SlackUserView> getSlackUsers( | |
| 273 | @Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 274 | Course course = | |
| 275 | courseRepository | |
| 276 | .findById(courseId) | |
| 277 |
1
1. lambda$getSlackUsers$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::lambda$getSlackUsers$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 278 | List<SlackUser> slackUsers = listSlackUsers(course); | |
| 279 | ||
| 280 | Map<String, String> roleByEmail = new HashMap<>(); | |
| 281 | for (RosterStudent student : enrolledStudents(courseId)) { | |
| 282 |
1
1. getSlackUsers : negated conditional → KILLED |
if (student.getEmail() != null) { |
| 283 | roleByEmail.put(canonical(student.getEmail()), ROLE_STUDENT); | |
| 284 | } | |
| 285 | } | |
| 286 | for (CourseStaff staff : courseStaffRepository.findByCourseId(courseId)) { | |
| 287 |
1
1. getSlackUsers : negated conditional → KILLED |
if (staff.getEmail() != null) { |
| 288 | roleByEmail.put(canonical(staff.getEmail()), ROLE_STAFF); | |
| 289 | } | |
| 290 | } | |
| 291 |
1
1. getSlackUsers : negated conditional → KILLED |
if (course.getInstructorEmail() != null) { |
| 292 | roleByEmail.put(canonical(course.getInstructorEmail()), ROLE_INSTRUCTOR); | |
| 293 | } | |
| 294 | ||
| 295 | List<SlackUserView> result = new ArrayList<>(); | |
| 296 | for (SlackUser user : slackUsers) { | |
| 297 |
1
1. getSlackUsers : negated conditional → KILLED |
if (user.isActivePerson()) { |
| 298 | String role = | |
| 299 |
1
1. getSlackUsers : negated conditional → KILLED |
user.email() == null |
| 300 | ? ROLE_NONE | |
| 301 | : roleByEmail.getOrDefault(canonical(user.email()), ROLE_NONE); | |
| 302 | result.add( | |
| 303 | new SlackUserView( | |
| 304 | user.getId(), | |
| 305 | user.getName(), | |
| 306 | user.getRealName(), | |
| 307 | user.displayName(), | |
| 308 | user.email(), | |
| 309 | role)); | |
| 310 | } | |
| 311 | } | |
| 312 |
1
1. getSlackUsers : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/SlackController::getSlackUsers → KILLED |
return result; |
| 313 | } | |
| 314 | ||
| 315 | /** | |
| 316 | * Lists the roster students (only those with status ROSTER or MANUAL; not dropped students) and | |
| 317 | * the staff of the course that do not have an active account in the course's Slack workspace, | |
| 318 | * matching by email. For each, slackStatus indicates whether they have been invited but have not | |
| 319 | * signed in yet, have a deactivated account, or are unknown to the workspace. | |
| 320 | * | |
| 321 | * @param courseId the id of the course | |
| 322 | * @return staff first, then students | |
| 323 | */ | |
| 324 | @Operation(summary = "List roster students and staff that are not active in the Slack workspace") | |
| 325 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 326 | @GetMapping("/missing") | |
| 327 | public List<SlackMissingMemberView> getMissingMembers( | |
| 328 | @Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 329 | Course course = | |
| 330 | courseRepository | |
| 331 | .findById(courseId) | |
| 332 |
1
1. lambda$getMissingMembers$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::lambda$getMissingMembers$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 333 | List<SlackUser> slackUsers = listSlackUsers(course); | |
| 334 | ||
| 335 | // An active account wins over an invitation, which wins over a deactivated account | |
| 336 | Map<String, String> statusByEmail = new HashMap<>(); | |
| 337 | for (String status : List.of(STATUS_DEACTIVATED, STATUS_INVITED, STATUS_ACTIVE)) { | |
| 338 | for (SlackUser user : slackUsers) { | |
| 339 |
3
1. getMissingMembers : negated conditional → KILLED 2. getMissingMembers : negated conditional → KILLED 3. getMissingMembers : negated conditional → KILLED |
if (user.isPerson() && user.email() != null && status.equals(slackStatus(user))) { |
| 340 | statusByEmail.put(canonical(user.email()), status); | |
| 341 | } | |
| 342 | } | |
| 343 | } | |
| 344 | ||
| 345 | List<SlackMissingMemberView> result = new ArrayList<>(); | |
| 346 | for (CourseStaff staff : courseStaffRepository.findByCourseId(courseId)) { | |
| 347 | String status = missingStatus(statusByEmail, staff.getEmail()); | |
| 348 |
1
1. getMissingMembers : negated conditional → KILLED |
if (status != null) { |
| 349 | result.add( | |
| 350 | new SlackMissingMemberView( | |
| 351 | ROLE_STAFF, staff.getFirstName(), staff.getLastName(), staff.getEmail(), status)); | |
| 352 | } | |
| 353 | } | |
| 354 | for (RosterStudent student : enrolledStudents(courseId)) { | |
| 355 | String status = missingStatus(statusByEmail, student.getEmail()); | |
| 356 |
1
1. getMissingMembers : negated conditional → KILLED |
if (status != null) { |
| 357 | result.add( | |
| 358 | new SlackMissingMemberView( | |
| 359 | ROLE_STUDENT, | |
| 360 | student.getFirstName(), | |
| 361 | student.getLastName(), | |
| 362 | student.getEmail(), | |
| 363 | status)); | |
| 364 | } | |
| 365 | } | |
| 366 |
1
1. getMissingMembers : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/SlackController::getMissingMembers → KILLED |
return result; |
| 367 | } | |
| 368 | ||
| 369 | /** | |
| 370 | * Launches a job that creates the public Slack channels named in the sections table of the | |
| 371 | * course, adds the roster students of each section to its channel, and removes from those | |
| 372 | * channels anybody who is neither a student of the section, nor staff, nor the instructor. See | |
| 373 | * {@link SetupSectionSlackChannelsJob}. | |
| 374 | * | |
| 375 | * @param courseId the id of the course | |
| 376 | * @return the job that was launched; its log can be seen on the Jobs tab of the course | |
| 377 | */ | |
| 378 | @Operation(summary = "Launch job that sets up the Slack channels of the sections of a course") | |
| 379 | @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") | |
| 380 | @PostMapping("/sectionChannels") | |
| 381 | public Job setupSectionChannels(@Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 382 | Course course = | |
| 383 | courseRepository | |
| 384 | .findById(courseId) | |
| 385 |
1
1. lambda$setupSectionChannels$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::lambda$setupSectionChannels$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 386 | for (CourseOptions option : | |
| 387 | List.of(CourseOptions.SLACK_INTEGRATION, CourseOptions.TRANSLATE_SECTIONS)) { | |
| 388 | boolean enabled = | |
| 389 | courseOptionRepository | |
| 390 | .findByCourseIdAndOption(courseId, option.name()) | |
| 391 | .map(CourseOption::getEnabled) | |
| 392 | .orElse(false); | |
| 393 |
1
1. setupSectionChannels : negated conditional → KILLED |
if (!enabled) { |
| 394 | throw new IllegalArgumentException( | |
| 395 | "The course option %s must be enabled to set up section Slack channels." | |
| 396 | .formatted(option.name())); | |
| 397 | } | |
| 398 | } | |
| 399 | String token = tokenSecurityService.decrypt(course.getSlackBotToken()); | |
| 400 |
2
1. setupSectionChannels : negated conditional → KILLED 2. setupSectionChannels : negated conditional → KILLED |
if (token == null || token.isEmpty()) { |
| 401 | throw new IllegalArgumentException(NO_TOKEN_MESSAGE); | |
| 402 | } | |
| 403 | ||
| 404 | SetupSectionSlackChannelsJob job = | |
| 405 | SetupSectionSlackChannelsJob.builder() | |
| 406 | .course(course) | |
| 407 | .courseRepository(courseRepository) | |
| 408 | .sectionRepository(sectionRepository) | |
| 409 | .rosterStudentRepository(rosterStudentRepository) | |
| 410 | .courseStaffRepository(courseStaffRepository) | |
| 411 | .slackService(slackService) | |
| 412 | .tokenSecurityService(tokenSecurityService) | |
| 413 | .build(); | |
| 414 |
1
1. setupSectionChannels : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::setupSectionChannels → KILLED |
return jobService.runAsJob(job); |
| 415 | } | |
| 416 | ||
| 417 | /** | |
| 418 | * Launches a job that creates a public Slack channel for each team of the course (named "team-" | |
| 419 | * followed by the team name in Slack's form), adds the members of each team and the instructor to | |
| 420 | * the team's channel, and removes from those channels anybody who is neither on the team, nor the | |
| 421 | * instructor, nor staff. See {@link SetupTeamSlackChannelsJob}. | |
| 422 | * | |
| 423 | * @param courseId the id of the course | |
| 424 | * @return the job that was launched; its log can be seen on the Jobs tab of the course | |
| 425 | */ | |
| 426 | @Operation(summary = "Launch job that sets up the Slack channels of the teams of a course") | |
| 427 | @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") | |
| 428 | @PostMapping("/teamChannels") | |
| 429 | public Job setupTeamChannels(@Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 430 | Course course = | |
| 431 | courseRepository | |
| 432 | .findById(courseId) | |
| 433 |
1
1. lambda$setupTeamChannels$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::lambda$setupTeamChannels$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 434 | boolean enabled = | |
| 435 | courseOptionRepository | |
| 436 | .findByCourseIdAndOption(courseId, CourseOptions.SLACK_INTEGRATION.name()) | |
| 437 | .map(CourseOption::getEnabled) | |
| 438 | .orElse(false); | |
| 439 |
1
1. setupTeamChannels : negated conditional → KILLED |
if (!enabled) { |
| 440 | throw new IllegalArgumentException( | |
| 441 | "The course option SLACK_INTEGRATION must be enabled to set up team Slack channels."); | |
| 442 | } | |
| 443 | String token = tokenSecurityService.decrypt(course.getSlackBotToken()); | |
| 444 |
2
1. setupTeamChannels : negated conditional → KILLED 2. setupTeamChannels : negated conditional → KILLED |
if (token == null || token.isEmpty()) { |
| 445 | throw new IllegalArgumentException(NO_TOKEN_MESSAGE); | |
| 446 | } | |
| 447 | ||
| 448 | SetupTeamSlackChannelsJob job = | |
| 449 | SetupTeamSlackChannelsJob.builder() | |
| 450 | .course(course) | |
| 451 | .courseRepository(courseRepository) | |
| 452 | .teamRepository(teamRepository) | |
| 453 | .courseStaffRepository(courseStaffRepository) | |
| 454 | .slackService(slackService) | |
| 455 | .tokenSecurityService(tokenSecurityService) | |
| 456 | .build(); | |
| 457 |
1
1. setupTeamChannels : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::setupTeamChannels → KILLED |
return jobService.runAsJob(job); |
| 458 | } | |
| 459 | ||
| 460 | /** | |
| 461 | * Launches a job that gives each roster student of the course who has a Slack account a private | |
| 462 | * Slack channel (named "private-" followed by the first and last name of the student), and adds | |
| 463 | * the student, the instructor and the staff of the course to it. A student's channel is | |
| 464 | * recognized by its members rather than by its name, so running the job again creates no | |
| 465 | * duplicates; it does remove from the existing channels anybody who is neither the student, nor | |
| 466 | * the instructor, nor staff. See {@link SetupPrivateSlackChannelsJob}. | |
| 467 | * | |
| 468 | * @param courseId the id of the course | |
| 469 | * @return the job that was launched; its log can be seen on the Jobs tab of the course | |
| 470 | */ | |
| 471 | @Operation( | |
| 472 | summary = "Launch job that sets up a private Slack channel for each student plus the staff") | |
| 473 | @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") | |
| 474 | @PostMapping("/privateChannels") | |
| 475 | public Job setupPrivateChannels(@Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 476 | Course course = | |
| 477 | courseRepository | |
| 478 | .findById(courseId) | |
| 479 |
1
1. lambda$setupPrivateChannels$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::lambda$setupPrivateChannels$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 480 | boolean enabled = | |
| 481 | courseOptionRepository | |
| 482 | .findByCourseIdAndOption(courseId, CourseOptions.SLACK_INTEGRATION.name()) | |
| 483 | .map(CourseOption::getEnabled) | |
| 484 | .orElse(false); | |
| 485 |
1
1. setupPrivateChannels : negated conditional → KILLED |
if (!enabled) { |
| 486 | throw new IllegalArgumentException( | |
| 487 | "The course option SLACK_INTEGRATION must be enabled to set up private Slack channels."); | |
| 488 | } | |
| 489 | String token = tokenSecurityService.decrypt(course.getSlackBotToken()); | |
| 490 |
2
1. setupPrivateChannels : negated conditional → KILLED 2. setupPrivateChannels : negated conditional → KILLED |
if (token == null || token.isEmpty()) { |
| 491 | throw new IllegalArgumentException(NO_TOKEN_MESSAGE); | |
| 492 | } | |
| 493 | ||
| 494 | SetupPrivateSlackChannelsJob job = | |
| 495 | SetupPrivateSlackChannelsJob.builder() | |
| 496 | .course(course) | |
| 497 | .courseRepository(courseRepository) | |
| 498 | .rosterStudentRepository(rosterStudentRepository) | |
| 499 | .courseStaffRepository(courseStaffRepository) | |
| 500 | .slackService(slackService) | |
| 501 | .tokenSecurityService(tokenSecurityService) | |
| 502 | .build(); | |
| 503 |
1
1. setupPrivateChannels : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::setupPrivateChannels → KILLED |
return jobService.runAsJob(job); |
| 504 | } | |
| 505 | ||
| 506 | public static final String NO_TOKEN_MESSAGE = | |
| 507 | "No Slack token has been set for this course; enter one on the Settings tab."; | |
| 508 | ||
| 509 | /** Only these roster students are considered: in particular, not dropped students. */ | |
| 510 | public static final List<RosterStatus> ENROLLED_STATUSES = | |
| 511 | List.of(RosterStatus.ROSTER, RosterStatus.MANUAL); | |
| 512 | ||
| 513 | private List<RosterStudent> enrolledStudents(Long courseId) { | |
| 514 |
1
1. enrolledStudents : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/SlackController::enrolledStudents → KILLED |
return rosterStudentRepository |
| 515 | .findByCourseIdAndRosterStatusInOrderByFirstNameAscLastNameAscIgnoreCase( | |
| 516 | courseId, ENROLLED_STATUSES); | |
| 517 | } | |
| 518 | ||
| 519 | private static String slackStatus(SlackUser user) { | |
| 520 |
1
1. slackStatus : negated conditional → KILLED |
if (user.isActivePerson()) { |
| 521 |
1
1. slackStatus : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SlackController::slackStatus → KILLED |
return STATUS_ACTIVE; |
| 522 | } | |
| 523 |
2
1. slackStatus : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SlackController::slackStatus → KILLED 2. slackStatus : negated conditional → KILLED |
return user.getDeleted() ? STATUS_DEACTIVATED : STATUS_INVITED; |
| 524 | } | |
| 525 | ||
| 526 | /** | |
| 527 | * @return null if the email belongs to an active Slack user; otherwise the status to report | |
| 528 | */ | |
| 529 | private static String missingStatus(Map<String, String> statusByEmail, String email) { | |
| 530 |
1
1. missingStatus : negated conditional → KILLED |
if (email == null) { |
| 531 |
1
1. missingStatus : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SlackController::missingStatus → KILLED |
return STATUS_NOT_IN_SLACK; |
| 532 | } | |
| 533 | String status = statusByEmail.getOrDefault(canonical(email), STATUS_NOT_IN_SLACK); | |
| 534 |
2
1. missingStatus : negated conditional → KILLED 2. missingStatus : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SlackController::missingStatus → KILLED |
return status.equals(STATUS_ACTIVE) ? null : status; |
| 535 | } | |
| 536 | ||
| 537 | private static String canonical(String email) { | |
| 538 |
1
1. canonical : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SlackController::canonical → KILLED |
return CanonicalFormConverter.convertToValidEmail(email.strip()); |
| 539 | } | |
| 540 | ||
| 541 | private List<SlackUser> listSlackUsers(Course course) { | |
| 542 | String token = tokenSecurityService.decrypt(course.getSlackBotToken()); | |
| 543 |
2
1. listSlackUsers : negated conditional → KILLED 2. listSlackUsers : negated conditional → KILLED |
if (token == null || token.isEmpty()) { |
| 544 | throw new IllegalArgumentException(NO_TOKEN_MESSAGE); | |
| 545 | } | |
| 546 |
1
1. listSlackUsers : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/SlackController::listSlackUsers → KILLED |
return slackService.listUsers(token); |
| 547 | } | |
| 548 | ||
| 549 | /** | |
| 550 | * A failed call to Slack maps to a 502/Bad Gateway, with the Slack error code and a message. | |
| 551 | * | |
| 552 | * @param e the exception; its message is the Slack error code | |
| 553 | * @return a map with ok=false, error (Slack error code) and message | |
| 554 | */ | |
| 555 | @ExceptionHandler({SlackApiException.class}) | |
| 556 | public ResponseEntity<Map<String, Object>> handleSlackApiException(SlackApiException e) { | |
| 557 | Map<String, Object> body = new LinkedHashMap<>(); | |
| 558 | body.put("ok", false); | |
| 559 | body.put("error", e.getMessage()); | |
| 560 | body.put("message", describeError(e.getMessage())); | |
| 561 |
1
1. handleSlackApiException : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SlackController::handleSlackApiException → KILLED |
return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(body); |
| 562 | } | |
| 563 | ||
| 564 | /** | |
| 565 | * Masks a token so that only the first four and last four characters are visible. Tokens too | |
| 566 | * short to mask meaningfully (eight characters or fewer) are masked entirely; Slack does not | |
| 567 | * issue tokens that short. | |
| 568 | * | |
| 569 | * @param token the plaintext token | |
| 570 | * @return the masked token, or an empty string if there is no token | |
| 571 | */ | |
| 572 | public static String maskToken(String token) { | |
| 573 |
2
1. maskToken : negated conditional → KILLED 2. maskToken : negated conditional → KILLED |
if (token == null || token.isEmpty()) { |
| 574 | return ""; | |
| 575 | } | |
| 576 |
2
1. maskToken : negated conditional → KILLED 2. maskToken : changed conditional boundary → KILLED |
if (token.length() <= 2 * VISIBLE_TOKEN_CHARS) { |
| 577 |
1
1. maskToken : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SlackController::maskToken → KILLED |
return "*".repeat(token.length()); |
| 578 | } | |
| 579 |
1
1. maskToken : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SlackController::maskToken → KILLED |
return token.substring(0, VISIBLE_TOKEN_CHARS) |
| 580 |
1
1. maskToken : Replaced integer subtraction with addition → KILLED |
+ "*".repeat(token.length() - 2 * VISIBLE_TOKEN_CHARS) |
| 581 |
1
1. maskToken : Replaced integer subtraction with addition → KILLED |
+ token.substring(token.length() - VISIBLE_TOKEN_CHARS); |
| 582 | } | |
| 583 | ||
| 584 | /** | |
| 585 | * Message shown when a valid token could not be encrypted. | |
| 586 | * | |
| 587 | * @param details message from the encryption service, describing the problem with the key | |
| 588 | * @return a human readable message | |
| 589 | */ | |
| 590 | public static String encryptionFailedMessage(String details) { | |
| 591 |
1
1. encryptionFailedMessage : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SlackController::encryptionFailedMessage → KILLED |
return "Slack accepted this token, but the server could not encrypt it, so the token was not saved. Ask an administrator to check the TOKEN_ENCRYPTION_KEY environment variable (see docs/slack.md). Details: %s" |
| 592 | .formatted(details); | |
| 593 | } | |
| 594 | ||
| 595 | /** | |
| 596 | * Translates a Slack error code into a message suitable for showing to an instructor who has just | |
| 597 | * tried to save a token. | |
| 598 | * | |
| 599 | * @param error error code from Slack, e.g. <code>invalid_auth</code> | |
| 600 | * @return a human readable message | |
| 601 | */ | |
| 602 | public static String errorMessage(String error) { | |
| 603 |
1
1. errorMessage : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SlackController::errorMessage → KILLED |
return describeError(error) + " The token was not saved."; |
| 604 | } | |
| 605 | ||
| 606 | /** | |
| 607 | * Translates a Slack error code into a message suitable for showing to an instructor. | |
| 608 | * | |
| 609 | * @param error error code from Slack, e.g. <code>invalid_auth</code> | |
| 610 | * @return a human readable message | |
| 611 | */ | |
| 612 | public static String describeError(String error) { | |
| 613 |
1
1. describeError : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SlackController::describeError → KILLED |
return switch (String.valueOf(error)) { |
| 614 | case "invalid_auth", "not_authed" -> | |
| 615 | "Slack rejected this token as invalid. Copy the Bot User OAuth Token (xoxb-...) from your Slack app's OAuth & Permissions page and try again."; | |
| 616 | case "token_revoked", "token_expired", "account_inactive" -> | |
| 617 | "This Slack token has been revoked or is no longer active. Reinstall the Slack app to the workspace to get a new token."; | |
| 618 | case "missing_scope" -> | |
| 619 | "This Slack token is missing a required scope. Add the scope under OAuth & Permissions, reinstall the Slack app to the workspace, and enter the new token."; | |
| 620 | case "ratelimited" -> | |
| 621 | "Slack is limiting requests from this app. Please try again in a minute."; | |
| 622 | case SlackService.SLACK_UNREACHABLE -> "Could not reach Slack. Please try again later."; | |
| 623 | default -> "Slack reported an error (%s).".formatted(error); | |
| 624 | }; | |
| 625 | } | |
| 626 | } | |
Mutations | ||
| 140 |
1.1 |
|
| 142 |
1.1 |
|
| 157 |
1.1 2.2 |
|
| 158 |
1.1 2.2 |
|
| 159 |
1.1 2.2 |
|
| 160 |
1.1 |
|
| 164 |
1.1 2.2 3.3 |
|
| 165 |
1.1 |
|
| 167 |
1.1 |
|
| 168 |
1.1 |
|
| 195 |
1.1 |
|
| 200 |
1.1 |
|
| 205 |
1.1 |
|
| 218 |
1.1 |
|
| 221 |
1.1 |
|
| 222 |
1.1 |
|
| 223 |
1.1 |
|
| 224 |
1.1 |
|
| 229 |
1.1 |
|
| 230 |
1.1 |
|
| 237 |
1.1 |
|
| 238 |
1.1 |
|
| 240 |
1.1 |
|
| 251 |
1.1 2.2 |
|
| 252 |
1.1 |
|
| 254 |
1.1 2.2 |
|
| 255 |
1.1 |
|
| 277 |
1.1 |
|
| 282 |
1.1 |
|
| 287 |
1.1 |
|
| 291 |
1.1 |
|
| 297 |
1.1 |
|
| 299 |
1.1 |
|
| 312 |
1.1 |
|
| 332 |
1.1 |
|
| 339 |
1.1 2.2 3.3 |
|
| 348 |
1.1 |
|
| 356 |
1.1 |
|
| 366 |
1.1 |
|
| 385 |
1.1 |
|
| 393 |
1.1 |
|
| 400 |
1.1 2.2 |
|
| 414 |
1.1 |
|
| 433 |
1.1 |
|
| 439 |
1.1 |
|
| 444 |
1.1 2.2 |
|
| 457 |
1.1 |
|
| 479 |
1.1 |
|
| 485 |
1.1 |
|
| 490 |
1.1 2.2 |
|
| 503 |
1.1 |
|
| 514 |
1.1 |
|
| 520 |
1.1 |
|
| 521 |
1.1 |
|
| 523 |
1.1 2.2 |
|
| 530 |
1.1 |
|
| 531 |
1.1 |
|
| 534 |
1.1 2.2 |
|
| 538 |
1.1 |
|
| 543 |
1.1 2.2 |
|
| 546 |
1.1 |
|
| 561 |
1.1 |
|
| 573 |
1.1 2.2 |
|
| 576 |
1.1 2.2 |
|
| 577 |
1.1 |
|
| 579 |
1.1 |
|
| 580 |
1.1 |
|
| 581 |
1.1 |
|
| 591 |
1.1 |
|
| 603 |
1.1 |
|
| 613 |
1.1 |