From 99431fd077776b1a8ac3d0ed9b6a37e298d9abe4 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Fri, 9 Apr 2021 20:05:18 +0900 Subject: [PATCH] Null checks. Fixes https://github.com/austinhuang0131/barinsta/issues/981 --- .../instagrabber/managers/ThreadManager.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/awais/instagrabber/managers/ThreadManager.java b/app/src/main/java/awais/instagrabber/managers/ThreadManager.java index c76728e3..5b84c97c 100644 --- a/app/src/main/java/awais/instagrabber/managers/ThreadManager.java +++ b/app/src/main/java/awais/instagrabber/managers/ThreadManager.java @@ -27,6 +27,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @@ -564,6 +565,7 @@ public final class ThreadManager { private List addEmoji(final List reactionList, final String emoji, final boolean shouldReplaceIfAlreadyReacted) { + if (currentUser == null) return reactionList; final List temp = reactionList == null ? new ArrayList<>() : new ArrayList<>(reactionList); int index = -1; for (int i = 0; i < temp.size(); i++) { @@ -1311,6 +1313,7 @@ public final class ThreadManager { final MutableLiveData> data = new MutableLiveData<>(); final Call addUsersRequest = service.addUsers(threadId, users.stream() + .filter(Objects::nonNull) .map(User::getPk) .collect(Collectors.toList())); handleDetailsChangeRequest(data, addUsersRequest); @@ -1319,6 +1322,10 @@ public final class ThreadManager { public LiveData> removeMember(final User user) { final MutableLiveData> data = new MutableLiveData<>(); + if (user == null) { + data.postValue(Resource.error("user is null!", null)); + return data; + } final Call request = service.removeUsers(threadId, Collections.singleton(user.getPk())); request.enqueue(new Callback() { @Override @@ -1337,6 +1344,7 @@ public final class ThreadManager { leftUsersValue = Collections.emptyList(); } final List updatedActiveUsers = activeUsers.stream() + .filter(Objects::nonNull) .filter(u -> u.getPk() != user.getPk()) .collect(Collectors.toList()); final ImmutableList.Builder updatedLeftUsersBuilder = ImmutableList.builder().addAll(leftUsersValue); @@ -1357,12 +1365,14 @@ public final class ThreadManager { } public boolean isAdmin(final User user) { + if (user == null) return false; final List adminUserIdsValue = adminUserIds.getValue(); return adminUserIdsValue != null && adminUserIdsValue.contains(user.getPk()); } public LiveData> makeAdmin(final User user) { final MutableLiveData> data = new MutableLiveData<>(); + if (user == null) return data; if (isAdmin(user)) return data; final Call request = service.addAdmins(threadId, Collections.singleton(user.getPk())); request.enqueue(new Callback() { @@ -1399,6 +1409,7 @@ public final class ThreadManager { public LiveData> removeAdmin(final User user) { final MutableLiveData> data = new MutableLiveData<>(); + if (user == null) return data; if (!isAdmin(user)) return data; final Call request = service.removeAdmins(threadId, Collections.singleton(user.getPk())); request.enqueue(new Callback() { @@ -1411,6 +1422,7 @@ public final class ThreadManager { final List currentAdmins = adminUserIds.getValue(); if (currentAdmins == null) return; final List updatedAdminUserIds = currentAdmins.stream() + .filter(Objects::nonNull) .filter(userId1 -> userId1 != user.getPk()) .collect(Collectors.toList()); final DirectThread currentThread = ThreadManager.this.thread.getValue(); @@ -1583,6 +1595,7 @@ public final class ThreadManager { public LiveData> blockUser(final User user) { final MutableLiveData> data = new MutableLiveData<>(); + if (user == null) return data; friendshipService.block(user.getPk(), new ServiceCallback() { @Override public void onSuccess(final FriendshipChangeResponse result) { @@ -1600,6 +1613,7 @@ public final class ThreadManager { public LiveData> unblockUser(final User user) { final MutableLiveData> data = new MutableLiveData<>(); + if (user == null) return data; friendshipService.unblock(user.getPk(), new ServiceCallback() { @Override public void onSuccess(final FriendshipChangeResponse result) { @@ -1617,6 +1631,7 @@ public final class ThreadManager { public LiveData> restrictUser(final User user) { final MutableLiveData> data = new MutableLiveData<>(); + if (user == null) return data; friendshipService.toggleRestrict(user.getPk(), true, new ServiceCallback() { @Override public void onSuccess(final FriendshipRestrictResponse result) { @@ -1634,6 +1649,7 @@ public final class ThreadManager { public LiveData> unRestrictUser(final User user) { final MutableLiveData> data = new MutableLiveData<>(); + if (user == null) return data; friendshipService.toggleRestrict(user.getPk(), false, new ServiceCallback() { @Override public void onSuccess(final FriendshipRestrictResponse result) { @@ -1654,7 +1670,10 @@ public final class ThreadManager { data.postValue(Resource.loading(null)); final Call approveUsersRequest = service .approveParticipantRequests(threadId, - users.stream().map(User::getPk).collect(Collectors.toList())); + users.stream() + .filter(Objects::nonNull) + .map(User::getPk) + .collect(Collectors.toList())); handleDetailsChangeRequest(data, approveUsersRequest, () -> pendingUserApproveDenySuccessAction(users)); return data; } @@ -1811,6 +1830,7 @@ public final class ThreadManager { @Override public void onResponse(@NonNull final Call call, @NonNull final Response response) { + if (currentUser == null) return; if (!response.isSuccessful()) { handleErrorBody(call, response, data); return;