Skip to content

Commit

Permalink
Adding ability to block domains (#529)
Browse files Browse the repository at this point in the history
- Adding the ability to block domains in posts and account pages
- Fixing issue with block domain database queries

---------

Co-authored-by: John Oberhauser <j.git-global@obez.io>
  • Loading branch information
JohnOberhauser and John Oberhauser committed May 20, 2024
1 parent 4667296 commit a6499a4
Show file tree
Hide file tree
Showing 21 changed files with 184 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface FederatedTimelineStatusDao : BaseDao<FederatedTimelineStatus> {
)
suspend fun removePostsFromAccount(accountId: String)

@Transaction
@Query(
"DELETE FROM federatedTimeline " +
"WHERE statusId IN " +
Expand All @@ -38,7 +39,7 @@ interface FederatedTimelineStatusDao : BaseDao<FederatedTimelineStatus> {
"WHERE accountId IN " +
"(" +
"SELECT accountId FROM accounts " +
"WHERE acct LIKE '%' + :domain + '%' " +
"WHERE acct LIKE '%' || :domain || '%' " +
")" +
")"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface HomeTimelineStatusDao : BaseDao<HomeTimelineStatus> {
"WHERE accountId IN " +
"(" +
"SELECT accountId FROM accounts " +
"WHERE acct LIKE '%' + :domain + '%' " +
"WHERE acct LIKE '%' || :domain || '%' " +
")" +
")"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface LocalTimelineStatusDao : BaseDao<LocalTimelineStatus> {
"WHERE accountId IN " +
"(" +
"SELECT accountId FROM accounts " +
"WHERE acct LIKE '%' + :domain + '%' " +
"WHERE acct LIKE '%' || :domain || '%' " +
")" +
")"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface RelationshipsDao : BaseDao<DatabaseRelationship> {
"WHERE accountId IN " +
"( " +
"SELECT accountId from accounts " +
"WHERE acct LIKE '%' + :domain + '%' " +
"WHERE acct LIKE '%' || :domain || '%' " +
")"
)
suspend fun updateDomainBlocking(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ data class Account(
/**
* Indicates that the profile is currently inactive and that its user has moved to a new account.
*/
val movedTo: social.firefly.core.model.Account? = null,
val movedTo: Account? = null,
/**
* Whether this account represents a group.
*/
val isGroup: Boolean,
/**
* Additional metadata attached to a profile as name-value pairs.
*/
val fields: List<social.firefly.core.model.Field>? = null,
val fields: List<Field>? = null,
/**
* Whether this account is a robot.
*
Expand All @@ -98,7 +98,7 @@ data class Account(
/**
* An entity to be used with API methods to verify and update credentials.
*/
val source: social.firefly.core.model.Source? = null,
val source: Source? = null,
/**
* Whether the account is suspended.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package social.firefly.core.ui.common.dialog

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.res.stringResource
import social.firefly.core.ui.common.R
import social.firefly.core.ui.common.text.FfTextButton

@Composable
fun blockDomainConfirmationDialog(
domain: String,
onConfirmation: () -> Unit
) : DialogOpener {
var isOpen by remember { mutableStateOf(false) }

if (isOpen) {
FfAlertDialog(
onDismissRequest = { isOpen = false },
title = {
Text(
text = stringResource(
id = R.string.block_domain_confirmation,
domain
)
)
},
confirmButton = {
FfTextButton(
onClick = {
onConfirmation()
isOpen = false
}
) {
Text(text = stringResource(id = R.string.block_domain_confirmed))
}
},
dismissButton = {
FfTextButton(onClick = { isOpen = false }) {
Text(text = stringResource(id = R.string.cancel))
}
}
)
}

return object : DialogOpener {
override fun open() {
isOpen = true
}
}
}
5 changes: 5 additions & 0 deletions core/ui/common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<string name="block_user">Block %s</string>
<string name="unblock_user">Unblock %s</string>

<string name="block_domain_confirmation">Are you sure you want to block the entire domain %s?</string>
<string name="block_domain_confirmed">Block</string>
<string name="block_domain">Block domain %s</string>
<string name="unblock_domain">Unblock domain %s</string>

<string name="mute_user_confirmation">Are you sure you want to mute %s?</string>
<string name="mute_user_confirmed">Mute</string>
<string name="mute_user">Mute %s</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import social.firefly.core.navigation.NavigationDestination
import social.firefly.core.navigation.usecases.NavigateTo
import social.firefly.core.navigation.usecases.OpenLink
import social.firefly.core.usecase.mastodon.account.BlockAccount
import social.firefly.core.usecase.mastodon.account.BlockDomain
import social.firefly.core.usecase.mastodon.account.MuteAccount
import social.firefly.core.usecase.mastodon.status.BookmarkStatus
import social.firefly.core.usecase.mastodon.status.BoostStatus
Expand Down Expand Up @@ -37,6 +38,7 @@ class PostCardDelegate(
private val bookmarkStatus: BookmarkStatus,
private val undoBookmarkStatus: UndoBookmarkStatus,
private val analytics: PostCardAnalytics,
private val blockDomain: BlockDomain,
) : PostCardInteractions {

private val baseAnalyticsIdentifier: String = feedLocation.baseAnalyticsIdentifier
Expand Down Expand Up @@ -173,6 +175,16 @@ class PostCardDelegate(
)
}

override fun onOverflowBlockDomainClicked(domain: String) {
appScope.launch {
try {
blockDomain(domain)
} catch (e: BlockDomain.BlockDomainFailedException) {
Timber.e(e)
}
}
}

override fun onOverflowDeleteClicked(statusId: String) {
appScope.launch {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ interface PostCardInteractions : PollInteractions, HtmlContentInteractions {
statusId: String,
)

fun onOverflowBlockDomainClicked(
domain: String,
)

fun onOverflowDeleteClicked(statusId: String)

fun onOverflowEditClicked(statusId: String)
Expand Down Expand Up @@ -77,6 +81,8 @@ object PostCardInteractionsNoOp: PostCardInteractions {
statusId: String
) = Unit

override fun onOverflowBlockDomainClicked(domain: String) = Unit

override fun onOverflowDeleteClicked(statusId: String) = Unit

override fun onOverflowEditClicked(statusId: String) = Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ private fun Status.toMainPostCardUiState(
url = url,
profilePictureUrl = account.avatarStaticUrl,
postTimeSince = createdAt.timeSinceNow(),
accountName = StringFactory.literal(account.acct),
accountName = account.acct,
replyCount = repliesCount.toShortenedStringValue(),
boostCount = boostsCount.toShortenedStringValue(),
favoriteCount = favouritesCount.toShortenedStringValue(),
username = account.displayName,
domain = account.acct.substringAfter(
delimiter = "@",
missingDelimiterValue = ""
),
statusId = statusId,
userBoosted = isBoosted ?: false,
isFavorited = isFavourited ?: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ val postCardModule =
bookmarkStatus = get(),
undoBookmarkStatus = get(),
analytics = get(),
blockDomain = get(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ data class PostCardUiState(
data class MainPostCardUiState(
val url: String?,
val username: String,
val domain: String, // if the domain is the user's domain, value will be blank
val profilePictureUrl: String,
val postTimeSince: StringFactory,
val accountName: StringFactory,
val accountName: String,
val replyCount: String?,
val boostCount: String?,
val favoriteCount: String?,
Expand All @@ -36,11 +37,6 @@ data class MainPostCardUiState(
val shouldShowUnbookmarkConfirmation: Boolean,
)

data class DropDownOption(
val text: StringFactory,
val onOptionClicked: () -> Unit,
)

data class PostContentUiState(
val statusId: String,
val pollUiState: PollUiState?,
Expand Down Expand Up @@ -92,9 +88,10 @@ data class PreviewCard(
internal val postCardUiStatePreview = MainPostCardUiState(
url = "",
username = "Cool guy",
domain = "mozilla.social",
profilePictureUrl = "",
postTimeSince = Instant.fromEpochMilliseconds(1695308821000L).timeSinceNow(),
accountName = StringFactory.literal("coolguy"),
accountName = "coolguy",
replyCount = "4",
boostCount = "300k",
favoriteCount = "4.4m",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import social.firefly.common.utils.StringFactory
import social.firefly.core.designsystem.icon.FfIcons
import social.firefly.core.designsystem.theme.FfTheme
import social.firefly.core.ui.common.dialog.blockAccountConfirmationDialog
import social.firefly.core.ui.common.dialog.blockDomainConfirmationDialog
import social.firefly.core.ui.common.dialog.deleteStatusConfirmationDialog
import social.firefly.core.ui.common.dialog.muteAccountConfirmationDialog
import social.firefly.core.ui.common.dropdown.FfDropDownItem
Expand Down Expand Up @@ -44,7 +45,7 @@ internal fun MetaData(
style = FfTheme.typography.labelMedium,
)
Text(
text = "${post.postTimeSince.build(context)} - @${post.accountName.build(context)}",
text = "${post.postTimeSince.build(context)} - @${post.accountName}",
style = FfTheme.typography.bodyMedium,
color = FfTheme.colors.textSecondary,
)
Expand Down Expand Up @@ -78,6 +79,10 @@ private fun OverflowMenu(
)
}

val blockDomainDialog = blockDomainConfirmationDialog(domain = post.domain) {
postCardInteractions.onOverflowBlockDomainClicked(post.domain)
}

val deleteStatusDialog = deleteStatusConfirmationDialog {
postCardInteractions.onOverflowDeleteClicked(post.statusId)
}
Expand Down Expand Up @@ -124,11 +129,21 @@ private fun OverflowMenu(
onClick = {
postCardInteractions.onOverflowReportClicked(
accountId = post.accountId,
accountHandle = post.accountName.build(context),
accountHandle = post.accountName,
statusId = post.statusId,
)
}
)
if (post.domain.isNotBlank()) {
FfDropDownItem(
text = StringFactory.resource(
R.string.block_domain,
post.domain
).build(context),
expanded = overflowMenuExpanded,
onClick = { blockDomainDialog.open() }
)
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core/ui/postcard/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<string name="mute_user">Mute %s</string>
<string name="block_user">Block %s</string>
<string name="report_user">Report %s</string>
<string name="block_domain">Block domain %s</string>

<string name="view_more_replies">View more replies</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ fun Account.toUiState(relationship: Relationship) =
AccountUiState(
accountId = accountId,
username = username,
domain = acct.substringAfter(
delimiter = "@",
missingDelimiterValue = ""
),
webFinger = acct,
displayName = displayName,
accountUrl = url,
Expand All @@ -29,6 +33,7 @@ fun Account.toUiState(relationship: Relationship) =
},
isMuted = relationship.isMuting,
isBlocked = relationship.isBlocking,
isDomainBlocked = relationship.isDomainBlocking,
joinDate = createdAt.toLocalDateTime(TimeZone.currentSystemDefault()),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ val accountModule =
muteAccount = get(),
unmuteAccount = get(),
timelineRepository = get(),
blockDomain = get(),
unblockDomain = get(),
)
}

Expand Down

0 comments on commit a6499a4

Please sign in to comment.