Manage conversations

This commit is contained in:
stom79 2018-10-29 15:49:13 +01:00
parent 0160588284
commit f934a4800f
6 changed files with 146 additions and 19 deletions

View File

@ -847,9 +847,22 @@ public abstract class BaseMainActivity extends BaseActivity
displayStatusFragment.scrollToTop();
break;
case 2:
if (display_direct)
updateTimeLine(RetrieveFeedsAsyncTask.Type.DIRECT, 0);
else if (display_local)
if (display_direct) {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String instanceVersion = sharedpreferences.getString(Helper.INSTANCE_VERSION + userId + instance, null);
if (instanceVersion != null) {
Version currentVersion = new Version(instanceVersion);
Version minVersion = new Version("2.6");
if (currentVersion.compareTo(minVersion) == 1 || currentVersion.equals(minVersion)) {
updateTimeLine(RetrieveFeedsAsyncTask.Type.CONVERSATION, 0);
} else {
updateTimeLine(RetrieveFeedsAsyncTask.Type.DIRECT, 0);
}
}else{
updateTimeLine(RetrieveFeedsAsyncTask.Type.DIRECT, 0);
}
}else if (display_local)
updateTimeLine(RetrieveFeedsAsyncTask.Type.LOCAL, 0);
else if (display_global)
updateTimeLine(RetrieveFeedsAsyncTask.Type.PUBLIC, 0);
@ -2210,7 +2223,20 @@ public abstract class BaseMainActivity extends BaseActivity
return notificationsFragment;
}else if( position == 2 && display_direct) {
statusFragment = new DisplayStatusFragment();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.DIRECT);
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String instanceVersion = sharedpreferences.getString(Helper.INSTANCE_VERSION + userId + instance, null);
if (instanceVersion != null) {
Version currentVersion = new Version(instanceVersion);
Version minVersion = new Version("2.6");
if (currentVersion.compareTo(minVersion) == 1 || currentVersion.equals(minVersion)) {
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.CONVERSATION);
} else {
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.DIRECT);
}
}else{
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.DIRECT);
}
statusFragment.setArguments(bundle);
return statusFragment;
}else if(position == 2 && display_local ){
@ -2373,7 +2399,7 @@ public abstract class BaseMainActivity extends BaseActivity
public void updateTimeLine(RetrieveFeedsAsyncTask.Type type, int value){
if( type == RetrieveFeedsAsyncTask.Type.DIRECT ){
if( type == RetrieveFeedsAsyncTask.Type.DIRECT || type == RetrieveFeedsAsyncTask.Type.CONVERSATION){
if (tabLayout.getTabAt(2) != null && display_direct) {
View tabDirect = tabLayout.getTabAt(2).getCustomView();
assert tabDirect != null;

View File

@ -60,6 +60,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
HOME,
LOCAL,
DIRECT,
CONVERSATION,
PUBLIC,
HASHTAG,
LIST,
@ -152,6 +153,9 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
case DIRECT:
apiResponse = api.getDirectTimeline(max_id);
break;
case CONVERSATION:
apiResponse = api.getConversationTimeline(max_id);
break;
case REMOTE_INSTANCE:
if( this.name != null && this.remoteInstance != null){ //For Peertube channels
apiResponse = api.getPeertubeChannelVideos(this.remoteInstance, this.name);

View File

@ -56,6 +56,7 @@ public class API {
private Attachment attachment;
private List<Account> accounts;
private List<Status> statuses;
private List<Conversation> conversations;
private int tootPerPage, accountPerPage, notificationPerPage;
private int actionCode;
private String instance;
@ -495,6 +496,54 @@ public class API {
return getDirectTimeline(max_id, null, tootPerPage);
}
/**
* Retrieves conversation timeline for the account *synchronously*
* @param max_id String id max
* @return APIResponse
*/
public APIResponse getConversationTimeline( String max_id) {
return getConversationTimeline(max_id, null, tootPerPage);
}
/**
* Retrieves conversation timeline for the account *synchronously*
* @param max_id String id max
* @param since_id String since the id
* @param limit int limit - max value 40
* @return APIResponse
*/
private APIResponse getConversationTimeline(String max_id, String since_id, int limit) {
HashMap<String, String> params = new HashMap<>();
if (max_id != null)
params.put("max_id", max_id);
if (since_id != null)
params.put("since_id", since_id);
if (0 > limit || limit > 80)
limit = 80;
params.put("limit",String.valueOf(limit));
conversations = new ArrayList<>();
try {
HttpsConnection httpsConnection = new HttpsConnection(context);
String response = httpsConnection.get(getAbsoluteUrl("/conversations"), 60, params, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
conversations = parseConversations(new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
apiResponse.setConversations(conversations);
return apiResponse;
}
/**
* Retrieves direct timeline for the account since an Id value *synchronously*
* @return APIResponse
@ -2576,6 +2625,46 @@ public class API {
return howToVideo;
}
/**
* Parse json response for several conversations
* @param jsonArray JSONArray
* @return List<Conversation>
*/
private List<Conversation> parseConversations(JSONArray jsonArray){
List<Conversation> conversations = new ArrayList<>();
try {
int i = 0;
while (i < jsonArray.length() ){
JSONObject resobj = jsonArray.getJSONObject(i);
Conversation conversation = parseConversation(context, resobj);
i++;
conversations.add(conversation);
}
} catch (JSONException e) {
setDefaultError(e);
}
return conversations;
}
/**
* Parse json response for unique conversation
* @param resobj JSONObject
* @return Conversation
*/
@SuppressWarnings("InfiniteRecursion")
private Conversation parseConversation(Context context, JSONObject resobj) {
Conversation conversation = new Conversation();
try {
conversation.setId(resobj.get("id").toString());
conversation.setUnread(Boolean.parseBoolean(resobj.get("unread").toString()));
conversation.setAccounts(parseAccountResponse(resobj.getJSONArray("accounts")));
conversation.setLast_status(parseStatuses(context, resobj.getJSONObject("last_status")));
}catch (JSONException ignored) {}
return conversation;
}
/**
* Parse json response for several status
* @param jsonArray JSONArray

View File

@ -28,6 +28,7 @@ public class APIResponse {
private List<Account> accounts = null;
private List<Status> statuses = null;
private List<Context> contexts = null;
private List<Conversation> conversations = null;
private List<Notification> notifications = null;
private List<Relationship> relationships = null;
private List<HowToVideo> howToVideos = null;
@ -159,4 +160,12 @@ public class APIResponse {
public void setPeertubes(List<Peertube> peertubes) {
this.peertubes = peertubes;
}
public List<Conversation> getConversations() {
return conversations;
}
public void setConversations(List<Conversation> conversations) {
this.conversations = conversations;
}
}

View File

@ -43,6 +43,7 @@ import fr.gouv.etalab.mastodon.asynctasks.RetrieveMissingFeedsAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrievePeertubeSearchAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Conversation;
import fr.gouv.etalab.mastodon.client.Entities.Peertube;
import fr.gouv.etalab.mastodon.client.Entities.RemoteInstance;
import fr.gouv.etalab.mastodon.drawers.PeertubeAdapter;
@ -366,6 +367,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
flag_loading = false;
return;
}
if( type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE && peertubeAdapater != null){
int previousPosition = this.peertubes.size();
if( max_id == null)
@ -381,6 +383,14 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
firstLoad = false;
flag_loading = false;
}else {
//Add conversation in status
if( type == RetrieveFeedsAsyncTask.Type.CONVERSATION ){
List<Conversation> conversations = apiResponse.getConversations();
List<Status> statusesConversations = new ArrayList<>();
for( Conversation conversation: conversations)
statusesConversations.add(conversation.getLast_status());
apiResponse.setStatuses(statusesConversations);
}
int previousPosition = this.statuses.size();
List<Status> statuses = Helper.filterToots(context, apiResponse.getStatuses(), mutedAccount, type);
List<Status> notFilteredStatuses = apiResponse.getStatuses();

View File

@ -1188,33 +1188,22 @@ public class Helper {
final NavigationView navigationView = activity.findViewById(R.id.nav_view);
navigationView.getMenu().clear();
MainActivity.lastNotificationId = null;
MainActivity.lastHomeId = null;
navigationView.inflateMenu(R.menu.activity_main_drawer);
SQLiteDatabase db = Sqlite.getInstance(activity, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account account = new AccountDAO(activity,db).getAccountByID(userID);
//Can happen when an account has been deleted and there is a click on an old notification
if( account == null)
return;
//Locked account can see follow request
if (account.isLocked()) {
navigationView.getMenu().findItem(R.id.nav_follow_request).setVisible(true);
} else {
navigationView.getMenu().findItem(R.id.nav_follow_request).setVisible(false);
}
SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, account.getToken());
editor.putString(Helper.PREF_KEY_ID, account.getId());
editor.putString(Helper.PREF_INSTANCE, account.getInstance().trim());
editor.apply();
activity.recreate();
if( checkItem ) {
Intent intent = new Intent(activity, MainActivity.class);
intent.putExtra(INTENT_ACTION, CHANGE_USER_INTENT);
activity.startActivity(intent);
}
Intent intent = activity.getIntent();
activity.finish();
activity.startActivity(intent);
}