From 2998e390b70618e214032e87c683664ac22b6a0f Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 30 Sep 2018 23:04:44 +0300 Subject: subscribe over api --- juick-server/src/main/assets/scripts.js | 28 ++++++++++++++++++++++ .../src/main/java/com/juick/server/api/Post.java | 19 +++++++++++++++ .../src/main/resources/templates/views/thread.html | 2 +- 3 files changed, 48 insertions(+), 1 deletion(-) (limited to 'juick-server/src/main') diff --git a/juick-server/src/main/assets/scripts.js b/juick-server/src/main/assets/scripts.js index 21e3fdee..1c090256 100644 --- a/juick-server/src/main/assets/scripts.js +++ b/juick-server/src/main/assets/scripts.js @@ -562,6 +562,26 @@ function likeMessage(e, mid) { return false; } +function subscribeMessage(e, mid) { + fetch('/api/subscribe?mid=' + mid + + '&hash=' + document.getElementById('body').getAttribute('data-hash'), { + method: 'POST', + mode: 'cors', + credentials: 'omit' + }) + .then(function (response) { + if (response.ok) { + window.location.reload(true); + } else { + alert('Something went wrong :('); + } + }) + .catch(error => { + alert(error.message); + }); + return false; +} + /******************************************************************************/ function setPopular(e, mid, popular) { @@ -774,6 +794,14 @@ ready(function () { e.preventDefault(); }); }); + document.querySelectorAll('.l .a-sub').forEach(function (e) { + e.addEventListener('click', function (e) { + subscribeMessage( + e.target, + e.target.closest('article').getAttribute('data-mid')); + e.preventDefault(); + }); + }); document.querySelectorAll('.a-login').forEach(function (el) { el.addEventListener('click', function (e) { openDialogLogin(); diff --git a/juick-server/src/main/java/com/juick/server/api/Post.java b/juick-server/src/main/java/com/juick/server/api/Post.java index b36f324a..74a29de7 100644 --- a/juick-server/src/main/java/com/juick/server/api/Post.java +++ b/juick-server/src/main/java/com/juick/server/api/Post.java @@ -173,6 +173,25 @@ public class Post { return Status.getStatus(status.getText()); } + @PostMapping("/api/subscribe") + @ResponseStatus(value = HttpStatus.OK) + public Status doPostSubscribe(@RequestParam Integer mid) throws Exception { + com.juick.User visitor = UserUtils.getCurrentUser(); + if (visitor.isAnonymous()) { + throw new HttpForbiddenException(); + } + com.juick.Message msg = messagesService.getMessage(mid); + if (msg == null) { + throw new HttpNotFoundException(); + } + if (msg.getUser().getUid() == visitor.getUid()) { + throw new HttpForbiddenException(); + } + CommandResult status = commandsManager.processCommand(visitor, String.format("S #%d", mid), + URI.create(StringUtils.EMPTY)); + return Status.getStatus(status.getText()); + } + @GetMapping("/api/reactions") @ResponseStatus(value = HttpStatus.OK) public List reactionsList() { diff --git a/juick-server/src/main/resources/templates/views/thread.html b/juick-server/src/main/resources/templates/views/thread.html index 343a2591..2da3645f 100644 --- a/juick-server/src/main/resources/templates/views/thread.html +++ b/juick-server/src/main/resources/templates/views/thread.html @@ -70,7 +70,7 @@  {{ i18n("messages","message.subscribed") }} {% else %} - +  {{ i18n("messages","message.subscribe") }} -- cgit v1.2.3