Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thesuzerain committed Aug 23, 2023
1 parent e5798fc commit 43c775d
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 67 deletions.
20 changes: 15 additions & 5 deletions migrations/20230816085700_collections_and_more.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ CREATE TABLE uploaded_images (
url varchar(2048) NOT NULL,
size integer NOT NULL,
created timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
owner_id bigint REFERENCES users NOT NULL,
owner_id bigint REFERENCES users NOT NULL
);

-- Currently, images will be associated with mod descriptions and thread messages
-- In the future, we may want to allow images to be associated with other things like reports, comments, etc.

-- Image will be associated with a mod (in description) or a thread message (not both)
-- Icons are not a part of this table
mod_id bigint REFERENCES mods NULL,
thread_message_id bigint REFERENCES threads_messages NULL
CREATE TABLE images_mods (
mod_id bigint REFERENCES mods NOT NULL,
image_id bigint REFERENCES uploaded_images NOT NULL,
PRIMARY KEY (mod_id, image_id)
);

CREATE TABLE images_threads (
thread_message_id bigint REFERENCES threads_messages NOT NULL,
image_id bigint REFERENCES uploaded_images NOT NULL,
PRIMARY KEY (thread_message_id, image_id)
);
14 changes: 14 additions & 0 deletions src/database/models/collection_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ impl Collection {
.execute(&mut *transaction)
.await?;

for project_id in self.projects.iter() {
sqlx::query!(
"
INSERT INTO collections_mods (collection_id, mod_id)
VALUES ($1, $2)
ON CONFLICT DO NOTHING
",
self.id as CollectionId,
*project_id as ProjectId,
)
.execute(&mut *transaction)
.await?;
}

Ok(())
}

Expand Down
111 changes: 88 additions & 23 deletions src/database/models/image_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ pub struct Image {
pub size: u64,
pub created: DateTime<Utc>,
pub owner_id: UserId,

pub mod_id: Option<ProjectId>,
pub thread_message_id: Option<ThreadMessageId>,
}

impl Image {
Expand All @@ -29,19 +26,17 @@ impl Image {
sqlx::query!(
"
INSERT INTO uploaded_images (
id, url, size, created, owner_id, mod_id, thread_message_id
id, url, size, created, owner_id
)
VALUES (
$1, $2, $3, $4, $5, $6, $7
$1, $2, $3, $4, $5
);
",
self.id as ImageId,
self.url,
self.size as i64,
self.created,
self.owner_id as UserId,
self.mod_id.map(|x| x.0),
self.thread_message_id.map(|x| x.0),
)
.execute(&mut *transaction)
.await?;
Expand All @@ -67,6 +62,82 @@ impl Image {
.execute(&mut *transaction)
.await?;

sqlx::query!(
"
DELETE FROM images_mods
WHERE image_id = $1
",
id as ImageId,
)
.execute(&mut *transaction)
.await?;

sqlx::query!(
"
DELETE FROM images_threads
WHERE image_id = $1
",
id as ImageId,
)
.execute(&mut *transaction)
.await?;

Image::clear_cache(image.id, image.url, redis).await?;

Ok(Some(()))
} else {
Ok(None)
}
}

pub async fn remove_from_project(
id: ImageId,
project_id: ProjectId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &deadpool_redis::Pool,
) -> Result<Option<()>, DatabaseError> {
let image = Self::get_id(id, &mut *transaction, redis).await?;

if let Some(image) = image {
sqlx::query!(
"
DELETE FROM images_mods
WHERE image_id = $1 AND mod_id = $2
",
id as ImageId,
project_id as ProjectId,
)
.execute(&mut *transaction)
.await?;

Image::clear_cache(image.id, image.url, redis).await?;

Ok(Some(()))
} else {
Ok(None)
}
}

pub async fn remove_from_thread_message(
id: ImageId,
thread_message_id: ThreadMessageId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &deadpool_redis::Pool,
) -> Result<Option<()>, DatabaseError> {
let image = Self::get_id(id, &mut *transaction, redis).await?;

if let Some(image) = image {
sqlx::query!(
"
DELETE FROM images_threads
WHERE image_id = $1 AND thread_message_id = $2
",
id as ImageId,
thread_message_id as ThreadMessageId,
)
.execute(&mut *transaction)
.await?;

Image::clear_cache(image.id, image.url, redis).await?;

Ok(Some(()))
Expand All @@ -87,11 +158,12 @@ impl Image {
sqlx::query!(
"
SELECT i.id, i.url, i.size, i.created, i.owner_id,
i.mod_id, i.thread_message_id
im.mod_id
FROM uploaded_images i
WHERE i.mod_id = $1
GROUP BY i.id;
",
LEFT JOIN images_mods im ON im.image_id = i.id
WHERE im.mod_id = $1
GROUP BY i.id, im.mod_id;
",
project_id as ProjectId
)
.fetch_many(exec)
Expand All @@ -105,8 +177,6 @@ impl Image {
size: row.size as u64,
created: row.created,
owner_id: UserId(row.owner_id),
mod_id: row.mod_id.map(ProjectId),
thread_message_id: row.thread_message_id.map(ThreadMessageId),
}
}))
})
Expand All @@ -126,11 +196,11 @@ impl Image {
sqlx::query!(
"
SELECT i.id, i.url, i.size, i.created, i.owner_id,
i.mod_id, i.thread_message_id
it.thread_message_id
FROM uploaded_images i
WHERE i.thread_message_id = $1
GROUP BY i.id;
",
LEFT JOIN images_threads it ON it.image_id = i.id
WHERE it.thread_message_id = $1
",
thread_message_id as ThreadMessageId
)
.fetch_many(exec)
Expand All @@ -144,8 +214,6 @@ impl Image {
size: row.size as u64,
created: row.created,
owner_id: UserId(row.owner_id),
mod_id: row.mod_id.map(ProjectId),
thread_message_id: row.thread_message_id.map(ThreadMessageId),
}
}))
})
Expand Down Expand Up @@ -252,8 +320,7 @@ impl Image {
.collect();
let db_images: Vec<Image> = sqlx::query!(
"
SELECT i.id, i.url, i.size, i.created, i.owner_id,
i.mod_id, i.thread_message_id
SELECT i.id, i.url, i.size, i.created, i.owner_id
FROM uploaded_images i
WHERE i.id = ANY($1) OR i.url = ANY($2)
GROUP BY i.id;
Expand All @@ -275,8 +342,6 @@ impl Image {
size: i.size as u64,
created: i.created,
owner_id: UserId(i.owner_id),
mod_id: i.mod_id.map(ProjectId),
thread_message_id: i.thread_message_id.map(ThreadMessageId),
}
}))
})
Expand Down
31 changes: 24 additions & 7 deletions src/models/images.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::ids::{Base62Id, ThreadMessageId};
use super::ids::Base62Id;
use crate::database::models::image_item::Image as DBImage;
use crate::models::ids::{ProjectId, UserId};
use crate::models::ids::UserId;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

Expand All @@ -16,9 +16,6 @@ pub struct Image {
pub size: u64,
pub created: DateTime<Utc>,
pub owner_id: UserId,

pub mod_id: Option<ProjectId>,
pub thread_message_id: Option<ThreadMessageId>,
}

impl From<DBImage> for Image {
Expand All @@ -29,9 +26,29 @@ impl From<DBImage> for Image {
size: x.size,
created: x.created,
owner_id: x.owner_id.into(),
}
}
}

#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ImageContext {
Project,
ThreadMessage,
Unknown,
}

impl std::fmt::Display for ImageContext {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "{}", self.as_str())
}
}

mod_id: x.mod_id.map(|x| x.into()),
thread_message_id: x.thread_message_id.map(|x| x.into()),
impl ImageContext {
pub fn as_str(&self) -> &'static str {
match self {
ImageContext::Project => "project",
ImageContext::ThreadMessage => "thread_message",
ImageContext::Unknown => "unknown",
}
}
}
6 changes: 4 additions & 2 deletions src/models/pats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ bitflags::bitflags! {
const IMAGE_POST = 1 << 35;
// delete an owned image
const IMAGE_DELETE = 1 << 36;
// edit an owned image
const IMAGE_WRITE = 1 << 37;

const ALL = 0b1111111111111111111111111111111111111;
const NOT_RESTRICTED = 0b11111100000011111111111111100111;
const ALL = 0b11111111111111111111111111111111111111;
const NOT_RESTRICTED = 0b111111100000011111111111111100111;
const NONE = 0b0;
}
}
Expand Down
Loading

0 comments on commit 43c775d

Please sign in to comment.