diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/assets/scripts.js | 24 | ||||
-rw-r--r-- | src/main/java/com/juick/service/MessagesService.java | 3 | ||||
-rw-r--r-- | src/main/java/com/juick/service/MessagesServiceImpl.java | 9 | ||||
-rw-r--r-- | src/main/java/com/juick/www/api/Messages.java | 6 | ||||
-rw-r--r-- | src/main/resources/templates/views/settings_main.html | 7 |
5 files changed, 47 insertions, 2 deletions
diff --git a/src/main/assets/scripts.js b/src/main/assets/scripts.js index 3c83bba4..50406f1c 100644 --- a/src/main/assets/scripts.js +++ b/src/main/assets/scripts.js @@ -533,6 +533,20 @@ function fetchUserUri(dataUri) { }) } +function markRead() { + return new Promise((resolve) => { + fetch('/api/messages/mark_read', { + method: 'POST' + }).then(handleErrors) + .then(() => { + resolve(true) + }) + .catch(() => { + resolve(false) + }) + }) +} + const registerServiceWorker = () => { const publicKey = 'BPU0LniKKR0QiaUvILPd9AystmSOU8rWDZobxKm7IJN5HYxOSQdktRdc74TZvyRS9_kyUz7LDN6gUAmAVOmObAU' navigator.serviceWorker.register('/sw.js', { scope: '/' }) @@ -825,4 +839,14 @@ ready(() => { }) notificationsCheckPermissions(button) } + const markReadButton = document.getElementById('messages_mark_read') + if (markReadButton) { + markReadButton.addEventListener('click', () => { + if (markRead()) { + window.location.reload() + } else { + alert('Something went wrong :(') + } + }) + } }) diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java index f2321b2a..707a04c3 100644 --- a/src/main/java/com/juick/service/MessagesService.java +++ b/src/main/java/com/juick/service/MessagesService.java @@ -143,6 +143,9 @@ public interface MessagesService { void setLastReadComment(User user, Integer mid, Integer rid); @CacheEvict(value = { "discover", "discussions", "messages", "replies" }, allEntries = true, condition = "{ #user.uid != 0 }") + void setAllRead(User user); + + @CacheEvict(value = { "discover", "discussions", "messages", "replies" }, allEntries = true, condition = "{ #user.uid != 0 }") void setRead(User user, Integer mid); List<Integer> getUnread(User user); diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index 940cdaba..105b86cf 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -1084,6 +1084,15 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ rid, mid, user.getUid()); } + @Override + public void setAllRead(User user) { + jdbcTemplate.update(""" + update subscr_messages set last_read_rid=total.replies FROM messages + INNER JOIN messages total on total.message_id=messages.message_id + WHERE messages.message_id=subscr_messages.message_id AND subscr_messages.suser_id=? + """, user.getUid()); + } + @Transactional @Override public void setRead(User user, Integer mid) { diff --git a/src/main/java/com/juick/www/api/Messages.java b/src/main/java/com/juick/www/api/Messages.java index 969bef4f..5a90bf0b 100644 --- a/src/main/java/com/juick/www/api/Messages.java +++ b/src/main/java/com/juick/www/api/Messages.java @@ -210,6 +210,12 @@ public class Messages { } throw new HttpBadRequestException(); } + @PostMapping("/api/messages/mark_read") + public void markRead(@Parameter(hidden = true) User visitor) { + if (!visitor.isAnonymous()) { + messagesService.setAllRead(visitor); + } + } @PostMapping("/api/messages/set_privacy") public void togglePrivacy(@Parameter(hidden = true) User visitor, @RequestParam Integer mid) { if (visitor.isPremium() || visitor.isAdmin()) { diff --git a/src/main/resources/templates/views/settings_main.html b/src/main/resources/templates/views/settings_main.html index 8937f507..954edb98 100644 --- a/src/main/resources/templates/views/settings_main.html +++ b/src/main/resources/templates/views/settings_main.html @@ -6,8 +6,11 @@ <h2 style="color: red; padding: 20px;">Verify your account by adding email or social account</h2> {% endif %} <fieldset> - <legend>Browser notifications</legend> - <button class="Button" id="notifications_toggle">...</button> + <legend>Notifications</legend> + <div>Browser notifications: <button class="Button" id="notifications_toggle">...</button></div> + {% if visitor.unreadCount > 0 %} + <div>Unread messages: <b>{{ visitor.unreadCount }}</b> <button class="Button" id="messages_mark_read">Mark read</button></div> + {% endif %} </fieldset> <fieldset> <legend |