aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/service/CrosspostServiceImpl.java
blob: 0f043292e735d6aeba63f275f4ea313fce10ff01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.juick.service;

import com.juick.server.helpers.ApplicationStatus;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.List;
import java.util.Optional;

/**
 * Created by aalexeev on 11/13/16.
 */
@Repository
public class CrosspostServiceImpl extends BaseJdbcService implements CrosspostService {

    @Inject
    public CrosspostServiceImpl(JdbcTemplate jdbcTemplate) {
        super(jdbcTemplate, null);
    }

    @Transactional(readOnly = true)
    @Override
    public Optional<Pair<String, String>> getTwitterTokens(final int uid) {
        List<Optional<Pair<String, String>>> list = getJdbcTemplate().query(
                "SELECT access_token,access_token_secret FROM twitter 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 boolean deleteTwitterToken(Integer uid) {
        return getJdbcTemplate().update("DELETE FROM twitter WHERE user_id=?", uid) > 0
                && getJdbcTemplate().update("DELETE FROM subscr_users WHERE user_id=? AND suser_id=1741", uid) > 0;
    }

    @Transactional(readOnly = true)
    @Override
    public Optional<String> getFacebookToken(final int uid) {
        List<String> list = getJdbcTemplate().queryForList(
                "SELECT access_token FROM facebook WHERE user_id = ? AND access_token IS NOT NULL AND crosspost = 1",
                String.class,
                uid);
        return list.isEmpty() ?
                Optional.empty() : Optional.of(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
                && getJdbcTemplate().update(
                "INSERT INTO subscr_users(user_id,suser_id,jid,active) VALUES (?,5863,'juick@facebook.juick.com',1)",
                uid) > 0;
    }

    @Transactional
    @Override
    public void disableFBCrosspost(Integer uid) {
        getJdbcTemplate().update("UPDATE facebook SET crosspost=0 WHERE user_id=?", uid);
        // TODO: stop using magic numbers for system users
        getJdbcTemplate().update("DELETE FROM subscr_users WHERE user_id=? AND suser_id=5863", 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() ?
                "" : 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() ?
                "" : 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);
    }
}