Skip to content

Commit 25c84ce

Browse files
authored
Adding user and community blocking. Fixes #71 Fixes #58 (#75)
1 parent 2822c0d commit 25c84ce

20 files changed

+578
-128
lines changed

app/src/main/java/com/jerboa/api/Http.kt

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ interface API {
198198
@POST("post/report")
199199
suspend fun createPostReport(@Body form: CreatePostReport): Response<PostReportResponse>
200200

201+
/**
202+
* Block a person.
203+
*/
204+
@POST("user/block")
205+
suspend fun blockPerson(@Body form: BlockPerson): Response<BlockPersonResponse>
206+
207+
/**
208+
* Block a community.
209+
*/
210+
@POST("community/block")
211+
suspend fun blockCommunity(@Body form: BlockCommunity): Response<BlockCommunityResponse>
212+
201213
/**
202214
* Upload an image.
203215
*/
@@ -637,6 +649,38 @@ suspend fun createPostReportWrapper(
637649
return createdReport
638650
}
639651

652+
suspend fun blockPersonWrapper(
653+
form: BlockPerson,
654+
ctx: Context,
655+
): BlockPersonResponse? {
656+
657+
var blockPersonRes: BlockPersonResponse? = null
658+
val api = API.getInstance()
659+
660+
try {
661+
blockPersonRes = retrofitErrorHandler(api.blockPerson(form))
662+
} catch (e: Exception) {
663+
toastException(ctx = ctx, error = e)
664+
}
665+
return blockPersonRes
666+
}
667+
668+
suspend fun blockCommunityWrapper(
669+
form: BlockCommunity,
670+
ctx: Context,
671+
): BlockCommunityResponse? {
672+
673+
var blockCommunityRes: BlockCommunityResponse? = null
674+
val api = API.getInstance()
675+
676+
try {
677+
blockCommunityRes = retrofitErrorHandler(api.blockCommunity(form))
678+
} catch (e: Exception) {
679+
toastException(ctx = ctx, error = e)
680+
}
681+
return blockCommunityRes
682+
}
683+
640684
suspend fun uploadPictrsImage(account: Account, imageIs: InputStream, ctx: Context): String? {
641685
var imageUrl: String? = null
642686
val api = API.getInstance()
@@ -750,12 +794,6 @@ fun <T> retrofitErrorHandler(res: Response<T>): T {
750794
// }
751795
//
752796
//
753-
// /**
754-
// * Block a community.
755-
// */
756-
// async blockCommunity(form: BlockCommunity): Promise<BlockCommunityResponse> {
757-
// return this.wrapper(HttpType.Post, "/community/block", form);
758-
// }
759797
//
760798
// /**
761799
// * Delete a community.
@@ -933,13 +971,6 @@ fun <T> retrofitErrorHandler(res: Response<T>): T {
933971
// return this.wrapper(HttpType.Post, "/user/ban", form);
934972
// }
935973
//
936-
// /**
937-
// * Block a person.
938-
// */
939-
// async blockPerson(form: BlockPerson): Promise<BlockPersonResponse> {
940-
// return this.wrapper(HttpType.Post, "/user/block", form);
941-
// }
942-
943974
// /**
944975
// * Verify your email
945976
// */

app/src/main/java/com/jerboa/ui/components/comment/CommentNode.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ fun CommentNode(
113113
onCommunityClick: (community: CommunitySafe) -> Unit,
114114
onPostClick: (postId: Int) -> Unit,
115115
onReportClick: (commentView: CommentView) -> Unit,
116+
onBlockCreatorClick: (creator: PersonSafe) -> Unit,
116117
showPostAndCommunityContext: Boolean = false,
117118
showRead: Boolean = false,
118119
account: Account?,
@@ -198,6 +199,7 @@ fun CommentNode(
198199
onSaveClick = onSaveClick,
199200
onMarkAsReadClick = onMarkAsReadClick,
200201
onReportClick = onReportClick,
202+
onBlockCreatorClick = onBlockCreatorClick,
201203
showRead = showRead,
202204
myVote = myVote.value,
203205
upvotes = upvotes.value,
@@ -227,6 +229,7 @@ fun CommentNode(
227229
onReportClick = onReportClick,
228230
showRead = showRead,
229231
onReplyClick = onReplyClick,
232+
onBlockCreatorClick = onBlockCreatorClick,
230233
account = account,
231234
moderators = moderators,
232235
)
@@ -281,6 +284,7 @@ fun CommentFooterLine(
281284
onViewSourceClick: () -> Unit,
282285
onEditCommentClick: (commentView: CommentView) -> Unit,
283286
onReportClick: (commentView: CommentView) -> Unit,
287+
onBlockCreatorClick: (creator: PersonSafe) -> Unit,
284288
showRead: Boolean = false,
285289
myVote: Int?,
286290
upvotes: Int,
@@ -306,6 +310,10 @@ fun CommentFooterLine(
306310
showMoreOptions = false
307311
onReportClick(commentView)
308312
},
313+
onBlockCreatorClick = {
314+
showMoreOptions = false
315+
onBlockCreatorClick(commentView.creator)
316+
},
309317
isCreator = account?.id == commentView.creator.id,
310318
)
311319
}
@@ -395,6 +403,7 @@ fun CommentNodesPreview() {
395403
onReplyClick = {},
396404
onSaveClick = {},
397405
onUpvoteClick = {},
406+
onBlockCreatorClick = {},
398407
)
399408
}
400409

@@ -404,6 +413,7 @@ fun CommentOptionsDialog(
404413
onViewSourceClick: () -> Unit,
405414
onEditCommentClick: () -> Unit,
406415
onReportClick: () -> Unit,
416+
onBlockCreatorClick: () -> Unit,
407417
isCreator: Boolean,
408418
commentView: CommentView,
409419
) {
@@ -435,6 +445,11 @@ fun CommentOptionsDialog(
435445
icon = Icons.Default.Flag,
436446
onClick = onReportClick,
437447
)
448+
IconAndTextDrawerItem(
449+
text = "Block ${commentView.creator.name}",
450+
icon = Icons.Default.Block,
451+
onClick = onBlockCreatorClick,
452+
)
438453
}
439454
if (isCreator) {
440455
IconAndTextDrawerItem(
@@ -458,7 +473,8 @@ fun CommentOptionsDialogPreview() {
458473
onDismissRequest = {},
459474
onEditCommentClick = {},
460475
onReportClick = {},
461-
onViewSourceClick = {}
476+
onViewSourceClick = {},
477+
onBlockCreatorClick = {},
462478
)
463479
}
464480

app/src/main/java/com/jerboa/ui/components/comment/CommentNodes.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package com.jerboa.ui.components.comment
33
import androidx.compose.foundation.layout.Column
44
import androidx.compose.runtime.Composable
55
import com.jerboa.CommentNodeData
6-
import com.jerboa.datatypes.CommentView
7-
import com.jerboa.datatypes.CommunityModeratorView
8-
import com.jerboa.datatypes.CommunitySafe
9-
import com.jerboa.datatypes.SortType
6+
import com.jerboa.datatypes.*
107
import com.jerboa.db.Account
118
import com.jerboa.sortNodes
129

@@ -22,6 +19,7 @@ fun CommentNodes(
2219
onReportClick: (commentView: CommentView) -> Unit,
2320
onPersonClick: (personId: Int) -> Unit,
2421
onCommunityClick: (community: CommunitySafe) -> Unit,
22+
onBlockCreatorClick: (creator: PersonSafe) -> Unit,
2523
onPostClick: (postId: Int) -> Unit,
2624
account: Account? = null,
2725
moderators: List<CommunityModeratorView>,
@@ -44,6 +42,7 @@ fun CommentNodes(
4442
onPostClick = onPostClick,
4543
onEditCommentClick = onEditCommentClick,
4644
onReportClick = onReportClick,
45+
onBlockCreatorClick = onBlockCreatorClick,
4746
showPostAndCommunityContext = showPostAndCommunityContext,
4847
showRead = showRead,
4948
)

app/src/main/java/com/jerboa/ui/components/comment/CommentRoutines.kt

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -109,28 +109,6 @@ fun markPersonMentionAsReadRoutine(
109109
}
110110
}
111111

112-
fun markPrivateMessageAsReadRoutine(
113-
privateMessageView: MutableState<PrivateMessageView?>,
114-
messages: MutableList<PrivateMessageView>? = null,
115-
account: Account,
116-
ctx: Context,
117-
scope: CoroutineScope,
118-
) {
119-
scope.launch {
120-
privateMessageView.value?.also { pmv ->
121-
val updatedPmv = markPrivateMessageAsReadWrapper(
122-
pmv,
123-
account,
124-
ctx,
125-
)?.private_message_view
126-
privateMessageView.value = updatedPmv
127-
messages?.also {
128-
findAndUpdatePrivateMessage(messages, updatedPmv)
129-
}
130-
}
131-
}
132-
}
133-
134112
fun createCommentRoutine(
135113
loading: MutableState<Boolean>,
136114
content: String,
@@ -269,20 +247,6 @@ fun findAndUpdateMention(
269247
}
270248
}
271249

272-
fun findAndUpdatePrivateMessage(
273-
messages: MutableList<PrivateMessageView>,
274-
updatedMessageView: PrivateMessageView?
275-
) {
276-
updatedMessageView?.also { ucv ->
277-
val foundIndex = messages.indexOfFirst {
278-
it.private_message.id == ucv.private_message.id
279-
}
280-
if (foundIndex != -1) {
281-
messages[foundIndex] = ucv
282-
}
283-
}
284-
}
285-
286250
fun fetchRepliesRoutine(
287251
replies: MutableList<CommentView>,
288252
loading: MutableState<Boolean>,
@@ -406,60 +370,3 @@ fun fetchPersonMentionsRoutine(
406370
}
407371
}
408372
}
409-
410-
fun fetchPrivateMessagesRoutine(
411-
messages: MutableList<PrivateMessageView>,
412-
loading: MutableState<Boolean>,
413-
page: MutableState<Int>,
414-
unreadOnly: MutableState<Boolean>,
415-
nextPage: Boolean = false,
416-
clear: Boolean = false,
417-
changeUnreadOnly: Boolean? = null,
418-
account: Account,
419-
ctx: Context,
420-
scope: CoroutineScope,
421-
) {
422-
scope.launch {
423-
val api = API.getInstance()
424-
try {
425-
loading.value = true
426-
427-
if (nextPage) {
428-
page.value++
429-
}
430-
431-
if (clear) {
432-
page.value = 1
433-
}
434-
435-
changeUnreadOnly?.also {
436-
unreadOnly.value = it
437-
}
438-
439-
val form = GetPrivateMessages(
440-
page = page.value,
441-
unread_only = unreadOnly.value,
442-
auth = account.jwt,
443-
)
444-
Log.d(
445-
"jerboa",
446-
"Fetching unread replies: $form"
447-
)
448-
val newMessages = retrofitErrorHandler(
449-
api.getPrivateMessages(
450-
form = form
451-
.serializeToMap()
452-
)
453-
).private_messages
454-
455-
if (clear) {
456-
messages.clear()
457-
}
458-
messages.addAll(newMessages)
459-
} catch (e: Exception) {
460-
toastException(ctx = ctx, error = e)
461-
} finally {
462-
loading.value = false
463-
}
464-
}
465-
}

app/src/main/java/com/jerboa/ui/components/community/Community.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ fun CommunityTopSectionPreview() {
102102
fun CommunityHeader(
103103
communityName: String,
104104
onClickSortType: (SortType) -> Unit,
105+
onBlockCommunityClick: () -> Unit,
105106
selectedSortType: SortType,
106107
navController: NavController = rememberNavController(),
107108
) {
@@ -139,6 +140,10 @@ fun CommunityHeader(
139140
if (showMoreOptions) {
140141
CommunityMoreDialog(
141142
onDismissRequest = { showMoreOptions = false },
143+
onBlockCommunityClick = {
144+
showMoreOptions = false
145+
onBlockCommunityClick()
146+
},
142147
navController = navController,
143148
)
144149
}
@@ -203,6 +208,7 @@ fun CommunityHeaderTitle(
203208
@Composable
204209
fun CommunityMoreDialog(
205210
onDismissRequest: () -> Unit,
211+
onBlockCommunityClick: () -> Unit,
206212
navController: NavController,
207213
) {
208214
AlertDialog(
@@ -217,6 +223,11 @@ fun CommunityMoreDialog(
217223
onDismissRequest()
218224
},
219225
)
226+
IconAndTextDrawerItem(
227+
text = "Block Community",
228+
icon = Icons.Default.Block,
229+
onClick = onBlockCommunityClick,
230+
)
220231
}
221232
},
222233
buttons = {},

app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ fun CommunityActivity(
5353
scaffoldState = scaffoldState,
5454
topBar = {
5555
Column {
56-
communityViewModel.communityView?.community?.name?.also {
56+
communityViewModel.communityView?.community?.also { com ->
5757
CommunityHeader(
58-
communityName = it,
58+
communityName = com.name,
5959
selectedSortType = communityViewModel.sortType.value,
6060
onClickSortType = { sortType ->
6161
communityViewModel.fetchPosts(
@@ -65,6 +65,14 @@ fun CommunityActivity(
6565
ctx = ctx,
6666
)
6767
},
68+
onBlockCommunityClick = {
69+
account?.also { acct ->
70+
communityViewModel.blockCommunity(
71+
account = acct,
72+
ctx = ctx,
73+
)
74+
}
75+
},
6876
navController = navController,
6977
)
7078
}
@@ -126,6 +134,23 @@ fun CommunityActivity(
126134
ctx = ctx,
127135
)
128136
},
137+
onBlockCommunityClick = {
138+
account?.also { acct ->
139+
communityViewModel.blockCommunity(
140+
account = acct,
141+
ctx = ctx,
142+
)
143+
}
144+
},
145+
onBlockCreatorClick = {
146+
account?.also { acct ->
147+
communityViewModel.blockCreator(
148+
creator = it,
149+
account = acct,
150+
ctx = ctx,
151+
)
152+
}
153+
},
129154
onCommunityClick = { community ->
130155
communityClickWrapper(
131156
communityViewModel,

0 commit comments

Comments
 (0)