Follow/unfollow accounts/channels - #692

This commit is contained in:
stom79 2019-01-04 11:48:15 +01:00
parent 3d70ceaad9
commit 15abff8c2b
5 changed files with 101 additions and 100 deletions

View File

@ -262,10 +262,7 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
}
String accountIdRelation = accountId;
if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE) {
if( !ischannel)
accountIdRelation = account.getUuid() + "@" + account.getHost();
else
accountIdRelation = account.getUuid() + "@" + account.getHost();
accountIdRelation = account.getAcct();
}
new RetrieveRelationshipAsyncTask(getApplicationContext(), accountIdRelation, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
@ -280,7 +277,7 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
.load(urlHeader)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
ImageView banner_pp = findViewById(R.id.banner_pp);
banner_pp.setImageBitmap(resource);
if( theme == THEME_LIGHT){
@ -738,6 +735,10 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
}
});
//Follow button
String target = account.getId();
if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE)
target = account.getAcct();
String finalTarget = target;
account_follow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -745,13 +746,13 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
Toasty.info(getApplicationContext(), getString(R.string.nothing_to_do), Toast.LENGTH_LONG).show();
}else if( doAction == action.FOLLOW){
account_follow.setEnabled(false);
new PostActionAsyncTask(getApplicationContext(), API.StatusAction.FOLLOW, account.getId(), ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new PostActionAsyncTask(getApplicationContext(), API.StatusAction.FOLLOW, finalTarget, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}else if( doAction == action.UNFOLLOW){
account_follow.setEnabled(false);
new PostActionAsyncTask(getApplicationContext(), API.StatusAction.UNFOLLOW, account.getId(), ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new PostActionAsyncTask(getApplicationContext(), API.StatusAction.UNFOLLOW, finalTarget, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}else if( doAction == action.UNBLOCK){
account_follow.setEnabled(false);
new PostActionAsyncTask(getApplicationContext(), API.StatusAction.UNBLOCK, account.getId(), ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new PostActionAsyncTask(getApplicationContext(), API.StatusAction.UNBLOCK, finalTarget, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
});
@ -1192,17 +1193,23 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
}
@Override
public void onPostAction(int statusCode,API.StatusAction statusAction, String targetedId, Error error) {
Log.v(Helper.TAG,statusAction + " * " + statusCode + " - error4: " + error);
if( error != null){
Toasty.error(getApplicationContext(), error.getError(),Toast.LENGTH_LONG).show();
return;
}
Helper.manageMessageStatusCode(getApplicationContext(), statusCode, statusAction);
new RetrieveRelationshipAsyncTask(getApplicationContext(), account.getId(),ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
String target = account.getId();
if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE)
target = account.getAcct();
new RetrieveRelationshipAsyncTask(getApplicationContext(), target,ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
public void onRetrieveAccount(final Account account, Error error) {
if( error != null || account.getAcct() == null){
if( error == null)
Toasty.error(ShowAccountActivity.this, getString(R.string.toast_error),Toast.LENGTH_LONG).show();

View File

@ -20,9 +20,12 @@ import android.os.AsyncTask;
import java.lang.ref.WeakReference;
import java.util.List;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.client.Entities.Results;
import fr.gouv.etalab.mastodon.client.PeertubeAPI;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
@ -40,12 +43,11 @@ public class PostActionAsyncTask extends AsyncTask<Void, Void, Void> {
private String targetedId;
private String comment;
private fr.gouv.etalab.mastodon.client.Entities.Status status;
private API api;
private Account account, remoteAccount;
private fr.gouv.etalab.mastodon.client.Entities.Status remoteStatus;
private WeakReference<Context> contextReference;
private boolean muteNotifications;
private Error error;
public PostActionAsyncTask(Context context, API.StatusAction apiAction, String targetedId, OnPostActionInterface onPostActionInterface){
@ -98,60 +100,76 @@ public class PostActionAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
//Remote action
if (account != null)
api = new API(contextReference.get(), account.getInstance(), account.getToken());
else
api = new API(contextReference.get());
if (remoteStatus != null) {
String uri;
if (remoteStatus.getReblog() != null) {
if (remoteStatus.getReblog().getUri().startsWith("http"))
uri = remoteStatus.getReblog().getUri();
else
uri = remoteStatus.getReblog().getUrl();
} else {
if (remoteStatus.getUri().startsWith("http"))
uri = remoteStatus.getUri();
else
uri = remoteStatus.getUrl();
}
Results search = api.search(uri);
if (search != null) {
List<fr.gouv.etalab.mastodon.client.Entities.Status> remoteStatuses = search.getStatuses();
if (remoteStatuses != null && remoteStatuses.size() > 0) {
fr.gouv.etalab.mastodon.client.Entities.Status statusTmp = remoteStatuses.get(0);
this.targetedId = statusTmp.getId();
statusCode = api.postAction(apiAction, targetedId);
}
}
}else if(remoteAccount != null){
String searchString = remoteAccount.getAcct().contains("@")?"@" + remoteAccount.getAcct():"@" + remoteAccount.getAcct() + "@" + Helper.getLiveInstance(contextReference.get());
Results search = api.search(searchString);
if (search != null) {
List<Account> accounts = search.getAccounts();
if (accounts != null && accounts.size() > 0) {
Account accountTmp = accounts.get(0);
this.targetedId = accountTmp.getId();
statusCode = api.postAction(apiAction, targetedId);
}
}
}else {
if (apiAction == API.StatusAction.REPORT)
statusCode = api.reportAction(status, comment);
else if (apiAction == API.StatusAction.CREATESTATUS)
statusCode = api.statusAction(status);
else if( apiAction == API.StatusAction.MUTE_NOTIFICATIONS)
statusCode = api.muteNotifications(targetedId, muteNotifications);
if(MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON) {
//Remote action
API api;
if (account != null)
api = new API(contextReference.get(), account.getInstance(), account.getToken());
else
statusCode = api.postAction(apiAction, targetedId);
api = new API(contextReference.get());
if (remoteStatus != null) {
String uri;
if (remoteStatus.getReblog() != null) {
if (remoteStatus.getReblog().getUri().startsWith("http"))
uri = remoteStatus.getReblog().getUri();
else
uri = remoteStatus.getReblog().getUrl();
} else {
if (remoteStatus.getUri().startsWith("http"))
uri = remoteStatus.getUri();
else
uri = remoteStatus.getUrl();
}
Results search = api.search(uri);
if (search != null) {
List<fr.gouv.etalab.mastodon.client.Entities.Status> remoteStatuses = search.getStatuses();
if (remoteStatuses != null && remoteStatuses.size() > 0) {
fr.gouv.etalab.mastodon.client.Entities.Status statusTmp = remoteStatuses.get(0);
this.targetedId = statusTmp.getId();
statusCode = api.postAction(apiAction, targetedId);
}
}
} else if (remoteAccount != null) {
String searchString = remoteAccount.getAcct().contains("@") ? "@" + remoteAccount.getAcct() : "@" + remoteAccount.getAcct() + "@" + Helper.getLiveInstance(contextReference.get());
Results search = api.search(searchString);
if (search != null) {
List<Account> accounts = search.getAccounts();
if (accounts != null && accounts.size() > 0) {
Account accountTmp = accounts.get(0);
this.targetedId = accountTmp.getId();
statusCode = api.postAction(apiAction, targetedId);
}
}
} else {
if (apiAction == API.StatusAction.REPORT)
statusCode = api.reportAction(status, comment);
else if (apiAction == API.StatusAction.CREATESTATUS)
statusCode = api.statusAction(status);
else if (apiAction == API.StatusAction.MUTE_NOTIFICATIONS)
statusCode = api.muteNotifications(targetedId, muteNotifications);
else
statusCode = api.postAction(apiAction, targetedId);
}
error = api.getError();
}else if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE){
//Remote action
PeertubeAPI peertubeAPI;
if (account != null)
peertubeAPI = new PeertubeAPI(contextReference.get(), account.getInstance(), account.getToken());
else
peertubeAPI = new PeertubeAPI(contextReference.get());
if( apiAction == API.StatusAction.FOLLOW || apiAction == API.StatusAction.UNFOLLOW)
statusCode = peertubeAPI.postAction(apiAction, targetedId);
error = peertubeAPI.getError();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
listener.onPostAction(statusCode, apiAction, targetedId, api.getError());
listener.onPostAction(statusCode, apiAction, targetedId, error);
}
}

View File

@ -1560,7 +1560,6 @@ public class HttpsConnection {
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
httpsURLConnection.getOutputStream().write(postDataBytes);
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
getSinceMaxId();
httpsURLConnection.getInputStream().close();

View File

@ -85,37 +85,7 @@ public class PeertubeAPI {
private Error APIError;
private List<String> domains;
public enum StatusAction{
FAVOURITE,
UNFAVOURITE,
REBLOG,
UNREBLOG,
MUTE,
MUTE_NOTIFICATIONS,
UNMUTE,
BLOCK,
UNBLOCK,
FOLLOW,
UNFOLLOW,
CREATESTATUS,
UNSTATUS,
AUTHORIZE,
REJECT,
REPORT,
REMOTE_FOLLOW,
PIN,
UNPIN,
ENDORSE,
UNENDORSE,
SHOW_BOOST,
HIDE_BOOST,
BLOCK_DOMAIN
}
public enum accountPrivacy {
PUBLIC,
LOCKED
}
public PeertubeAPI(Context context) {
this.context = context;
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
@ -189,7 +159,7 @@ public class PeertubeAPI {
* Update credential of the authenticated user *synchronously*
* @return APIResponse
*/
public APIResponse updateCredential(String display_name, String note, ByteArrayInputStream avatar, String avatarName, ByteArrayInputStream header, String headerName, accountPrivacy privacy, HashMap<String, String> customFields) {
public APIResponse updateCredential(String display_name, String note, ByteArrayInputStream avatar, String avatarName, ByteArrayInputStream header, String headerName, API.accountPrivacy privacy, HashMap<String, String> customFields) {
HashMap<String, String> requestParams = new HashMap<>();
if( display_name != null)
@ -205,7 +175,7 @@ public class PeertubeAPI {
requestParams.put("note",note);
}
if( privacy != null)
requestParams.put("locked",privacy== accountPrivacy.LOCKED?"true":"false");
requestParams.put("locked",privacy== API.accountPrivacy.LOCKED?"true":"false");
int i = 0;
if( customFields != null && customFields.size() > 0){
Iterator it = customFields.entrySet().iterator();
@ -293,7 +263,7 @@ public class PeertubeAPI {
HashMap<String, String> params = new HashMap<>();
params.put("uris", uri);
List<Relationship> relationships = new ArrayList<>();
try {
HttpsConnection httpsConnection = new HttpsConnection(context);
String response = httpsConnection.get(getAbsoluteUrl("/users/me/subscriptions/exist"), 60, params, prefKeyOauthTokenT);
@ -1344,7 +1314,7 @@ public class PeertubeAPI {
* @param targetedId String id of the targeted Id *can be this of a status or an account*
* @return in status code - Should be equal to 200 when action is done
*/
public int postAction(StatusAction statusAction, String targetedId){
public int postAction(API.StatusAction statusAction, String targetedId){
return postAction(statusAction, targetedId, null, null);
}
@ -1382,11 +1352,11 @@ public class PeertubeAPI {
* @return in status code - Should be equal to 200 when action is done
*/
public int reportAction(Status status, String comment){
return postAction(PeertubeAPI.StatusAction.REPORT, null, status, comment);
return postAction(API.StatusAction.REPORT, null, status, comment);
}
public int statusAction(Status status){
return postAction(StatusAction.CREATESTATUS, null, status, null);
return postAction(API.StatusAction.CREATESTATUS, null, status, null);
}
/**
@ -1397,9 +1367,10 @@ public class PeertubeAPI {
* @param comment String comment for the report
* @return in status code - Should be equal to 200 when action is done
*/
private int postAction(StatusAction statusAction, String targetedId, Status status, String comment ){
private int postAction(API.StatusAction statusAction, String targetedId, Status status, String comment ){
String action;
String actionCall = "POST";
HashMap<String, String> params = null;
switch (statusAction){
case FAVOURITE:
@ -1415,7 +1386,9 @@ public class PeertubeAPI {
action = String.format("/statuses/%s/unreblog", targetedId);
break;
case FOLLOW:
action = String.format("/users/me/subscriptions/%s", targetedId);
action = "/users/me/subscriptions";
params = new HashMap<>();
params.put("uri", targetedId);
break;
case REMOTE_FOLLOW:
action = "/follows";
@ -1424,6 +1397,7 @@ public class PeertubeAPI {
break;
case UNFOLLOW:
action = String.format("/users/me/subscriptions/%s", targetedId);
actionCall = "DELETE";
break;
case BLOCK:
action = String.format("/accounts/%s/block", targetedId);
@ -1510,11 +1484,14 @@ public class PeertubeAPI {
default:
return -1;
}
if(statusAction != StatusAction.UNSTATUS ) {
if(statusAction != API.StatusAction.UNSTATUS ) {
try {
HttpsConnection httpsConnection = new HttpsConnection(context);
httpsConnection.post(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT);
if( actionCall.equals("POST"))
httpsConnection.post(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT);
else if( actionCall.equals("DELETE"))
httpsConnection.delete(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT);
actionCode = httpsConnection.getActionCode();
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);

View File

@ -739,7 +739,7 @@ public class Helper {
*/
public static void manageMessageStatusCode(Context context, int statusCode,API.StatusAction statusAction){
String message = "";
if( statusCode == 200){
if( statusCode >= 200 && statusCode < 400){
if( statusAction == API.StatusAction.BLOCK){
message = context.getString(R.string.toast_block);
}else if(statusAction == API.StatusAction.UNBLOCK){