-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUser.java
More file actions
163 lines (144 loc) · 3.34 KB
/
User.java
File metadata and controls
163 lines (144 loc) · 3.34 KB
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
package api.user;
import api.search.user.UserSearch;
import api.son.MySon;
import api.soup.MySoup;
import api.util.CouldNotLoadException;
import api.util.Tuple;
import java.util.ArrayList;
import java.util.List;
/**
* A User, needs to be created using fromId contains the user profile.
*
* @author Gwindow
*/
public class User {
/**
* The response.
*/
private Profile response;
/**
* The status.
*/
private String status;
private String error;
/**
* The id.
*/
private transient int id;
/**
* Return a User object created from an id.
*
* @param id id of user
* @return User object
*/
public static User fromId(int id){
String authkey = MySoup.getAuthKey();
String url = "ajax.php?action=user&id=" + id + "&auth=" + authkey;
User user = (User)MySon.toObject(url, User.class);
if (user != null){
user.id = id;
}
return user;
}
/**
* User from name.
*
* @param username the username
* @return the user
* @throws CouldNotLoadException the could not load exception
*/
public static User userFromName(String username) throws CouldNotLoadException{
UserSearch us = UserSearch.search(username.toLowerCase());
if (us.getResponse().getResults() != null && !us.getResponse().getResults().isEmpty()){
return fromId(us.getResponse().getResults().get(0).getUserId().intValue());
}
else {
throw new CouldNotLoadException("User not found");
}
}
/**
* Add user to your friend list.
*/
public void addToFriends(){
if (!getProfile().isFriend()){
MySoup.pressLink("friends.php?action=add&friendid=" + id + "&auth=" + MySoup.getAuthKey());
System.out.println("Added to friends");
}
else {
System.out.println("Already added as friend");
}
}
/**
* Send a message to a user
*
* @param id user id to send to
* @param subject message subject
* @param body message body
* @return true if sent successfully
*/
public static boolean sendMessage(int id, String subject, String body){
try {
List<Tuple<String, String>> list = new ArrayList<Tuple<String, String>>();
list.add(new Tuple<String, String>("action", "takecompose"));
list.add(new Tuple<String, String>("toid", Integer.toString(id)));
list.add(new Tuple<String, String>("auth", MySoup.getAuthKey()));
list.add(new Tuple<String, String>("subject", subject));
list.add(new Tuple<String, String>("body", body));
MySoup.postMethod("inbox.php", list);
}
catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
/**
* Send message to the user.
*
* @param subject the subject
* @param body the body
* @return true if message sent successfully
*/
public boolean sendMessage(String subject, String body){
return sendMessage(id, subject, body);
}
/**
* Send rippy.
*
* @param body the body
*/
public void sendRippy(String body){
new Rippy(id, body).sendRippy();
}
/**
* Get the user's id.
*
* @return user id
*/
public int getId(){
return id;
}
/**
* Gets the profile.
*
* @return the profile
*/
public Profile getProfile(){
return response;
}
/**
* Gets the status.
*
* @return the status
*/
public boolean getStatus(){
return status.equalsIgnoreCase("success");
}
public String getError(){
return error;
}
@Override
public String toString(){
return "User [id=" + id + ", response=" + response + ", status=" + status + "]";
}
}