import net.pterodactylus.sone.data.Image
import net.pterodactylus.sone.data.Post
import net.pterodactylus.sone.data.PostReply
+import net.pterodactylus.sone.data.PostReplyShell
+import net.pterodactylus.sone.data.PostShell
import net.pterodactylus.sone.data.Sone
import net.pterodactylus.sone.data.allAlbums
import net.pterodactylus.sone.data.allImages
import net.pterodactylus.sone.data.impl.AlbumBuilderImpl
import net.pterodactylus.sone.data.impl.ImageBuilderImpl
import net.pterodactylus.sone.data.newestReplyFirst
+import net.pterodactylus.sone.data.toShell
import net.pterodactylus.sone.database.AlbumBuilder
import net.pterodactylus.sone.database.Database
import net.pterodactylus.sone.database.DatabaseException
private val writeLock: WriteLock by lazy { lock.writeLock() }
private val configurationLoader = ConfigurationLoader(configuration)
private val allSones = mutableMapOf<String, Sone>()
- private val allPosts = mutableMapOf<String, Post>()
- private val sonePosts: Multimap<String, Post> = HashMultimap.create<String, Post>()
+ private val allPosts = mutableMapOf<String, PostShell>()
+ private val sonePosts: Multimap<String, PostShell> = HashMultimap.create<String, PostShell>()
private val knownPosts = mutableSetOf<String>()
- private val allPostReplies = mutableMapOf<String, MemoryPostReply.Shell>()
+ private val allPostReplies = mutableMapOf<String, PostReplyShell>()
private val sonePostReplies: Multimap<String, PostReply> = TreeMultimap.create<String, PostReply>(Comparator { leftString, rightString -> leftString.compareTo(rightString) }, newestReplyFirst)
private val knownPostReplies = mutableSetOf<String>()
private val allAlbums = mutableMapOf<String, Album>()
removeSone(sone)
allSones[sone.id] = sone
- sonePosts.putAll(sone.id, sone.posts)
- for (post in sone.posts) {
+ sonePosts.putAll(sone.id, sone.posts.map(Post::toShell))
+ for (post in sone.posts.map(Post::toShell)) {
allPosts[post.id] = post
}
sonePostReplies.putAll(sone.id, sone.replies)
override fun getFollowingTime(friendSoneId: String) =
memoryFriendDatabase.getFollowingTime(friendSoneId)
- override fun getPost(postId: String) =
- readLock.withLock { allPosts[postId] }
+ override fun getPost(postId: String): Post? =
+ readLock.withLock { allPosts[postId]?.build(newPostBuilder()) }
override fun getPosts(soneId: String): Collection<Post> =
- sonePosts[soneId].toSet()
+ sonePosts[soneId].map { it.build(newPostBuilder()) }.toSet()
override fun getDirectedPosts(recipientId: String) =
readLock.withLock {
- allPosts.values.filter {
- it.recipientId.orNull() == recipientId
- }
+ allPosts.values
+ .filter { it.recipientId == recipientId }
+ .map { it.build(newPostBuilder()) }
}
override fun newPostBuilder(): PostBuilder = MemoryPostBuilder(this, this)
override fun storePost(post: Post) {
checkNotNull(post, "post must not be null")
writeLock.withLock {
- allPosts[post.id] = post
- sonePosts[post.sone.id].add(post)
+ post.toShell().also { shell ->
+ allPosts[post.id] = shell
+ sonePosts[post.sone.id].add(shell)
+ }
}
}
checkNotNull(post, "post must not be null")
writeLock.withLock {
allPosts.remove(post.id)
- sonePosts[post.sone.id].remove(post)
+ sonePosts[post.sone.id].remove(post.toShell())
post.sone.removePost(post)
}
}