aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/main/java/com/juick/api/controllers
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-09-27 15:19:06 +0300
committerGravatar Vitaly Takmazov2017-09-28 12:54:39 +0300
commit751b2a9b0fc523d17c9f7a410fadcc82eaaa1fb7 (patch)
tree1be25ccef6d38b4ec45084e389aed3045c1cdd70 /juick-api/src/main/java/com/juick/api/controllers
parente6e25791d6b9c8360247d92340b0dccda33c86ed (diff)
api: Telegram posting
Diffstat (limited to 'juick-api/src/main/java/com/juick/api/controllers')
-rw-r--r--juick-api/src/main/java/com/juick/api/controllers/PM.java6
-rw-r--r--juick-api/src/main/java/com/juick/api/controllers/Post.java79
-rw-r--r--juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java52
3 files changed, 74 insertions, 63 deletions
diff --git a/juick-api/src/main/java/com/juick/api/controllers/PM.java b/juick-api/src/main/java/com/juick/api/controllers/PM.java
index e909a4b5..b72e0a0f 100644
--- a/juick-api/src/main/java/com/juick/api/controllers/PM.java
+++ b/juick-api/src/main/java/com/juick/api/controllers/PM.java
@@ -98,10 +98,10 @@ public class PM {
jmsg.setUser(userService.getUserByUID(vuid).get());
jmsg.setText(body);
msg.addExtension(jmsg);
- apiServer.getXmpp().send(msg);
+ apiServer.sendMessage(msg);
msg.setTo(Jid.of(String.format("%d@ws.juick.com", uid)));
- apiServer.getXmpp().send(msg);
+ apiServer.sendMessage(msg);
List<String> jids = userService.getJIDsbyUID(uid);
for (String jid : jids) {
@@ -115,7 +115,7 @@ public class PM {
mm.setFrom(Jid.of("juick", "juick.com", "Juick"));
mm.setBody("Private message from @" + jmsg.getUser().getName() + ":\n" + body);
}
- apiServer.getXmpp().send(mm);
+ apiServer.sendMessage(mm);
}
return jmsg;
diff --git a/juick-api/src/main/java/com/juick/api/controllers/Post.java b/juick-api/src/main/java/com/juick/api/controllers/Post.java
index 5a32ade0..33d83d2c 100644
--- a/juick-api/src/main/java/com/juick/api/controllers/Post.java
+++ b/juick-api/src/main/java/com/juick/api/controllers/Post.java
@@ -101,22 +101,7 @@ public class Post {
throw new HttpBadRequestException();
}
}
-
- if (apiServer.getXmpp() != null) {
- Message xmsg = new Message();
- xmsg.setFrom(Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "perl"));
- xmsg.setTo(Jid.of("juick@juick.com/Juick"));
- xmsg.setBody(body);
- try {
- if (StringUtils.isNotEmpty(attachmentFName)) {
- String attachmentUrl = String.format("juick://%s", attachmentFName);
- xmsg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!"));
- }
- apiServer.getXmpp().sendMessage(xmsg);
- } catch (URISyntaxException e1) {
- logger.warn("attachment error", e1);
- }
- }
+ apiServer.processMessage(visitor, body, attachmentFName);
}
@RequestMapping(value = "/comment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@@ -175,48 +160,44 @@ public class Post {
com.juick.Message jmsg = messagesService.getReply(mid, ridnew);
- if (apiServer.getXmpp() != null) {
- Message xmsg = new Message();
- xmsg.setFrom(Jid.of("juick@juick.com"));
- xmsg.setType(Message.Type.CHAT);
- xmsg.setThread("juick-" + mid);
- xmsg.addExtension(jmsg);
+ Message xmsg = new Message();
+ xmsg.setFrom(Jid.of("juick@juick.com"));
+ xmsg.setType(Message.Type.CHAT);
+ xmsg.setThread("juick-" + mid);
+ xmsg.addExtension(jmsg);
- String quote = reply != null ? reply.getText() : msg.getText();
- if (quote.length() >= 50) {
- quote = quote.substring(0, 47) + "...";
- }
+ String quote = reply != null ? reply.getText() : msg.getText();
+ if (quote.length() >= 50) {
+ quote = quote.substring(0, 47) + "...";
+ }
- xmsg.addExtension(new Nickname("@" + jmsg.getUser().getName()));
+ xmsg.addExtension(new Nickname("@" + jmsg.getUser().getName()));
- if (StringUtils.isNotEmpty(attachmentFName)) {
- String fname = mid + "-" + ridnew + "." + attachmentType;
- String attachmentURL = "http://i.juick.com/photos-1024/" + fname;
+ if (StringUtils.isNotEmpty(attachmentFName)) {
+ String fname = mid + "-" + ridnew + "." + attachmentType;
+ String attachmentURL = "http://i.juick.com/photos-1024/" + fname;
- ImageUtils.saveImageWithPreviews(attachmentFName, fname, tmpDir, imgDir);
+ ImageUtils.saveImageWithPreviews(attachmentFName, fname, tmpDir, imgDir);
- body = attachmentURL + "\n" + body;
- try {
- xmsg.addExtension(new OobX(new URI(attachmentURL)));
- } catch (URISyntaxException e) {
- logger.error("invalid uri: {}, exception {}", attachmentURL, e);
- }
+ body = attachmentURL + "\n" + body;
+ try {
+ xmsg.addExtension(new OobX(new URI(attachmentURL)));
+ } catch (URISyntaxException e) {
+ logger.error("invalid uri: {}, exception {}", attachmentURL, e);
}
+ }
- xmsg.setBody("Reply by @" + jmsg.getUser().getName() + ":\n>" + quote + "\n" + body + "\n\n#" +
- mid + "/" + ridnew + " http://juick.com/" + mid + "#" + ridnew);
+ xmsg.setBody("Reply by @" + jmsg.getUser().getName() + ":\n>" + quote + "\n" + body + "\n\n#" +
+ mid + "/" + ridnew + " http://juick.com/" + mid + "#" + ridnew);
- xmsg.setTo(Jid.of("juick@s2s.juick.com"));
- apiServer.getXmpp().send(xmsg);
+ xmsg.setTo(Jid.of("juick@s2s.juick.com"));
+ apiServer.sendMessage(xmsg);
- xmsg.setTo(Jid.of("juick@ws.juick.com"));
- apiServer.getXmpp().send(xmsg);
+ xmsg.setTo(Jid.of("juick@ws.juick.com"));
+ apiServer.sendMessage(xmsg);
- xmsg.setTo(Jid.of("juick@push.juick.com"));
- apiServer.getXmpp().send(xmsg);
- } else {
- logger.error("XMPP unavailable");
- }
+ xmsg.setTo(Jid.of("juick@push.juick.com"));
+ apiServer.sendMessage(xmsg);
return jmsg;
}
@@ -277,7 +258,7 @@ public class Post {
String attachmentUrl = String.format("juick://%s", attachmentFName[0]);
xmsg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!"));
}
- apiServer.getXmpp().sendMessage(xmsg);
+ apiServer.sendMessage(xmsg);
} catch (URISyntaxException e1) {
logger.warn("attachment error", e1);
}
diff --git a/juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java b/juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java
index ae436b22..1141d6d8 100644
--- a/juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java
+++ b/juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java
@@ -18,14 +18,21 @@
package com.juick.api.controllers;
import com.juick.User;
-import com.juick.api.TGBot;
+import com.juick.api.ApiServer;
+import com.juick.api.TelegramBotManager;
+import com.juick.server.util.HttpUtils;
import com.juick.service.TelegramService;
import com.juick.service.UserService;
import com.pengrad.telegrambot.BotUtils;
import com.pengrad.telegrambot.model.Message;
+import com.pengrad.telegrambot.model.PhotoSize;
import com.pengrad.telegrambot.model.Update;
+import com.pengrad.telegrambot.request.GetFile;
+import com.pengrad.telegrambot.response.GetFileResponse;
+import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@@ -37,7 +44,10 @@ import javax.inject.Inject;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.net.URL;
import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Comparator;
import java.util.List;
/**
@@ -52,12 +62,16 @@ public class TelegramWebhook {
@Inject
private TelegramService telegramService;
@Inject
- private TGBot tgBot;
+ private TelegramBotManager telegramBotManager;
+ @Inject
+ private ApiServer apiServer;
+ @Value("${upload_tmp_dir:/var/www/juick.com/i/tmp/}")
+ private String tmpDir;
@RequestMapping(value = "/tlgmbtwbhk", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
- public void processUpdate(InputStream body) throws IOException {
+ public void processUpdate(InputStream body) throws Exception {
Update update = BotUtils.parseUpdate(new InputStreamReader(body, StandardCharsets.UTF_8));
Message message = update.message();
if (update.message() == null) {
@@ -67,6 +81,7 @@ public class TelegramWebhook {
return;
}
}
+
logger.info("got telegram msg {}", message);
User user_from = usersService.getUserByUID(telegramService.getUser(message.chat().id())).orElse(new User());
logger.info("Found juick user {}", user_from.getUid());
@@ -80,21 +95,36 @@ public class TelegramWebhook {
telegramService.addChat(message.chat().id());
logger.info("added chat with {}", username);
telegramService.createTelegramUser(message.from().id(), username);
- tgBot.telegramSignupNotify(message.from().id().longValue(), usersService.getSignUpHashByTelegramID(message.from().id().longValue(), username));
+ telegramBotManager.telegramSignupNotify(message.from().id().longValue(), usersService.getSignUpHashByTelegramID(message.from().id().longValue(), username));
} else {
if (user_from.getUid() == 0) {
- tgBot.telegramSignupNotify(message.from().id().longValue(), usersService.getSignUpHashByTelegramID(message.from().id().longValue(), username));
+ telegramBotManager.telegramSignupNotify(message.from().id().longValue(), usersService.getSignUpHashByTelegramID(message.from().id().longValue(), username));
} else {
+ String attachment = StringUtils.EMPTY;
+ if (message.photo() != null) {
+ String fileId = Arrays.stream(message.photo()).max(Comparator.comparingInt(PhotoSize::fileSize))
+ .orElse(new PhotoSize()).fileId();
+ if (StringUtils.isNotEmpty(fileId)) {
+ GetFile request = new GetFile(fileId);
+ GetFileResponse response = telegramBotManager.getBot().execute(request);
+ logger.info("got file {}", response.file());
+ URL fileURL = new URL(telegramBotManager.getBot().getFullFilePath(response.file()));
+ attachment = HttpUtils.downloadImage(fileURL, tmpDir);
+ logger.info("received {}", attachment);
+ }
+ }
String text = message.text();
- if (text != null) {
+ if (StringUtils.isBlank(text)) {
+ text = message.caption();
+ }
+ if (StringUtils.isNotEmpty(text)) {
if (text.equalsIgnoreCase("/login")) {
String msg = String.format("Hi, %s!\nTap to log in", user_from.getName());
String msgUrl = "http://juick.com/login?" + usersService.getHashByUID(user_from.getUid());
- tgBot.telegramNotify(message.from().id().longValue(), msg, msgUrl);
- } /* else {
- ProtocolReply reply = protocol.getReply(user_from, text);
- telegramNotify(message.from().id().longValue(), reply.getResult(), null);
- }*/
+ telegramBotManager.telegramNotify(message.from().id().longValue(), msg, msgUrl);
+ } else if (text.toLowerCase().startsWith("/post ")) {
+ apiServer.processMessage(user_from, text.substring(6), attachment);
+ }
}
}
}