Skip to content

Commit

Permalink
banning and reporting topics
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Sep 10, 2019
1 parent f1e6635 commit 070520a
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 77 deletions.
14 changes: 11 additions & 3 deletions app/src/main/java/co/tinode/tindroid/MessagesFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

Expand All @@ -58,6 +59,7 @@
import co.tinode.tinodesdk.NotConnectedException;
import co.tinode.tinodesdk.PromisedReply;
import co.tinode.tinodesdk.Storage;
import co.tinode.tinodesdk.Tinode;
import co.tinode.tinodesdk.Topic;
import co.tinode.tinodesdk.model.AccessChange;
import co.tinode.tinodesdk.model.Acs;
Expand Down Expand Up @@ -529,9 +531,15 @@ public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
break;

case R.id.buttonBlock:
Acs am = new Acs(mTopic.getAccessMode());
am.update(AccessChange.asWant("-JP"));
response = mTopic.setMeta(new MsgSetMeta<VxCard, PrivateType>(new MetaSetSub(am.getWant())));
mTopic.updateMode(null, "-JP");
break;

case R.id.buttonReport:
mTopic.updateMode(null, "-JP");
HashMap<String, Object> json = new HashMap<>();
json.put("action", "report");
json.put("tagret", mTopic.getName());
Cache.getTinode().publish(Tinode.TOPIC_SYS, new Drafty().attachJSON(json));
break;

default:
Expand Down
92 changes: 75 additions & 17 deletions app/src/main/java/co/tinode/tindroid/TopicInfoFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.android.flexbox.FlexboxLayout;

import java.util.Collection;
import java.util.HashMap;

import co.tinode.tindroid.account.ContactsManager;
import co.tinode.tindroid.db.StoredSubscription;
Expand All @@ -45,8 +46,10 @@
import co.tinode.tinodesdk.ComTopic;
import co.tinode.tinodesdk.NotConnectedException;
import co.tinode.tinodesdk.PromisedReply;
import co.tinode.tinodesdk.Tinode;
import co.tinode.tinodesdk.Topic;
import co.tinode.tinodesdk.model.Acs;
import co.tinode.tinodesdk.model.Drafty;
import co.tinode.tinodesdk.model.MsgSetMeta;
import co.tinode.tinodesdk.model.PrivateType;
import co.tinode.tinodesdk.model.ServerMessage;
Expand All @@ -61,8 +64,10 @@ public class TopicInfoFragment extends Fragment {

private static final String TAG = "TopicInfoFragment";

private static final int ACTION_REPORT = 3;
private static final int ACTION_REMOVE = 4;
private static final int ACTION_BAN = 5;
private static final int ACTION_BAN_TOPIC = 5;
private static final int ACTION_BAN_MEMBER = 6;

private ComTopic<VxCard> mTopic;
private MembersAdapter mAdapter;
Expand Down Expand Up @@ -128,7 +133,7 @@ public void onClick(View v) {
}
});

view.findViewById(R.id.buttonLeaveGroup).setOnClickListener(new View.OnClickListener() {
view.findViewById(R.id.buttonLeave).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTopic.isOwner()) {
Expand All @@ -150,6 +155,32 @@ public void onClick(View v) {
}
});

view.findViewById(R.id.buttonBlock).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
VxCard pub = mTopic.getPub();
String topicTitle = pub != null ? pub.fn : null;
topicTitle = TextUtils.isEmpty(topicTitle) ?
activity.getString(R.string.placeholder_topic_title) :
topicTitle;
showConfirmationDialog(topicTitle, null, null, R.string.confirm_contact_ban, ACTION_BAN_TOPIC);
}
});

final View.OnClickListener reportListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
VxCard pub = mTopic.getPub();
String topicTitle = pub != null ? pub.fn : null;
topicTitle = TextUtils.isEmpty(topicTitle) ?
activity.getString(R.string.placeholder_topic_title) :
topicTitle;
showConfirmationDialog(topicTitle, null, null, R.string.confirm_report, ACTION_REPORT);
}
};
view.findViewById(R.id.buttonReportContact).setOnClickListener(reportListener);
view.findViewById(R.id.buttonReportGroup).setOnClickListener(reportListener);

view.findViewById(R.id.buttonAddMembers).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -209,6 +240,11 @@ public void onResume() {

String name = bundle.getString("topic");
mTopic = (ComTopic<VxCard>) Cache.getTinode().getTopic(name);
if (mTopic == null) {
Log.d(TAG, "TopicInfo resumed with null topic.");
activity.finish();
return;
}

final TextView title = activity.findViewById(R.id.topicTitle);
final TextView subtitle = activity.findViewById(R.id.topicSubtitle);
Expand All @@ -218,6 +254,8 @@ public void onResume() {
final View defaultPermissions = activity.findViewById(R.id.defaultPermissionsWrapper);
final View uploadAvatarButton = activity.findViewById(R.id.uploadAvatar);
final View tagManager = activity.findViewById(R.id.tagsManagerWrapper);
final View reportGroup = activity.findViewById(R.id.buttonReportGroup);
final View reportContact = activity.findViewById(R.id.buttonReportContact);

// Launch edit dialog when title or subtitle is clicked.
final View.OnClickListener l = new View.OnClickListener() {
Expand All @@ -239,15 +277,18 @@ public void onClick(View v) {
uploadAvatarButton.setVisibility(mTopic.isManager() ? View.VISIBLE : View.GONE);

groupMembers.setVisibility(View.VISIBLE);
reportContact.setVisibility(View.GONE);

activity.findViewById(R.id.singleUserPermissions).setVisibility(View.VISIBLE);
activity.findViewById(R.id.p2pPermissions).setVisibility(View.GONE);

Button button = activity.findViewById(R.id.buttonLeaveGroup);
Button button = activity.findViewById(R.id.buttonLeave);
if (mTopic.isOwner()) {
button.setEnabled(false);
button.setAlpha(0.5f);

reportGroup.setVisibility(View.GONE);

tagManager.setVisibility(View.VISIBLE);
tagManager.findViewById(R.id.buttonManageTags)
.setOnClickListener(new View.OnClickListener() {
Expand All @@ -274,6 +315,8 @@ public void onClick(View view) {
button.setEnabled(true);
button.setAlpha(1f);

reportGroup.setVisibility(View.VISIBLE);

tagManager.setVisibility(View.GONE);
}

Expand All @@ -296,6 +339,8 @@ public void onClick(View view) {
uploadAvatarButton.setVisibility(View.GONE);

groupMembers.setVisibility(View.GONE);
reportGroup.setVisibility(View.GONE);
reportContact.setVisibility(View.VISIBLE);

tagManager.setVisibility(View.GONE);

Expand Down Expand Up @@ -364,28 +409,41 @@ public void onClick(DialogInterface dialog, int which) {
}

// Confirmation dialog "Do you really want to do X?"
private void showConfirmationDialog(final String topicTitle,
final String title, final String uid, int message_id, final int what) {
// uid - user to apply action to
// message_id - id of the string resource to use as an explanation.
// what - action to take on success, ACTION_*
private void showConfirmationDialog(final String arg1, final String arg2,
final String uid, int message_id, final int what) {
final Activity activity = getActivity();
if (activity == null) {
return;
}

final AlertDialog.Builder confirmBuilder = new AlertDialog.Builder(activity);
confirmBuilder.setNegativeButton(android.R.string.no, null);
String message = activity.getString(message_id, title, topicTitle);
String message = activity.getString(message_id, arg1, arg2);
confirmBuilder.setMessage(message);

confirmBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
switch (what) {
case ACTION_REPORT:
HashMap<String, Object> json = new HashMap<>();
json.put("action", "report");
json.put("target", mTopic.getName());
Cache.getTinode().publish(Tinode.TOPIC_SYS, new Drafty().attachJSON(json));
mTopic.updateMode(null, "-JP").thenCatch(mFailureListener);
break;
case ACTION_REMOVE:
mTopic.eject(uid, false).thenApply(null, mFailureListener);
mTopic.eject(uid, false).thenCatch(mFailureListener);
break;
case ACTION_BAN_TOPIC:
mTopic.updateMode(null, "-JP").thenCatch(mFailureListener);
break;
case ACTION_BAN:
mTopic.eject(uid, true).thenApply(null, mFailureListener);
case ACTION_BAN_MEMBER:
mTopic.eject(uid, true).thenCatch(mFailureListener);
break;
}
} catch (NotConnectedException ignored) {
Expand All @@ -399,7 +457,7 @@ public void onClick(DialogInterface dialog, int which) {
}

// Dialog-menu with actions for individual subscribers, like "send message", "change permissions", "ban", etc.
private void showMemberAction(final String topicTitle, final String title, final String uid, final String mode) {
private void showMemberAction(final String topicTitle, final String userTitle, final String uid, final String mode) {
final Activity activity = getActivity();
if (activity == null) {
return;
Expand All @@ -409,19 +467,19 @@ private void showMemberAction(final String topicTitle, final String title, final
return;
}

final String titleFixed = TextUtils.isEmpty(title) ?
final String userTitleFixed = TextUtils.isEmpty(userTitle) ?
activity.getString(R.string.placeholder_contact_title) :
title;
userTitle;
final String topicTitleFixed = TextUtils.isEmpty(topicTitle) ?
activity.getString(R.string.placeholder_topic_title) :
topicTitle;

AlertDialog.Builder actionBuilder = new AlertDialog.Builder(activity);
final LinearLayout actions = (LinearLayout) View.inflate(activity, R.layout.dialog_member_actions, null);
actionBuilder
.setTitle(TextUtils.isEmpty(title) ?
.setTitle(TextUtils.isEmpty(userTitle) ?
activity.getString(R.string.placeholder_contact_title) :
title)
userTitle)
.setView(actions)
.setCancelable(true)
.setNegativeButton(android.R.string.cancel, null);
Expand Down Expand Up @@ -454,13 +512,13 @@ public void onClick(View v) {
mTopic.updateMode(uid, "+O").thenApply(null, mFailureListener);
break;
case R.id.buttonRemove: {
showConfirmationDialog(topicTitleFixed, titleFixed, uid,
showConfirmationDialog(userTitleFixed, topicTitleFixed, uid,
R.string.confirm_member_removal, ACTION_REMOVE);
break;
}
case R.id.buttonBlock: {
showConfirmationDialog(topicTitleFixed, titleFixed, uid,
R.string.confirm_member_ban, ACTION_BAN);
showConfirmationDialog(userTitleFixed, topicTitleFixed, uid,
R.string.confirm_member_ban, ACTION_BAN_MEMBER);
break;
}
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_report_gray.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF757575" android:pathData="M15.73,3L8.27,3L3,8.27v7.46L8.27,21h7.46L21,15.73L21,8.27L15.73,3zM12,17.3c-0.72,0 -1.3,-0.58 -1.3,-1.3 0,-0.72 0.58,-1.3 1.3,-1.3 0.72,0 1.3,0.58 1.3,1.3 0,0.72 -0.58,1.3 -1.3,1.3zM13,13h-2L11,7h2v6z"/>
</vector>
18 changes: 18 additions & 0 deletions app/src/main/res/layout/dialog_accept_chat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,22 @@
app:layout_constraintTop_toBottomOf="@id/buttonIgnore"
tools:ignore="RtlSymmetry"/>

<androidx.appcompat.widget.AppCompatTextView
style="@style/textSecondary"
android:id="@+id/buttonReport"
android:background="?attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:clickable="true"
android:drawablePadding="4sp"
app:drawableStartCompat="@drawable/ic_report_gray"
android:focusable="true"
android:gravity="center_vertical"
android:text="@string/new_chat_report"
android:paddingStart="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/buttonBlock"
tools:ignore="RtlSymmetry"/>

</androidx.constraintlayout.widget.ConstraintLayout>
50 changes: 26 additions & 24 deletions app/src/main/res/layout/fragment_account_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,35 @@
android:textIsSelectable="true"
tools:text="Not sure what this should be"/>

<LinearLayout
android:id="@+id/topicAddressWrapper"
android:layout_below="@id/imageAvatar"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
style="@style/textPrimary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/address"/>

<TextView
android:id="@+id/topicAddress"
style="@style/textPrimarySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:textIsSelectable="true"
android:typeface="monospace"
tools:text="usrfv2qrUr6vAd"/>

</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="@id/imageAvatar"
android:layout_below="@id/topicAddressWrapper"
android:layout_marginTop="@dimen/activity_padding"
android:layout_marginBottom="@dimen/activity_padding"
android:background="@color/grey"/>
Expand Down Expand Up @@ -200,29 +225,6 @@
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
style="@style/textPrimary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/address"/>

<TextView
android:id="@+id/topicAddress"
style="@style/textPrimarySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:textIsSelectable="true"
android:typeface="monospace"
tools:text="usrfv2qrUr6vAd"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
Expand Down
Loading

0 comments on commit 070520a

Please sign in to comment.