Skip to content

Commit b46c31e

Browse files
committed
Automatically setup friendship between accounts and fix accounts list
1 parent 70a8567 commit b46c31e

5 files changed

Lines changed: 93 additions & 1 deletion

File tree

core/src/main/java/com/rtm516/mcxboxbroadcast/core/FriendManager.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.rtm516.mcxboxbroadcast.core;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.JsonMappingException;
35
import com.rtm516.mcxboxbroadcast.core.configs.FriendSyncConfig;
46
import com.rtm516.mcxboxbroadcast.core.exceptions.XboxFriendsException;
57
import com.rtm516.mcxboxbroadcast.core.models.FriendModifyResponse;
8+
import com.rtm516.mcxboxbroadcast.core.models.FriendStatusResponse;
69
import com.rtm516.mcxboxbroadcast.core.models.session.FollowerResponse;
710

811
import java.io.IOException;
@@ -118,6 +121,7 @@ public List<FollowerResponse.Person> get() throws XboxFriendsException {
118121
* Add a friend from xbox live
119122
*
120123
* @param xuid The XUID of the friend to add
124+
* @param gamertag The gamertag of the friend to add
121125
*/
122126
public void add(String xuid, String gamertag) {
123127
// Remove the user from the remove list (if they are on it)
@@ -130,10 +134,47 @@ public void add(String xuid, String gamertag) {
130134
internalProcess();
131135
}
132136

137+
/**
138+
* Add a friend from xbox live if they aren't already a friend
139+
*
140+
* @param xuid The XUID of the friend to add
141+
* @param gamertag The gamertag of the friend to add
142+
* @return True if the friend was added, false if they are already a friend
143+
*/
144+
public boolean addIfRequired(String xuid, String gamertag) {
145+
// Check if they are already in the list to be added
146+
if (toAdd.containsKey(xuid)) {
147+
return false;
148+
}
149+
150+
// Check if we are already friends
151+
HttpRequest xboxFriendStatus = HttpRequest.newBuilder()
152+
.uri(URI.create(Constants.PEOPLE.formatted(xuid)))
153+
.header("Authorization", sessionManager.getTokenHeader())
154+
.GET()
155+
.build();
156+
157+
try {
158+
HttpResponse<String> response = httpClient.send(xboxFriendStatus, HttpResponse.BodyHandlers.ofString());
159+
FriendStatusResponse modifyResponse = Constants.OBJECT_MAPPER.readValue(response.body(), FriendStatusResponse.class);
160+
161+
if (modifyResponse.isFollowingCaller() && modifyResponse.isFollowedByCaller()) {
162+
return false;
163+
}
164+
} catch (InterruptedException | IOException e) {
165+
// Debug log it failed and assume we aren't friends
166+
logger.debug("Failed to check if " + gamertag + " (" + xuid + ") is a friend: " + e.getMessage());
167+
}
168+
169+
add(xuid, gamertag);
170+
return true;
171+
}
172+
133173
/**
134174
* Remove a friend from xbox live
135175
*
136176
* @param xuid The XUID of the friend to remove
177+
* @param gamertag The gamertag of the friend to remove
137178
*/
138179
public void remove(String xuid, String gamertag) {
139180
// Remove the user from the add list (if they are on it)

core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManager.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ public void init(SessionInfo sessionInfo, FriendSyncConfig friendSyncConfig) thr
9999
}
100100
}
101101

102+
@Override
103+
protected boolean handleFriendship() {
104+
// Don't do anything as we are the main session
105+
return false;
106+
}
107+
102108
/**
103109
* Update the current session with new information
104110
*
@@ -260,7 +266,7 @@ public void listSessions() {
260266
messages.add(" - Gamertag: " + getXboxToken().gamertag());
261267
messages.add(" Following: " + socialSummary().targetFollowingCount() + "/1000");
262268

263-
if (subSessionManagers.isEmpty()) {
269+
if (!subSessionManagers.isEmpty()) {
264270
messages.add("Sub-sessions:");
265271
for (Map.Entry<String, SubSessionManager> subSession : subSessionManagers.entrySet()) {
266272
messages.add(" - ID: " + subSession.getKey());

core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManagerCore.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ public void init() throws SessionCreationException, SessionUpdateException {
157157
XboxTokenInfo tokenInfo = getXboxToken();
158158
logger.info("Successfully authenticated as " + tokenInfo.gamertag() + " (" + tokenInfo.userXUID() + ")");
159159

160+
if (handleFriendship()) {
161+
logger.info("Waiting for friendship to be processed...");
162+
try {
163+
Thread.sleep(5000); // TODO Do a real callback not just wait
164+
} catch (InterruptedException e) {
165+
logger.error("Failed to wait for friendship to be processed", e);
166+
}
167+
}
168+
160169
logger.info("Creating Xbox LIVE session...");
161170

162171
// Create the session
@@ -171,6 +180,13 @@ public void init() throws SessionCreationException, SessionUpdateException {
171180
initialized = true;
172181
}
173182

183+
/**
184+
* Handle the friendship of the current user to the main session if needed
185+
*
186+
* @return True if the friendship is being handled, false otherwise
187+
*/
188+
protected abstract boolean handleFriendship();
189+
174190
/**
175191
* Setup a new session and its prerequisites
176192
*

core/src/main/java/com/rtm516/mcxboxbroadcast/core/SubSessionManager.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ public String getSessionId() {
3434
return parent.sessionInfo().getSessionId();
3535
}
3636

37+
@Override
38+
protected boolean handleFriendship() {
39+
// TODO Some form of force flag just in case the master friends list is full
40+
41+
// Add the main account
42+
boolean subAdd = friendManager().addIfRequired(parent.getXboxToken().userXUID(), parent.getXboxToken().gamertag());
43+
44+
// Get the main account to add us
45+
boolean mainAdd = parent.friendManager().addIfRequired(getXboxToken().userXUID(), getXboxToken().gamertag());
46+
47+
return subAdd || mainAdd;
48+
}
49+
3750
@Override
3851
protected void updateSession() throws SessionUpdateException {
3952
super.updateSessionInternal(Constants.JOIN_SESSION.formatted(parent.sessionInfo().getHandleId()), new JoinSessionRequest(parent.sessionInfo()));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rtm516.mcxboxbroadcast.core.models;
2+
3+
import java.time.Instant;
4+
5+
public record FriendStatusResponse(
6+
String xuid,
7+
Instant addedDateTimeUtc,
8+
boolean isFavorite,
9+
Object[] socialNetworks,
10+
boolean isFollowedByCaller,
11+
boolean isFollowingCaller,
12+
boolean isIdentityShared,
13+
boolean isSquadMateWith,
14+
boolean isUnfollowingFeed
15+
) {
16+
}

0 commit comments

Comments
 (0)