#68 Clean follow Entity
This commit is contained in:
@@ -27,6 +27,15 @@ open class User(
|
||||
CreatedAt by CreatedAt.Imp(),
|
||||
UpdatedAt by UpdatedAt.Imp()
|
||||
|
||||
class UserCreator(
|
||||
id: UUID = UUID.randomUUID(),
|
||||
override val username: String,
|
||||
) : UserRef(id), UserWithUsername
|
||||
|
||||
interface UserWithUsername {
|
||||
val username: String
|
||||
}
|
||||
|
||||
interface UserWithPasswordI {
|
||||
val id: UUID
|
||||
val password: String
|
||||
|
||||
@@ -5,6 +5,7 @@ import fr.dcproject.common.entity.DeletedAt
|
||||
import fr.dcproject.common.entity.Entity
|
||||
import fr.dcproject.common.entity.EntityI
|
||||
import fr.dcproject.component.auth.User
|
||||
import fr.dcproject.component.auth.UserCreator
|
||||
import fr.dcproject.component.auth.UserForCreate
|
||||
import fr.dcproject.component.auth.UserI
|
||||
import fr.dcproject.component.auth.UserRef
|
||||
@@ -50,6 +51,28 @@ class Citizen(
|
||||
)
|
||||
}
|
||||
|
||||
data class CitizenCreator(
|
||||
override var id: UUID = UUID.randomUUID(),
|
||||
override var name: Name,
|
||||
override var email: String,
|
||||
override var voteAnonymous: Boolean = true,
|
||||
override var followAnonymous: Boolean = true,
|
||||
override val user: UserCreator,
|
||||
override val deletedAt: DateTime? = null
|
||||
) : CitizenCreatorI,
|
||||
CitizenRefWithUser(id, user),
|
||||
DeletedAt by DeletedAt.Imp(deletedAt)
|
||||
|
||||
interface CitizenCreatorI : CitizenWithUserI, CitizenWithEmail, CitizenCartI, DeletedAt {
|
||||
override val id: UUID
|
||||
override val name: Name
|
||||
override val email: String
|
||||
val voteAnonymous: Boolean
|
||||
val followAnonymous: Boolean
|
||||
override val user: UserCreator
|
||||
override val deletedAt: DateTime?
|
||||
}
|
||||
|
||||
@Deprecated("")
|
||||
data class CitizenBasic(
|
||||
override var id: UUID = UUID.randomUUID(),
|
||||
|
||||
@@ -6,27 +6,18 @@ import fr.dcproject.common.entity.EntityI
|
||||
import fr.dcproject.common.entity.ExtraI
|
||||
import fr.dcproject.common.entity.HasTarget
|
||||
import fr.dcproject.common.entity.TargetI
|
||||
import fr.dcproject.component.citizen.CitizenBasic
|
||||
import fr.dcproject.component.citizen.CitizenBasicI
|
||||
import fr.dcproject.component.citizen.CitizenCreator
|
||||
import fr.dcproject.component.citizen.CitizenI
|
||||
import fr.dcproject.component.citizen.CitizenRef
|
||||
import java.util.UUID
|
||||
|
||||
@Deprecated("")
|
||||
class Follow<T : TargetI>(
|
||||
open class FollowForView<T : TargetI>(
|
||||
id: UUID = UUID.randomUUID(),
|
||||
override val createdBy: CitizenBasic,
|
||||
override val createdBy: CitizenCreator,
|
||||
override var target: T
|
||||
) : ExtraI<T, CitizenBasicI>,
|
||||
FollowSimple<T, CitizenBasicI>(id, createdBy, target)
|
||||
|
||||
@Deprecated("")
|
||||
open class FollowSimple<T : TargetI, C : CitizenI>(
|
||||
id: UUID = UUID.randomUUID(),
|
||||
override val createdBy: C,
|
||||
override var target: T
|
||||
) : ExtraI<T, C>,
|
||||
) : ExtraI<T, CitizenRef>,
|
||||
FollowRef(id),
|
||||
Created<C> by Created.Imp(createdBy)
|
||||
Created<CitizenRef> by Created.Imp(createdBy)
|
||||
|
||||
class FollowForUpdate<T : TargetI, C : CitizenI>(
|
||||
id: UUID = UUID.randomUUID(),
|
||||
|
||||
@@ -3,7 +3,6 @@ package fr.dcproject.component.follow
|
||||
import fr.dcproject.common.security.AccessControl
|
||||
import fr.dcproject.common.security.AccessResponse
|
||||
import fr.dcproject.component.citizen.CitizenI
|
||||
import fr.dcproject.component.follow.Follow as FollowEntity
|
||||
|
||||
class FollowAccessControl : AccessControl() {
|
||||
fun canCreate(subject: FollowI, citizen: CitizenI?): AccessResponse {
|
||||
@@ -16,10 +15,10 @@ class FollowAccessControl : AccessControl() {
|
||||
else granted()
|
||||
}
|
||||
|
||||
fun <S : FollowEntity<*>> canView(subjects: List<S>, citizen: CitizenI?): AccessResponse =
|
||||
fun <S : FollowForView<*>> canView(subjects: List<S>, citizen: CitizenI?): AccessResponse =
|
||||
canAll(subjects) { canView(it, citizen) }
|
||||
|
||||
fun canView(subject: FollowEntity<*>, citizen: CitizenI?): AccessResponse {
|
||||
fun canView(subject: FollowForView<*>, citizen: CitizenI?): AccessResponse {
|
||||
return if ((citizen != null && subject.createdBy.id == citizen.id) || !subject.createdBy.followAnonymous) granted()
|
||||
else denied("You cannot view an anonymous follow", "follow.view.anonymous")
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import fr.dcproject.common.entity.TargetRef
|
||||
import fr.dcproject.component.article.ArticleForView
|
||||
import fr.dcproject.component.article.ArticleRef
|
||||
import fr.dcproject.component.citizen.CitizenI
|
||||
import fr.dcproject.component.citizen.CitizenRef
|
||||
import fr.dcproject.component.constitution.ConstitutionRef
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
import fr.postgresjson.connexion.Requester
|
||||
@@ -14,21 +13,20 @@ import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import java.util.UUID
|
||||
import fr.dcproject.component.constitution.Constitution as ConstitutionEntity
|
||||
import fr.dcproject.component.follow.Follow as FollowEntity
|
||||
|
||||
sealed class FollowRepository<IN : TargetRef, OUT : TargetRef>(override var requester: Requester) : RepositoryI {
|
||||
open fun findByCitizen(
|
||||
citizen: CitizenI,
|
||||
page: Int = 1,
|
||||
limit: Int = 50
|
||||
): Paginated<FollowEntity<OUT>> =
|
||||
): Paginated<FollowForView<OUT>> =
|
||||
findByCitizen(citizen.id, page, limit)
|
||||
|
||||
open fun findByCitizen(
|
||||
citizenId: UUID,
|
||||
page: Int = 1,
|
||||
limit: Int = 50
|
||||
): Paginated<FollowEntity<OUT>> {
|
||||
): Paginated<FollowForView<OUT>> {
|
||||
return requester
|
||||
.getFunction("find_follows_by_citizen")
|
||||
.select(
|
||||
@@ -61,7 +59,7 @@ sealed class FollowRepository<IN : TargetRef, OUT : TargetRef>(override var requ
|
||||
open fun findFollow(
|
||||
citizen: CitizenI,
|
||||
target: TargetRef
|
||||
): FollowEntity<OUT>? =
|
||||
): FollowForView<OUT>? =
|
||||
requester
|
||||
.getFunction("find_follow")
|
||||
.selectOne(
|
||||
@@ -73,7 +71,7 @@ sealed class FollowRepository<IN : TargetRef, OUT : TargetRef>(override var requ
|
||||
fun findFollowsByTarget(
|
||||
target: Entity,
|
||||
bulkSize: Int = 300
|
||||
): Flow<FollowSimple<IN, CitizenRef>> = flow {
|
||||
): Flow<FollowForView<IN>> = flow {
|
||||
var nextPage = 1
|
||||
do {
|
||||
val paginate = findFollowsByTarget(target, nextPage, bulkSize)
|
||||
@@ -88,7 +86,7 @@ sealed class FollowRepository<IN : TargetRef, OUT : TargetRef>(override var requ
|
||||
target: Entity,
|
||||
page: Int = 1,
|
||||
limit: Int = 300
|
||||
): Paginated<FollowSimple<IN, CitizenRef>>
|
||||
): Paginated<FollowForView<IN>>
|
||||
}
|
||||
|
||||
class FollowArticleRepository(requester: Requester) : FollowRepository<ArticleRef, ArticleForView>(requester) {
|
||||
@@ -96,7 +94,7 @@ class FollowArticleRepository(requester: Requester) : FollowRepository<ArticleRe
|
||||
citizenId: UUID,
|
||||
page: Int,
|
||||
limit: Int
|
||||
): Paginated<FollowEntity<ArticleForView>> {
|
||||
): Paginated<FollowForView<ArticleForView>> {
|
||||
return requester.run {
|
||||
getFunction("find_follows_article_by_citizen")
|
||||
.select(
|
||||
@@ -111,7 +109,7 @@ class FollowArticleRepository(requester: Requester) : FollowRepository<ArticleRe
|
||||
target: Entity,
|
||||
page: Int,
|
||||
limit: Int
|
||||
): Paginated<FollowSimple<ArticleRef, CitizenRef>> {
|
||||
): Paginated<FollowForView<ArticleRef>> {
|
||||
return requester
|
||||
.getFunction("find_follows_article_by_target")
|
||||
.select(
|
||||
@@ -127,7 +125,7 @@ class FollowConstitutionRepository(requester: Requester) : FollowRepository<Cons
|
||||
citizenId: UUID,
|
||||
page: Int,
|
||||
limit: Int
|
||||
): Paginated<FollowEntity<ConstitutionEntity>> {
|
||||
): Paginated<FollowForView<ConstitutionEntity>> {
|
||||
return requester.run {
|
||||
getFunction("find_follows_constitution_by_citizen")
|
||||
.select(
|
||||
@@ -142,7 +140,7 @@ class FollowConstitutionRepository(requester: Requester) : FollowRepository<Cons
|
||||
target: Entity,
|
||||
page: Int,
|
||||
limit: Int
|
||||
): Paginated<FollowSimple<ConstitutionRef, CitizenRef>> {
|
||||
): Paginated<FollowForView<ConstitutionRef>> {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,9 @@ import com.rabbitmq.client.Consumer
|
||||
import com.rabbitmq.client.DefaultConsumer
|
||||
import com.rabbitmq.client.Envelope
|
||||
import fr.dcproject.common.entity.TargetRef
|
||||
import fr.dcproject.component.citizen.CitizenRef
|
||||
import fr.dcproject.component.follow.FollowArticleRepository
|
||||
import fr.dcproject.component.follow.FollowConstitutionRepository
|
||||
import fr.dcproject.component.follow.FollowSimple
|
||||
import fr.dcproject.component.follow.FollowForView
|
||||
import io.ktor.utils.io.errors.IOException
|
||||
import io.lettuce.core.RedisClient
|
||||
import io.lettuce.core.api.async.RedisAsyncCommands
|
||||
@@ -108,6 +107,6 @@ class NotificationConsumer(
|
||||
private class DecodedMessage(
|
||||
val event: EntityNotification,
|
||||
val rawMessage: String,
|
||||
val follow: FollowSimple<out TargetRef, CitizenRef>
|
||||
val follow: FollowForView<out TargetRef>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -9,9 +9,8 @@ import fr.dcproject.common.entity.TargetRef
|
||||
import fr.dcproject.component.article.ArticleRepository
|
||||
import fr.dcproject.component.article.ArticleWithTitleI
|
||||
import fr.dcproject.component.citizen.CitizenBasicI
|
||||
import fr.dcproject.component.citizen.CitizenRef
|
||||
import fr.dcproject.component.citizen.CitizenRepository
|
||||
import fr.dcproject.component.follow.FollowSimple
|
||||
import fr.dcproject.component.follow.FollowForView
|
||||
import java.util.UUID
|
||||
|
||||
class NotificationEmailSender(
|
||||
@@ -20,7 +19,7 @@ class NotificationEmailSender(
|
||||
private val citizenRepo: CitizenRepository,
|
||||
private val articleRepo: ArticleRepository
|
||||
) {
|
||||
fun sendEmail(follow: FollowSimple<out TargetRef, CitizenRef>) {
|
||||
fun sendEmail(follow: FollowForView<out TargetRef>) {
|
||||
val citizen = citizenRepo.findById(follow.createdBy.id) ?: noCitizen(follow.createdBy.id)
|
||||
val target = when (follow.target.reference) {
|
||||
"article" ->
|
||||
|
||||
Reference in New Issue
Block a user