feat: comment reporting

This commit is contained in:
rebelonion 2024-02-24 22:43:55 -06:00
parent a251dd4ffb
commit 55e156579b
6 changed files with 90 additions and 16 deletions

View file

@ -1,6 +1,5 @@
package ani.dantotsu.connections.comments package ani.dantotsu.connections.comments
import ani.dantotsu.BuildConfig
import ani.dantotsu.connections.anilist.Anilist import ani.dantotsu.connections.anilist.Anilist
import ani.dantotsu.settings.saving.PrefManager import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName import ani.dantotsu.settings.saving.PrefName
@ -33,7 +32,12 @@ object CommentsAPI {
suspend fun getCommentsForId(id: Int, page: Int = 1): CommentResponse? { suspend fun getCommentsForId(id: Int, page: Int = 1): CommentResponse? {
val url = "$address/comments/$id/$page" val url = "$address/comments/$id/$page"
val request = requestBuilder() val request = requestBuilder()
val json = request.get(url) val json = try {
request.get(url)
} catch (e: IOException) {
snackString("Failed to fetch comments")
return null
}
if (!json.text.startsWith("{")) return null if (!json.text.startsWith("{")) return null
val res = json.code == 200 val res = json.code == 200
if (!res && json.code != 404) { if (!res && json.code != 404) {
@ -42,7 +46,6 @@ object CommentsAPI {
val parsed = try { val parsed = try {
Json.decodeFromString<CommentResponse>(json.text) Json.decodeFromString<CommentResponse>(json.text)
} catch (e: Exception) { } catch (e: Exception) {
println("comments: $e")
return null return null
} }
return parsed return parsed
@ -51,7 +54,12 @@ object CommentsAPI {
suspend fun getRepliesFromId(id: Int, page: Int = 1): CommentResponse? { suspend fun getRepliesFromId(id: Int, page: Int = 1): CommentResponse? {
val url = "$address/comments/parent/$id/$page" val url = "$address/comments/parent/$id/$page"
val request = requestBuilder() val request = requestBuilder()
val json = request.get(url) val json = try {
request.get(url)
} catch (e: IOException) {
snackString("Failed to fetch comments")
return null
}
if (!json.text.startsWith("{")) return null if (!json.text.startsWith("{")) return null
val res = json.code == 200 val res = json.code == 200
if (!res && json.code != 404) { if (!res && json.code != 404) {
@ -60,7 +68,6 @@ object CommentsAPI {
val parsed = try { val parsed = try {
Json.decodeFromString<CommentResponse>(json.text) Json.decodeFromString<CommentResponse>(json.text)
} catch (e: Exception) { } catch (e: Exception) {
println("comments: $e")
return null return null
} }
return parsed return parsed
@ -69,7 +76,12 @@ object CommentsAPI {
suspend fun vote(commentId: Int, voteType: Int): Boolean { suspend fun vote(commentId: Int, voteType: Int): Boolean {
val url = "$address/comments/vote/$commentId/$voteType" val url = "$address/comments/vote/$commentId/$voteType"
val request = requestBuilder() val request = requestBuilder()
val json = request.post(url) val json = try {
request.post(url)
} catch (e: IOException) {
snackString("Failed to vote")
return false
}
val res = json.code == 200 val res = json.code == 200
if (!res) { if (!res) {
errorReason(json.code, json.text) errorReason(json.code, json.text)
@ -87,7 +99,12 @@ object CommentsAPI {
body.add("parent_comment_id", it.toString()) body.add("parent_comment_id", it.toString())
} }
val request = requestBuilder() val request = requestBuilder()
val json = request.post(url, requestBody = body.build()) val json = try{
request.post(url, requestBody = body.build())
} catch (e: IOException) {
snackString("Failed to comment")
return null
}
val res = json.code == 200 val res = json.code == 200
if (!res) { if (!res) {
errorReason(json.code, json.text) errorReason(json.code, json.text)
@ -96,7 +113,6 @@ object CommentsAPI {
val parsed = try { val parsed = try {
Json.decodeFromString<ReturnedComment>(json.text) Json.decodeFromString<ReturnedComment>(json.text)
} catch (e: Exception) { } catch (e: Exception) {
println("comment: $e")
snackString("Failed to parse comment") snackString("Failed to parse comment")
return null return null
} }
@ -120,7 +136,12 @@ object CommentsAPI {
suspend fun deleteComment(commentId: Int): Boolean { suspend fun deleteComment(commentId: Int): Boolean {
val url = "$address/comments/$commentId" val url = "$address/comments/$commentId"
val request = requestBuilder() val request = requestBuilder()
val json = request.delete(url) val json = try{
request.delete(url)
} catch (e: IOException) {
snackString("Failed to delete comment")
return false
}
val res = json.code == 200 val res = json.code == 200
if (!res) { if (!res) {
errorReason(json.code, json.text) errorReason(json.code, json.text)
@ -134,7 +155,12 @@ object CommentsAPI {
.add("content", content) .add("content", content)
.build() .build()
val request = requestBuilder() val request = requestBuilder()
val json = request.put(url, requestBody = body) val json = try {
request.put(url, requestBody = body)
} catch (e: IOException) {
snackString("Failed to edit comment")
return false
}
val res = json.code == 200 val res = json.code == 200
if (!res) { if (!res) {
errorReason(json.code, json.text) errorReason(json.code, json.text)
@ -145,7 +171,32 @@ object CommentsAPI {
suspend fun banUser(userId: String): Boolean { suspend fun banUser(userId: String): Boolean {
val url = "$address/ban/$userId" val url = "$address/ban/$userId"
val request = requestBuilder() val request = requestBuilder()
val json = request.post(url) val json = try{
request.post(url)
} catch (e: IOException) {
snackString("Failed to ban user")
return false
}
val res = json.code == 200
if (!res) {
errorReason(json.code, json.text)
}
return res
}
suspend fun reportComment(commentId: Int, username: String, mediaTitle: String): Boolean {
val url = "$address/report/$commentId"
val body = FormBody.Builder()
.add("username", username)
.add("mediaName", mediaTitle)
.build()
val request = requestBuilder()
val json = try{
request.post(url, requestBody = body)
} catch (e: IOException) {
snackString("Failed to report comment")
return false
}
val res = json.code == 200 val res = json.code == 200
if (!res) { if (!res) {
errorReason(json.code, json.text) errorReason(json.code, json.text)
@ -154,6 +205,7 @@ object CommentsAPI {
} }
suspend fun fetchAuthToken() { suspend fun fetchAuthToken() {
if (authToken != null) return
val url = "$address/authenticate" val url = "$address/authenticate"
val token = PrefManager.getVal(PrefName.AnilistToken, null as String?) ?: return val token = PrefManager.getVal(PrefName.AnilistToken, null as String?) ?: return
val body: FormBody = FormBody.Builder() val body: FormBody = FormBody.Builder()

View file

@ -66,7 +66,8 @@ class AnimeWatchAdapter(
startActivity( startActivity(
fragment.requireContext(), fragment.requireContext(),
Intent(fragment.requireContext(), CommentsActivity::class.java) Intent(fragment.requireContext(), CommentsActivity::class.java)
.putExtra("mediaId", media.id), .putExtra("mediaId", media.id)
.putExtra("mediaName", media.mainName()),
null null
) )
} }

View file

@ -17,7 +17,6 @@ import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.snackString import ani.dantotsu.snackString
import com.xwray.groupie.GroupieAdapter import com.xwray.groupie.GroupieAdapter
import com.xwray.groupie.Section import com.xwray.groupie.Section
import com.xwray.groupie.viewbinding.BindableItem import com.xwray.groupie.viewbinding.BindableItem
import io.noties.markwon.Markwon import io.noties.markwon.Markwon
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@ -127,6 +126,15 @@ class CommentItem(val comment: Comment,
} }
} }
} }
viewBinding.commentReport.setOnClickListener {
val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())
scope.launch {
val success = CommentsAPI.reportComment(comment.commentId, comment.username, commentsActivity.mediaName)
if (success) {
snackString("Comment Reported")
}
}
}
//fill the icon if the user has liked the comment //fill the icon if the user has liked the comment
setVoteButtons(viewBinding) setVoteButtons(viewBinding)
viewBinding.commentUpVote.setOnClickListener { viewBinding.commentUpVote.setOnClickListener {

View file

@ -57,6 +57,7 @@ class CommentsActivity : AppCompatActivity() {
private val section = Section() private val section = Section()
private val adapter = GroupieAdapter() private val adapter = GroupieAdapter()
private var mediaId: Int = -1 private var mediaId: Int = -1
var mediaName: String = ""
private var backgroundColor: Int = 0 private var backgroundColor: Int = 0
var pagesLoaded = 1 var pagesLoaded = 1
var totalPages = 1 var totalPages = 1
@ -69,6 +70,7 @@ class CommentsActivity : AppCompatActivity() {
initActivity(this) initActivity(this)
//get the media id from the intent //get the media id from the intent
val mediaId = intent.getIntExtra("mediaId", -1) val mediaId = intent.getIntExtra("mediaId", -1)
mediaName = intent.getStringExtra("mediaName")?:"unknown"
if (mediaId == -1) { if (mediaId == -1) {
snackString("Invalid Media ID") snackString("Invalid Media ID")
finish() finish()

View file

@ -72,7 +72,8 @@ class MangaReadAdapter(
startActivity( startActivity(
fragment.requireContext(), fragment.requireContext(),
Intent(fragment.requireContext(), CommentsActivity::class.java) Intent(fragment.requireContext(), CommentsActivity::class.java)
.putExtra("mediaId", media.id), .putExtra("mediaId", media.id)
.putExtra("mediaName", media.mainName()),
null null
) )
} }

View file

@ -159,6 +159,17 @@
android:textSize="12sp" android:textSize="12sp"
tools:ignore="HardcodedText" /> tools:ignore="HardcodedText" />
<TextView
android:id="@+id/commentReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:alpha="0.6"
android:fontFamily="@font/poppins_semi_bold"
android:text="Report"
android:textSize="12sp"
tools:ignore="HardcodedText" />
<TextView <TextView
android:id="@+id/commentBanUser" android:id="@+id/commentBanUser"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -234,8 +245,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="8dp" android:paddingStart="8dp"
android:visibility="visible" android:visibility="gone"
tools:visibility="visible"
tools:ignore="RtlSymmetry" /> tools:ignore="RtlSymmetry" />
</LinearLayout> </LinearLayout>