aboutsummaryrefslogtreecommitdiff
path: root/juick-server-jdbc/src/main/java/com/juick/service/SubscriptionServiceImpl.java
blob: d7ac6c84f2a8bdc151175af0338123f860bf6c8b (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * 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.Tag;
import com.juick.User;
import com.juick.server.helpers.NotifyOpts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.inject.Inject;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by aalexeev on 11/13/16.
 */
@Repository
public class SubscriptionServiceImpl extends BaseJdbcService implements SubscriptionService {
    private final UserService userService;
    private final MessagesService messagesService;
    private final TagService tagService;

    @Inject
    public SubscriptionServiceImpl(JdbcTemplate jdbcTemplate, UserService userService,
                                   MessagesService messagesService, TagService tagService) {
        super(jdbcTemplate, null);

        Assert.notNull(userService, "UserService must be initialized");
        this.userService = userService;

        Assert.notNull(messagesService,"MessagesService must be initialized");
        this.messagesService = messagesService;

        Assert.notNull(tagService, "TagService must be initialized");
        this.tagService = tagService;
    }

    @Transactional(readOnly = true)
    @Override
    public List<String> getJIDSubscribedToUser(final int uid, final boolean friendsonly) {
        return getNamedParameterJdbcTemplate().queryForList(
                "SELECT jids.jid FROM subscr_users INNER JOIN jids " +
                        "ON (subscr_users.user_id = :uid AND subscr_users.suser_id = jids.user_id) WHERE jids.active = 1 " +
                        (friendsonly ?
                                " AND EXISTS (SELECT 1 FROM wl_users w WHERE w.user_id = :uid and jids.user_id = w.wl_user_id)" : StringUtils.EMPTY),
                new MapSqlParameterSource("uid", uid),
                String.class);
    }

    @Transactional(readOnly = true)
    @Override
    public List<User> getSubscribedUsers(final int uid, final int mid) {
        User author = messagesService.getMessageAuthor(mid);

        List<User> userids = userService.getUserReaders(uid);
        List<Integer> tags = tagService.getMessageTagsIDs(mid);
        List<String> tagsStr = tagService.getMessageTags(mid).stream().map(t -> t.getTag().getName()).collect(Collectors.toList());

        Set<Integer> set = new HashSet<>();
        set.addAll(
                userids.stream()
                        .map(User::getUid).filter(u -> Collections.disjoint(tagService.getUserBLTags(u), tagsStr))
                        .collect(Collectors.toList()));


        if (!tags.isEmpty()) {
            List<Integer> tagUsers = getNamedParameterJdbcTemplate().queryForList(
                    "SELECT st.suser_id FROM subscr_tags st " +
                            "WHERE st.tag_id IN (:ids) AND st.suser_id != :uid " +
                            " AND NOT EXISTS (SELECT 1 FROM bl_users bu WHERE bu.bl_user_id = :authorUid and st.suser_id = bu.user_id)" +
                            " AND NOT EXISTS (SELECT 1 FROM bl_tags bt WHERE bt.tag_id IN (:ids) and st.suser_id = bt.user_id)",
                    new MapSqlParameterSource()
                            .addValue("ids", tags)
                            .addValue("uid", uid)
                            .addValue("authorUid", author.getUid()),
                    Integer.class);
            set.addAll(tagUsers);
        }
        return userService.getUsersByID(set);
    }

    @Transactional(readOnly = true)
    @Override
    public List<User> getUsersSubscribedToComments(final int mid, final int ignore_uid) {
        List<Integer> userids = getJdbcTemplate().queryForList(
                "SELECT suser_id FROM subscr_messages WHERE message_id=? AND suser_id!=?",
                Integer.class,
                mid, ignore_uid);

        if (!userids.isEmpty())
            return userService.getUsersByID(userids);

        return Collections.emptyList();
    }

    @Transactional(readOnly = true)
    @Override
    public List<User> getUsersSubscribedToUserRecommendations(final int uid, final int mid, final int muid) {
        List<Integer> tags = tagService.getMessageTagsIDs(mid);

        String query = "SELECT s.suser_id FROM subscr_users s WHERE s.user_id = :uid " +
                " AND NOT EXISTS (SELECT 1 FROM bl_users b WHERE b.bl_user_id = :muid and b.user_id = s.user_id) " +
                " AND NOT EXISTS (SELECT 1 FROM subscr_users s1 WHERE s1.user_id = :muid AND s.user_id = s1.suser_id) " +
                " AND NOT EXISTS (SELECT 1 FROM subscr_messages sm WHERE sm.message_id = :mid AND s.user_id = sm.suser_id) " +
                " AND NOT EXISTS (SELECT 1 FROM favorites WHERE favorites.message_id = :mid AND favorites.user_id = s.user_id) " +
                " AND s.user_id NOT IN (SELECT s2.suser_id FROM subscr_users s2 " +
                "  INNER JOIN favorites f ON (f.message_id = :mid AND s2.user_id = f.user_id AND f.user_id != :uid))";

        MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource()
                .addValue("uid", uid)
                .addValue("muid", muid)
                .addValue("mid", mid);

        if (!tags.isEmpty()) {
            sqlParameterSource.addValue("ids", tags);
            query += " AND NOT EXISTS (SELECT 1 FROM subscr_tags st WHERE st.tag_id IN (:ids) AND s.user_id = st.suser_id) " +
                    " AND NOT EXISTS (SELECT 1 FROM bl_tags b WHERE b.tag_id IN (:ids) AND s.user_id = b.user_id)";
        }

        List<Integer> userids = getNamedParameterJdbcTemplate().queryForList(
                query, sqlParameterSource, Integer.class);

        return userService.getUsersByID(userids);
    }

    @Transactional
    @Override
    public boolean subscribeMessage(final int mid, final int vuid) {
        return getJdbcTemplate().update(
                "INSERT IGNORE INTO subscr_messages(suser_id, message_id) VALUES (?, ?)", vuid, mid) == 1;
    }

    @Transactional
    @Override
    public boolean unSubscribeMessage(final int mid, final int vuid) {
        return getJdbcTemplate().update(
                "DELETE FROM subscr_messages WHERE message_id=? AND suser_id=?", mid, vuid) > 0;
    }

    @Transactional
    @Override
    public boolean subscribeUser(final User user, final User toUser) {
        return getJdbcTemplate().update(
                "INSERT IGNORE INTO subscr_users(user_id,suser_id) VALUES (?,?)", toUser.getUid(), user.getUid()) == 1;
    }

    @Transactional
    @Override
    public boolean unSubscribeUser(final User user, final User fromUser) {
        return getJdbcTemplate().update(
                "DELETE FROM subscr_users WHERE suser_id=? AND user_id=?", user.getUid(), fromUser.getUid()) > 0;
    }

    @Transactional
    @Override
    public boolean subscribeTag(final User user, final Tag toTag) {
        return getJdbcTemplate().update(
                "INSERT IGNORE INTO subscr_tags(tag_id,suser_id) VALUES (?,?)", toTag.TID, user.getUid()) == 1;
    }

    @Transactional
    @Override
    public boolean unSubscribeTag(final User user, final Tag toTag) {
        return getJdbcTemplate().update(
                "DELETE FROM subscr_tags WHERE tag_id=? AND suser_id=?", toTag.TID, user.getUid()) > 0;
    }

    @Transactional(readOnly = true)
    @Override
    public List<String> getSubscribedTags(User user) {
        return getJdbcTemplate().queryForList("SELECT tags.name FROM subscr_tags INNER JOIN tags USING(tag_id) " +
                "WHERE subscr_tags.suser_id=? ORDER BY tags.name", String.class, user.getUid());
    }

    @Transactional(readOnly = true)
    @Override
    public NotifyOpts getNotifyOptions(final User user) {
        List<NotifyOpts> list = getJdbcTemplate().query(
                "SELECT jnotify,subscr_notify,recommendations FROM useroptions WHERE user_id=?",
                (rs, num) -> {
                    NotifyOpts options = new NotifyOpts();
                    options.setRepliesEnabled(rs.getInt(1) > 0);
                    options.setSubscriptionsEnabled(rs.getInt(2) > 0);
                    options.setRecommendationsEnabled(rs.getInt(3) > 0);
                    return options;
                },
                user.getUid());

        return list.isEmpty() ?
                new NotifyOpts() : list.get(0);
    }

    @Transactional
    @Override
    public boolean setNotifyOptions(final User user, final NotifyOpts options) {
        int jnotify = getJdbcTemplate().update(
                "UPDATE useroptions SET jnotify=? WHERE user_id=?",
                options.isRepliesEnabled() ? 1 : 0,
                user.getUid());

        int subscr_notify = getJdbcTemplate().update(
                "UPDATE useroptions SET subscr_notify=? WHERE user_id=?",
                options.isSubscriptionsEnabled() ? 1 : 0,
                user.getUid());

        int recommendations = getJdbcTemplate().update(
                "UPDATE useroptions SET recommendations=? WHERE user_id=?",
                options.isRecommendationsEnabled() ? 1 : 0,
                user.getUid());

        return jnotify > 0 && subscr_notify > 0 && recommendations > 0;
    }
}