Skip to content

Commit

Permalink
Add moderation log
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Feb 5, 2023
1 parent f9938e2 commit c8ca17f
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 5 deletions.
14 changes: 14 additions & 0 deletions assets/styles/lemmybb.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,17 @@ a.lastsubject {
color: #ff0000;
font-weight: bold;
}

#mod_log {
width: 95%;
margin: 0 auto;
}

#mod_log th,
td {
border-bottom: 1px solid;
}

#mod_log p {
margin-bottom: 0.5em;
}
2 changes: 1 addition & 1 deletion lemmybb-translations
11 changes: 10 additions & 1 deletion src/api/moderation.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::api::post;
use crate::api::{get, post};
use anyhow::Error;
use lemmy_api_common::{
comment::{CommentResponse, RemoveComment},
lemmy_db_schema::newtypes::{CommentId, PostId},
post::{PostResponse, RemovePost},
sensitive::Sensitive,
site::{GetModlog, GetModlogResponse},
};

pub async fn remove_post(
Expand Down Expand Up @@ -34,3 +35,11 @@ pub async fn remove_comment(
};
post("/comment/remove", &params).await
}

pub async fn get_mod_log(auth: Option<Sensitive<String>>) -> Result<GetModlogResponse, Error> {
let params = GetModlog {
auth,
..Default::default()
};
get("/modlog", &params).await
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ fn init_rocket() -> Result<Rocket<Build>, Error> {
node_info,
api_site,
remove_item,
do_remove_item
do_remove_item,
mod_log
],
)
.mount("/assets", FileServer::from(relative!("assets"))))
Expand Down
83 changes: 81 additions & 2 deletions src/routes/moderation.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
use crate::{
api::{
comment::get_comment,
moderation::{remove_comment, remove_post},
moderation::{get_mod_log, remove_comment, remove_post},
post::get_post,
},
error::ErrorPage,
site_fairing::SiteData,
template_helpers::i18n_,
utils::Context,
};
use anyhow::anyhow;

use chrono::NaiveDateTime;
use comrak::{markdown_to_html, ComrakOptions};
use itertools::Itertools;
use lemmy_api_common::lemmy_db_schema::source::community::CommunitySafe;
use rocket::{form::Form, response::Redirect, Either};
use rocket_dyn_templates::{context, Template};
use serde::Serialize;

#[get("/remove_item?<t>&<r>")]
pub async fn remove_item(
Expand Down Expand Up @@ -83,3 +88,77 @@ pub async fn do_remove_item<'r>(
.build();
Ok(Either::Left(Template::render("message", ctx)))
}

#[get("/mod_log")]
pub async fn mod_log(site_data: SiteData) -> Result<Template, ErrorPage> {
let mod_log = get_mod_log(site_data.auth.clone()).await?;
// TODO: consider moving this upstream
let entries: Vec<Vec<ModLogEntry>> = vec![
mod_log
.removed_posts
.into_iter()
.map(|m| {
// TODO: why is removed an option??
let action = if m.mod_remove_post.removed.unwrap() {
"Removed"
} else {
"Restored"
};
let message = format!("{action} post [{}]({})", m.post.name, m.post.ap_id);
ModLogEntry {
community: Some(m.community),
reason: m.mod_remove_post.reason,
when: m.mod_remove_post.when_,
message,
}
})
.collect(),
mod_log
.removed_comments
.into_iter()
.map(|m| {
let action = if m.mod_remove_comment.removed.unwrap() {
"Removed"
} else {
"Restored"
};
let mut content = m.comment.content.replace('\n', " ");
if content.chars().count() > 100 {
content = format!("{}...", content.chars().take(100).collect::<String>());
}
let message = format!("{action} comment [{}]({})", content, m.comment.ap_id);
ModLogEntry {
community: Some(m.community),
reason: m.mod_remove_comment.reason,
when: m.mod_remove_comment.when_,
message,
}
})
.collect(),
];
let entries: Vec<_> = entries
.into_iter()
.flatten()
.map(|mut e| {
e.message = markdown_to_html(&e.message, &ComrakOptions::default());
e
})
.sorted_by_key(|e| e.when)
.rev()
.collect();

let ctx = Context::builder()
.title(i18n_(&site_data, "mod_log_title"))
.site_data(site_data)
.other(context! { entries })
.build();
Ok(Template::render("site/mod_log", ctx))
}

#[derive(Debug, Serialize)]
pub struct ModLogEntry {
community: Option<CommunitySafe>,
reason: Option<String>,
when: NaiveDateTime,
message: String,
}
6 changes: 6 additions & 0 deletions templates/components/header.html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<div class="navbar" role="navigation">
<div class="inner">
<ul id="nav-main" class="nav-main linklist" role="menubar">
<li data-skip-responsive="true">
<a href="/mod_log" role="menuitem">
<span>{{{i18n site_data "mod_log_title" }}}</span>
</a>
</li>

<!--
<li class="breadcrumbs" itemscope itemtype="https://schema.org/BreadcrumbList">
<span class="crumb" itemtype="https://schema.org/ListItem" itemprop="itemListElement" itemscope>
Expand Down
20 changes: 20 additions & 0 deletions templates/site/mod_log.html.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{> components/header }}

<table id="mod_log">
{{# each entries }}

{{log this}}
<tr>
<td>{{timestamp_human this.when}}</td>
<td>
<span>{{{this.message}}}</span>
{{#if this.reason}}
<span><p>Reason: {{this.reason}}</p></span>
{{/if}}
</td>
</tr>

{{/each}}
</table>

{{> components/footer }}

0 comments on commit c8ca17f

Please sign in to comment.