Add user pages

This commit is contained in:
Elnu 2023-07-04 11:34:05 -07:00
parent e729eee137
commit 5aa4c4d203
13 changed files with 269 additions and 164 deletions

View file

@ -139,7 +139,7 @@ impl Database {
},
};
}
for submission in legacy.parse().unwrap() {
for submission in legacy.parse(n as u32).unwrap() {
conn.execute(
"INSERT INTO Submission(author_id, timestamp, image, challenge) VALUES (?1, ?2, ?3, ?4)",
params![
@ -181,6 +181,40 @@ impl Database {
author_id: row.get(0)?,
timestamp: row.get(1)?,
image: row.get(2)?,
challenge,
})
})?
.collect::<std::result::Result<Vec<Submission>, rusqlite::Error>>()?)
}
pub fn get_user_by_name(&self, name: &str) -> Result<Option<User>> {
Ok(self.conn()?
.prepare("SELECT id, discriminator, avatar, deleted FROM User where name = ?1")?
.query_row(params![name], |row| {
Ok(Some(User {
id: row.get(0)?,
name: name.to_owned(),
discriminator: row.get(1)?,
avatar: row.get(2)?,
deleted: row.get(3)?,
}))
})?)
}
pub fn get_submissions_by_user_name(&self, name: &str) -> Result<Vec<Submission>> {
Ok(self.conn()?
.prepare("
SELECT author_id, timestamp, image, challenge
FROM Submission s
JOIN User u ON s.author_id = u.id
WHERE u.name = ?1",
)?
.query_map(params![name], |row| {
Ok(Submission {
author_id: row.get(0)?,
timestamp: row.get(1)?,
image: row.get(2)?,
challenge: row.get(3)?,
})
})?
.collect::<std::result::Result<Vec<Submission>, rusqlite::Error>>()?)

View file

@ -26,7 +26,7 @@ pub enum LegacySubmissionParseError {
}
impl LegacySubmission {
pub fn parse(&self) -> Result<Vec<Submission>, LegacySubmissionParseError> {
pub fn parse(&self, challenge: u32) -> Result<Vec<Submission>, LegacySubmissionParseError> {
let author_id = self.id;
Ok(self.images
.iter()
@ -53,6 +53,7 @@ impl LegacySubmission {
})?)()
},
image: image.clone(),
challenge,
}
})
.collect())

View file

@ -15,4 +15,7 @@ pub struct Submission {
// Image path relative to submission folder.
// Thumbnail image path will be determined from this.
pub image: String,
// Not necessary for challenge pages,
// but needed for user profile pages
pub challenge: u32,
}

View file

@ -81,7 +81,7 @@ impl User {
"https://cdn.discordapp.com/avatars/{}/{}.{}?size=1024",
self.id,
avatar,
if avatar.starts_with("a_") { "gif "} else { "webp" }
if avatar.starts_with("a_") { "gif" } else { "webp" }
),
// Archived user or user with no avatar, calculate default avatar
// https://www.reddit.com/r/discordapp/comments/au6v4e/comment/eh61dm6/