Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions app/src/main/graphql/pub/hackers/android/operations.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@ mutation SaveArticleDraft($title: String!, $content: Markdown!, $tags: [String!]
id
title
content
contentHtml
tags
created
updated
Expand Down Expand Up @@ -867,6 +868,7 @@ query ArticleDraft($id: ID!) {
id
title
content
contentHtml
tags
created
updated
Expand Down Expand Up @@ -930,3 +932,51 @@ mutation UpdateAccount($input: UpdateAccountInput!) {
mutation MarkNotificationsAsRead {
markNotificationsAsRead
}

query ArticleForEdit($id: ID!) {
node(id: $id) {
... on Article {
id
tags
allowLlmTranslation
contents {
title
rawContent
language
originalLanguage
}
}
}
}

mutation UpdateArticle(
$articleId: ID!,
$title: String,
$content: Markdown,
$tags: [String!],
$language: Locale,
$allowLlmTranslation: Boolean
) {
updateArticle(input: {
articleId: $articleId,
title: $title,
content: $content,
tags: $tags,
language: $language,
allowLlmTranslation: $allowLlmTranslation
}) {
... on UpdateArticlePayload {
article {
id
name
url
}
}
... on InvalidInputError {
inputPath
}
... on NotAuthenticatedError {
notAuthenticated
}
}
}
31 changes: 31 additions & 0 deletions app/src/main/graphql/pub/hackers/android/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,11 @@ type ArticleContent implements Node {

published: DateTime!

"""
The raw markdown content for editing.
"""
rawContent: Markdown!

summary: String

summaryStarted: DateTime
Expand Down Expand Up @@ -947,6 +952,8 @@ type Mutation {

updateAccount(input: UpdateAccountInput!): UpdateAccountPayload!

updateArticle(input: UpdateArticleInput!): UpdateArticleResult!

uploadMedia(input: UploadMediaInput!): UploadMediaResult!

verifyPasskeyRegistration(accountId: ID!, name: String!, platform: String = "web", registrationResponse: JSON!): PasskeyRegistrationResult!
Expand Down Expand Up @@ -1979,6 +1986,30 @@ type UpdateAccountPayload {
clientMutationId: ID
}

input UpdateArticleInput {
allowLlmTranslation: Boolean

articleId: ID!

clientMutationId: ID

content: Markdown

language: Locale

tags: [String!]

title: String
}

type UpdateArticlePayload {
article: Article!

clientMutationId: ID
}

union UpdateArticleResult = InvalidInputError|NotAuthenticatedError|UpdateArticlePayload

input UploadMediaInput {
clientMutationId: ID

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/pub/hackers/android/FeatureFlags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package pub.hackers.android
object FeatureFlags {
const val PASSKEY_AUTH_ENABLED = false
const val MARK_NOTIFICATIONS_AS_READ_ENABLED = false
const val ARTICLE_EDIT_ENABLED = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlinx.coroutines.withContext
import pub.hackers.android.domain.model.*
import pub.hackers.android.graphql.ArticleDraftQuery
import pub.hackers.android.graphql.ArticleDraftsQuery
import pub.hackers.android.graphql.ArticleForEditQuery
import pub.hackers.android.graphql.ActorArticlesQuery
import pub.hackers.android.graphql.ActorByHandleQuery
import pub.hackers.android.graphql.ActorNotesQuery
Expand Down Expand Up @@ -52,6 +53,7 @@ import pub.hackers.android.graphql.UnblockActorMutation
import pub.hackers.android.graphql.UnfollowActorMutation
import pub.hackers.android.graphql.UnsharePostMutation
import pub.hackers.android.graphql.UpdateAccountMutation
import pub.hackers.android.graphql.UpdateArticleMutation
import pub.hackers.android.graphql.ViewerQuery
import pub.hackers.android.graphql.type.AccountLinkInput
import pub.hackers.android.graphql.type.UpdateAccountInput
Expand Down Expand Up @@ -1239,6 +1241,7 @@ class HackersPubRepository @Inject constructor(
id = draft.id,
title = draft.title,
content = draft.content.toString(),
contentHtml = draft.contentHtml.toString(),
tags = draft.tags,
created = Instant.parse(draft.created.toString()),
updated = Instant.parse(draft.updated.toString())
Expand Down Expand Up @@ -1341,6 +1344,7 @@ class HackersPubRepository @Inject constructor(
id = node.id,
title = node.title,
content = node.content.toString(),
contentHtml = "",
tags = node.tags,
created = Instant.parse(node.created.toString()),
updated = Instant.parse(node.updated.toString())
Expand Down Expand Up @@ -1369,6 +1373,7 @@ class HackersPubRepository @Inject constructor(
id = draft.id,
title = draft.title,
content = draft.content.toString(),
contentHtml = draft.contentHtml.toString(),
tags = draft.tags,
created = Instant.parse(draft.created.toString()),
updated = Instant.parse(draft.updated.toString())
Expand All @@ -1380,6 +1385,83 @@ class HackersPubRepository @Inject constructor(
}
}

suspend fun getEditableArticle(articleId: String): Result<EditableArticle> {
return try {
val response = apolloClient.query(ArticleForEditQuery(articleId))
.fetchPolicy(FetchPolicy.NetworkOnly)
.execute()

if (response.hasErrors()) {
Result.failure(Exception(response.errors?.firstOrNull()?.message ?: "Unknown error"))
} else {
val article = response.data?.node?.onArticle
?: return Result.failure(Exception("Article not found"))
val original = article.contents.firstOrNull { it.originalLanguage == null }
?: article.contents.firstOrNull()
?: return Result.failure(Exception("Article content not found"))
Result.success(
EditableArticle(
id = article.id,
title = original.title,
content = original.rawContent.toString(),
tags = article.tags,
language = original.language.toString(),
allowLlmTranslation = article.allowLlmTranslation
)
)
}
} catch (e: Exception) {
Result.failure(e)
}
}

suspend fun updateArticle(
articleId: String,
title: String,
content: String,
tags: List<String>,
language: String,
allowLlmTranslation: Boolean
): Result<PublishedArticle> {
return try {
val response = apolloClient.mutation(
UpdateArticleMutation(
articleId = articleId,
title = Optional.present(title),
content = Optional.present(content),
tags = Optional.present(tags),
language = Optional.present(language),
allowLlmTranslation = Optional.present(allowLlmTranslation)
)
).execute()

if (response.hasErrors()) {
Result.failure(Exception(response.errors?.firstOrNull()?.message ?: "Unknown error"))
} else {
val result = response.data?.updateArticle
when {
result?.onUpdateArticlePayload != null -> {
val article = result.onUpdateArticlePayload.article
Result.success(
PublishedArticle(
id = article.id,
name = article.name,
url = article.url?.toString()
)
)
}
result?.onInvalidInputError != null ->
Result.failure(Exception("Invalid input: ${result.onInvalidInputError.inputPath}"))
result?.onNotAuthenticatedError != null ->
Result.failure(Exception("Not authenticated"))
else -> Result.failure(Exception("Unknown error"))
}
}
} catch (e: Exception) {
Result.failure(e)
}
}

// Extension functions to convert GraphQL fragment types to domain models
private fun PostFields.toPost(
sharedPost: Post? = null,
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/pub/hackers/android/domain/model/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ data class ArticleDraft(
val id: String,
val title: String,
val content: String,
val contentHtml: String,
val tags: List<String>,
val created: Instant,
val updated: Instant
Expand All @@ -287,6 +288,16 @@ data class PublishedArticle(
val url: String?
)

@Immutable
data class EditableArticle(
val id: String,
val title: String,
val content: String,
val tags: List<String>,
val language: String,
val allowLlmTranslation: Boolean
)

@Immutable
data class ProfileResult(
val actor: Actor,
Expand Down
30 changes: 24 additions & 6 deletions app/src/main/java/pub/hackers/android/ui/HackersPubApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,13 @@ sealed class DetailScreen(val route: String) {
}
}
data object RecommendedActors : DetailScreen("recommended-actors")
data object ComposeArticle : DetailScreen("compose-article?draftId={draftId}") {
fun createRoute(draftId: String? = null): String {
return if (draftId != null) "compose-article?draftId=$draftId" else "compose-article"
data object ComposeArticle : DetailScreen("compose-article?draftId={draftId}&articleId={articleId}") {
fun createRoute(draftId: String? = null, articleId: String? = null): String {
val params = buildList {
if (draftId != null) add("draftId=$draftId")
if (articleId != null) add("articleId=$articleId")
}
return if (params.isEmpty()) "compose-article" else "compose-article?" + params.joinToString("&")
}
}
data object Drafts : DetailScreen("drafts")
Expand Down Expand Up @@ -532,6 +536,9 @@ fun HackersPubApp(
onPostClick = { id ->
navController.navigate(DetailScreen.PostDetail.createRoute(id))
},
onEditArticleClick = { id ->
navController.navigate(DetailScreen.ComposeArticle.createRoute(articleId = id))
},
isLoggedIn = isLoggedIn
)
}
Expand Down Expand Up @@ -612,18 +619,29 @@ fun HackersPubApp(
type = NavType.StringType
nullable = true
defaultValue = null
},
navArgument("articleId") {
type = NavType.StringType
nullable = true
defaultValue = null
}
)
) { backStackEntry ->
val draftId = backStackEntry.arguments?.getString("draftId")
val articleId = backStackEntry.arguments?.getString("articleId")
ComposeArticleScreen(
draftId = draftId,
articleId = articleId,
onSaveSuccess = {
navController.popBackStack()
},
onPublishSuccess = { articleId ->
navController.popBackStack()
navController.navigate(DetailScreen.PostDetail.createRoute(articleId))
onPublishSuccess = { updatedId ->
if (articleId != null) {
navController.popBackStack()
} else {
navController.popBackStack()
navController.navigate(DetailScreen.PostDetail.createRoute(updatedId))
}
},
onNavigateBack = {
navController.popBackStack()
Expand Down
Loading