aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-www/src/main/java/com/juick/www/controllers/NewMessage.java')
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/NewMessage.java147
1 files changed, 67 insertions, 80 deletions
diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java
index c26d7c0c..3d6bc8cf 100644
--- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java
+++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java
@@ -16,8 +16,11 @@
*/
package com.juick.www.controllers;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.User;
import com.juick.server.helpers.AnonymousUser;
+import com.juick.server.helpers.CommandResult;
import com.juick.server.util.*;
import com.juick.service.*;
import com.juick.www.WebApp;
@@ -26,23 +29,29 @@ import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
-import rocks.xmpp.addr.Jid;
-import rocks.xmpp.core.stanza.model.Message;
-import rocks.xmpp.extensions.component.accept.ExternalComponent;
-import rocks.xmpp.extensions.nick.model.Nickname;
-import rocks.xmpp.extensions.oob.model.x.OobX;
import javax.inject.Inject;
import java.io.IOException;
import java.net.URI;
-import java.net.URISyntaxException;
import java.net.URL;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
import java.util.stream.Collectors;
/**
@@ -66,20 +75,19 @@ public class NewMessage {
@Inject
private WebApp webApp;
@Inject
- private ExternalComponent xmpp;
+ private ObjectMapper jsonMapper;
@Inject
private ImagesService imagesService;
@Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}")
private String imgDir;
@Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}")
private String tmpDir;
- @Value("${xmppbot_jid:juick@localhost}")
- private Jid botJid;
+ private RestTemplate rest = new RestTemplate();
private static final Logger logger = LoggerFactory.getLogger(NewMessage.class);
@GetMapping("/post")
- protected String postAction(@RequestParam(required = false) String body, ModelMap model) throws IOException {
+ protected String postAction(@RequestParam(required = false) String body, ModelMap model) {
com.juick.User visitor = UserUtils.getCurrentUser();
model.addAttribute("title", "Написать");
model.addAttribute("headers", "");
@@ -146,51 +154,22 @@ public class NewMessage {
}
}
- String attachmentType = StringUtils.isNotEmpty(attachmentFName.toString()) ? attachmentFName.toString().substring(attachmentFName.toString().length() - 3) : null;
- int ridnew = messagesService.createReply(mid, rid, visitor, body, attachmentType);
- subscriptionService.subscribeMessage(msg, visitor);
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
+ MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
+ HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);
- Message xmsg = new Message();
- xmsg.setFrom(botJid);
- xmsg.setType(Message.Type.CHAT);
- xmsg.setThread("juick-" + mid);
-
- com.juick.Message jmsg = messagesService.getReply(mid, ridnew);
- xmsg.addExtension(jmsg);
-
- String quote = reply != null ? StringUtils.defaultString(reply.getText())
- : StringUtils.defaultString(msg.getText());
- if (quote.length() >= 50) {
- quote = quote.substring(0, 47) + "...";
- }
- xmsg.addExtension(new Nickname("@" + jmsg.getUser().getName()));
+ params.add("body", rid == 0 ? String.format("#%d %s", mid, body) : String.format("#%d/%d %s", mid, rid, body));
+ params.add("hash", userService.getHashByUID(visitor.getUid()));
if (StringUtils.isNotEmpty(attachmentFName.toString())) {
- String fname = mid + "-" + ridnew + "." + attachmentType;
- String attachmentURL = "http://i.juick.com/photos-1024/" + fname;
-
- imagesService.saveImageWithPreviews(attachmentFName.getHost(), fname);
-
- body = attachmentURL + "\n" + body;
- try {
- xmsg.addExtension(new OobX(new URI(attachmentURL)));
- } catch (URISyntaxException e) {
- logger.warn("invalid uri: {}, exception {}", attachmentURL, e);
- }
- }
-
- if (xmpp.isConnected()) {
-
- xmsg.setBody("Reply by @" + jmsg.getUser().getName() + ":\n>" + quote + "\n" + body + "\n\n#" +
- mid + "/" + ridnew + " http://juick.com/" + mid + "#" + ridnew);
-
- xmsg.setTo(botJid);
- xmpp.send(xmsg);
- } else {
- logger.warn("XMPP unavailable");
+ params.add("img", attachmentFName.toASCIIString());
}
-
- return "redirect:/" + msg.getUser().getName() + "/" + mid + "#" + ridnew;
+ ResponseEntity<CommandResult> result = rest.postForEntity(
+ "http://localhost:8081/post",
+ request, CommandResult.class);
+ logger.info("/comment: {}", jsonMapper.writeValueAsString(result.getBody()));
+ return "redirect:/" + msg.getUser().getName() + "/" + mid + "#" + result.getBody().getNewMessage().get().getRid();
}
@PostMapping("/pm/send")
@@ -217,30 +196,25 @@ public class NewMessage {
throw new HttpForbiddenException();
}
- if (pmQueriesService.createPM(visitor.getUid(), userTo.getUid(), body)) {
- if (xmpp.isConnected()) {
- Message msg = new Message();
- msg.setFrom(botJid);
- msg.setTo(botJid.withLocal("pm"));
- com.juick.Message jmsg = new com.juick.Message();
- jmsg.setUser(visitor);
- jmsg.setText(body);
- jmsg.setTo(userTo);
- msg.addExtension(jmsg);
- xmpp.send(msg);
- } else {
- logger.warn("XMPP unavailable");
- }
- return "redirect:/pm/sent";
- } else {
- throw new HttpBadRequestException();
- }
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
+ MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
+ HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);
+
+ params.add("body", String.format("@%s %s", userTo.getName(), body));
+ params.add("hash", userService.getHashByUID(visitor.getUid()));
+ ResponseEntity<CommandResult> result = rest.postForEntity(
+ "http://localhost:8081/post",
+ request, CommandResult.class);
+ logger.info("/pm: {}", jsonMapper.writeValueAsString(result.getBody()));
+ return "redirect:/pm/sent";
+
}
@PostMapping("/post2")
public String doPostMessage(@RequestParam(name = "body", required = false) String bodyParam,
@RequestParam(required = false) String img,
@RequestParam(required = false) String referer,
- @RequestParam(required = false) MultipartFile attach) throws IOException {
+ @RequestParam(required = false) MultipartFile attach) throws JsonProcessingException {
com.juick.User visitor = UserUtils.getCurrentUser();
if (visitor.getUid() == 0 || visitor.isBanned()) {
@@ -259,18 +233,31 @@ public class NewMessage {
throw new HttpBadRequestException();
}
}
- Message msg = new Message();
- msg.setType(Message.Type.CHAT);
- msg.setFrom(Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "perl"));
- msg.setTo(botJid);
- msg.setBody(body);
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
+ MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
+ HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);
+
+ params.add("body", body);
+ params.add("hash", userService.getHashByUID(visitor.getUid()));
if (StringUtils.isNotEmpty(attachmentFName.toString())) {
- msg.addExtension(new OobX(attachmentFName, "!!!!Juick!!"));
+ params.add("img", attachmentFName.toASCIIString());
}
- xmpp.sendMessage(msg);
- if (StringUtils.isBlank(referer) || referer.substring(0, 21).equals("http://juick.com/post")
- || referer.substring(0, 22).equals("https://juick.com/post")) {
- return "redirect:/?show=my";
+ try {
+ ResponseEntity<CommandResult> result = rest.postForEntity(
+ "http://localhost:8081/post",
+ request, CommandResult.class);
+ if (result.getBody().getNewMessage().isPresent()) {
+ logger.info("/post: {}", jsonMapper.writeValueAsString(result.getBody()));
+ } else {
+ logger.info("{} : {}", body, result.getBody().getText());
+ }
+ if (StringUtils.isBlank(referer) || referer.substring(0, 21).equals("http://juick.com/post")
+ || referer.substring(0, 22).equals("https://juick.com/post")) {
+ return "redirect:/?show=my";
+ }
+ } catch (HttpClientErrorException e) {
+ logger.error("post error", e);
}
return "redirect:" + referer;
}