2024-08-06 23:30:00 +02:00
|
|
|
package mp.code;
|
|
|
|
|
2024-09-17 23:16:39 +02:00
|
|
|
import lombok.Getter;
|
2024-09-17 17:37:22 +02:00
|
|
|
import mp.code.data.Config;
|
2024-09-15 02:00:04 +02:00
|
|
|
import mp.code.data.User;
|
2024-09-05 02:45:33 +02:00
|
|
|
import mp.code.exceptions.ConnectionException;
|
|
|
|
import mp.code.exceptions.ConnectionRemoteException;
|
2024-08-06 23:30:00 +02:00
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
2024-09-17 23:16:39 +02:00
|
|
|
/**
|
|
|
|
* The main entrypoint of the library.
|
|
|
|
* This is the only object you are expected to hold yourself; unlike all the others,
|
|
|
|
* there are no copies of it managed exclusively by the library. When this is garbage
|
|
|
|
* collected, it will free the underlying memory.
|
|
|
|
* A Client is used to join and manage workspaces, and to obtain information about
|
|
|
|
* the current session.
|
|
|
|
*/
|
|
|
|
@Getter
|
|
|
|
public final class Client {
|
2024-08-06 23:30:00 +02:00
|
|
|
private final long ptr;
|
|
|
|
|
2024-08-07 10:22:01 +02:00
|
|
|
Client(long ptr) {
|
|
|
|
this.ptr = ptr;
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|
|
|
|
|
2024-09-17 23:16:39 +02:00
|
|
|
/**
|
|
|
|
* Connects to a remote CodeMP server and creates a {@link Client} instance
|
|
|
|
* for interacting with it.
|
|
|
|
* @param config a {@link Config} object containing the connection settings
|
|
|
|
* @return a holder for the Client's pointer
|
|
|
|
* @throws ConnectionException if an error occurs in communicating with the server
|
|
|
|
*/
|
|
|
|
public static native Client connect(Config config) throws ConnectionException;
|
|
|
|
|
2024-09-15 02:00:04 +02:00
|
|
|
private static native User get_user(long self);
|
2024-09-17 23:16:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information about the current user.
|
|
|
|
* @return a {@link User} object representing the user
|
|
|
|
*/
|
2024-09-15 02:00:04 +02:00
|
|
|
public User getUser() {
|
|
|
|
return get_user(this.ptr);
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|
|
|
|
|
2024-09-17 23:16:39 +02:00
|
|
|
private static native Workspace join_workspace(long self, String workspaceId) throws ConnectionException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Joins a {@link Workspace} and returns it.
|
|
|
|
* @param workspaceId the id of the workspace to connect to
|
|
|
|
* @return the relevant {@link Workspace}
|
|
|
|
* @throws ConnectionException if an error occurs in communicating with the server
|
|
|
|
*/
|
|
|
|
public Workspace joinWorkspace(String workspaceId) throws ConnectionException {
|
|
|
|
return join_workspace(this.ptr, workspaceId);
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|
|
|
|
|
2024-09-17 23:16:39 +02:00
|
|
|
private static native void create_workspace(long self, String workspaceId) throws ConnectionRemoteException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a workspace. You need to call {@link #joinWorkspace(String)} to actually join
|
|
|
|
* and interact with it.
|
|
|
|
* @param workspaceId the id of the new workspace
|
|
|
|
* @throws ConnectionRemoteException if an error occurs in communicating with the server
|
|
|
|
*/
|
|
|
|
public void createWorkspace(String workspaceId) throws ConnectionRemoteException {
|
|
|
|
create_workspace(this.ptr, workspaceId);
|
2024-08-27 23:04:56 +02:00
|
|
|
}
|
|
|
|
|
2024-09-17 23:16:39 +02:00
|
|
|
private static native void delete_workspace(long self, String workspaceId) throws ConnectionRemoteException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a workspace.
|
|
|
|
* @param workspaceId the id of the workspace to delete
|
|
|
|
* @throws ConnectionRemoteException if an error occurs in communicating with the server
|
|
|
|
*/
|
|
|
|
public void deleteWorkspace(String workspaceId) throws ConnectionRemoteException {
|
|
|
|
delete_workspace(this.ptr, workspaceId);
|
2024-08-27 23:04:56 +02:00
|
|
|
}
|
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
private static native void invite_to_workspace(long self, String workspaceId, String user) throws ConnectionRemoteException;
|
2024-09-17 23:16:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invites a user to a workspace.
|
|
|
|
* @param workspaceId the id of the new workspace
|
|
|
|
* @param user the name of the user to invite
|
|
|
|
* @throws ConnectionRemoteException if an error occurs in communicating with the server
|
|
|
|
*/
|
2024-09-17 17:37:22 +02:00
|
|
|
public void inviteToWorkspace(String workspaceId, String user) throws ConnectionRemoteException {
|
2024-09-17 23:16:39 +02:00
|
|
|
invite_to_workspace(this.ptr, workspaceId, user);
|
2024-08-27 23:04:56 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
private static native String[] list_workspaces(long self, boolean owned, boolean invited) throws ConnectionRemoteException;
|
2024-09-17 23:16:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists available workspaces according to certain filters.
|
|
|
|
* @param owned if owned workspaces should be included
|
|
|
|
* @param invited if workspaces the user is invited to should be included
|
|
|
|
* @return an array of workspace IDs
|
|
|
|
* @throws ConnectionRemoteException if an error occurs in communicating with the server
|
|
|
|
*/
|
2024-09-05 02:45:33 +02:00
|
|
|
public String[] listWorkspaces(boolean owned, boolean invited) throws ConnectionRemoteException {
|
2024-08-27 23:04:56 +02:00
|
|
|
return list_workspaces(this.ptr, owned, invited);
|
|
|
|
}
|
|
|
|
|
2024-09-15 02:00:04 +02:00
|
|
|
private static native String[] active_workspaces(long self);
|
2024-09-17 23:16:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists the currently active workspaces (the ones the user has currently joined).
|
|
|
|
* @return an array of workspace IDs
|
|
|
|
*/
|
2024-09-15 02:00:04 +02:00
|
|
|
public String[] activeWorkspaces() {
|
|
|
|
return active_workspaces(this.ptr);
|
|
|
|
}
|
|
|
|
|
2024-09-17 23:16:39 +02:00
|
|
|
private static native boolean leave_workspace(long self, String workspaceId);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Leaves a workspace.
|
|
|
|
* @param workspaceId the id of the workspaces to leave
|
|
|
|
* @return true if it succeeded (usually fails if the workspace wasn't active)
|
|
|
|
*/
|
|
|
|
public boolean leaveWorkspace(String workspaceId) {
|
|
|
|
return leave_workspace(this.ptr, workspaceId);
|
2024-08-08 02:45:52 +02:00
|
|
|
}
|
|
|
|
|
2024-09-09 16:06:05 +02:00
|
|
|
private static native Workspace get_workspace(long self, String workspace);
|
2024-09-17 23:16:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an active workspace.
|
|
|
|
* @param workspaceId the id of the workspaces to get
|
|
|
|
* @return a {@link Workspace} with that name, if it was present and active
|
|
|
|
*/
|
|
|
|
public Optional<Workspace> getWorkspace(String workspaceId) {
|
|
|
|
return Optional.ofNullable(get_workspace(this.ptr, workspaceId));
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|
2024-08-27 23:04:56 +02:00
|
|
|
|
2024-09-15 02:00:04 +02:00
|
|
|
private static native void refresh(long self) throws ConnectionRemoteException;
|
2024-09-17 23:16:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Refreshes the current access token.
|
|
|
|
* @throws ConnectionRemoteException if an error occurs in communicating with the server
|
|
|
|
*/
|
2024-09-05 02:45:33 +02:00
|
|
|
public void refresh() throws ConnectionRemoteException {
|
2024-09-15 02:00:04 +02:00
|
|
|
refresh(this.ptr);
|
2024-08-27 23:04:56 +02:00
|
|
|
}
|
2024-09-17 23:16:39 +02:00
|
|
|
|
2024-08-06 23:30:00 +02:00
|
|
|
private static native void free(long self);
|
|
|
|
@Override
|
|
|
|
protected void finalize() {
|
|
|
|
free(this.ptr);
|
|
|
|
}
|
2024-08-07 10:22:01 +02:00
|
|
|
|
|
|
|
static {
|
2024-09-18 14:54:54 +02:00
|
|
|
NativeUtils.loadLibraryIfNeeded();
|
2024-08-07 10:22:01 +02:00
|
|
|
}
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|