aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/service/CrosspostServiceImpl.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-11-08 21:38:27 +0300
committerGravatar Vitaly Takmazov2018-11-08 21:38:27 +0300
commit7aaa3f9a29c280f01c677c918932620be45cdbd7 (patch)
tree39947b2c889afd08f9c73ba54fab91159d2af258 /src/main/java/com/juick/service/CrosspostServiceImpl.java
parent3ea9770d0d43fbe45449ac4531ec4b0a374d98ea (diff)
Merge everything into single Spring Boot application
Diffstat (limited to 'src/main/java/com/juick/service/CrosspostServiceImpl.java')
-rw-r--r--src/main/java/com/juick/service/CrosspostServiceImpl.java282
1 files changed, 282 insertions, 0 deletions
diff --git a/src/main/java/com/juick/service/CrosspostServiceImpl.java b/src/main/java/com/juick/service/CrosspostServiceImpl.java
new file mode 100644
index 00000000..d190faba
--- /dev/null
+++ b/src/main/java/com/juick/service/CrosspostServiceImpl.java
@@ -0,0 +1,282 @@
+/*
+ * 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.service;
+
+import com.juick.ExternalToken;
+import com.juick.model.ApplicationStatus;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
+import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Created by aalexeev on 11/13/16.
+ */
+@Repository
+public class CrosspostServiceImpl extends BaseJdbcService implements CrosspostService {
+
+ @Transactional(readOnly = true)
+ @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 = 1",
+ (rs, num) -> new ExternalToken(rs.getString(1), "twitter",
+ rs.getString(2), rs.getString(3)),
+ uid);
+
+ return list.isEmpty() ?
+ Optional.empty() : Optional.of(list.get(0));
+ }
+
+ @Transactional
+ @Override
+ public boolean deleteTwitterToken(Integer uid) {
+ return getJdbcTemplate().update("DELETE FROM twitter WHERE user_id=?", uid) > 0;
+ }
+
+ @Override
+ public void addFacebookState(String state, String redirectUri) {
+ jdbcTemplate.update("INSERT INTO facebook(loginhash, fb_link) VALUES(?, ?)", state, redirectUri);
+ }
+
+ @Override
+ public void addVKState(String state, String redirectUri) {
+ jdbcTemplate.update("INSERT INTO vk(loginhash, vk_link) VALUES(?, ?)", state, redirectUri);
+ }
+
+ @Override
+ public String verifyFacebookState(String state) {
+ try {
+ return jdbcTemplate.queryForObject("SELECT fb_link FROM facebook WHERE loginhash=?",
+ String.class, state);
+ } catch (EmptyResultDataAccessException e) {
+ return StringUtils.EMPTY;
+ }
+ }
+
+ @Override
+ public String verifyVKState(String state) {
+ try {
+ return jdbcTemplate.queryForObject("SELECT vk_link FROM vk WHERE loginhash=?",
+ String.class, state);
+ } catch (EmptyResultDataAccessException e) {
+ return StringUtils.EMPTY;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public Optional<Pair<String, String>> getFacebookTokens(final int uid) {
+ List<Optional<Pair<String, String>>> list = getJdbcTemplate().query(
+ "SELECT fb_id, access_token FROM facebook WHERE user_id = ? AND access_token IS NOT NULL AND crosspost = 1",
+ (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))),
+ uid);
+ return list.isEmpty() ?
+ Optional.empty() : list.get(0);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public ApplicationStatus getFbCrossPostStatus(final int uid) {
+ List<ApplicationStatus> list = getJdbcTemplate().query(
+ "SELECT 1, crosspost FROM facebook WHERE user_id = ? LIMIT 1",
+ (rs, num) -> {
+ ApplicationStatus status = new ApplicationStatus();
+
+ status.setConnected(rs.getInt(1) > 0);
+ status.setCrosspostEnabled(rs.getBoolean(2));
+
+ return status;
+ },
+ uid);
+
+ return list.isEmpty() ?
+ new ApplicationStatus() : list.get(0);
+ }
+
+ @Transactional
+ @Override
+ public boolean enableFBCrosspost(Integer uid) {
+ return getJdbcTemplate().update("UPDATE facebook SET crosspost=1 WHERE user_id=?", uid) > 0;
+ }
+
+ @Transactional
+ @Override
+ public void disableFBCrosspost(Integer uid) {
+ getJdbcTemplate().update("UPDATE facebook SET crosspost=0 WHERE user_id=?", uid);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public String getTwitterName(final int uid) {
+ List<String> list = getJdbcTemplate().queryForList(
+ "SELECT uname FROM twitter WHERE user_id = ?",
+ String.class,
+ uid);
+
+ return list.isEmpty() ?
+ StringUtils.EMPTY : list.get(0);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public String getTelegramName(final int uid) {
+ List<String> list = getJdbcTemplate().queryForList(
+ "SELECT tg_name FROM telegram WHERE user_id = ?",
+ String.class,
+ uid);
+
+ return list.isEmpty() ?
+ StringUtils.EMPTY : list.get(0);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public Optional<Pair<String, String>> getVkTokens(final int uid) {
+ List<Optional<Pair<String, String>>> list = getJdbcTemplate().query(
+ "SELECT vk_id, access_token FROM vk WHERE user_id = ? AND crosspost = 1",
+ (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))),
+ uid);
+
+ return list.isEmpty() ?
+ Optional.empty() : list.get(0);
+ }
+
+ @Transactional
+ @Override
+ public void deleteVKUser(Integer uid) {
+ getJdbcTemplate().update("DELETE FROM vk WHERE user_id=?", uid);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public int getUIDbyFBID(long fbID) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT user_id FROM facebook WHERE fb_id=? AND user_id IS NOT NULL",
+ Integer.class, fbID);
+ } catch (EmptyResultDataAccessException e) {
+ return 0;
+ }
+ }
+
+ @Transactional
+ @Override
+ public boolean createFacebookUser(long fbID, String loginhash, String token, String fbName, String fbLink) {
+ return getJdbcTemplate().update("UPDATE facebook SET fb_id=?, access_token=?, fb_name=?, fb_link=? WHERE loginhash=?",
+ fbID, token, fbName, fbLink, loginhash) > 0;
+ }
+
+ @Transactional
+ @Override
+ public boolean updateFacebookUser(long fbID, String token, String fbName, String fbLink) {
+ return getJdbcTemplate().update("UPDATE facebook SET access_token=?,fb_name=?,fb_link=? WHERE fb_id=?",
+ token, fbName, fbLink, fbID) > 0;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public int getUIDbyVKID(long vkID) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT user_id FROM vk WHERE vk_id=? AND user_id IS NOT NULL", Integer.class, vkID);
+ } catch (EmptyResultDataAccessException e) {
+ return 0;
+ }
+ }
+
+ @Transactional
+ @Override
+ public boolean createVKUser(long vkID, String loginhash, String token, String vkName, String vkLink) {
+ return getJdbcTemplate().update("INSERT INTO vk(vk_id,loginhash,access_token,vk_name,vk_link) VALUES (?,?,?,?,?)",
+ vkID, loginhash, token, vkName, vkLink) > 0;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public String getFacebookNameByHash(String hash) {
+ try {
+ List<Pair<String, String>> fb = getJdbcTemplate().query("SELECT fb_name,fb_link FROM facebook WHERE loginhash=?",
+ (rs, num) -> Pair.of(rs.getString(1), rs.getString(2)), hash);
+ if (fb.size() > 0) {
+ return "<a href=\"" + fb.get(0).getRight() + "\" rel=\"nofollow\">" + fb.get(0).getLeft() + "</a>";
+ }
+ return null;
+ } catch (EmptyResultDataAccessException e) {
+ return null;
+ }
+ }
+
+ @Transactional
+ @Override
+ public String getTelegramNameByHash(String hash) {
+ try {
+ String name = getJdbcTemplate().queryForObject("SELECT tg_name FROM telegram WHERE loginhash=?", String.class, hash);
+ return "<a href=\"https://telegram.me/" + name + "\" rel=\"nofollow\">" + name + "</a>";
+ } catch (EmptyResultDataAccessException e) {
+ return null;
+ }
+ }
+
+ @Transactional
+ @Override
+ public boolean setFacebookUser(String hash, int uid) {
+ return getJdbcTemplate().update("UPDATE facebook SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0;
+ }
+
+ @Transactional
+ @Override
+ public String getVKNameByHash(String hash) {
+ List<Pair<String, String>> logins = getJdbcTemplate().query("SELECT vk_name,vk_link FROM vk WHERE loginhash=?",
+ (rs, num) -> Pair.of(rs.getString(1), rs.getString(2)), hash);
+ if (logins.size() > 0) {
+ return "<a href=\"http://vk.com/" + logins.get(0).getRight() + "\" rel=\"nofollow\">" + logins.get(0).getLeft() + "</a>";
+ }
+ return null;
+ }
+
+ @Transactional
+ @Override
+ public boolean setVKUser(String hash, int uid) {
+ return getJdbcTemplate().update("UPDATE vk SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0;
+ }
+
+ @Transactional
+ @Override
+ public boolean setTelegramUser(String hash, int uid) {
+ return getJdbcTemplate().update("UPDATE telegram SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public String getJIDByHash(String hash) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT jid FROM jids WHERE loginhash=?", String.class, hash);
+ } catch (EmptyResultDataAccessException e) {
+ return null;
+ }
+ }
+
+ @Transactional
+ @Override
+ public boolean setJIDUser(String hash, int uid) {
+ return getJdbcTemplate().update("UPDATE jids SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0;
+ }
+}