+ @Subscribe
+ fun onNewPostReply(event: NewPostReplyFoundEvent) {
+ event.postReply.let { postReply ->
+ postReply.sone.isLocal.onFalse {
+ if (postReply.text.hasLinksToLocalSones()) {
+ postReply.post
+ .also { mentionedPosts += it }
+ .let(::MentionOfLocalSoneFoundEvent)
+ ?.also(eventBus::post)
+ }
+ }
+ }
+ }
+
+ @Subscribe
+ fun onPostRemoved(event: PostRemovedEvent) {
+ unmentionPost(event.post)
+ }
+
+ @Subscribe
+ fun onPostMarkedKnown(event: MarkPostKnownEvent) {
+ unmentionPost(event.post)
+ }
+
+ @Subscribe
+ fun onReplyRemoved(event: PostReplyRemovedEvent) {
+ event.postReply.post.let {
+ if ((!it.text.hasLinksToLocalSones() || it.isKnown) && (it.replies.filterNot { it == event.postReply }.none { it.text.hasLinksToLocalSones() && !it.isKnown })) {
+ unmentionPost(it)
+ }
+ }
+ }
+
+ private fun unmentionPost(post: Post) {
+ if (post in mentionedPosts) {
+ eventBus.post(MentionOfLocalSoneRemovedEvent(post))
+ mentionedPosts -= post
+ }
+ }
+
+ private val mentionedPosts = mutableSetOf<Post>()
+
+ private fun String.hasLinksToLocalSones() = soneTextParser.parse(this, null)
+ .filterIsInstance<SonePart>()
+ .any { it.sone.isLocal }
+
+ private val Post.replies get() = postReplyProvider.getReplies(id)
+