Browse Source

Hide drafts unless logged in

master
Dylan Baker 3 years ago
parent
commit
964d667d9b
4 changed files with 49 additions and 25 deletions
  1. 10
    3
      oslo-lib/src/fs.rs
  2. 7
    2
      oslo-lib/src/routes.rs
  3. 28
    20
      templates/components.html
  4. 4
    0
      templates/edit.html

+ 10
- 3
oslo-lib/src/fs.rs View File

@@ -25,13 +25,20 @@ pub async fn get_all_posts() -> Result<Vec<Post>, Error> {
25 25
     Ok(posts)
26 26
 }
27 27
 
28
-pub async fn get_one_post(slug: String) -> Result<Post, Error> {
28
+pub async fn get_one_post(slug: String, show_drafts: bool) -> Result<Post, Error> {
29 29
     let posts_dir = get_posts_directory_path()?;
30 30
     let path = posts_dir.join(format!("{}.json", slug));
31 31
     let contents = read_post_from_disk(&path).await?;
32 32
     let post = Post::from_str(&contents)?;
33 33
 
34
-    Ok(post)
34
+    if post.draft && !show_drafts {
35
+        Err(Error::new(
36
+            ErrorKind::NotFound,
37
+            format!("Can't find post with the slug '{}'", post.slug),
38
+        ))
39
+    } else {
40
+        Ok(post)
41
+    }
35 42
 }
36 43
 
37 44
 pub fn delete_post(slug: String) -> Result<(), Error> {
@@ -47,7 +54,7 @@ async fn read_post_from_disk(path: &PathBuf) -> Result<String, Error> {
47 54
         Err(_) => Err(Error::new(
48 55
             ErrorKind::NotFound,
49 56
             format!(
50
-                "Can't find post with the id '{}'",
57
+                "Can't find post with the slug '{}'",
51 58
                 path.file_stem().unwrap().to_str().unwrap()
52 59
             ),
53 60
         )),

+ 7
- 2
oslo-lib/src/routes.rs View File

@@ -25,7 +25,8 @@ pub async fn index(req: Request<State>) -> Result {
25 25
 pub async fn single_post(req: Request<State>) -> Result {
26 26
     let mut context = Context::new();
27 27
     let slug = req.param("slug")?;
28
-    let post = fs::get_one_post(slug).await?;
28
+    let post = fs::get_one_post(slug, is_logged_in(&req)).await?;
29
+
29 30
     context.insert("post", &post);
30 31
     render_html_response("single.html", &context, req)
31 32
 }
@@ -38,7 +39,7 @@ pub async fn new_post(req: Request<State>) -> Result {
38 39
 pub async fn edit_post(req: Request<State>) -> Result {
39 40
     let mut context = Context::new();
40 41
     let slug = req.param("slug")?;
41
-    let mut post = fs::get_one_post(slug).await?;
42
+    let mut post = fs::get_one_post(slug, is_logged_in(&req)).await?;
42 43
     post.body = post.body.replace("<br>", "\n");
43 44
     post.html = post.generate_html();
44 45
     context.insert("post", &post);
@@ -160,3 +161,7 @@ fn prepare_flash_messages(mut req: Request<State>) -> Context {
160 161
 
161 162
     context
162 163
 }
164
+
165
+fn is_logged_in(req: &Request<State>) -> bool {
166
+    req.session().get("logged_in").unwrap_or(false)
167
+}

+ 28
- 20
templates/components.html View File

@@ -1,27 +1,35 @@
1 1
 {% macro post(post, type) %}
2
-  <div class="post">
2
+  {% if not post.draft or logged_in %}
3
+    <div class="post">
4
+        {% if type != 'index' and post.draft %}
5
+          <p class="message message--success">this post is a draft</p>
6
+        {% endif %}
3 7
 
4
-      <div class="post__heading">
5
-        <div class="post__title">
6
-          <span class="post__date">:: {{ post.date }} //</span>
7
-          <a class="post__link" href="/posts/{{ post.slug }}">
8
-            {{ post.title }}
9
-          </a>
10
-        </div>
11
-        {% if logged_in %}
12
-          <div class="post__meta">
13
-            <a class="post__link" href="/posts/{{ post.slug }}/edit">edit</a>
14
-            <span class="post__link danger" data-delete="{{ post.slug }}">delete</span>
8
+        <div class="post__heading">
9
+          <div class="post__title">
10
+            <span class="post__date">:: {{ post.date }} //</span>
11
+            <a class="post__link" href="/posts/{{ post.slug }}">
12
+              {{ post.title }}
13
+            </a>
15 14
           </div>
16
-        {% endif %}
17
-      </div>
15
+          {% if logged_in %}
16
+            <div class="post__meta">
17
+              <a class="post__link" href="/posts/{{ post.slug }}/edit">edit</a>
18
+              <span class="post__link danger" data-delete="{{ post.slug }}">delete</span>
19
+              {% if post.draft %}
20
+                <strong>~ draft</strong>
21
+              {% endif %}
22
+            </div>
23
+          {% endif %}
24
+        </div>
18 25
 
19
-    {% if type != "index" %}
20
-      <div class="post__body">
21
-        {{ post.html | safe }}
22
-      </div>
23
-    {% endif %}
24
-  </div>
26
+      {% if type != "index" %}
27
+        <div class="post__body">
28
+          {{ post.html | safe }}
29
+        </div>
30
+      {% endif %}
31
+    </div>
32
+  {% endif %}
25 33
 {% endmacro %}
26 34
 
27 35
 {% macro form(post=false, action) %}

+ 4
- 0
templates/edit.html View File

@@ -2,6 +2,10 @@
2 2
 {% block content %}
3 3
 
4 4
   <div class="posts">
5
+    {% if post.draft %}
6
+      <p class="message message--success">this post is a draft</p>
7
+    {% endif %}
8
+
5 9
     {{ components::form(post=post, action="/posts/" ~ post.slug) }}
6 10
   </div>
7 11
 

Loading…
Cancel
Save