Skip to content

Commit

Permalink
Adding user and community blocking. Fixes #71 Fixes #58 (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines authored Feb 3, 2022
1 parent 2822c0d commit 25c84ce
Show file tree
Hide file tree
Showing 20 changed files with 578 additions and 128 deletions.
57 changes: 44 additions & 13 deletions app/src/main/java/com/jerboa/api/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ interface API {
@POST("post/report")
suspend fun createPostReport(@Body form: CreatePostReport): Response<PostReportResponse>

/**
* Block a person.
*/
@POST("user/block")
suspend fun blockPerson(@Body form: BlockPerson): Response<BlockPersonResponse>

/**
* Block a community.
*/
@POST("community/block")
suspend fun blockCommunity(@Body form: BlockCommunity): Response<BlockCommunityResponse>

/**
* Upload an image.
*/
Expand Down Expand Up @@ -637,6 +649,38 @@ suspend fun createPostReportWrapper(
return createdReport
}

suspend fun blockPersonWrapper(
form: BlockPerson,
ctx: Context,
): BlockPersonResponse? {

var blockPersonRes: BlockPersonResponse? = null
val api = API.getInstance()

try {
blockPersonRes = retrofitErrorHandler(api.blockPerson(form))
} catch (e: Exception) {
toastException(ctx = ctx, error = e)
}
return blockPersonRes
}

suspend fun blockCommunityWrapper(
form: BlockCommunity,
ctx: Context,
): BlockCommunityResponse? {

var blockCommunityRes: BlockCommunityResponse? = null
val api = API.getInstance()

try {
blockCommunityRes = retrofitErrorHandler(api.blockCommunity(form))
} catch (e: Exception) {
toastException(ctx = ctx, error = e)
}
return blockCommunityRes
}

suspend fun uploadPictrsImage(account: Account, imageIs: InputStream, ctx: Context): String? {
var imageUrl: String? = null
val api = API.getInstance()
Expand Down Expand Up @@ -750,12 +794,6 @@ fun <T> retrofitErrorHandler(res: Response<T>): T {
// }
//
//
// /**
// * Block a community.
// */
// async blockCommunity(form: BlockCommunity): Promise<BlockCommunityResponse> {
// return this.wrapper(HttpType.Post, "/community/block", form);
// }
//
// /**
// * Delete a community.
Expand Down Expand Up @@ -933,13 +971,6 @@ fun <T> retrofitErrorHandler(res: Response<T>): T {
// return this.wrapper(HttpType.Post, "/user/ban", form);
// }
//
// /**
// * Block a person.
// */
// async blockPerson(form: BlockPerson): Promise<BlockPersonResponse> {
// return this.wrapper(HttpType.Post, "/user/block", form);
// }

// /**
// * Verify your email
// */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ fun CommentNode(
onCommunityClick: (community: CommunitySafe) -> Unit,
onPostClick: (postId: Int) -> Unit,
onReportClick: (commentView: CommentView) -> Unit,
onBlockCreatorClick: (creator: PersonSafe) -> Unit,
showPostAndCommunityContext: Boolean = false,
showRead: Boolean = false,
account: Account?,
Expand Down Expand Up @@ -198,6 +199,7 @@ fun CommentNode(
onSaveClick = onSaveClick,
onMarkAsReadClick = onMarkAsReadClick,
onReportClick = onReportClick,
onBlockCreatorClick = onBlockCreatorClick,
showRead = showRead,
myVote = myVote.value,
upvotes = upvotes.value,
Expand Down Expand Up @@ -227,6 +229,7 @@ fun CommentNode(
onReportClick = onReportClick,
showRead = showRead,
onReplyClick = onReplyClick,
onBlockCreatorClick = onBlockCreatorClick,
account = account,
moderators = moderators,
)
Expand Down Expand Up @@ -281,6 +284,7 @@ fun CommentFooterLine(
onViewSourceClick: () -> Unit,
onEditCommentClick: (commentView: CommentView) -> Unit,
onReportClick: (commentView: CommentView) -> Unit,
onBlockCreatorClick: (creator: PersonSafe) -> Unit,
showRead: Boolean = false,
myVote: Int?,
upvotes: Int,
Expand All @@ -306,6 +310,10 @@ fun CommentFooterLine(
showMoreOptions = false
onReportClick(commentView)
},
onBlockCreatorClick = {
showMoreOptions = false
onBlockCreatorClick(commentView.creator)
},
isCreator = account?.id == commentView.creator.id,
)
}
Expand Down Expand Up @@ -395,6 +403,7 @@ fun CommentNodesPreview() {
onReplyClick = {},
onSaveClick = {},
onUpvoteClick = {},
onBlockCreatorClick = {},
)
}

Expand All @@ -404,6 +413,7 @@ fun CommentOptionsDialog(
onViewSourceClick: () -> Unit,
onEditCommentClick: () -> Unit,
onReportClick: () -> Unit,
onBlockCreatorClick: () -> Unit,
isCreator: Boolean,
commentView: CommentView,
) {
Expand Down Expand Up @@ -435,6 +445,11 @@ fun CommentOptionsDialog(
icon = Icons.Default.Flag,
onClick = onReportClick,
)
IconAndTextDrawerItem(
text = "Block ${commentView.creator.name}",
icon = Icons.Default.Block,
onClick = onBlockCreatorClick,
)
}
if (isCreator) {
IconAndTextDrawerItem(
Expand All @@ -458,7 +473,8 @@ fun CommentOptionsDialogPreview() {
onDismissRequest = {},
onEditCommentClick = {},
onReportClick = {},
onViewSourceClick = {}
onViewSourceClick = {},
onBlockCreatorClick = {},
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package com.jerboa.ui.components.comment
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import com.jerboa.CommentNodeData
import com.jerboa.datatypes.CommentView
import com.jerboa.datatypes.CommunityModeratorView
import com.jerboa.datatypes.CommunitySafe
import com.jerboa.datatypes.SortType
import com.jerboa.datatypes.*
import com.jerboa.db.Account
import com.jerboa.sortNodes

Expand All @@ -22,6 +19,7 @@ fun CommentNodes(
onReportClick: (commentView: CommentView) -> Unit,
onPersonClick: (personId: Int) -> Unit,
onCommunityClick: (community: CommunitySafe) -> Unit,
onBlockCreatorClick: (creator: PersonSafe) -> Unit,
onPostClick: (postId: Int) -> Unit,
account: Account? = null,
moderators: List<CommunityModeratorView>,
Expand All @@ -44,6 +42,7 @@ fun CommentNodes(
onPostClick = onPostClick,
onEditCommentClick = onEditCommentClick,
onReportClick = onReportClick,
onBlockCreatorClick = onBlockCreatorClick,
showPostAndCommunityContext = showPostAndCommunityContext,
showRead = showRead,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,6 @@ fun markPersonMentionAsReadRoutine(
}
}

fun markPrivateMessageAsReadRoutine(
privateMessageView: MutableState<PrivateMessageView?>,
messages: MutableList<PrivateMessageView>? = null,
account: Account,
ctx: Context,
scope: CoroutineScope,
) {
scope.launch {
privateMessageView.value?.also { pmv ->
val updatedPmv = markPrivateMessageAsReadWrapper(
pmv,
account,
ctx,
)?.private_message_view
privateMessageView.value = updatedPmv
messages?.also {
findAndUpdatePrivateMessage(messages, updatedPmv)
}
}
}
}

fun createCommentRoutine(
loading: MutableState<Boolean>,
content: String,
Expand Down Expand Up @@ -269,20 +247,6 @@ fun findAndUpdateMention(
}
}

fun findAndUpdatePrivateMessage(
messages: MutableList<PrivateMessageView>,
updatedMessageView: PrivateMessageView?
) {
updatedMessageView?.also { ucv ->
val foundIndex = messages.indexOfFirst {
it.private_message.id == ucv.private_message.id
}
if (foundIndex != -1) {
messages[foundIndex] = ucv
}
}
}

fun fetchRepliesRoutine(
replies: MutableList<CommentView>,
loading: MutableState<Boolean>,
Expand Down Expand Up @@ -406,60 +370,3 @@ fun fetchPersonMentionsRoutine(
}
}
}

fun fetchPrivateMessagesRoutine(
messages: MutableList<PrivateMessageView>,
loading: MutableState<Boolean>,
page: MutableState<Int>,
unreadOnly: MutableState<Boolean>,
nextPage: Boolean = false,
clear: Boolean = false,
changeUnreadOnly: Boolean? = null,
account: Account,
ctx: Context,
scope: CoroutineScope,
) {
scope.launch {
val api = API.getInstance()
try {
loading.value = true

if (nextPage) {
page.value++
}

if (clear) {
page.value = 1
}

changeUnreadOnly?.also {
unreadOnly.value = it
}

val form = GetPrivateMessages(
page = page.value,
unread_only = unreadOnly.value,
auth = account.jwt,
)
Log.d(
"jerboa",
"Fetching unread replies: $form"
)
val newMessages = retrofitErrorHandler(
api.getPrivateMessages(
form = form
.serializeToMap()
)
).private_messages

if (clear) {
messages.clear()
}
messages.addAll(newMessages)
} catch (e: Exception) {
toastException(ctx = ctx, error = e)
} finally {
loading.value = false
}
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/jerboa/ui/components/community/Community.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ fun CommunityTopSectionPreview() {
fun CommunityHeader(
communityName: String,
onClickSortType: (SortType) -> Unit,
onBlockCommunityClick: () -> Unit,
selectedSortType: SortType,
navController: NavController = rememberNavController(),
) {
Expand Down Expand Up @@ -139,6 +140,10 @@ fun CommunityHeader(
if (showMoreOptions) {
CommunityMoreDialog(
onDismissRequest = { showMoreOptions = false },
onBlockCommunityClick = {
showMoreOptions = false
onBlockCommunityClick()
},
navController = navController,
)
}
Expand Down Expand Up @@ -203,6 +208,7 @@ fun CommunityHeaderTitle(
@Composable
fun CommunityMoreDialog(
onDismissRequest: () -> Unit,
onBlockCommunityClick: () -> Unit,
navController: NavController,
) {
AlertDialog(
Expand All @@ -217,6 +223,11 @@ fun CommunityMoreDialog(
onDismissRequest()
},
)
IconAndTextDrawerItem(
text = "Block Community",
icon = Icons.Default.Block,
onClick = onBlockCommunityClick,
)
}
},
buttons = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ fun CommunityActivity(
scaffoldState = scaffoldState,
topBar = {
Column {
communityViewModel.communityView?.community?.name?.also {
communityViewModel.communityView?.community?.also { com ->
CommunityHeader(
communityName = it,
communityName = com.name,
selectedSortType = communityViewModel.sortType.value,
onClickSortType = { sortType ->
communityViewModel.fetchPosts(
Expand All @@ -65,6 +65,14 @@ fun CommunityActivity(
ctx = ctx,
)
},
onBlockCommunityClick = {
account?.also { acct ->
communityViewModel.blockCommunity(
account = acct,
ctx = ctx,
)
}
},
navController = navController,
)
}
Expand Down Expand Up @@ -126,6 +134,23 @@ fun CommunityActivity(
ctx = ctx,
)
},
onBlockCommunityClick = {
account?.also { acct ->
communityViewModel.blockCommunity(
account = acct,
ctx = ctx,
)
}
},
onBlockCreatorClick = {
account?.also { acct ->
communityViewModel.blockCreator(
creator = it,
account = acct,
ctx = ctx,
)
}
},
onCommunityClick = { community ->
communityClickWrapper(
communityViewModel,
Expand Down
Loading

0 comments on commit 25c84ce

Please sign in to comment.