Skip to content

Commit

Permalink
Add some checks for age verification.
Browse files Browse the repository at this point in the history
  • Loading branch information
mopsalarm committed Oct 1, 2021
1 parent c1a9696 commit 22159bc
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 20 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ dependencies {

implementation project(":model")

implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.30"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.31"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"

Expand All @@ -170,7 +170,7 @@ dependencies {
implementation 'androidx.concurrent:concurrent-futures:1.1.0'
// implementation 'com.google.guava:listenablefuture:1.0'
implementation 'androidx.paging:paging-runtime:3.0.1'
implementation "androidx.constraintlayout:constraintlayout:2.1.0"
implementation "androidx.constraintlayout:constraintlayout:2.1.1"

implementation 'androidx.activity:activity-ktx:1.3.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
Expand All @@ -180,7 +180,7 @@ dependencies {

implementation 'androidx.fragment:fragment-ktx:1.3.6'

implementation "com.google.android.gms:play-services-ads-lite:20.3.0"
implementation "com.google.android.gms:play-services-ads-lite:20.4.0"
implementation 'com.google.firebase:firebase-analytics:19.0.1'
implementation 'com.google.firebase:firebase-crashlytics:18.2.1'

Expand Down Expand Up @@ -212,7 +212,7 @@ dependencies {

implementation 'com.github.paolorotolo:appintro:5.1.0'

implementation 'com.google.android.exoplayer:exoplayer-core:2.15.0'
implementation 'com.google.android.exoplayer:exoplayer-core:2.15.1'

debugImplementation 'androidx.multidex:multidex:2.0.1'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ class LoginCookieJar(context: Context, private val preferences: SharedPreference

// do nothing if the cookie value has not changed.
val notChanged = previousCookie?.value == cookie.value
if (notChanged)
if (notChanged) {
logger.debug { "Cookie has not changed" }
return true
}

val parsedCookie = parseCookie(cookie)

Expand Down
21 changes: 17 additions & 4 deletions app/src/main/java/com/pr0gramm/app/services/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.pr0gramm.app.model.user.LoginCookie
import com.pr0gramm.app.model.user.LoginState
import com.pr0gramm.app.orm.BenisRecord
import com.pr0gramm.app.ui.base.AsyncScope
import com.pr0gramm.app.ui.base.launchIgnoreErrors
import com.pr0gramm.app.util.catchAll
import com.pr0gramm.app.util.debugOnly
import com.pr0gramm.app.util.doInBackground
Expand Down Expand Up @@ -44,7 +45,9 @@ class UserService(private val api: Api,
@Suppress("PrivatePropertyName")
private val NotAuthorized = LoginState(
id = -1, score = 0, mark = 0, admin = false,
premium = false, authorized = false, name = null, uniqueToken = null)
premium = false, authorized = false, name = null,
uniqueToken = null, verified = false,
)

private val fullSyncInProgress = AtomicBoolean()

Expand Down Expand Up @@ -145,6 +148,12 @@ class UserService(private val api: Api,
if (cookie == null) {
logger.info { "LoginCookie was removed, performing logout now." }
AsyncScope.launch { logout() }

} else {
// cookie has changed, re-build cached user info
AsyncScope.launchIgnoreErrors {
updateCachedUserInfo()
}
}
}

Expand Down Expand Up @@ -375,7 +384,7 @@ class UserService(private val api: Api,
private fun persistLatestLoginState(state: LoginState) {
try {
if (state.authorized) {
logger.debug { "Persisting login state now." }
logger.debug { "Persisting login state now: $loginState" }

preferences.edit {
val encoded = MoshiInstance.adapter<LoginState>().toJson(state)
Expand All @@ -397,10 +406,13 @@ class UserService(private val api: Api,
get() = loginState.authorized && cookieJar.parsedCookie?.id?.isNotBlank() == true

val userIsPremium: Boolean
get() = loginState.authorized && cookieJar.parsedCookie?.paid == true
get() = isAuthorized && loginState.premium

val userIsAdmin: Boolean
get() = loginState.authorized && cookieJar.parsedCookie?.admin == true
get() = isAuthorized && loginState.admin

val userIsVerified: Boolean
get() = isAuthorized && loginState.verified

val loginStateWithBenisGraph = run {
val graphs = loginStates.mapLatest { loginState ->
Expand Down Expand Up @@ -486,5 +498,6 @@ private fun createLoginStateFromInfo(user: Api.Info.User, cookie: LoginCookie?,
score = user.score,
premium = cookie?.paid == true,
admin = cookie?.admin == true,
verified = cookie?.verified == true,
uniqueToken = token)
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/pr0gramm/app/ui/AdService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class AdService(private val configService: ConfigService, private val userServic
}

fun newAdView(context: Context): AdView {
val view = AdView(context.applicationContext)
val view = AdView(context)
view.adUnitId = bannerUnitId

val backgroundColor = AndroidUtility.resolveColorAttribute(context, android.R.attr.windowBackground)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ class FeedFragment : BaseFragment("FeedFragment", R.layout.fragment_feed), Filte
activity.invalidateOptionsMenu()
}
}

launchInViewScope {
userService.loginStates.drop(1).collect {
activity.invalidateOptionsMenu()
}
}
}

private fun updateAdapterState(feedState: FeedViewModel.FeedState, userState: UserStateModel.UserState) {
Expand Down Expand Up @@ -691,8 +697,16 @@ class FeedFragment : BaseFragment("FeedFragment", R.layout.fragment_feed), Filte
R.string.action_switch_to_top else R.string.action_switch_to_new)
}

menu.findItem(R.id.action_change_content_type__not_verified)?.let { item ->
item.isVisible = !userService.userIsVerified
item.icon = ContentTypeDrawable(activity, listOf(ContentType.SFW)).also { icon ->
icon.textSize = resources.getDimensionPixelSize(
R.dimen.feed_content_type_action_icon_text_size).toFloat()
}
}

menu.findItem(R.id.action_change_content_type)?.let { item ->
if (userService.isAuthorized) {
if (userService.userIsVerified) {
val icon = ContentTypeDrawable(activity, selectedContentType)
icon.textSize = resources.getDimensionPixelSize(
R.dimen.feed_content_type_action_icon_text_size).toFloat()
Expand Down Expand Up @@ -762,6 +776,7 @@ class FeedFragment : BaseFragment("FeedFragment", R.layout.fragment_feed), Filte
R.id.action_open_in_admin -> openUserInAdmin()
R.id.action_scroll_seen -> scrollToNextSeenAsync()
R.id.action_scroll_unseen -> scrollToNextUnseenAsync()
R.id.action_change_content_type__not_verified -> hintUserIsNotVerified()

else -> super.onOptionsItemSelected(item)
}
Expand Down Expand Up @@ -816,6 +831,13 @@ class FeedFragment : BaseFragment("FeedFragment", R.layout.fragment_feed), Filte
(activity as MainActionHandler).bookmarkFilter(filter, title)
}

private fun hintUserIsNotVerified() {
showDialog(this) {
content(R.string.user_is_not_verified)
positive()
}
}

private fun preloadCurrentFeed() {
if (AndroidUtility.isOnMobile(activity)) {
showDialog(this) {
Expand Down
17 changes: 16 additions & 1 deletion app/src/main/java/com/pr0gramm/app/ui/upload/UploadFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.pr0gramm.app.feed.FeedType
import com.pr0gramm.app.services.RulesService
import com.pr0gramm.app.services.UploadService
import com.pr0gramm.app.services.UriHelper
import com.pr0gramm.app.services.UserService
import com.pr0gramm.app.ui.*
import com.pr0gramm.app.ui.base.BaseFragment
import com.pr0gramm.app.ui.base.bindViews
Expand All @@ -47,6 +48,7 @@ class UploadFragment : BaseFragment("UploadFragment", R.layout.fragment_upload)
private val rulesService: RulesService by instance()

private val tagSuggestions: TagSuggestionService by instance()
private val userService: UserService by instance()

private val views by bindViews(FragmentUploadBinding::bind)

Expand Down Expand Up @@ -117,6 +119,12 @@ class UploadFragment : BaseFragment("UploadFragment", R.layout.fragment_upload)
}
}

launchInViewScope {
userService.loginStates.drop(1).collect {
updateFormState(vm.state.value)
}
}

launchInViewScope {
vm.state.collect { state ->
logger.debug { "Current state is: $state" }
Expand Down Expand Up @@ -234,17 +242,24 @@ class UploadFragment : BaseFragment("UploadFragment", R.layout.fragment_upload)

views.tags.isEnabled = enabled

val loginState = userService.loginState

for (childView in views.contentTypeGroup.children) {
childView.isEnabled = enabled

if (!loginState.verified && childView.id != R.id.upload_type_sfw) {
childView.isEnabled = false
}
}

val hasTagSelected = views.contentTypeGroup.children.any { childView ->
childView is Checkable && childView.isChecked
}

val imageSizeOkay = state.fileSizeOkay
val isVerifiedOrUploadTypeIsSFW = selectedContentType() == ContentType.SFW || loginState.verified

views.actionUpload.isEnabled = enabled && hasTagSelected && imageSizeOkay
views.actionUpload.isEnabled = enabled && hasTagSelected && imageSizeOkay && isVerifiedOrUploadTypeIsSFW
}

private fun onUploadComplete(postId: Long) {
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/res/menu/menu_feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

<item
android:id="@+id/action_change_content_type"
android:icon="@drawable/ic_white_action_refresh"
android:title="@string/action_change_content_type"
app:iconTint="?android:textColorPrimary"
app:showAsAction="always">
Expand All @@ -25,21 +24,27 @@

<item
android:id="@+id/action_content_type_nsfw"
android:title="@string/type_nsfw"/>
android:title="@string/type_nsfw" />

<item
android:id="@+id/action_content_type_nsfl"
android:title="@string/type_nsfl"/>
android:title="@string/type_nsfl" />
</group>
</menu>
</item>

<item
android:id="@+id/action_change_content_type__not_verified"
android:title="@string/action_change_content_type"
app:iconTint="?android:textColorPrimary"
app:showAsAction="always"></item>

<item
android:id="@+id/action_bookmark"
android:icon="@drawable/ic_white_action_pin"
android:title="@string/action_pin"
app:iconTint="?android:textColorPrimary"
app:showAsAction="ifRoom"/>
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_feedtype"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@
<string name="action_scroll_seen">Nächster gesehener Hochlad</string>
<string name="error_max_scroll_reached">Maximale Scrollweite erreicht. Probiere es einfach nochmal.</string>
<string name="changelog">Changelog</string>
<string name="user_is_not_verified">Deine Altersverifikation ist noch nicht abgeschlossen.</string>


</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -581,5 +581,6 @@
<string name="action_scroll_seen">Scroll next seen</string>
<string name="error_max_scroll_reached">Max scroll distance reached. Please try again.</string>
<string name="changelog">Changelog</string>
<string name="user_is_not_verified">You are not age verified.</string>
</resources>

4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ buildscript {
}

plugins {
id "org.jetbrains.kotlin.android" version "1.5.30" apply false
id "org.jetbrains.kotlin.kapt" version "1.5.30" apply false
id "org.jetbrains.kotlin.android" version "1.5.31" apply false
id "org.jetbrains.kotlin.kapt" version "1.5.31" apply false
id "com.github.ben-manes.versions" version "0.39.0"
}

Expand Down
2 changes: 1 addition & 1 deletion model/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ repositories {
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.30"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.31"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data class LoginState(
val uniqueToken: String?,
val admin: Boolean,
val premium: Boolean,
val verified: Boolean = false,
val authorized: Boolean)


Expand All @@ -20,4 +21,6 @@ data class LoginCookie(
val id: String,
@Json(name = "n") val name: String,
@Json(name = "paid") val paid: Boolean = false,
@Json(name = "a") val admin: Boolean = false)
@Json(name = "a") val admin: Boolean = false,
@Json(name = "verified") val verified: Boolean = false,
)

0 comments on commit 22159bc

Please sign in to comment.