Skip to content

Commit

Permalink
NT-2058:Updates – Showing project comments instead of update's (#1298)
Browse files Browse the repository at this point in the history
* Add get the post using update id query

* Add getProjectUpdateComments

* call  getProjectUpdateComments

* Add project id encoded to query

* commentableId set to project or post id in comment envolpe to use it to create Post

* commentableId  set to project or update to post comment

* Update test

* Fix code comments
  • Loading branch information
hadia committed Jun 21, 2021
1 parent ae9ac68 commit ee62ec5
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 61 deletions.
15 changes: 15 additions & 0 deletions app/src/main/graphql/fragments.graphql
Expand Up @@ -142,3 +142,18 @@ fragment payment on CreditCard {
type
state
}

fragment freeformPost on FreeformPost {
comments (first: $limit, after: $cursor) {
edges {
cursor
node {
...comment
}
}
pageInfo {
...pageInfo
}
totalCount
}
}
50 changes: 28 additions & 22 deletions app/src/main/graphql/project.graphql
Expand Up @@ -46,29 +46,35 @@ mutation CreateComment($body: String!, $commentableId: ID!, $parentId: ID, $clie
}
}

query GetProjectComments($slug: String!, $limit: Int!, $cursor: String){
project(slug: $slug) {
collaborators {
edges {
node {
id
name
}
}
query GetProjectComments($slug: String!, $limit: Int!, $cursor: String) {
project(slug: $slug) {
id
collaborators {
edges {
node {
id
name
}
comments(first: $limit, after: $cursor) {
edges {
cursor
node {
authorBadges
...comment
}
}
pageInfo {
...pageInfo
}
totalCount
}
}
comments(first: $limit, after: $cursor) {
edges {
cursor
node {
...comment
}
}
}
pageInfo {
...pageInfo
}
totalCount
}
}
}

query GetProjectUpdateComments($id: ID!,$limit: Int!, $cursor: String){
post(id: $id) {
id
...freeformPost
}
}
Expand Up @@ -71,6 +71,7 @@ class CommentFactory {
}

fun liveCommentCardData(comment: String = "Some Comment", createdAt: DateTime, currentUser: User, isDelete: Boolean = false, repliesCount: Int = 0): CommentCardData {
val project = ProjectFactory.project().toBuilder().creator(UserFactory.creator().toBuilder().id(278438049).build()).build()
return CommentCardData(
Comment.builder()
.body(comment)
Expand All @@ -84,7 +85,8 @@ class CommentFactory {
.author(currentUser)
.build(),
0,
ProjectFactory.project().toBuilder().creator(UserFactory.creator().toBuilder().id(278438049).build()).build()
project.id().toString(),
project

)
}
Expand Down
Expand Up @@ -71,6 +71,23 @@ open class MockApolloClient : ApolloClientType {
)
}

override fun getProjectUpdateComments(
updateId: String,
cursor: String?,
limit: Int
): Observable<CommentEnvelope> {
return Observable.just(
CommentEnvelope.builder()
.pageInfoEnvelope(
PageInfoEnvelopeFactory.pageInfoEnvelope()
)
.comments(listOf(CommentFactory.comment()))
.commentableId(updateId)
.totalCount(1)
.build()
)
}

override fun getRepliesForComment(comment: Comment, cursor: String, pageSize: Int): Observable<CommentEnvelope> {
return Observable.just(CommentEnvelopeFactory.emptyCommentsEnvelope())
}
Expand Down
Expand Up @@ -36,6 +36,8 @@ interface ApolloClientType {

fun getProjectComments(slug: String, cursor: String?, limit: Int = 25): Observable<CommentEnvelope>

fun getProjectUpdateComments(updateId: String, cursor: String?, limit: Int = 25): Observable<CommentEnvelope>

fun getRepliesForComment(comment: Comment, cursor: String = "", pageSize: Int = 25): Observable<CommentEnvelope>

fun createComment(comment: PostCommentData): Observable<Comment>
Expand Down
50 changes: 49 additions & 1 deletion app/src/main/java/com/kickstarter/services/KSApolloClient.kt
Expand Up @@ -218,6 +218,7 @@ class KSApolloClient(val service: ApolloClient) : ApolloClientType {
}

CommentEnvelope.builder()
.commentableId(project?.id())
.comments(comments)
.totalCount(project?.comments()?.totalCount() ?: 0)
.pageInfoEnvelope(createPageInfoObject(project?.comments()?.pageInfo()?.fragments()?.pageInfo()))
Expand All @@ -235,6 +236,53 @@ class KSApolloClient(val service: ApolloClient) : ApolloClientType {
}.subscribeOn(Schedulers.io())
}

override fun getProjectUpdateComments(updateId: String, cursor: String?, limit: Int): Observable<CommentEnvelope> {
return Observable.defer {
val ps = PublishSubject.create<CommentEnvelope>()

this.service.query(
GetProjectUpdateCommentsQuery.builder()
.cursor(cursor)
.id(updateId)
.limit(limit)
.build()
)
.enqueue(object : ApolloCall.Callback<GetProjectUpdateCommentsQuery.Data>() {
override fun onFailure(e: ApolloException) {
ps.onError(e)
}

override fun onResponse(response: Response<GetProjectUpdateCommentsQuery.Data>) {
response.data?.let { data ->
Observable.just(data.post())
.filter { it?.fragments()?.freeformPost()?.comments() != null }
.map { post ->

val comments = post?.fragments()?.freeformPost()?.comments()?.edges()?.map { edge ->
createCommentObject(edge?.node()?.fragments()?.comment()).toBuilder()
.cursor(edge?.cursor())
.build()
}

CommentEnvelope.builder()
.comments(comments)
.commentableId(post?.id())
.totalCount(post?.fragments()?.freeformPost()?.comments()?.totalCount() ?: 0)
.pageInfoEnvelope(createPageInfoObject(post?.fragments()?.freeformPost()?.comments()?.pageInfo()?.fragments()?.pageInfo()))
.build()
}
.filter { ObjectUtils.isNotNull(it) }
.subscribe {
ps.onNext(it)
ps.onCompleted()
}
}
}
})
return@defer ps
}.subscribeOn(Schedulers.io())
}

override fun getRepliesForComment(comment: Comment, cursor: String, pageSize: Int): Observable<CommentEnvelope> {
return Observable.defer {
val ps = PublishSubject.create<CommentEnvelope>()
Expand Down Expand Up @@ -269,7 +317,7 @@ class KSApolloClient(val service: ApolloClient) : ApolloClientType {
this.service.mutate(
CreateCommentMutation.builder()
.parentId(comment.parentId)
.commentableId(encodeRelayId(comment.project))
.commentableId(comment.commentableId)
.clientMutationId(comment.clientMutationId)
.body(comment.body)
.build()
Expand Down
Expand Up @@ -9,6 +9,7 @@ import kotlinx.android.parcel.Parcelize
@AutoGson
class CommentEnvelope(
val comments: List<Comment>?,
val commentableId: String?,
val pageInfoEnvelope: PageInfoEnvelope?,
val totalCount: Int?
) : Parcelable {
Expand All @@ -17,19 +18,21 @@ class CommentEnvelope(
@AutoGson
data class Builder(
var comments: List<Comment>? = null,
var commentableId: String? = null,
var pageInfoEnvelope: PageInfoEnvelope? = null,
var totalCount: Int? = 0
) : Parcelable {

fun comments(comments: List<Comment>?) = apply { this.comments = comments }
fun commentableId(commentableId: String?) = apply { this.commentableId = commentableId }
fun pageInfoEnvelope(pageInfoEnvelope: PageInfoEnvelope?) = apply { this.pageInfoEnvelope = pageInfoEnvelope }
fun totalCount(totalCount: Int?) = apply { this.totalCount = totalCount }
fun build() = CommentEnvelope(comments, pageInfoEnvelope, totalCount)
fun build() = CommentEnvelope(comments, commentableId, pageInfoEnvelope, totalCount)
}

companion object {
fun builder() = Builder()
}

fun toBuilder() = Builder(this.comments, this.pageInfoEnvelope, this.totalCount)
fun toBuilder() = Builder(this.comments, this.commentableId, this.pageInfoEnvelope, this.totalCount)
}
@@ -1,10 +1,8 @@
package com.kickstarter.services.mutations

import com.kickstarter.models.Project

data class PostCommentData(
val body: String,
val parentId: String?,
val project: Project,
val commentableId: String,
val clientMutationId: String?
)
7 changes: 5 additions & 2 deletions app/src/main/java/com/kickstarter/ui/data/CommentCardData.kt
Expand Up @@ -9,24 +9,27 @@ import kotlinx.android.parcel.Parcelize
class CommentCardData(
val comment: Comment?,
val commentCardState: Int,
val commentableId: String?,
val project: Project?
) : Parcelable {

@Parcelize
data class Builder(
var comment: Comment? = null,
var commentCardState: Int = 0,
var commentableId: String? = null,
var project: Project? = null
) : Parcelable {
fun comment(comment: Comment?) = apply { this.comment = comment }
fun commentCardState(commentCardState: Int) = apply { this.commentCardState = commentCardState }
fun project(project: Project?) = apply { this.project = project }
fun build() = CommentCardData(comment, commentCardState, project)
fun commentableId(commentableId: String?) = apply { this.commentableId = commentableId }
fun build() = CommentCardData(comment, commentCardState, commentableId, project)
}

companion object {
fun builder() = CommentCardData.Builder()
}

fun toBuilder() = CommentCardData.Builder(this.comment, this.commentCardState, this.project)
fun toBuilder() = CommentCardData.Builder(this.comment, this.commentCardState, this.commentableId, this.project)
}
Expand Up @@ -141,8 +141,9 @@ interface CommentsViewHolderViewModel {
.withLatestFrom(currentUser.loggedInUser()) { input, user -> Pair(input, user) }
.filter { shouldCommentBePosted(it) }
.map {
Pair(requireNotNull(it.first.comment?.body()), requireNotNull(it.first.project))
Pair(requireNotNull(it.first), requireNotNull(it.first.project))
}

postComment(commentData, internalError)

this.internalError
Expand Down Expand Up @@ -247,11 +248,26 @@ interface CommentsViewHolderViewModel {
* Handles the logic for posting comments (new ones, and the retry attempts)
* @param commentData will emmit only in case we need to post a new comment
*/
private fun postComment(commentData: Observable<Pair<String, Project>>, errorObservable: BehaviorSubject<Throwable>) {
commentData
private fun postComment(commentData: Observable<Pair<CommentCardData, Project>>, errorObservable: BehaviorSubject<Throwable>) {
val postCommentData = commentData
.map {
executePostCommentMutation(it, errorObservable)
Pair(
requireNotNull(it.first.commentableId),
requireNotNull(it.first?.comment?.body())
)
}
.map {
PostCommentData(
commentableId = it.first,
body = it.second,
clientMutationId = null,
parentId = null
)
}

postCommentData.map {
executePostCommentMutation(it, errorObservable)
}
.switchMap {
it
}
Expand All @@ -262,7 +278,7 @@ interface CommentsViewHolderViewModel {
}

Observable
.combineLatest(onRetryViewClicked, commentData) { _, newData ->
.combineLatest(onRetryViewClicked, postCommentData) { _, newData ->
return@combineLatest executePostCommentMutation(newData, errorObservable)
}.switchMap {
it
Expand Down Expand Up @@ -300,19 +316,14 @@ interface CommentsViewHolderViewModel {

/**
* Function that will execute the PostCommentMutation
* @param commentData holds the comment body and the project to be posted
* @param postCommentData holds the comment body and the commentableId for project or update to be posted
* // TODO: for the future threads wi will need to send to the mutation not just the body,
* // TODO: we will need the entire comment plus very important the [parentId]
* @return Observable<Comment>
*/
private fun executePostCommentMutation(commentData: Pair<String, Project>, errorObservable: BehaviorSubject<Throwable>) =
private fun executePostCommentMutation(postCommentData: PostCommentData, errorObservable: BehaviorSubject<Throwable>) =
this.apolloClient.createComment(
PostCommentData(
project = commentData.second,
body = commentData.first,
clientMutationId = null,
parentId = null
)
postCommentData
).doOnError {
errorObservable.onNext(it)
}
Expand Down

0 comments on commit ee62ec5

Please sign in to comment.