Skip to content

Commit

Permalink
delthas#45 Solved login and contact listing problems. Added IDLE status.
Browse files Browse the repository at this point in the history
  • Loading branch information
Renan de Souza committed Jun 29, 2018
1 parent c3d3cd2 commit e349236
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
6 changes: 1 addition & 5 deletions src/main/java/fr/delthas/skype/NotifConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -824,11 +824,7 @@ private String getXMLField(String XML, String fieldName) throws ParseException {
}

private String getSelfLiveUsername() {
if (microsoft) {
return "live:" + username.substring(0, username.indexOf('@'));
} else {
return username;
}
return skype.getUser(username).getLiveUsername();
}

private static class Packet {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/fr/delthas/skype/Presence.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public enum Presence {
* Away / Be Right Back (orange clock on Skype)
*/
AWAY("AWY"),
/**
* Idle / Absent
*/
IDLE("IDL"),
/**
* Busy / Do Not Disturb (red sign on Skype)
*/
Expand Down
1 change: 1 addition & 0 deletions src/main/java/fr/delthas/skype/Skype.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public final class Skype {
private boolean connecting = false;
private volatile long expires;
private IOException exceptionDuringConnection;
private User me;

// --- Public API (except listeners add/remove methods) --- //

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/fr/delthas/skype/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class User {
private String city;
private String displayName;
private String avatarUrl;
private String liveUsername;
private Presence presence = Presence.OFFLINE;

User(Skype skype, String username) {
Expand Down Expand Up @@ -247,6 +248,14 @@ void setPresence(Presence presence, boolean triggerListeners) {
}
}

public String getLiveUsername() {
return liveUsername;
}

public void setLiveUsername(String liveUsername) {
this.liveUsername = liveUsername;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
35 changes: 20 additions & 15 deletions src/main/java/fr/delthas/skype/WebConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ private void updateContacts() throws IOException {
updated = true;
String selfResponse = sendRequest(Method.GET, "/users/self/profile").body();
JSONObject selfJSON = new JSONObject(selfResponse);
updateUser(selfJSON, false);

User loggedUser = updateUser(selfJSON, false, username);

String profilesResponse =
sendRequest(Method.GET, "https://contacts.skype.com/contacts/v2/users/" + getSelfLiveUsername() + "/contacts", true).body();
sendRequest(Method.GET, "https://contacts.skype.com/contacts/v2/users/" + loggedUser.getLiveUsername() + "/contacts", true).body();

try {
JSONObject json = new JSONObject(profilesResponse);
if (json.optString("message", null) != null) {
Expand All @@ -108,6 +110,10 @@ private void updateContacts() throws IOException {
}

private User updateUser(JSONObject userJSON, boolean newContactType) throws ParseException {
return updateUser(userJSON, newContactType, null);
}

private User updateUser(JSONObject userJSON, boolean newContactType, String username) throws ParseException {
String userUsername;
String userFirstName = null;
String userLastName = null;
Expand Down Expand Up @@ -146,9 +152,15 @@ private User updateUser(JSONObject userJSON, boolean newContactType) throws Pars
userUsername = mri.substring(senderBegin + 1);
userDisplayName = userJSON.optString("display_name", null);
JSONObject profileJSON = userJSON.getJSONObject("profile");
JSONObject nameJSON = profileJSON.getJSONObject("name");
userFirstName = nameJSON.optString("first", null);
userLastName = nameJSON.optString("surname", null);

userFirstName = userUsername;
userLastName = "";
if (profileJSON.has("name")) {
JSONObject nameJSON = profileJSON.getJSONObject("name");
userFirstName = nameJSON.optString("first", null);
userLastName = nameJSON.optString("surname", null);
}

userMood = profileJSON.optString("mood", null);
if (profileJSON.has("locations")) {
JSONObject locationJSON = profileJSON.optJSONArray("locations").optJSONObject(0);
Expand All @@ -162,14 +174,15 @@ private User updateUser(JSONObject userJSON, boolean newContactType) throws Pars
} catch (JSONException e) {
throw new ParseException(e);
}
User user = skype.getUser(userUsername);
User user = skype.getUser(username != null ? username : userUsername);
user.setCity(getPlaintext(userCity));
user.setCountry(getPlaintext(userCountry));
user.setDisplayName(getPlaintext(userDisplayName));
user.setFirstName(getPlaintext(userFirstName));
user.setLastName(getPlaintext(userLastName));
user.setMood(getPlaintext(userMood));
user.setAvatarUrl(userAvatarUrl);
user.setLiveUsername(userUsername);
return user;
}

Expand Down Expand Up @@ -231,12 +244,4 @@ private Response sendRequest(Method method, String apiPath, boolean absoluteApiP
private Response sendRequest(Method method, String apiPath, String... keyval) throws IOException {
return sendRequest(method, apiPath, false, keyval);
}

private String getSelfLiveUsername() {
if (username.contains("@")) {
return "live:" + username.substring(0, username.indexOf('@'));
} else {
return username;
}
}
}
}

0 comments on commit e349236

Please sign in to comment.