begin concept: insert_new_post

new-idea
Avril 4 years ago
parent e6aa5da965
commit 519cd814eb
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -171,6 +171,12 @@ impl Post
{
&self.id
}
/// `UserID` of the post owner, if there is one.
#[inline] pub fn owner_id(&self) -> Option<&user::UserID>
{
self.owner.as_ref()
}
/// The user-set name for this post if there is one.
#[inline] pub fn own_name(&self) -> Option<&str>
{

@ -1,10 +1,14 @@
use super::*;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::sync::{
RwLock,
mpsc,
};
use generational_arena::{
Arena,Index,
};
use futures::prelude::*;
use user::{User, UserID};
use post::{Post, PostID};
@ -12,6 +16,14 @@ use post::{Post, PostID};
mod freeze;
pub use freeze::*;
/// An `Arc<RwLock<T>>` wrapper
type SharedMut<T> = Arc<RwLock<T>>;
/// A shared pointer to a post
pub type SharedPost = SharedMut<Post>;
/// A shared pointer to a user
pub type SharedUser = SharedMut<User>;
#[derive(Debug)]
struct Oneesan
{
@ -57,4 +69,56 @@ impl State
}
))
}
/// Insert a new post
pub async fn insert_new_post(&self, post: Post) -> SharedPost
{
let mut onee = self.0.oneesan.write().await;
let id = *post.post_id();
if let Some(&owner) = post.owner_id() {
//TODO: Add `index` to `posts_user_map`. (how do we get `index` before this)
}
let post = Arc::new(RwLock::new(post));
//TODO: If post exists with the same ID, remove it first.
let index = onee.posts.insert(Arc::clone(&post));
onee.posts_map.insert(id, index);
post
}
/// Consume into a stream that yields all users lazily.
pub fn all_users_stream(self: Arc<Self>) -> impl Stream<Item=SharedUser>
{
let (mut tx, rx) = mpsc::channel(1);
tokio::spawn(async move {
let onee = self.0.oneesan.read().await;
for (_, user) in onee.users.iter()
{
if tx.send(user.clone()).await.is_err() {
break;
}
}
});
rx
}
/// Consume into a stream that yields all posts lazily.
pub fn all_posts_stream(self: Arc<Self>) -> impl Stream<Item=SharedPost>
{
let (mut tx, rx) = mpsc::channel(1);
tokio::spawn(async move {
let onee = self.0.oneesan.read().await;
for (_, post) in onee.posts.iter()
{
if tx.send(post.clone()).await.is_err() {
break;
}
}
});
rx
}
}

Loading…
Cancel
Save