Faster way to count reports.

This commit is contained in:
Vitor Pamplona
2023-03-04 07:12:42 -05:00
parent 308a79a305
commit c0bdb40c7d
3 changed files with 11 additions and 3 deletions
@@ -479,19 +479,19 @@ class Account(
fun isAcceptable(user: User): Boolean { fun isAcceptable(user: User): Boolean {
return !isHidden(user) // if user hasn't hided this author return !isHidden(user) // if user hasn't hided this author
&& user.reportsBy( userProfile() ).isEmpty() // if user has not reported this post && user.reportsBy( userProfile() ).isEmpty() // if user has not reported this post
&& user.reportAuthorsBy( userProfile().follows ).size < 5 && user.countReportAuthorsBy( userProfile().follows ) < 5
} }
fun isAcceptableDirect(note: Note): Boolean { fun isAcceptableDirect(note: Note): Boolean {
return note.reportsBy( userProfile() ).isEmpty() // if user has not reported this post return note.reportsBy( userProfile() ).isEmpty() // if user has not reported this post
&& note.reportAuthorsBy( userProfile().follows ).size < 5 // if it has 5 reports by reliable users && note.countReportAuthorsBy( userProfile().follows ) < 5 // if it has 5 reports by reliable users
} }
fun isAcceptable(note: Note): Boolean { fun isAcceptable(note: Note): Boolean {
return note.author?.let { isAcceptable(it) } ?: true // if user hasn't hided this author return note.author?.let { isAcceptable(it) } ?: true // if user hasn't hided this author
&& isAcceptableDirect(note) && isAcceptableDirect(note)
&& (note.event !is RepostEvent && (note.event !is RepostEvent
|| (note.event is RepostEvent && note.replyTo?.firstOrNull { isAcceptableDirect(it) } != null) || (note.event is RepostEvent && note.replyTo?.firstOrNull { isAcceptableDirect(it) } != null)
) // is not a reaction about a blocked post ) // is not a reaction about a blocked post
} }
@@ -229,6 +229,10 @@ open class Note(val idHex: String) {
return reports.keys.filter { it in users } return reports.keys.filter { it in users }
} }
fun countReportAuthorsBy(users: Set<User>): Int {
return reports.keys.count { it in users }
}
fun reportsBy(users: Set<User>): List<Note> { fun reportsBy(users: Set<User>): List<Note> {
return reportAuthorsBy(users).mapNotNull { return reportAuthorsBy(users).mapNotNull {
reports[it] reports[it]
@@ -201,6 +201,10 @@ class User(val pubkeyHex: String) {
return reports.keys.filter { it in users } return reports.keys.filter { it in users }
} }
fun countReportAuthorsBy(users: Set<User>): Int {
return reports.keys.count { it in users }
}
fun reportsBy(users: Set<User>): List<Note> { fun reportsBy(users: Set<User>): List<Note> {
return reportAuthorsBy(users).mapNotNull { return reportAuthorsBy(users).mapNotNull {
reports[it] reports[it]