aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'juick-server/src/main')
-rw-r--r--juick-server/src/main/java/com/juick/server/FacebookPageManager.java32
-rw-r--r--juick-server/src/main/java/com/juick/server/MessengerManager.java186
-rw-r--r--juick-server/src/main/java/com/juick/server/TopManager.java10
-rw-r--r--juick-server/src/main/java/com/juick/server/api/webhooks/MessengerWebhook.java62
-rw-r--r--juick-server/src/main/java/com/juick/server/api/webhooks/SkypeWebhook.java47
5 files changed, 0 insertions, 337 deletions
diff --git a/juick-server/src/main/java/com/juick/server/FacebookPageManager.java b/juick-server/src/main/java/com/juick/server/FacebookPageManager.java
deleted file mode 100644
index 971b206c..00000000
--- a/juick-server/src/main/java/com/juick/server/FacebookPageManager.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package com.juick.server;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.ResponseEntity;
-import org.springframework.stereotype.Component;
-import org.springframework.web.client.RestTemplate;
-import org.springframework.web.util.UriComponents;
-import org.springframework.web.util.UriComponentsBuilder;
-
-@Component
-public class FacebookPageManager {
- private static Logger logger = LoggerFactory.getLogger(FacebookPageManager.class);
- @Value("${facebook_page_id:12345678}")
- private String pageId;
- @Value("${fb_page_access_token:12345678}")
- private String accessToken;
-
- public void post(String status, String link) {
- UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://graph.facebook.com/{page_id}/feed")
- .queryParam("message", status)
- .queryParam("link", link)
- .queryParam("access_token", accessToken)
- .buildAndExpand(pageId);
- RestTemplate api = new RestTemplate();
- ResponseEntity<String> response = api.exchange(uriComponents.toUri(),
- HttpMethod.POST, null, String.class);
- logger.info("Facebook response: {}", response.getBody());
- }
-}
diff --git a/juick-server/src/main/java/com/juick/server/MessengerManager.java b/juick-server/src/main/java/com/juick/server/MessengerManager.java
deleted file mode 100644
index 96320b3b..00000000
--- a/juick-server/src/main/java/com/juick/server/MessengerManager.java
+++ /dev/null
@@ -1,186 +0,0 @@
-package com.juick.server;
-
-import com.github.messenger4j.Messenger;
-import com.github.messenger4j.exception.MessengerApiException;
-import com.github.messenger4j.exception.MessengerIOException;
-import com.github.messenger4j.exception.MessengerVerificationException;
-import com.github.messenger4j.send.MessagePayload;
-import com.github.messenger4j.send.MessageTag;
-import com.github.messenger4j.send.MessagingType;
-import com.github.messenger4j.send.NotificationType;
-import com.github.messenger4j.send.message.TemplateMessage;
-import com.github.messenger4j.send.message.TextMessage;
-import com.github.messenger4j.send.message.template.ButtonTemplate;
-import com.github.messenger4j.send.message.template.button.UrlButton;
-import com.github.messenger4j.send.recipient.IdRecipient;
-import com.github.messenger4j.userprofile.UserProfile;
-import com.github.messenger4j.webhook.event.TextMessageEvent;
-import com.juick.Message;
-import com.juick.User;
-import com.juick.model.AnonymousUser;
-import com.juick.service.MessagesService;
-import com.juick.service.MessengerService;
-import com.juick.service.UserService;
-import com.juick.service.component.*;
-import com.juick.util.MessageUtils;
-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.stereotype.Component;
-import org.springframework.web.client.HttpClientErrorException;
-
-import javax.annotation.Nonnull;
-import javax.annotation.PostConstruct;
-import javax.inject.Inject;
-import java.net.MalformedURLException;
-import java.net.URI;
-import java.net.URL;
-import java.time.Instant;
-import java.util.Collections;
-import java.util.List;
-import java.util.Optional;
-
-import static com.juick.formatters.PlainTextFormatter.formatPost;
-import static com.juick.formatters.PlainTextFormatter.formatUrl;
-
-@Component
-public class MessengerManager implements NotificationListener {
- private static final Logger logger = LoggerFactory.getLogger(MessengerManager.class);
- @Inject
- private MessagesService messagesService;
- @Inject
- private UserService userService;
- @Inject
- private MessengerService messengerService;
- @Inject
- private CommandsManager commandsManager;
- @Inject
- private FacebookPageManager facebookPageManager;
-
- @Value("${fb_page_access_token:12345678}")
- private String facebookPageAccessToken;
- @Value("${fb_verify_token:12345678}")
- private String facebookVerifyToken;
- @Value("${fb_secret:12345678}")
- private String facebookSecret;
-
- private Messenger messenger;
-
- @PostConstruct
- public void init() {
- messenger = Messenger.create(facebookPageAccessToken, facebookSecret, facebookVerifyToken);
- }
-
- public String getFacebookVerifyToken() {
- return facebookVerifyToken;
- }
-
- public void processUpdate(String signature , String data) throws MessengerVerificationException {
- messenger.onReceiveEvents(data, Optional.of(signature), event -> {
- final String senderId = event.senderId();
- final Instant timestamp = event.timestamp();
-
- User user_from = userService.getUserByUID(messengerService.getUserId(senderId)).orElse(AnonymousUser.INSTANCE);
- logger.info("Found juick user {}", user_from.getUid());
- if (user_from.isAnonymous()) {
- try {
- UserProfile profile = messenger.queryUserProfile(senderId);
- signupNotify(senderId, messengerService.getSignUpHash(senderId, profile.firstName()));
- } catch (MessengerApiException | MessengerIOException | MalformedURLException e) {
- logger.warn("messenger profile error", e);
- try {
- signupNotify(senderId, messengerService.getSignUpHash(senderId, "anonymous"));
- } catch (MalformedURLException | MessengerApiException | MessengerIOException e1) {
- logger.warn("signup error", e1);
- }
- }
- } else {
- if (event.isTextMessageEvent()) {
- final TextMessageEvent textMessageEvent = event.asTextMessageEvent();
- final String messageId = textMessageEvent.messageId();
- final String text = textMessageEvent.text();
- logger.info("Received text message from '{}' at '{}' with content: {} (mid: {})",
- senderId, timestamp, text, messageId);
- try {
- commandsManager.processCommand(user_from, text, URI.create(StringUtils.EMPTY));
- } catch (Exception e) {
- logger.warn("messenger error", e);
- }
- messengerNotify(senderId, "Message sent", null);
- }
- }
- });
- }
-
- @Override
- public void processMessageEvent(@Nonnull MessageEvent event) {
- Message msg = event.getMessage();
- List<User> subscribedUsers = event.getUsers();
- if (msg.isService()) {
- return;
- }
- if (!MessageUtils.isPM(msg)) {
- String subject = formatPost(msg);
- subscribedUsers
- .forEach(user -> messengerService.getSenderId(user)
- .ifPresent(t -> messengerNotify(t, subject, formatUrl(msg))));
- }
- }
-
- @Override
- public void processSubscribeEvent(SubscribeEvent subscribeEvent) {
-
- }
-
- @Override
- public void processLikeEvent(LikeEvent likeEvent) {
-
- }
-
- @Override
- public void processPingEvent(PingEvent pingEvent) {
-
- }
-
- @Override
- public void processMessageReadEvent(MessageReadEvent messageReadEvent) {
-
- }
-
- @Override
- public void processTopEvent(TopEvent topEvent) {
- Message jmsg = topEvent.getMessage();
- String status = MessageUtils.getMessageHashTags(jmsg) + StringUtils.defaultString(jmsg.getText());
- String link = "https://juick.com/m/" + jmsg.getMid();
- try {
- facebookPageManager.post(status, link);
- } catch (HttpClientErrorException ex) {
- HttpStatus statusCode = ex.getStatusCode();
- String responseString = ex.getResponseBodyAsString();
- logger.warn("facebook error {}: {}", statusCode.value(), responseString);
- }
- }
-
- private void messengerNotify(String messengerUser, String text, String url) {
- try {
- if (!StringUtils.isEmpty(url)) {
- final UrlButton showMessage = UrlButton.create("VIEW MESSAGE", new URL(url));
- ButtonTemplate template = ButtonTemplate.create(text, Collections.singletonList(showMessage));
- messenger.send(MessagePayload.create(IdRecipient.create(messengerUser), MessagingType.MESSAGE_TAG, TemplateMessage.create(template), Optional.of(NotificationType.REGULAR), Optional.of(MessageTag.NON_PROMOTIONAL_SUBSCRIPTION)));
- } else {
- messenger.send(MessagePayload.create(IdRecipient.create(messengerUser), MessagingType.MESSAGE_TAG, TextMessage.create(text), Optional.of(NotificationType.REGULAR), Optional.of(MessageTag.NON_PROMOTIONAL_SUBSCRIPTION)));
- }
- } catch (MessengerApiException | MessengerIOException | MalformedURLException e) {
- logger.warn("messenger error", e);
- }
- }
- private void signupNotify(String messengerUser, String hash) throws MalformedURLException, MessengerApiException, MessengerIOException {
- final UrlButton urlButton = UrlButton.create("LOGIN",
- new URL("https://juick.com/signup?type=messenger&hash=" + hash));
- ButtonTemplate template = ButtonTemplate.create("Login to receive notifications",
- Collections.singletonList(urlButton));
- messenger.send(MessagePayload.create(messengerUser, MessagingType.RESPONSE, TemplateMessage.create(template)));
- }
-}
diff --git a/juick-server/src/main/java/com/juick/server/TopManager.java b/juick-server/src/main/java/com/juick/server/TopManager.java
index a9157841..e5c00242 100644
--- a/juick-server/src/main/java/com/juick/server/TopManager.java
+++ b/juick-server/src/main/java/com/juick/server/TopManager.java
@@ -19,22 +19,14 @@ package com.juick.server;
import com.juick.Message;
import com.juick.Tag;
-import com.juick.User;
import com.juick.service.MessagesService;
-import com.juick.service.UserService;
import com.juick.service.component.TopEvent;
-import com.juick.util.MessageUtils;
-import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
-import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
-import org.springframework.web.client.HttpClientErrorException;
-import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.List;
import java.util.stream.Collectors;
@@ -46,8 +38,6 @@ public class TopManager {
private MessagesService messagesService;
@Inject
private ApplicationEventPublisher applicationEventPublisher;
- @Inject
- private TwitterManager twitterManager;
@Scheduled(fixedRate = 3600000)
public void updateTop() {
diff --git a/juick-server/src/main/java/com/juick/server/api/webhooks/MessengerWebhook.java b/juick-server/src/main/java/com/juick/server/api/webhooks/MessengerWebhook.java
deleted file mode 100644
index 0ba68182..00000000
--- a/juick-server/src/main/java/com/juick/server/api/webhooks/MessengerWebhook.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package com.juick.server.api.webhooks;
-
-import com.github.messenger4j.exception.MessengerVerificationException;
-import com.juick.server.MessengerManager;
-import com.juick.server.util.HttpForbiddenException;
-import org.apache.commons.io.IOUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.*;
-import springfox.documentation.annotations.ApiIgnore;
-
-import javax.inject.Inject;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
-
-/**
- * Created by vitalyster on 27.03.2017.
- */
-@ApiIgnore
-@RestController
-public class MessengerWebhook {
- private static Logger logger = LoggerFactory.getLogger(MessengerWebhook.class);
-
- @Inject
- private MessengerManager messengerManager;
-
- @RequestMapping(value = "/api/fbwbhk", method = RequestMethod.GET)
- public ResponseEntity<Integer> verifyHook(@RequestParam(name = "hub.mode") String hubMode,
- @RequestParam(name = "hub.challenge") Integer hubChallenge,
- @RequestParam(name = "hub.verify_token") String verifyToken) {
- if (hubMode.equals("subscribe") && verifyToken.equals(messengerManager.getFacebookVerifyToken())) {
- return new ResponseEntity<>(hubChallenge, HttpStatus.OK);
- }
- throw new HttpForbiddenException();
- }
- @RequestMapping(value = "/api/fbwbhk", method = RequestMethod.POST)
- @ResponseStatus(value = HttpStatus.OK)
- public void processUpdate(@RequestHeader(name = "X-Hub-Signature", required = false) String signature, InputStream body) throws IOException, MessengerVerificationException {
- String data = IOUtils.toString(body, StandardCharsets.UTF_8);
- messengerManager.processUpdate(signature, data);
- }
-}
diff --git a/juick-server/src/main/java/com/juick/server/api/webhooks/SkypeWebhook.java b/juick-server/src/main/java/com/juick/server/api/webhooks/SkypeWebhook.java
deleted file mode 100644
index d7dbe0ef..00000000
--- a/juick-server/src/main/java/com/juick/server/api/webhooks/SkypeWebhook.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package com.juick.server.api.webhooks;
-
-import org.apache.commons.io.IOUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpStatus;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.ResponseStatus;
-import org.springframework.web.bind.annotation.RestController;
-import springfox.documentation.annotations.ApiIgnore;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
-
-/**
- * Created by vitalyster on 18.07.2016.
- */
-@ApiIgnore
-@RestController
-public class SkypeWebhook {
- private static final Logger logger = LoggerFactory.getLogger(SkypeWebhook.class);
- @RequestMapping(value = "/api/skypebotendpoint", method = RequestMethod.POST)
- @ResponseStatus(value = HttpStatus.OK)
- public void doPost(InputStream body) throws IOException {
- String data = IOUtils.toString(body, StandardCharsets.UTF_8);
- logger.info("got data: {}", data);
- }
-}