Browse Source
Convert FriendshipRepository and FriendshipService to kotlin
renovate/org.robolectric-robolectric-4.x
Convert FriendshipRepository and FriendshipService to kotlin
renovate/org.robolectric-robolectric-4.x
Ammar Githam
4 years ago
6 changed files with 450 additions and 400 deletions
-
138app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.java
-
58app/src/main/java/awais/instagrabber/fragments/NotificationsViewerFragment.java
-
129app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java
-
65app/src/main/java/awais/instagrabber/managers/ThreadManager.kt
-
47app/src/main/java/awais/instagrabber/repositories/FriendshipRepository.kt
-
409app/src/main/java/awais/instagrabber/webservices/FriendshipService.kt
@ -1,37 +1,36 @@ |
|||
package awais.instagrabber.repositories; |
|||
package awais.instagrabber.repositories |
|||
|
|||
import java.util.Map; |
|||
|
|||
import awais.instagrabber.repositories.responses.FriendshipChangeResponse; |
|||
import awais.instagrabber.repositories.responses.FriendshipRestrictResponse; |
|||
import retrofit2.Call; |
|||
import retrofit2.http.FieldMap; |
|||
import retrofit2.http.FormUrlEncoded; |
|||
import retrofit2.http.GET; |
|||
import retrofit2.http.POST; |
|||
import retrofit2.http.Path; |
|||
import retrofit2.http.QueryMap; |
|||
|
|||
public interface FriendshipRepository { |
|||
import awais.instagrabber.repositories.responses.FriendshipChangeResponse |
|||
import awais.instagrabber.repositories.responses.FriendshipRestrictResponse |
|||
import retrofit2.http.* |
|||
|
|||
interface FriendshipRepository { |
|||
@FormUrlEncoded |
|||
@POST("/api/v1/friendships/{action}/{id}/") |
|||
Call<FriendshipChangeResponse> change(@Path("action") String action, |
|||
@Path("id") long id, |
|||
@FieldMap Map<String, String> form); |
|||
suspend fun change( |
|||
@Path("action") action: String, |
|||
@Path("id") id: Long, |
|||
@FieldMap form: Map<String, String>, |
|||
): FriendshipChangeResponse |
|||
|
|||
@FormUrlEncoded |
|||
@POST("/api/v1/restrict_action/{action}/") |
|||
Call<FriendshipRestrictResponse> toggleRestrict(@Path("action") String action, |
|||
@FieldMap Map<String, String> form); |
|||
suspend fun toggleRestrict( |
|||
@Path("action") action: String, |
|||
@FieldMap form: Map<String, String>, |
|||
): FriendshipRestrictResponse |
|||
|
|||
@GET("/api/v1/friendships/{userId}/{type}/") |
|||
Call<String> getList(@Path("userId") long userId, |
|||
@Path("type") String type, // following or followers |
|||
@QueryMap(encoded = true) Map<String, String> queryParams); |
|||
suspend fun getList( |
|||
@Path("userId") userId: Long, |
|||
@Path("type") type: String, // following or followers |
|||
@QueryMap(encoded = true) queryParams: Map<String, String>, |
|||
): String |
|||
|
|||
@FormUrlEncoded |
|||
@POST("/api/v1/friendships/{action}/") |
|||
Call<FriendshipChangeResponse> changeMute(@Path("action") String action, |
|||
@FieldMap Map<String, String> form); |
|||
suspend fun changeMute( |
|||
@Path("action") action: String, |
|||
@FieldMap form: Map<String, String>, |
|||
): FriendshipChangeResponse |
|||
} |
@ -1,264 +1,155 @@ |
|||
package awais.instagrabber.webservices; |
|||
|
|||
import android.util.Log; |
|||
|
|||
import androidx.annotation.NonNull; |
|||
|
|||
import org.json.JSONArray; |
|||
import org.json.JSONException; |
|||
import org.json.JSONObject; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.Collections; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Objects; |
|||
|
|||
import awais.instagrabber.models.FollowModel; |
|||
import awais.instagrabber.repositories.FriendshipRepository; |
|||
import awais.instagrabber.repositories.responses.FriendshipChangeResponse; |
|||
import awais.instagrabber.repositories.responses.FriendshipListFetchResponse; |
|||
import awais.instagrabber.repositories.responses.FriendshipRestrictResponse; |
|||
import awais.instagrabber.utils.TextUtils; |
|||
import awais.instagrabber.utils.Utils; |
|||
import retrofit2.Call; |
|||
import retrofit2.Callback; |
|||
import retrofit2.Response; |
|||
|
|||
public class FriendshipService extends BaseService { |
|||
private static final String TAG = "FriendshipService"; |
|||
|
|||
private final FriendshipRepository repository; |
|||
private final String deviceUuid, csrfToken; |
|||
private final long userId; |
|||
|
|||
private static FriendshipService instance; |
|||
|
|||
private FriendshipService(final String deviceUuid, |
|||
final String csrfToken, |
|||
final long userId) { |
|||
this.deviceUuid = deviceUuid; |
|||
this.csrfToken = csrfToken; |
|||
this.userId = userId; |
|||
repository = RetrofitFactory.INSTANCE |
|||
.getRetrofit() |
|||
.create(FriendshipRepository.class); |
|||
} |
|||
|
|||
public String getCsrfToken() { |
|||
return csrfToken; |
|||
} |
|||
|
|||
public String getDeviceUuid() { |
|||
return deviceUuid; |
|||
} |
|||
|
|||
public long getUserId() { |
|||
return userId; |
|||
} |
|||
|
|||
public static FriendshipService getInstance(final String deviceUuid, final String csrfToken, final long userId) { |
|||
if (instance == null |
|||
|| !Objects.equals(instance.getCsrfToken(), csrfToken) |
|||
|| !Objects.equals(instance.getDeviceUuid(), deviceUuid) |
|||
|| !Objects.equals(instance.getUserId(), userId)) { |
|||
instance = new FriendshipService(deviceUuid, csrfToken, userId); |
|||
} |
|||
return instance; |
|||
} |
|||
|
|||
public void follow(final long targetUserId, |
|||
final ServiceCallback<FriendshipChangeResponse> callback) { |
|||
change("create", targetUserId, callback); |
|||
} |
|||
|
|||
public void unfollow(final long targetUserId, |
|||
final ServiceCallback<FriendshipChangeResponse> callback) { |
|||
change("destroy", targetUserId, callback); |
|||
} |
|||
|
|||
public void changeBlock(final boolean unblock, |
|||
final long targetUserId, |
|||
final ServiceCallback<FriendshipChangeResponse> callback) { |
|||
change(unblock ? "unblock" : "block", targetUserId, callback); |
|||
} |
|||
|
|||
public void toggleRestrict(final long targetUserId, |
|||
final boolean restrict, |
|||
final ServiceCallback<FriendshipRestrictResponse> callback) { |
|||
final Map<String, String> form = new HashMap<>(3); |
|||
form.put("_csrftoken", csrfToken); |
|||
form.put("_uuid", deviceUuid); |
|||
form.put("target_user_id", String.valueOf(targetUserId)); |
|||
final String action = restrict ? "restrict" : "unrestrict"; |
|||
final Call<FriendshipRestrictResponse> request = repository.toggleRestrict(action, form); |
|||
request.enqueue(new Callback<FriendshipRestrictResponse>() { |
|||
@Override |
|||
public void onResponse(@NonNull final Call<FriendshipRestrictResponse> call, |
|||
@NonNull final Response<FriendshipRestrictResponse> response) { |
|||
if (callback != null) { |
|||
callback.onSuccess(response.body()); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(@NonNull final Call<FriendshipRestrictResponse> call, |
|||
@NonNull final Throwable t) { |
|||
if (callback != null) { |
|||
callback.onFailure(t); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
|
|||
public void approve(final long targetUserId, |
|||
final ServiceCallback<FriendshipChangeResponse> callback) { |
|||
change("approve", targetUserId, callback); |
|||
} |
|||
|
|||
public void ignore(final long targetUserId, |
|||
final ServiceCallback<FriendshipChangeResponse> callback) { |
|||
change("ignore", targetUserId, callback); |
|||
} |
|||
|
|||
public void removeFollower(final long targetUserId, |
|||
final ServiceCallback<FriendshipChangeResponse> callback) { |
|||
change("remove_follower", targetUserId, callback); |
|||
} |
|||
|
|||
private void change(final String action, |
|||
final long targetUserId, |
|||
final ServiceCallback<FriendshipChangeResponse> callback) { |
|||
final Map<String, Object> form = new HashMap<>(5); |
|||
form.put("_csrftoken", csrfToken); |
|||
form.put("_uid", userId); |
|||
form.put("_uuid", deviceUuid); |
|||
form.put("radio_type", "wifi-none"); |
|||
form.put("user_id", targetUserId); |
|||
final Map<String, String> signedForm = Utils.sign(form); |
|||
final Call<FriendshipChangeResponse> request = repository.change(action, targetUserId, signedForm); |
|||
request.enqueue(new Callback<FriendshipChangeResponse>() { |
|||
@Override |
|||
public void onResponse(@NonNull final Call<FriendshipChangeResponse> call, |
|||
@NonNull final Response<FriendshipChangeResponse> response) { |
|||
if (callback != null) { |
|||
callback.onSuccess(response.body()); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(@NonNull final Call<FriendshipChangeResponse> call, |
|||
@NonNull final Throwable t) { |
|||
if (callback != null) { |
|||
callback.onFailure(t); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
|
|||
public void changeMute(final boolean unmute, |
|||
final long targetUserId, |
|||
final boolean story, // true for story, false for posts |
|||
final ServiceCallback<FriendshipChangeResponse> callback) { |
|||
final Map<String, String> form = new HashMap<>(4); |
|||
form.put("_csrftoken", csrfToken); |
|||
form.put("_uid", String.valueOf(userId)); |
|||
form.put("_uuid", deviceUuid); |
|||
form.put(story ? "target_reel_author_id" : "target_posts_author_id", String.valueOf(targetUserId)); |
|||
final Call<FriendshipChangeResponse> request = repository.changeMute(unmute ? |
|||
"unmute_posts_or_story_from_follow" : |
|||
"mute_posts_or_story_from_follow", |
|||
form); |
|||
request.enqueue(new Callback<FriendshipChangeResponse>() { |
|||
@Override |
|||
public void onResponse(@NonNull final Call<FriendshipChangeResponse> call, |
|||
@NonNull final Response<FriendshipChangeResponse> response) { |
|||
if (callback != null) { |
|||
callback.onSuccess(response.body()); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(@NonNull final Call<FriendshipChangeResponse> call, |
|||
@NonNull final Throwable t) { |
|||
if (callback != null) { |
|||
callback.onFailure(t); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
|
|||
public void getList(final boolean follower, |
|||
final long targetUserId, |
|||
final String maxId, |
|||
final ServiceCallback<FriendshipListFetchResponse> callback) { |
|||
final Map<String, String> queryMap = new HashMap<>(); |
|||
if (maxId != null) queryMap.put("max_id", maxId); |
|||
final Call<String> request = repository.getList( |
|||
targetUserId, |
|||
follower ? "followers" : "following", |
|||
queryMap); |
|||
request.enqueue(new Callback<String>() { |
|||
@Override |
|||
public void onResponse(@NonNull final Call<String> call, @NonNull final Response<String> response) { |
|||
try { |
|||
if (callback == null) { |
|||
return; |
|||
} |
|||
final String body = response.body(); |
|||
if (TextUtils.isEmpty(body)) { |
|||
callback.onSuccess(null); |
|||
return; |
|||
} |
|||
final FriendshipListFetchResponse friendshipListFetchResponse = parseListResponse(body); |
|||
callback.onSuccess(friendshipListFetchResponse); |
|||
} catch (JSONException e) { |
|||
Log.e(TAG, "onResponse", e); |
|||
callback.onFailure(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(@NonNull final Call<String> call, @NonNull final Throwable t) { |
|||
if (callback != null) { |
|||
callback.onFailure(t); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
|
|||
private FriendshipListFetchResponse parseListResponse(@NonNull final String body) throws JSONException { |
|||
final JSONObject root = new JSONObject(body); |
|||
final String nextMaxId = root.optString("next_max_id"); |
|||
final String status = root.optString("status"); |
|||
final JSONArray itemsJson = root.optJSONArray("users"); |
|||
final List<FollowModel> items = parseItems(itemsJson); |
|||
return new FriendshipListFetchResponse( |
|||
nextMaxId, |
|||
status, |
|||
items |
|||
); |
|||
} |
|||
|
|||
private List<FollowModel> parseItems(final JSONArray items) throws JSONException { |
|||
package awais.instagrabber.webservices |
|||
|
|||
import awais.instagrabber.models.FollowModel |
|||
import awais.instagrabber.repositories.FriendshipRepository |
|||
import awais.instagrabber.repositories.responses.FriendshipChangeResponse |
|||
import awais.instagrabber.repositories.responses.FriendshipListFetchResponse |
|||
import awais.instagrabber.repositories.responses.FriendshipRestrictResponse |
|||
import awais.instagrabber.utils.Utils |
|||
import awais.instagrabber.webservices.RetrofitFactory.retrofit |
|||
import org.json.JSONArray |
|||
import org.json.JSONException |
|||
import org.json.JSONObject |
|||
|
|||
object FriendshipService : BaseService() { |
|||
private val repository: FriendshipRepository = retrofit.create(FriendshipRepository::class.java) |
|||
|
|||
suspend fun follow( |
|||
csrfToken: String, |
|||
userId: Long, |
|||
deviceUuid: String, |
|||
targetUserId: Long, |
|||
): FriendshipChangeResponse = change(csrfToken, userId, deviceUuid, "create", targetUserId) |
|||
|
|||
suspend fun unfollow( |
|||
csrfToken: String, |
|||
userId: Long, |
|||
deviceUuid: String, |
|||
targetUserId: Long, |
|||
): FriendshipChangeResponse = change(csrfToken, userId, deviceUuid, "destroy", targetUserId) |
|||
|
|||
suspend fun changeBlock( |
|||
csrfToken: String, |
|||
userId: Long, |
|||
deviceUuid: String, |
|||
unblock: Boolean, |
|||
targetUserId: Long, |
|||
): FriendshipChangeResponse { |
|||
return change(csrfToken, userId, deviceUuid, if (unblock) "unblock" else "block", targetUserId) |
|||
} |
|||
|
|||
suspend fun toggleRestrict( |
|||
csrfToken: String, |
|||
deviceUuid: String, |
|||
targetUserId: Long, |
|||
restrict: Boolean, |
|||
): FriendshipRestrictResponse { |
|||
val form = mapOf( |
|||
"_csrftoken" to csrfToken, |
|||
"_uuid" to deviceUuid, |
|||
"target_user_id" to targetUserId.toString(), |
|||
) |
|||
val action = if (restrict) "restrict" else "unrestrict" |
|||
return repository.toggleRestrict(action, form) |
|||
} |
|||
|
|||
suspend fun approve( |
|||
csrfToken: String, |
|||
userId: Long, |
|||
deviceUuid: String, |
|||
targetUserId: Long, |
|||
): FriendshipChangeResponse = change(csrfToken, userId, deviceUuid, "approve", targetUserId) |
|||
|
|||
suspend fun ignore( |
|||
csrfToken: String, |
|||
userId: Long, |
|||
deviceUuid: String, |
|||
targetUserId: Long, |
|||
): FriendshipChangeResponse = change(csrfToken, userId, deviceUuid, "ignore", targetUserId) |
|||
|
|||
suspend fun removeFollower( |
|||
csrfToken: String, |
|||
userId: Long, |
|||
deviceUuid: String, |
|||
targetUserId: Long, |
|||
): FriendshipChangeResponse = change(csrfToken, userId, deviceUuid, "remove_follower", targetUserId) |
|||
|
|||
private suspend fun change( |
|||
csrfToken: String, |
|||
userId: Long, |
|||
deviceUuid: String, |
|||
action: String, |
|||
targetUserId: Long, |
|||
): FriendshipChangeResponse { |
|||
val form = mapOf( |
|||
"_csrftoken" to csrfToken, |
|||
"_uid" to userId, |
|||
"_uuid" to deviceUuid, |
|||
"radio_type" to "wifi-none", |
|||
"user_id" to targetUserId, |
|||
) |
|||
val signedForm = Utils.sign(form) |
|||
return repository.change(action, targetUserId, signedForm) |
|||
} |
|||
|
|||
suspend fun changeMute( |
|||
csrfToken: String, |
|||
userId: Long, |
|||
deviceUuid: String, |
|||
unmute: Boolean, |
|||
targetUserId: Long, |
|||
story: Boolean, // true for story, false for posts |
|||
): FriendshipChangeResponse { |
|||
val form = mapOf( |
|||
"_csrftoken" to csrfToken, |
|||
"_uid" to userId.toString(), |
|||
"_uuid" to deviceUuid, |
|||
(if (story) "target_reel_author_id" else "target_posts_author_id") to targetUserId.toString(), |
|||
) |
|||
return repository.changeMute( |
|||
if (unmute) "unmute_posts_or_story_from_follow" else "mute_posts_or_story_from_follow", |
|||
form |
|||
) |
|||
} |
|||
|
|||
suspend fun getList( |
|||
follower: Boolean, |
|||
targetUserId: Long, |
|||
maxId: String?, |
|||
): FriendshipListFetchResponse { |
|||
val queryMap = if (maxId != null) mapOf("max_id" to maxId) else emptyMap() |
|||
val response = repository.getList(targetUserId, if (follower) "followers" else "following", queryMap) |
|||
return parseListResponse(response) |
|||
} |
|||
|
|||
@Throws(JSONException::class) |
|||
private fun parseListResponse(body: String): FriendshipListFetchResponse { |
|||
val root = JSONObject(body) |
|||
val nextMaxId = root.optString("next_max_id") |
|||
val status = root.optString("status") |
|||
val itemsJson = root.optJSONArray("users") |
|||
val items = parseItems(itemsJson) |
|||
return FriendshipListFetchResponse( |
|||
nextMaxId, |
|||
status, |
|||
items |
|||
) |
|||
} |
|||
|
|||
@Throws(JSONException::class) |
|||
private fun parseItems(items: JSONArray?): List<FollowModel> { |
|||
if (items == null) { |
|||
return Collections.emptyList(); |
|||
return emptyList() |
|||
} |
|||
final List<FollowModel> followModels = new ArrayList<>(); |
|||
for (int i = 0; i < items.length(); i++) { |
|||
final JSONObject itemJson = items.optJSONObject(i); |
|||
if (itemJson == null) { |
|||
continue; |
|||
} |
|||
final FollowModel followModel = new FollowModel(itemJson.getString("pk"), |
|||
itemJson.getString("username"), |
|||
itemJson.optString("full_name"), |
|||
itemJson.getString("profile_pic_url")); |
|||
if (followModel != null) { |
|||
followModels.add(followModel); |
|||
} |
|||
val followModels = mutableListOf<FollowModel>() |
|||
for (i in 0 until items.length()) { |
|||
val itemJson = items.optJSONObject(i) ?: continue |
|||
val followModel = FollowModel(itemJson.getString("pk"), |
|||
itemJson.getString("username"), |
|||
itemJson.optString("full_name"), |
|||
itemJson.getString("profile_pic_url")) |
|||
followModels.add(followModel) |
|||
} |
|||
return followModels; |
|||
return followModels |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue