diff options
author | desperateCoder <echotodevnull@gmail.com> | 2020-08-02 17:32:27 +0300 |
---|---|---|
committer | Niedermann IT-Dienstleistungen <stefan-niedermann@users.noreply.github.com> | 2020-08-09 17:34:18 +0300 |
commit | 635d58dddbfaefad150be529a39fdcb79717d38c (patch) | |
tree | a84d1b1a4924f880514e08f42b1c3fae87669a29 | |
parent | 9d5c1b125613da8e6e29759e2f9a40f156a55d0a (diff) |
#492 #631 fetch group members
9 files changed, 188 insertions, 3 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/api/GsonConfig.java b/app/src/main/java/it/niedermann/nextcloud/deck/api/GsonConfig.java index fa03286b4..5da408e44 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/api/GsonConfig.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/api/GsonConfig.java @@ -17,6 +17,8 @@ import it.niedermann.nextcloud.deck.model.ocs.Activity; import it.niedermann.nextcloud.deck.model.ocs.Capabilities; import it.niedermann.nextcloud.deck.model.ocs.comment.OcsComment; import it.niedermann.nextcloud.deck.model.ocs.projects.OcsProjectList; +import it.niedermann.nextcloud.deck.model.ocs.user.GroupMemberUIDs; +import it.niedermann.nextcloud.deck.model.ocs.user.OcsUser; import it.niedermann.nextcloud.deck.model.ocs.user.OcsUserList; /** @@ -40,12 +42,14 @@ public class GsonConfig { Type stack = new TypeToken<FullStack>() {}.getType(); Type capabilities = new TypeToken<Capabilities>() {}.getType(); Type ocsUserList = new TypeToken<OcsUserList>() {}.getType(); + Type ocsUser = new TypeToken<OcsUser>() {}.getType(); Type activity = new TypeToken<Activity>() {}.getType(); Type activityList = new TypeToken<List<Activity>>() {}.getType(); Type attachment = new TypeToken<Attachment>() {}.getType(); Type attachmentList = new TypeToken<List<Attachment>>() {}.getType(); Type comment = new TypeToken<OcsComment>() {}.getType(); Type projectList = new TypeToken<OcsProjectList>() {}.getType(); + Type groupMembers = new TypeToken<GroupMemberUIDs>() {}.getType(); INSTANCE = new GsonBuilder() .setDateFormat(DATE_PATTERN) @@ -61,12 +65,14 @@ public class GsonConfig { .registerTypeAdapter(stack, new NextcloudDeserializer<>("stack", FullStack.class)) .registerTypeAdapter(capabilities, new NextcloudDeserializer<>("capability", Capabilities.class)) .registerTypeAdapter(ocsUserList, new NextcloudDeserializer<>("ocsUserList", OcsUserList.class)) + .registerTypeAdapter(ocsUser, new NextcloudDeserializer<>("ocsUser", OcsUser.class)) .registerTypeAdapter(activity, new NextcloudDeserializer<>("activity", Activity.class)) .registerTypeAdapter(activityList, new NextcloudDeserializer<>("activityList", Activity.class)) .registerTypeAdapter(attachmentList, new NextcloudArrayDeserializer<>("attachments", Attachment.class)) .registerTypeAdapter(attachment, new NextcloudDeserializer<>("attachment", Attachment.class)) .registerTypeAdapter(comment, new NextcloudDeserializer<>("comment", OcsComment.class)) .registerTypeAdapter(projectList, new NextcloudDeserializer<>("projectList", OcsProjectList.class)) + .registerTypeAdapter(groupMembers, new NextcloudDeserializer<>("groupMembers", GroupMemberUIDs.class)) .create(); } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/api/JsonToEntityParser.java b/app/src/main/java/it/niedermann/nextcloud/deck/api/JsonToEntityParser.java index 66e5089d7..ab9572687 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/api/JsonToEntityParser.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/api/JsonToEntityParser.java @@ -34,6 +34,7 @@ import it.niedermann.nextcloud.deck.model.ocs.comment.OcsComment; import it.niedermann.nextcloud.deck.model.ocs.projects.OcsProject; import it.niedermann.nextcloud.deck.model.ocs.projects.OcsProjectList; import it.niedermann.nextcloud.deck.model.ocs.projects.OcsProjectResource; +import it.niedermann.nextcloud.deck.model.ocs.user.GroupMemberUIDs; import it.niedermann.nextcloud.deck.model.ocs.user.OcsUser; import it.niedermann.nextcloud.deck.model.ocs.user.OcsUserList; @@ -57,16 +58,38 @@ public class JsonToEntityParser { return (T) parseCapabilities(obj); } else if (mType == OcsUserList.class) { return (T) parseOcsUserList(obj); + } else if (mType == OcsUser.class) { + return (T) parseSingleOcsUser(obj); } else if (mType == Attachment.class) { return (T) parseAttachment(obj); } else if (mType == OcsComment.class) { return (T) parseOcsComment(obj); + } else if (mType == GroupMemberUIDs.class) { + return (T) parseGroupMemberUIDs(obj); } else if (mType == OcsProjectList.class) { return (T) parseOcsProjectList(obj); } throw new IllegalArgumentException("unregistered type: " + mType.getCanonicalName()); } + private static GroupMemberUIDs parseGroupMemberUIDs(JsonObject obj) { + DeckLog.verbose(obj.toString()); + GroupMemberUIDs uids = new GroupMemberUIDs(); + makeTraceableIfFails(() -> { + JsonElement data = obj.get("ocs").getAsJsonObject().get("data"); + if (!data.isJsonNull() && data.getAsJsonObject().has("users")) { + JsonElement users = data.getAsJsonObject().get("users"); + if (!users.isJsonNull() && users.isJsonArray()) { + for (JsonElement userElement : users.getAsJsonArray()) { + uids.add(userElement.getAsString()); + } + } + } + + }, obj); + return uids; + } + private static OcsUserList parseOcsUserList(JsonObject obj) { DeckLog.verbose(obj.toString()); OcsUserList ocsUserList = new OcsUserList(); @@ -91,6 +114,24 @@ public class JsonToEntityParser { }, obj); return ocsUserList; } + private static OcsUser parseSingleOcsUser(JsonObject obj) { + DeckLog.verbose(obj.toString()); + OcsUser ocsUser = new OcsUser(); + makeTraceableIfFails(() -> { + JsonElement data = obj.get("ocs").getAsJsonObject().get("data"); + if (!data.isJsonNull()) { + JsonObject user = data.getAsJsonObject(); + if (user.has("id")){ + ocsUser.setId(user.get("id").getAsString()); + } + if (user.has("displayname")){ + ocsUser.setDisplayName(user.get("displayname").getAsString()); + } + } + + }, obj); + return ocsUser; + } private static OcsProjectList parseOcsProjectList(JsonObject obj) { DeckLog.verbose(obj.toString()); OcsProjectList projectList = new OcsProjectList(); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/api/NextcloudServerAPI.java b/app/src/main/java/it/niedermann/nextcloud/deck/api/NextcloudServerAPI.java index 07c1d745e..7b93ace49 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/api/NextcloudServerAPI.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/api/NextcloudServerAPI.java @@ -11,6 +11,8 @@ import it.niedermann.nextcloud.deck.model.ocs.Capabilities; import it.niedermann.nextcloud.deck.model.ocs.comment.DeckComment; import it.niedermann.nextcloud.deck.model.ocs.comment.OcsComment; import it.niedermann.nextcloud.deck.model.ocs.projects.OcsProjectList; +import it.niedermann.nextcloud.deck.model.ocs.user.GroupMemberUIDs; +import it.niedermann.nextcloud.deck.model.ocs.user.OcsUser; import it.niedermann.nextcloud.deck.model.ocs.user.OcsUserList; import retrofit2.http.Body; import retrofit2.http.DELETE; @@ -33,6 +35,12 @@ public interface NextcloudServerAPI { @GET("apps/files_sharing/api/v1/sharees?format=json&perPage=20&itemType=0%2C1%2C7") Observable<OcsUserList> searchUser(@Query("search") String searchTerm); + @GET("cloud/groups/{search}?format=json") + Observable<GroupMemberUIDs> searchGroupMembers(@Path("search") String groupUid); + + @GET("cloud/users/{search}?format=json") + Observable<OcsUser> getSingleUserData(@Path("search") String userUid); + @GET("apps/activity/api/v2/activity/filter?format=json&object_type=deck_card&limit=50&since=-1&sort=asc") Observable<List<Activity>> getActivitiesForCard(@Query("object_id") long cardId); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/AccessControl.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/AccessControl.java index 299dd7ca0..e2a3ccc3b 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/model/AccessControl.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/AccessControl.java @@ -8,6 +8,7 @@ import androidx.room.Index; import com.google.gson.annotations.SerializedName; import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; +import it.niedermann.nextcloud.deck.model.ocs.user.GroupMemberUIDs; @Entity(inheritSuperIndices = true, indices = { @@ -35,6 +36,8 @@ public class AccessControl extends AbstractRemoteEntity { @Ignore @SerializedName("participant") private User user; + @Ignore + private GroupMemberUIDs groupMemberUIDs; public AccessControl() { super(); @@ -115,6 +118,14 @@ public class AccessControl extends AbstractRemoteEntity { this.user = user; } + public GroupMemberUIDs getGroupMemberUIDs() { + return groupMemberUIDs; + } + + public void setGroupMemberUIDs(GroupMemberUIDs groupMemberUIDs) { + this.groupMemberUIDs = groupMemberUIDs; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/user/GroupMemberUIDs.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/user/GroupMemberUIDs.java new file mode 100644 index 000000000..085ca786e --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/user/GroupMemberUIDs.java @@ -0,0 +1,27 @@ +package it.niedermann.nextcloud.deck.model.ocs.user; + +import java.util.ArrayList; +import java.util.List; + +public class GroupMemberUIDs { + private List<String> uids = new ArrayList<>(); + + public List<String> getUids() { + return uids; + } + + public void setUids(List<String> uids) { + this.uids = uids; + } + + @Override + public String toString() { + return "GroupMemberUIDs{" + + "uids=" + uids + + '}'; + } + + public void add(String uid) { + uids.add(uid); + } +} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/ServerAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/ServerAdapter.java index 178e3975e..de32530bb 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/ServerAdapter.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/ServerAdapter.java @@ -42,6 +42,8 @@ import it.niedermann.nextcloud.deck.model.ocs.Capabilities; import it.niedermann.nextcloud.deck.model.ocs.comment.DeckComment; import it.niedermann.nextcloud.deck.model.ocs.comment.OcsComment; import it.niedermann.nextcloud.deck.model.ocs.projects.OcsProjectList; +import it.niedermann.nextcloud.deck.model.ocs.user.GroupMemberUIDs; +import it.niedermann.nextcloud.deck.model.ocs.user.OcsUser; import it.niedermann.nextcloud.deck.model.ocs.user.OcsUserList; import it.niedermann.nextcloud.deck.model.propagation.CardUpdate; import it.niedermann.nextcloud.deck.model.propagation.Reorder; @@ -164,6 +166,15 @@ public class ServerAdapter { ensureInternetConnection(); RequestHelper.request(provider, () -> provider.getNextcloudAPI().searchUser(searchTerm), responseCallback); } + public void getSingleUserData(String userUid, IResponseCallback<OcsUser> responseCallback) { + ensureInternetConnection(); + RequestHelper.request(provider, () -> provider.getNextcloudAPI().getSingleUserData(userUid), responseCallback); + } + + public void searchGroupMembers(String groupUID, IResponseCallback<GroupMemberUIDs> responseCallback) { + ensureInternetConnection(); + RequestHelper.request(provider, () -> provider.getNextcloudAPI().searchGroupMembers(groupUID), responseCallback); + } public void getActivitiesForCard(long cardId, IResponseCallback<List<it.niedermann.nextcloud.deck.model.ocs.Activity>> responseCallback) { ensureInternetConnection(); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/SyncHelper.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/SyncHelper.java index f53696a8d..b8e757b65 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/SyncHelper.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/SyncHelper.java @@ -31,7 +31,7 @@ public class SyncHelper { // Sync Server -> App public <T extends IRemoteEntity> void doSyncFor(final AbstractSyncDataProvider<T> provider){ provider.registerChildInParent(provider); - provider.getAllFromServer(serverAdapter, accountId, new IResponseCallback<List<T>>(account) { + provider.getAllFromServer(serverAdapter, dataBaseAdapter, accountId, new IResponseCallback<List<T>>(account) { @Override public void onResponse(List<T> response) { if (response != null) { diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/AbstractSyncDataProvider.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/AbstractSyncDataProvider.java index d43550729..42b034026 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/AbstractSyncDataProvider.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/AbstractSyncDataProvider.java @@ -67,7 +67,13 @@ public abstract class AbstractSyncDataProvider<T extends IRemoteEntity> { children.add(child); } - public abstract void getAllFromServer(ServerAdapter serverAdapter, long accountId, IResponseCallback<List<T>> responder, Date lastSync); + public void getAllFromServer(ServerAdapter serverAdapter, long accountId, IResponseCallback<List<T>> responder, Date lastSync) { + return; + } + public void getAllFromServer(ServerAdapter serverAdapter, DataBaseAdapter dataBaseAdapter, long accountId, IResponseCallback<List<T>> responder, Date lastSync) { + // Overridden, because we also need the DB-Adapter at some points here (see ACL data provider) + getAllFromServer(serverAdapter, accountId, responder, lastSync); + } public abstract T getSingleFromDB(DataBaseAdapter dataBaseAdapter, long accountId, T entity); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/AccessControlDataProvider.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/AccessControlDataProvider.java index 3242ab7c1..a5ca723f2 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/AccessControlDataProvider.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/AccessControlDataProvider.java @@ -2,16 +2,23 @@ package it.niedermann.nextcloud.deck.persistence.sync.helpers.providers; import java.util.Date; import java.util.List; +import java.util.concurrent.CountDownLatch; +import it.niedermann.nextcloud.deck.DeckLog; import it.niedermann.nextcloud.deck.api.IResponseCallback; import it.niedermann.nextcloud.deck.model.AccessControl; +import it.niedermann.nextcloud.deck.model.Account; import it.niedermann.nextcloud.deck.model.User; import it.niedermann.nextcloud.deck.model.full.FullBoard; +import it.niedermann.nextcloud.deck.model.ocs.user.GroupMemberUIDs; +import it.niedermann.nextcloud.deck.model.ocs.user.OcsUser; import it.niedermann.nextcloud.deck.persistence.sync.adapters.ServerAdapter; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.DataBaseAdapter; +import it.niedermann.nextcloud.deck.persistence.sync.helpers.SyncHelper; public class AccessControlDataProvider extends AbstractSyncDataProvider<AccessControl> { + private static final Long TYPE_GROUP = 1L; private List<AccessControl> acl; private FullBoard board; @@ -22,10 +29,69 @@ public class AccessControlDataProvider extends AbstractSyncDataProvider<AccessCo } @Override - public void getAllFromServer(ServerAdapter serverAdapter, long accountId, IResponseCallback<List<AccessControl>> responder, Date lastSync) { + public void getAllFromServer(ServerAdapter serverAdapter, DataBaseAdapter dataBaseAdapter, long accountId, IResponseCallback<List<AccessControl>> responder, Date lastSync) { + CountDownLatch latch = new CountDownLatch(acl.size()); + for (AccessControl accessControl : acl) { + if (accessControl.getType() == TYPE_GROUP) { + serverAdapter.searchGroupMembers(accessControl.getUser().getUid(), new IResponseCallback<GroupMemberUIDs>(responder.getAccount()) { + @Override + public void onResponse(GroupMemberUIDs response) { + accessControl.setGroupMemberUIDs(response); + if (response.getUids().size() > 0) { + ensureGroupMembersInDB(getAccount(), dataBaseAdapter, serverAdapter, response); + } + latch.countDown(); + } + + @Override + public void onError(Throwable throwable) { + super.onError(throwable); + latch.countDown(); + } + }); + } else latch.countDown(); + } + try { + latch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } responder.onResponse(acl); } + private void ensureGroupMembersInDB(Account account, DataBaseAdapter dataBaseAdapter, ServerAdapter serverAdapter, GroupMemberUIDs response) { + CountDownLatch memberLatch = new CountDownLatch(response.getUids().size()); + for (String uid : response.getUids()) { + User user = dataBaseAdapter.getUserByUidDirectly(account.getId(), uid); + if (user == null) { + // unknown user. fetch! + serverAdapter.getSingleUserData(uid, new IResponseCallback<OcsUser>(account) { + @Override + public void onResponse(OcsUser response) { + DeckLog.log(response.toString()); + User user = new User(); + user.setUid(response.getId()); + user.setPrimaryKey(response.getId()); + user.setDisplayname(response.getDisplayName()); + dataBaseAdapter.createUser(account.getId(), user); + memberLatch.countDown(); + } + + @Override + public void onError(Throwable throwable) { + super.onError(throwable); + memberLatch.countDown(); + } + }); + } else memberLatch.countDown(); + } + try { + memberLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + @Override public AccessControl getSingleFromDB(DataBaseAdapter dataBaseAdapter, long accountId, AccessControl entity) { return dataBaseAdapter.getAccessControlByRemoteIdDirectly(accountId, entity.getEntity().getId()); @@ -33,8 +99,10 @@ public class AccessControlDataProvider extends AbstractSyncDataProvider<AccessCo @Override public long createInDB(DataBaseAdapter dataBaseAdapter, long accountId, AccessControl entity) { + //TODO delete members from relation table prepareUser(dataBaseAdapter, accountId, entity); return dataBaseAdapter.createAccessControl(accountId, entity); + //TODO add members to relation table } private void prepareUser(DataBaseAdapter dataBaseAdapter, long accountId, AccessControl entity) { @@ -51,8 +119,10 @@ public class AccessControlDataProvider extends AbstractSyncDataProvider<AccessCo @Override public void updateInDB(DataBaseAdapter dataBaseAdapter, long accountId, AccessControl entity, boolean setStatus) { + //TODO delete members from relation table prepareUser(dataBaseAdapter, accountId, entity); dataBaseAdapter.updateAccessControl(entity, setStatus); + //TODO delete members from relation table } @Override @@ -91,4 +161,9 @@ public class AccessControlDataProvider extends AbstractSyncDataProvider<AccessCo public List<AccessControl> getAllChangedFromDB(DataBaseAdapter dataBaseAdapter, long accountId, Date lastSync) { return dataBaseAdapter.getLocallyChangedAccessControl(accountId, board.getLocalId()); } + + @Override + public void goDeeper(SyncHelper syncHelper, AccessControl existingEntity, AccessControl entityFromServer, IResponseCallback<Boolean> callback) { + super.goDeeper(syncHelper, existingEntity, entityFromServer, callback); + } } |