yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·6.8 MiB·27ceb05·Tahinli·8 hours ago·399 commits
| 1 | use common::{ |
| 2 | constant::LOG_PAGE_LIMIT, |
| 3 | model::{AdminAction, AdminActionKind, AdminLogEntry, Id, LogAction, LogEntry, User}, |
| 4 | time::now_ms, |
| 5 | }; |
| 6 | |
| 7 | use super::{Connection, all, run, user}; |
| 8 | use crate::error::ServerError; |
| 9 | |
| 10 | pub async fn optional_user(id: Option<Id>, db: &Connection) -> Result<Option<User>, ServerError> { |
| 11 | match id { |
| 12 | Some(id) => Ok(Some(user::read(id, db).await?)), |
| 13 | None => Ok(None), |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | pub async fn record( |
| 18 | community_id: Id, |
| 19 | actor_id: Option<Id>, |
| 20 | target_id: Option<Id>, |
| 21 | action: &LogAction, |
| 22 | db: &Connection, |
| 23 | ) -> Result<(), ServerError> { |
| 24 | run( |
| 25 | db, |
| 26 | "INSERT INTO community_log (community_id, created_at, actor_id, target_id, action) VALUES (?1, ?2, ?3, ?4, ?5)", |
| 27 | (community_id, now_ms(), actor_id, target_id, bitcode::encode(action)), |
| 28 | ) |
| 29 | .await?; |
| 30 | |
| 31 | Ok(()) |
| 32 | } |
| 33 | |
| 34 | pub async fn list(community_id: Id, db: &Connection) -> Result<Vec<LogEntry>, ServerError> { |
| 35 | let raws = all( |
| 36 | db, |
| 37 | "SELECT id, created_at, actor_id, target_id, action FROM community_log WHERE community_id = ?1 ORDER BY id DESC LIMIT ?2", |
| 38 | (community_id, LOG_PAGE_LIMIT), |
| 39 | |r| { |
| 40 | Ok(( |
| 41 | r.get::<Id>()?, |
| 42 | r.get::<u64>()?, |
| 43 | r.get::<Option<Id>>()?, |
| 44 | r.get::<Option<Id>>()?, |
| 45 | r.blob::<LogAction>()?, |
| 46 | )) |
| 47 | }, |
| 48 | ) |
| 49 | .await?; |
| 50 | |
| 51 | let mut entries = Vec::new(); |
| 52 | for (id, created_at, actor_id, target_id, action) in raws { |
| 53 | entries.push(LogEntry { |
| 54 | id, |
| 55 | created_at, |
| 56 | actor: optional_user(actor_id, db).await?, |
| 57 | target: optional_user(target_id, db).await?, |
| 58 | action, |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | Ok(entries) |
| 63 | } |
| 64 | |
| 65 | pub async fn record_admin( |
| 66 | actor_id: Option<Id>, |
| 67 | target_id: Option<Id>, |
| 68 | community: Option<&str>, |
| 69 | action: &AdminAction, |
| 70 | db: &Connection, |
| 71 | ) -> Result<(), ServerError> { |
| 72 | run( |
| 73 | db, |
| 74 | "INSERT INTO admin_log (created_at, actor_id, target_id, community, kind, action) VALUES (?1, ?2, ?3, ?4, ?5, ?6)", |
| 75 | ( |
| 76 | now_ms(), |
| 77 | actor_id, |
| 78 | target_id, |
| 79 | community, |
| 80 | action.kind().as_str(), |
| 81 | bitcode::encode(action), |
| 82 | ), |
| 83 | ) |
| 84 | .await?; |
| 85 | |
| 86 | Ok(()) |
| 87 | } |
| 88 | |
| 89 | pub async fn list_admin( |
| 90 | actor_id: Option<Id>, |
| 91 | target_id: Option<Id>, |
| 92 | kind: Option<AdminActionKind>, |
| 93 | after_ms: Option<u64>, |
| 94 | before_ms: Option<u64>, |
| 95 | db: &Connection, |
| 96 | ) -> Result<Vec<AdminLogEntry>, ServerError> { |
| 97 | let raws = all( |
| 98 | db, |
| 99 | "SELECT id, created_at, actor_id, target_id, community, action FROM admin_log WHERE (?1 IS NULL OR actor_id = ?1) AND (?2 IS NULL OR target_id = ?2) AND (?3 IS NULL OR kind = ?3) AND (?4 IS NULL OR created_at >= ?4) AND (?5 IS NULL OR created_at < ?5) ORDER BY id DESC LIMIT ?6", |
| 100 | ( |
| 101 | actor_id, |
| 102 | target_id, |
| 103 | kind.map(AdminActionKind::as_str), |
| 104 | after_ms.map(|ms| ms as i64), |
| 105 | before_ms.map(|ms| ms as i64), |
| 106 | LOG_PAGE_LIMIT, |
| 107 | ), |
| 108 | |r| { |
| 109 | Ok(( |
| 110 | r.get::<Id>()?, |
| 111 | r.get::<u64>()?, |
| 112 | r.get::<Option<Id>>()?, |
| 113 | r.get::<Option<Id>>()?, |
| 114 | r.get::<Option<String>>()?, |
| 115 | r.blob::<AdminAction>()?, |
| 116 | )) |
| 117 | }, |
| 118 | ) |
| 119 | .await?; |
| 120 | |
| 121 | let mut entries = Vec::new(); |
| 122 | for (id, created_at, actor_id, target_id, community, action) in raws { |
| 123 | entries.push(AdminLogEntry { |
| 124 | id, |
| 125 | actor: optional_user(actor_id, db).await?, |
| 126 | target: optional_user(target_id, db).await?, |
| 127 | created_at, |
| 128 | community, |
| 129 | action, |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | Ok(entries) |
| 134 | } |
| 135 | |
| 136 | pub async fn list_communities( |
| 137 | actor_id: Option<Id>, |
| 138 | target_id: Option<Id>, |
| 139 | after_ms: Option<u64>, |
| 140 | before_ms: Option<u64>, |
| 141 | db: &Connection, |
| 142 | ) -> Result<Vec<AdminLogEntry>, ServerError> { |
| 143 | let raws = all( |
| 144 | db, |
| 145 | "SELECT l.id, l.created_at, l.actor_id, l.target_id, c.name, l.action FROM community_log l JOIN community c ON c.id = l.community_id WHERE (?1 IS NULL OR l.actor_id = ?1) AND (?2 IS NULL OR l.target_id = ?2) AND (?3 IS NULL OR l.created_at >= ?3) AND (?4 IS NULL OR l.created_at < ?4) ORDER BY l.id DESC LIMIT ?5", |
| 146 | ( |
| 147 | actor_id, |
| 148 | target_id, |
| 149 | after_ms.map(|ms| ms as i64), |
| 150 | before_ms.map(|ms| ms as i64), |
| 151 | LOG_PAGE_LIMIT, |
| 152 | ), |
| 153 | |r| { |
| 154 | Ok(( |
| 155 | r.get::<Id>()?, |
| 156 | r.get::<u64>()?, |
| 157 | r.get::<Option<Id>>()?, |
| 158 | r.get::<Option<Id>>()?, |
| 159 | r.get::<String>()?, |
| 160 | r.blob::<LogAction>()?, |
| 161 | )) |
| 162 | }, |
| 163 | ) |
| 164 | .await?; |
| 165 | |
| 166 | let mut entries = Vec::new(); |
| 167 | for (id, created_at, actor_id, target_id, community, action) in raws { |
| 168 | entries.push(AdminLogEntry { |
| 169 | id, |
| 170 | actor: optional_user(actor_id, db).await?, |
| 171 | target: optional_user(target_id, db).await?, |
| 172 | created_at, |
| 173 | community: Some(community), |
| 174 | action: AdminAction::Community(action), |
| 175 | }); |
| 176 | } |
| 177 | |
| 178 | Ok(entries) |
| 179 | } |