Skip to content

Commit 183e602

Browse files
committed
Make notification sending dashboards remember the previously submitted values
1 parent f7303d6 commit 183e602

14 files changed

+110
-75
lines changed

backend/src/main/kotlin/hu/bme/sch/cmsch/admin/dashboard/DashboardPage.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract class DashboardPage(
3535
private var ignoreFromMenu: Boolean = false
3636
) {
3737

38-
abstract fun getComponents(user: CmschUser): List<DashboardComponent>
38+
abstract fun getComponents(user: CmschUser, requestParams: Map<String, String>): List<DashboardComponent>
3939

4040
@PostConstruct
4141
fun init() {
@@ -54,7 +54,7 @@ abstract class DashboardPage(
5454
}
5555

5656
@GetMapping("")
57-
fun view(model: Model, auth: Authentication, @RequestParam(defaultValue = "-1") card: Int, @RequestParam(defaultValue = "") message: String): String {
57+
fun view(model: Model, auth: Authentication, @RequestParam requestParams: Map<String, String>): String {
5858
val user = auth.getUser()
5959
adminMenuService.addPartsForMenu(user, model)
6060
if (showPermission.validate(user).not()) {
@@ -68,25 +68,30 @@ abstract class DashboardPage(
6868
model.addAttribute("description", description)
6969
model.addAttribute("view", view)
7070
model.addAttribute("wide", wide)
71-
model.addAttribute("components", getComponents(user))
71+
model.addAttribute("components", getComponents(user, requestParams))
7272
model.addAttribute("user", user)
73-
model.addAttribute("card", card)
74-
model.addAttribute("message", message)
73+
model.addAttribute("card", requestParams.getOrDefault("card", "-1"))
74+
model.addAttribute("message", requestParams.getOrDefault("message", ""))
7575

7676
return "dashboard"
7777
}
7878

7979
@ResponseBody
8080
@GetMapping("/export/{id}", produces = [ MediaType.APPLICATION_OCTET_STREAM_VALUE ])
81-
fun export(auth: Authentication, response: HttpServletResponse, @PathVariable id: Int): ByteArray {
81+
fun export(
82+
auth: Authentication,
83+
response: HttpServletResponse,
84+
@PathVariable id: Int,
85+
@RequestParam requestParams: Map<String, String>
86+
): ByteArray {
8287
val user = auth.getUser()
8388
if (!showPermission.validate(user)) {
8489
throw IllegalStateException("Insufficient permissions")
8590
}
8691

8792
val outputStream = ByteArrayOutputStream()
88-
val components = getComponents(user)
89-
val exportable = components.firstOrNull() { it.id == id }
93+
val components = getComponents(user, requestParams)
94+
val exportable = components.firstOrNull { it.id == id }
9095
if (exportable == null || exportable !is DashboardTableCard || !exportable.exportable)
9196
return outputStream.toByteArray()
9297

backend/src/main/kotlin/hu/bme/sch/cmsch/component/email/EmailDebugSenderDashboard.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class EmailDebugSenderDashboard(
4747
wide = false
4848
)
4949

50-
override fun getComponents(user: CmschUser): List<DashboardComponent> {
50+
override fun getComponents(user: CmschUser, requestParams: Map<String, String>): List<DashboardComponent> {
5151
return listOf(
5252
permissionCard,
5353
getForm(),

backend/src/main/kotlin/hu/bme/sch/cmsch/component/form/VoteByFormDashboard.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class VoteByFormDashboard(
125125
}
126126

127127

128-
override fun getComponents(user: CmschUser): List<DashboardComponent> {
128+
override fun getComponents(user: CmschUser, requestParams: Map<String, String>): List<DashboardComponent> {
129129
return listOf(permissionCard, error404)
130130
}
131131

backend/src/main/kotlin/hu/bme/sch/cmsch/component/messaging/MessagingDebugSenderDashboard.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class MessagingDebugSenderDashboard(
4949
wide = false
5050
)
5151

52-
override fun getComponents(user: CmschUser): List<DashboardComponent> {
52+
override fun getComponents(user: CmschUser, requestParams: Map<String, String>): List<DashboardComponent> {
5353
return listOf(
5454
permissionCard,
5555
getForm(),

backend/src/main/kotlin/hu/bme/sch/cmsch/component/pushnotification/PushNotificationService.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,35 @@ class PushNotificationService(
2121
private val log = LoggerFactory.getLogger(javaClass)
2222

2323
@Transactional(readOnly = false, isolation = Isolation.SERIALIZABLE)
24-
fun sendToUser(userId: Int, notification: CmschNotification) {
24+
fun sendToUser(userId: Int, notification: CmschNotification): Int {
2525
log.info("Sending notification {} to user: {}", notification, userId)
2626
val tokens = messagingTokenRepository.findAllTokensByUserId(userId)
27-
sendNotifications(tokens, notification)
27+
return sendNotifications(tokens, notification)
2828
}
2929

3030
@Transactional(readOnly = false, isolation = Isolation.SERIALIZABLE)
31-
fun sendToGroup(groupId: Int, notification: CmschNotification) {
31+
fun sendToGroup(groupId: Int, notification: CmschNotification): Int {
3232
log.info("Sending notification {} to group: {}", notification, groupId)
3333
val tokens = messagingTokenRepository.findAllTokensByGroupId(groupId)
34-
sendNotifications(tokens, notification)
34+
return sendNotifications(tokens, notification)
3535
}
3636

3737
@Transactional(readOnly = false, isolation = Isolation.SERIALIZABLE)
38-
fun sendToAllUsers(notification: CmschNotification) {
38+
fun sendToAllUsers(notification: CmschNotification): Int {
3939
log.info("Sending notification {} to all users", notification)
4040
val tokens = messagingTokenRepository.findAllTokens()
41-
sendNotifications(tokens, notification)
41+
return sendNotifications(tokens, notification)
4242
}
4343

4444
@Transactional(readOnly = false, isolation = Isolation.SERIALIZABLE)
45-
fun sendToRole(role: RoleType, notification: CmschNotification) {
45+
fun sendToRole(role: RoleType, notification: CmschNotification): Int {
4646
log.info("Sending notification {} to users with role {}", notification, role.displayName)
4747
val tokens = messagingTokenRepository.findAllTokensByRole(role)
48-
sendNotifications(tokens, notification)
48+
return sendNotifications(tokens, notification)
4949
}
5050

51-
private fun sendNotifications(tokens: List<String>, notification: CmschNotification) {
52-
if (tokens.isEmpty()) return
51+
private fun sendNotifications(tokens: List<String>, notification: CmschNotification): Int {
52+
if (tokens.isEmpty()) return 0
5353

5454
var tokensRemaining = tokens
5555
val sendResult = runCatching {
@@ -80,6 +80,7 @@ class PushNotificationService(
8080
log.info("Failed to send notification {} to {} devices", notification, tokensRemaining.size)
8181
// Maybe purge old and invalidated tokens?
8282
}
83+
return tokens.size - tokensRemaining.size
8384
}
8485

8586
@Transactional(readOnly = false, isolation = Isolation.SERIALIZABLE)

backend/src/main/kotlin/hu/bme/sch/cmsch/component/pushnotification/PushNotificationToGroupDashboard.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import hu.bme.sch.cmsch.service.AdminMenuService
1313
import hu.bme.sch.cmsch.service.AuditLogService
1414
import hu.bme.sch.cmsch.service.ControlPermissions
1515
import hu.bme.sch.cmsch.util.getUser
16+
import hu.bme.sch.cmsch.util.urlEncode
1617
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
1718
import org.springframework.security.core.Authentication
1819
import org.springframework.stereotype.Controller
@@ -45,10 +46,10 @@ class PushNotificationToGroupDashboard(
4546
5
4647
) {
4748

48-
override fun getComponents(user: CmschUser): List<DashboardComponent> {
49+
override fun getComponents(user: CmschUser, requestParams: Map<String, String>): List<DashboardComponent> {
4950
return listOf(
5051
permissionCard,
51-
getUserNotificationForm(),
52+
getUserNotificationForm(requestParams),
5253
)
5354
}
5455

@@ -59,7 +60,7 @@ class PushNotificationToGroupDashboard(
5960
wide = false
6061
)
6162

62-
fun getUserNotificationForm(): DashboardFormCard {
63+
fun getUserNotificationForm(requestParams: Map<String, String>): DashboardFormCard {
6364
return DashboardFormCard(
6465
2,
6566
false,
@@ -70,31 +71,31 @@ class PushNotificationToGroupDashboard(
7071
"groupId", "Csapat", FormElementType.SEARCHABLE_SELECT,
7172
".*", "", getGroupList(),
7273
"A csoport, ami tagjainak az értesítést küldöd",
73-
required = true
74+
required = true, defaultValue = requestParams.getOrDefault("groupId", "")
7475
),
7576
FormElement(
7677
"title", "Cím", FormElementType.TEXT,
7778
".*", "", "",
7879
"Az értesítés címe",
79-
required = true, permanent = false, defaultValue = ""
80+
required = true, permanent = false, defaultValue = requestParams.getOrDefault("title", "")
8081
),
8182
FormElement(
8283
"body", "Üzenet", FormElementType.TEXT,
8384
".*", "", "",
8485
"Az értesítés szövege",
85-
required = true, permanent = false, defaultValue = ""
86+
required = true, permanent = false, defaultValue = requestParams.getOrDefault("body", "")
8687
),
8788
FormElement(
8889
"image", "Kép", FormElementType.TEXT,
8990
".*", "", "",
9091
"Az értesítésben megjelenő kép URL-je (opcionális)",
91-
required = false, permanent = false, defaultValue = ""
92+
required = false, permanent = false, defaultValue = requestParams.getOrDefault("image", "")
9293
),
9394
FormElement(
9495
"url", "Link", FormElementType.TEXT,
9596
".*", "", "",
9697
"Ez a link nyílik meg, amikor a felhasználó az értesítésre kattint (opcionális)",
97-
required = false, permanent = false, defaultValue = ""
98+
required = false, permanent = false, defaultValue = requestParams.getOrDefault("url", "")
9899
),
99100
),
100101
buttonCaption = "Küldés",
@@ -123,11 +124,16 @@ class PushNotificationToGroupDashboard(
123124
"sent a push notification to group: $groupId title: $title, body: $body, image: $image url: $url"
124125
)
125126

126-
notificationService.sendToGroup(
127+
val count = notificationService.sendToGroup(
127128
groupId,
128129
CmschNotification(title = title, body = body, image = image, link = url)
129130
)
130-
return "redirect:/admin/control/$VIEW"
131+
132+
val params = HashMap<String, String>()
133+
params.putAll(allRequestParams)
134+
params["message"] = "Értesítés elküldve $count eszközre"
135+
params["card"] = "2"
136+
return "redirect:/admin/control/$VIEW?${params.urlEncode()}"
131137
}
132138

133139
private fun getGroupList(): String =

backend/src/main/kotlin/hu/bme/sch/cmsch/component/pushnotification/PushNotificationToRoleDashboard.kt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import hu.bme.sch.cmsch.component.form.FormElementType
99
import hu.bme.sch.cmsch.component.login.CmschUser
1010
import hu.bme.sch.cmsch.dto.CmschNotification
1111
import hu.bme.sch.cmsch.model.RoleType
12-
import hu.bme.sch.cmsch.repository.GroupRepository
1312
import hu.bme.sch.cmsch.service.AdminMenuService
1413
import hu.bme.sch.cmsch.service.AuditLogService
1514
import hu.bme.sch.cmsch.service.ControlPermissions
1615
import hu.bme.sch.cmsch.util.getUser
16+
import hu.bme.sch.cmsch.util.urlEncode
1717
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
1818
import org.springframework.security.core.Authentication
1919
import org.springframework.stereotype.Controller
@@ -31,7 +31,6 @@ class PushNotificationToRoleDashboard(
3131
component: PushNotificationComponent,
3232
private val auditLogService: AuditLogService,
3333
private val notificationService: PushNotificationService,
34-
private val groupRepository: GroupRepository
3534
) : DashboardPage(
3635
VIEW,
3736
"Push értesítés jogosultságkörnek",
@@ -46,10 +45,10 @@ class PushNotificationToRoleDashboard(
4645
5
4746
) {
4847

49-
override fun getComponents(user: CmschUser): List<DashboardComponent> {
48+
override fun getComponents(user: CmschUser, requestParams: Map<String, String>): List<DashboardComponent> {
5049
return listOf(
5150
permissionCard,
52-
getUserNotificationForm(),
51+
getUserNotificationForm(requestParams),
5352
)
5453
}
5554

@@ -60,7 +59,7 @@ class PushNotificationToRoleDashboard(
6059
wide = false
6160
)
6261

63-
fun getUserNotificationForm(): DashboardFormCard {
62+
fun getUserNotificationForm(requestParams: Map<String, String>): DashboardFormCard {
6463
return DashboardFormCard(
6564
2,
6665
false,
@@ -71,31 +70,31 @@ class PushNotificationToRoleDashboard(
7170
"role", "Jogosultság", FormElementType.SELECT,
7271
".*", "", getAllRoles(),
7372
"A jogosultságkör, mely felhasználóinak értesítést akarsz küldeni",
74-
required = true
73+
required = true, defaultValue = requestParams.getOrDefault("role", "")
7574
),
7675
FormElement(
7776
"title", "Cím", FormElementType.TEXT,
7877
".*", "", "",
7978
"Az értesítés címe",
80-
required = true, permanent = false, defaultValue = ""
79+
required = true, permanent = false, defaultValue = requestParams.getOrDefault("title", "")
8180
),
8281
FormElement(
8382
"body", "Üzenet", FormElementType.TEXT,
8483
".*", "", "",
8584
"Az értesítés szövege",
86-
required = true, permanent = false, defaultValue = ""
85+
required = true, permanent = false, defaultValue = requestParams.getOrDefault("body", "")
8786
),
8887
FormElement(
8988
"image", "Kép", FormElementType.TEXT,
9089
".*", "", "",
9190
"Az értesítésben megjelenő kép URL-je (opcionális)",
92-
required = false, permanent = false, defaultValue = ""
91+
required = false, permanent = false, defaultValue = requestParams.getOrDefault("image", "")
9392
),
9493
FormElement(
9594
"url", "Link", FormElementType.TEXT,
9695
".*", "", "",
9796
"Ez a link nyílik meg, amikor a felhasználó az értesítésre kattint (opcionális)",
98-
required = false, permanent = false, defaultValue = ""
97+
required = false, permanent = false, defaultValue = requestParams.getOrDefault("url", "")
9998
),
10099
),
101100
buttonCaption = "Küldés",
@@ -125,11 +124,16 @@ class PushNotificationToRoleDashboard(
125124
"sent a push notification to role: ${role.displayName} title: $title, body: $body, image: $image url: $url"
126125
)
127126

128-
notificationService.sendToRole(
127+
val count = notificationService.sendToRole(
129128
role,
130129
CmschNotification(title = title, body = body, image = image, link = url)
131130
)
132-
return "redirect:/admin/control/$VIEW"
131+
132+
val params = HashMap<String, String>()
133+
params.putAll(allRequestParams)
134+
params["message"] = "Értesítés elküldve $count eszközre"
135+
params["card"] = "2"
136+
return "redirect:/admin/control/$VIEW?${params.urlEncode()}"
133137
}
134138

135139
private fun getAllRoles(): String =

0 commit comments

Comments
 (0)