aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/service
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/service')
-rw-r--r--src/main/java/com/juick/service/TwitterService.java105
-rw-r--r--src/main/java/com/juick/service/UserService.java6
-rw-r--r--src/main/java/com/juick/service/UserServiceImpl.java24
3 files changed, 129 insertions, 6 deletions
diff --git a/src/main/java/com/juick/service/TwitterService.java b/src/main/java/com/juick/service/TwitterService.java
new file mode 100644
index 000000000..d0c105f26
--- /dev/null
+++ b/src/main/java/com/juick/service/TwitterService.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2008-2023, 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.service;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.github.scribejava.apis.TwitterApi20;
+import com.github.scribejava.core.builder.ServiceBuilder;
+import com.github.scribejava.core.model.OAuthRequest;
+import com.github.scribejava.core.model.Response;
+import com.github.scribejava.core.model.Verb;
+import com.github.scribejava.core.oauth.OAuth20Service;
+import com.juick.model.Message;
+import com.juick.util.MessageUtils;
+import jakarta.annotation.PostConstruct;
+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.MediaType;
+import org.springframework.stereotype.Service;
+
+import javax.inject.Inject;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+
+@Service
+public class TwitterService {
+ private static final Logger logger = LoggerFactory.getLogger("Twitter");
+ @Value("${twitter_client_id:12345678}")
+ private String clientId;
+ @Value("${twitter_client_secret:secret}")
+ private String clientSecret;
+ private static final String redirectUri = "https://juick.com/_twitter";
+ @Inject
+ private UserService userService;
+ private OAuth20Service twitterAuthService;
+
+ @Inject
+ private ObjectMapper jsonMapper;
+
+ @PostConstruct
+ public void init() {
+ ServiceBuilder twitterBuilder = new ServiceBuilder(clientId);
+ setTwitterAuthService(twitterBuilder.apiSecret(clientSecret)
+ .defaultScope("tweet.read tweet.write users.read offline.access")
+ .callback(redirectUri)
+ .build(TwitterApi20.instance()));
+ }
+
+ public void twitterPost(final Message jmsg) {
+ userService.getTwitterToken(jmsg.getUser().getUid()).ifPresent(t -> {
+ String status = MessageUtils.getMessageHashTags(jmsg) + StringUtils.defaultString(jmsg.getText());
+ if (status.length() > 253) {
+ status = status.substring(0, 252) + "…";
+ }
+ status += " http://juick.com/" + jmsg.getMid();
+ try {
+ var token = twitterAuthService.refreshAccessToken(t.secret());
+ userService.refreshTwitterToken(jmsg.getUser(), token.getAccessToken(), token.getRefreshToken());
+ OAuthRequest postRequest = new OAuthRequest(Verb.POST,
+ "https://api.twitter.com/2/tweets");
+ var body = jsonMapper.createObjectNode();
+ body.put("text", status);
+ postRequest.addHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
+ postRequest.setPayload(jsonMapper.writeValueAsString(body));
+ twitterAuthService.signRequest(token, postRequest);
+ try (Response twitterResponse = twitterAuthService.execute(postRequest)) {
+ if (twitterResponse.isSuccessful()) {
+ logger.info(twitterResponse.getBody());
+ } else {
+ logger.warn("Twitter error {}: {}", twitterResponse.getCode(), twitterResponse.getBody());
+ }
+ } catch (Exception e) {
+ logger.error("Twitter exception {}", jmsg.getUser().getName(), e);
+ }
+ } catch (IOException | InterruptedException | ExecutionException e) {
+ logger.error("Twitter exception {}", jmsg.getUser().getName(), e);
+ }
+ });
+ }
+
+ public OAuth20Service getTwitterAuthService() {
+ return twitterAuthService;
+ }
+
+ public void setTwitterAuthService(OAuth20Service twitterAuthService) {
+ this.twitterAuthService = twitterAuthService;
+ }
+}
diff --git a/src/main/java/com/juick/service/UserService.java b/src/main/java/com/juick/service/UserService.java
index 4acc5b6ae..31822b012 100644
--- a/src/main/java/com/juick/service/UserService.java
+++ b/src/main/java/com/juick/service/UserService.java
@@ -104,7 +104,11 @@ public interface UserService {
List<User> getUserIgnoredUsers(int uid);
List<User> getUserVipUsers(int uid);
@CacheEvict(value = "twitter_user", key="{ #user.getUid() }")
- boolean linkTwitterAccount(User user, String accessToken, String accessTokenSecret, String screenName);
+ boolean linkTwitterAccount(User user, String accessToken, String refreshToken, String screenName);
+
+ boolean refreshTwitterToken(User user, String accessToken, String refreshToken);
+
+ boolean isTwitter1User(User user);
int getStatsMessages(int uid);
diff --git a/src/main/java/com/juick/service/UserServiceImpl.java b/src/main/java/com/juick/service/UserServiceImpl.java
index d19af067e..7f73e6d1f 100644
--- a/src/main/java/com/juick/service/UserServiceImpl.java
+++ b/src/main/java/com/juick/service/UserServiceImpl.java
@@ -523,10 +523,24 @@ public class UserServiceImpl extends BaseJdbcService implements UserService {
@Transactional
@Override
public boolean linkTwitterAccount(
- final User user, final String accessToken, final String accessTokenSecret, final String screenName) {
- return getJdbcTemplate().update("INSERT INTO twitter(user_id,access_token,access_token_secret,uname) " +
- "VALUES (?,?,?,?)",
- user.getUid(), accessToken, accessTokenSecret, screenName) > 0;
+ final User user, final String accessToken, final String refreshToken, final String screenName) {
+ return getJdbcTemplate().update("INSERT INTO twitter(user_id,access_token,access_token_secret,refresh_token,uname) " +
+ "VALUES (?,?,'',?,?)",
+ user.getUid(), accessToken, refreshToken, screenName) > 0;
+ }
+
+ @Transactional
+ @Override
+ public boolean refreshTwitterToken(
+ final User user, final String accessToken, final String refreshToken) {
+ return getJdbcTemplate().update("UPDATE twitter SET access_token=?, refresh_token=?" +
+ " WHERE user_id=?",
+ accessToken, refreshToken, user.getUid()) > 0;
+ }
+ @Transactional(readOnly = true)
+ @Override
+ public boolean isTwitter1User(User user) {
+ return jdbcTemplate.queryForList("SELECT user_id FROM twitter WHERE user_id=? AND refresh_token=''", Integer.class, user.getUid()).size() > 0;
}
@Transactional(readOnly = true)
@@ -612,7 +626,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService {
@Override
public Optional<ExternalToken> getTwitterToken(final int uid) {
List<ExternalToken> list = getJdbcTemplate().query(
- "SELECT uname, access_token, access_token_secret FROM twitter WHERE user_id = ? AND crosspost = true",
+ "SELECT uname, access_token, refresh_token FROM twitter WHERE user_id = ? AND crosspost = true",
(rs, num) -> new ExternalToken(rs.getString(1), "twitter",
rs.getString(2), rs.getString(3)),
uid);