aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/api/Users.java
blob: 5bae4028e399734bbcb13280637da89029145226 (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
package com.juick.api;

import com.juick.json.UserSerializer;
import com.juick.server.UserQueries;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author ugnich
 */
public class Users {

    Connection sql;
    
    UserSerializer userSerializer = new UserSerializer();

    public Users(Connection sql) {
        this.sql = sql;
    }

    public void doGetUsers(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException {
        List<com.juick.User> users = new ArrayList<>();

        String punames[] = request.getParameterValues("uname");
        if (punames != null) {
            ArrayList<String> unames = new ArrayList<>(Arrays.asList(punames));
            Iterator<String> i = unames.iterator();
            while (i.hasNext()) {
                if (!i.next().matches("^[a-zA-Z0-9\\-]{2,16}$")) {
                    i.remove();
                }
            }
            if (!unames.isEmpty() && unames.size() < 20) {
                users.addAll(UserQueries.getUsersByName(sql, unames));
            }
        }

        String pjids[] = request.getParameterValues("jid");
        if (pjids != null) {
            List<String> jids = new ArrayList<>(Arrays.asList(pjids));
            Iterator<String> ii = jids.iterator();
            while (ii.hasNext()) {
                if (!ii.next().matches("^[a-zA-Z0-9\\-\\_\\@\\.]{6,64}$")) {
                    ii.remove();
                }
            }
            if (!jids.isEmpty() && jids.size() < 20) {
                users.addAll(UserQueries.getUsersByJID(sql, jids));
            }
        }

        if (!users.isEmpty()) {
            String json = userSerializer.serializeList(users);
            Main.replyJSON(request, response, json);
        } else {
            response.sendError(404);
        }
    }

    public void doGetUserRead(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException {
        int uid = 0;
        String uname = request.getParameter("uname");
        if (uname == null) {
            uid = vuid;
        } else {
            if (UserQueries.checkUserNameValid(uname)) {
                com.juick.User u = UserQueries.getUserByName(sql, uname);
                if (u != null && u.UID > 0) {
                    uid = u.UID;
                }
            }
        }

        if (uid > 0) {
            List<Integer> uids = UserQueries.getUserRead(sql, uid);
            if (uids.size() > 0) {
                List<com.juick.User> users = UserQueries.getUsersByID(sql, uids);
                if (users.size() > 0) {
                    String json = userSerializer.serializeList(users);
                    Main.replyJSON(request, response, json);
                    return;
                }
            }
        }
        response.sendError(404);
    }

    public void doGetUserReaders(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException {
        int uid = 0;
        String uname = request.getParameter("uname");
        if (uname == null) {
            uid = vuid;
        } else {
            if (UserQueries.checkUserNameValid(uname)) {
                com.juick.User u = UserQueries.getUserByName(sql, uname);
                if (u != null && u.UID > 0) {
                    uid = u.UID;
                }
            }
        }

        if (uid > 0) {
            List<Integer> uids = UserQueries.getUserReaders(sql, uid);
            if (uids.size() > 0) {
                List<com.juick.User> users = UserQueries.getUsersByID(sql, uids);
                if (users.size() > 0) {
                    String json = userSerializer.serializeList(users);
                    Main.replyJSON(request, response, json);
                    return;
                }
            }
        }
        response.sendError(404);
    }
}