Skip to content

Commit

Permalink
log ip
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Mar 17, 2024
1 parent 76c6d92 commit bc6ed91
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 28 deletions.
1 change: 1 addition & 0 deletions musidex-daemon/migrations/004-logs.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE TABLE IF NOT EXISTS logs (
id integer primary key autoincrement,
ip text not null,
timestamp text not null,
user_id integer not null,
type text not null, -- "user", "tag", "music"
Expand Down
11 changes: 11 additions & 0 deletions musidex-daemon/src/application/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub async fn delete_tag(mut req: Request<Body>) -> Result<Response<Body>> {
let tx = c.transaction().context("transaction begin failed")?;
db_log(&tx, DbLog {
user_id: uid,
ip: req.headers().get("x-real-ip").and_then(|x| x.to_str().ok()).map(|x| x.to_string()).unwrap_or_default(),
type_: LogType::Tag,
action: LogAction::Delete,
music_id: Some(tag.music_id),
Expand Down Expand Up @@ -155,6 +156,16 @@ pub async fn delete_music_handler(req: Request<Body>) -> Result<Response<Body>>
let uid = User::from_req(&req).context("no user id")?;

let tx = c.transaction().context("transaction begin failed")?;
db_log(&tx, DbLog {
user_id: uid,
ip: req.headers().get("x-real-ip").and_then(|x| x.to_str().ok()).map(|x| x.to_string()).unwrap_or_default(),
type_: LogType::Music,
action: LogAction::Delete,
music_id: Some(id),
target_key: None,
target_value: None,
});

let code = delete_music(&tx, uid, id)?;
tx.commit().context("transaction commit failed")?;

Expand Down
1 change: 1 addition & 0 deletions musidex-daemon/src/application/user_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub async fn delete(req: Request<Body>) -> Result<Response<Body>> {
let tx = c.transaction().context("transaction begin failed")?;
db_log(&tx, DbLog {
user_id: UserID(id),
ip: req.headers().get("x-real-ip").and_then(|x| x.to_str().ok()).map(|x| x.to_string()).unwrap_or_default(),
type_: LogType::User,
action: LogAction::Delete,
music_id: None,
Expand Down
10 changes: 0 additions & 10 deletions musidex-daemon/src/domain/music.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::domain::entity::{Music, MusicID, Tag, TagKey, UserID};
use anyhow::{Context, Result};
use hyper::StatusCode;
use rusqlite::Connection;
use crate::infrastructure::db::{db_log, DbLog, LogAction, LogType};

impl Music {
pub fn mk(c: &Connection) -> Result<MusicID> {
Expand Down Expand Up @@ -62,15 +61,6 @@ impl Music {
}

pub fn delete_music(c: &Connection, uid: UserID, id: MusicID) -> Result<StatusCode> {
db_log(c, DbLog {
user_id: uid,
type_: LogType::Music,
action: LogAction::Delete,
music_id: Some(id),
target_key: None,
target_value: None,
});

let tags = Tag::by_id(&c, id)?;
let owners: Vec<_> = tags
.iter()
Expand Down
17 changes: 0 additions & 17 deletions musidex-daemon/src/domain/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::infrastructure::youtube_dl::{ytdl_run_with_args, SingleVideo, Youtube
use anyhow::{Context, Result};
use hyper::StatusCode;
use rusqlite::Connection;
use crate::infrastructure::db::{db_log, DbLog, LogAction, LogType};

pub async fn youtube_upload(c: &mut Connection, url: String, uid: UserID) -> Result<StatusCode> {
let metadata = ytdl_run_with_args(vec!["--no-playlist", "-J", "--", &url])
Expand All @@ -18,14 +17,6 @@ pub async fn youtube_upload(c: &mut Connection, url: String, uid: UserID) -> Res
if Tag::has(&c, mid, k.clone())? {
return Ok(StatusCode::CONFLICT);
}
db_log(c, DbLog {
user_id: uid,
type_: LogType::Tag,
action: LogAction::Create,
music_id: Some(mid),
target_key: Some(k.clone()),
target_value: None,
});
Tag::insert(&c, Tag::new_key(mid, k))?;
return Ok(StatusCode::OK);
}
Expand All @@ -50,14 +41,6 @@ fn id_exists(c: &Connection, id: &str) -> Result<Option<MusicID>> {

fn push_for_treatment(c: &Connection, v: Box<SingleVideo>, url: String, uid: UserID) -> Result<()> {
let id = Music::mk(&c)?;
db_log(c, DbLog {
user_id: uid,
type_: LogType::Music,
action: LogAction::Create,
music_id: Some(id),
target_key: None,
target_value: None,
});

let mk_tag = |key, v| Tag::insert(&c, Tag::new_text(id, key, v));

Expand Down
3 changes: 2 additions & 1 deletion musidex-daemon/src/infrastructure/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ pub enum LogType {
Music,
}

#[allow(dead_code)]
pub enum LogAction {
Create,
#[allow(dead_code)]
Update,
Delete,
}

pub struct DbLog {
pub user_id: UserID,
pub ip: String,
pub type_: LogType,
pub action: LogAction,
pub music_id: Option<MusicID>,
Expand Down

0 comments on commit bc6ed91

Please sign in to comment.