Skip to content

Commit d7378a7

Browse files
committed
add submission invite/leave commands
1 parent 7bd7564 commit d7378a7

File tree

5 files changed

+247
-0
lines changed

5 files changed

+247
-0
lines changed

botfest/src/main/kotlin/net/modfest/botfest/Platform.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ class PlatformAuthenticated(var client: HttpClient, var discordUser: Snowflake)
211211
}.unwrapErrors()
212212
}
213213

214+
suspend fun leaveSubmission(eventId: String, subId: String) {
215+
client.delete("/event/$eventId/submission/$subId/authors/@me") {
216+
addAuth()
217+
}.unwrapErrors()
218+
}
219+
220+
suspend fun inviteSubmissionAuthor(eventId: String, subId: String, user: String) {
221+
client.put("/event/$eventId/submission/$subId/authors/dc:$user") {
222+
addAuth()
223+
}.unwrapErrors()
224+
}
225+
214226
suspend fun editSubmissionImage(eventId: String, subId: String, type: String, url: String) {
215227
client.patch("/event/$eventId/submission/$subId/image/$type") {
216228
addAuth()

botfest/src/main/kotlin/net/modfest/botfest/extensions/SubmissionCommands.kt

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import dev.kordex.core.commands.application.slash.ephemeralSubCommand
99
import dev.kordex.core.commands.application.slash.group
1010
import dev.kordex.core.commands.converters.impl.attachment
1111
import dev.kordex.core.commands.converters.impl.string
12+
import dev.kordex.core.commands.converters.impl.user
1213
import dev.kordex.core.components.components
1314
import dev.kordex.core.components.ephemeralStringSelectMenu
1415
import dev.kordex.core.components.forms.ModalForm
@@ -338,6 +339,143 @@ class SubmissionCommands : Extension(), KordExKoinComponent {
338339
}
339340
}
340341

342+
// Leave submission
343+
unsafeSubCommand(::SubmissionArg) {
344+
name = Translations.Commands.Submission.Leave.name
345+
description = Translations.Commands.Submission.Leave.description
346+
347+
initialResponse = InitialSlashCommandResponse.None
348+
349+
action {
350+
val subId = this.arguments.submission
351+
val curEvent = platform.getCurrentEvent().event
352+
if (curEvent == null) {
353+
ackEphemeral {
354+
content = Translations.Commands.Event.Submit.Response.unavailable
355+
.withContext(this@action)
356+
.translateNamed()
357+
}
358+
return@action
359+
}
360+
361+
val submission = platform.getUserSubmissions(this.user.id).find { it.id == subId }
362+
363+
if (submission == null) {
364+
ackEphemeral {
365+
content = Translations.Commands.Submission.Leave.Response.notfound
366+
.withContext(this@action)
367+
.translateNamed(
368+
"subId" to subId
369+
)
370+
}
371+
return@action
372+
}
373+
374+
if (!submission.authors.contains(this.user.id.value.toString())) {
375+
ackEphemeral {
376+
content = Translations.Commands.Submission.Leave.Response.notfound
377+
.withContext(this@action)
378+
.translateNamed(
379+
"subId" to subId
380+
)
381+
}
382+
return@action
383+
}
384+
385+
if (submission.authors.size < 2) {
386+
ackEphemeral {
387+
content = Translations.Commands.Submission.Leave.Response.last
388+
.withContext(this@action)
389+
.translateNamed(
390+
"subId" to subId
391+
)
392+
}
393+
return@action
394+
}
395+
396+
platform.withAuth(this.user).leaveSubmission(curEvent, subId)
397+
398+
ackEphemeral {
399+
content = Translations.Commands.Submission.Leave.Response.success
400+
.withContext(this@action)
401+
.translateNamed(
402+
"subId" to subId
403+
)
404+
}
405+
}
406+
}
407+
408+
// Invite user to submission
409+
unsafeSubCommand(::InviteSubmissionArgs) {
410+
name = Translations.Commands.Submission.Leave.name
411+
description = Translations.Commands.Submission.Leave.description
412+
413+
initialResponse = InitialSlashCommandResponse.None
414+
415+
action {
416+
val subId = this.arguments.submission
417+
val userId = this.arguments.user;
418+
val curEvent = platform.getCurrentEvent().event
419+
if (curEvent == null) {
420+
ackEphemeral {
421+
content = Translations.Commands.Event.Submit.Response.unavailable
422+
.withContext(this@action)
423+
.translateNamed()
424+
}
425+
return@action
426+
}
427+
428+
val submission = platform.getUserSubmissions(this.user.id).find { it.id == subId }
429+
430+
if (submission == null) {
431+
ackEphemeral {
432+
content = Translations.Commands.Submission.Invite.Response.notfound
433+
.withContext(this@action)
434+
.translateNamed(
435+
"subId" to subId
436+
)
437+
}
438+
return@action
439+
}
440+
441+
val author = platform.getUser(this.arguments.user)
442+
443+
if (author == null) {
444+
ackEphemeral {
445+
content = Translations.Commands.Submission.Invite.Response.usernotfound
446+
.withContext(this@action)
447+
.translateNamed(
448+
"userId" to userId
449+
)
450+
}
451+
return@action
452+
}
453+
454+
if (submission.authors.contains(author.id)) {
455+
ackEphemeral {
456+
content = Translations.Commands.Submission.Invite.Response.already
457+
.withContext(this@action)
458+
.translateNamed(
459+
"subId" to subId,
460+
"userId" to userId
461+
)
462+
}
463+
return@action
464+
}
465+
466+
platform.withAuth(this.user).inviteSubmissionAuthor(curEvent, subId, author.id)
467+
468+
ackEphemeral {
469+
content = Translations.Commands.Submission.Invite.Response.success
470+
.withContext(this@action)
471+
.translateNamed(
472+
"subId" to subId,
473+
"userId" to userId
474+
)
475+
}
476+
}
477+
}
478+
341479
// Edit submissions images
342480
group(Translations.Commands.Submission.EditImage.label) {
343481
description = Translations.Commands.Submission.EditImage.description
@@ -397,6 +535,26 @@ class SubmissionCommands : Extension(), KordExKoinComponent {
397535
}
398536
}
399537

538+
open inner class InviteSubmissionArgs : Arguments() {
539+
val submission by string {
540+
name = Translations.Arguments.Submission.Edit.name
541+
description = Translations.Arguments.Submission.Edit.description
542+
543+
autoComplete {
544+
val curEvent = platform.getCurrentEvent().event ?: return@autoComplete
545+
suggestStringCollection(
546+
platform.getUserSubmissions(this.user.id)
547+
.filter { it.event == curEvent }
548+
.map { it.id }
549+
)
550+
}
551+
}
552+
val user by user {
553+
name = Translations.Arguments.Submission.Invite.User.name
554+
description = Translations.Arguments.Submission.Invite.User.description
555+
}
556+
}
557+
400558
open inner class SubmissionArg : Arguments() {
401559
val submission by string {
402560
name = Translations.Arguments.Submission.Edit.name

botfest/src/main/resources/translations/botfest/strings.properties

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ commands.submission.delete.name=unsubmit
8989
commands.submission.delete.description=Withdraw your submission
9090
commands.submission.delete.response.notfound=Unknown submission {subId}
9191
commands.submission.delete.response.success=Successfully withdrew {subId}
92+
commands.submission.leave.name=leave
93+
commands.submission.leave.description=Un-associate yourself with a group submission
94+
commands.submission.leave.response.notfound=Unknown submission {subId}
95+
commands.submission.leave.response.last=Can't leave a solo submission! use `/submission unsubmit` instead.
96+
commands.submission.leave.response.success=Successfully left {subId}
97+
commands.submission.invite.name=invite
98+
commands.submission.invite.description=Associate a new user with your submission
99+
commands.submission.invite.response.notfound=Unknown submission {subId}
100+
commands.submission.invite.response.usernotfound=Unknown user {userId}
101+
commands.submission.invite.response.already=User {userId} is already associated with {subId}!
102+
commands.submission.invite.response.success=Successfully added user {userId} to {subId}
92103
commands.submission.edit_image.label=images
93104
commands.submission.edit_image.description=Change your submission's images
94105
commands.submission.edit_image.icon.label=icon
@@ -118,6 +129,8 @@ arguments.submission.edit.name=submission
118129
arguments.submission.edit.description=The submission you want to edit
119130
arguments.submission.edit_image.name=image
120131
arguments.submission.edit_image.description=Your image
132+
arguments.submission.invite.user.name=user
133+
arguments.submission.invite.user.description=The registered user to add to your submission
121134
arguments.minecraft.username.name=username
122135
arguments.minecraft.username.description=Minecraft username
123136

platform_api/src/main/java/net/modfest/platform/controller/EventController.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,60 @@ public void editSubmissionData(@PathVariable String eventId, @PathVariable Strin
161161
service.editSubmission(submission, editData);
162162
}
163163

164+
@DeleteMapping("/event/{eventId}/submission/{subId}/{userId}")
165+
public void deleteSubmissionAuthor(@PathVariable String eventId, @PathVariable String subId, @PathVariable String userId) {
166+
getEvent(eventId);
167+
var submission = service.getSubmission(eventId, subId);
168+
if (submission == null) {
169+
throw new IllegalArgumentException();// TODO
170+
}
171+
172+
if (!canEdit(submission)) {
173+
throw new ResponseStatusException(HttpStatus.FORBIDDEN,
174+
"You do not have permissions to edit this data");
175+
}
176+
177+
if (submission.authors().size() < 2) {
178+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
179+
"You cannot remove a user from a solo submission");
180+
}
181+
182+
UserData user = userController.getSingleUser(userId);
183+
184+
var subject = SecurityUtils.getSubject();
185+
var edit_others = subject.isPermitted(Permissions.Users.EDIT_OTHERS);
186+
var owns = PermissionUtils.owns(subject, user);
187+
188+
if (!owns && !edit_others) {
189+
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "You may not edit this user");
190+
}
191+
192+
service.leaveSubmission(submission, user);
193+
}
194+
195+
@PutMapping("/event/{eventId}/submission/{subId}/{userId}")
196+
public void addSubmissionAuthor(@PathVariable String eventId, @PathVariable String subId, @PathVariable String userId) {
197+
getEvent(eventId);
198+
var submission = service.getSubmission(eventId, subId);
199+
if (submission == null) {
200+
throw new IllegalArgumentException();// TODO
201+
}
202+
203+
if (!canEdit(submission)) {
204+
throw new ResponseStatusException(HttpStatus.FORBIDDEN,
205+
"You do not have permissions to edit this data");
206+
}
207+
208+
UserData user = userController.getSingleUser(userId);
209+
210+
if (submission.authors().contains(user.id())) {
211+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
212+
"You cannot add a user that's already in a submission");
213+
}
214+
215+
service.addSubmissionAuthor(submission, user);
216+
}
217+
164218
@PatchMapping("/event/{eventId}/submission/{subId}/image/{type}")
165219
public void editSubmissionImage(@PathVariable String eventId, @PathVariable String subId, @PathVariable String type, @RequestBody String url) {
166220
getEvent(eventId);

platform_api/src/main/java/net/modfest/platform/service/SubmissionService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ public void editSubmission(SubmissionData data, SubmissionPatchData edit) {
5656
submissionRepository.save(data);
5757
}
5858

59+
public void leaveSubmission(SubmissionData data, UserData author) {
60+
data.authors().remove(author.id());
61+
submissionRepository.save(data);
62+
}
63+
64+
public void addSubmissionAuthor(SubmissionData data, UserData author) {
65+
data.authors().add(author.id());
66+
submissionRepository.save(data);
67+
}
68+
5969
public void deleteSubmission(String eventId, String subId) {
6070
submissionRepository.delete(new SubmissionRepository.SubmissionId(eventId, subId));
6171
}

0 commit comments

Comments
 (0)