1
0
* Follows initial

* Fix #171, Fix #170, Fix #169, Fix #164

* More work on follows

* Fix compile error

* Upgrade meili version, add follows to search
This commit is contained in:
Geometrically
2021-03-04 20:35:23 -07:00
committed by GitHub
parent e46ff3de8b
commit 0ccb6cb873
25 changed files with 2298 additions and 782 deletions

View File

@@ -94,6 +94,14 @@ generate_ids!(
ReportId
);
generate_ids!(
pub generate_notification_id,
NotificationId,
8,
"SELECT EXISTS(SELECT 1 FROM notifications WHERE id=$1)",
NotificationId
);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Type)]
#[sqlx(transparent)]
pub struct UserId(pub i64);
@@ -152,6 +160,13 @@ pub struct FileId(pub i64);
#[sqlx(transparent)]
pub struct StateId(pub i64);
#[derive(Copy, Clone, Debug, Type)]
#[sqlx(transparent)]
pub struct NotificationId(pub i64);
#[derive(Copy, Clone, Debug, Type)]
#[sqlx(transparent)]
pub struct NotificationActionId(pub i32);
use crate::models::ids;
impl From<ids::ModId> for ModId {
@@ -204,3 +219,13 @@ impl From<ReportId> for ids::ReportId {
ids::ReportId(id.0 as u64)
}
}
impl From<ids::NotificationId> for NotificationId {
fn from(id: ids::NotificationId) -> Self {
NotificationId(id.0 as i64)
}
}
impl From<NotificationId> for ids::NotificationId {
fn from(id: NotificationId) -> Self {
ids::NotificationId(id.0 as u64)
}
}

View File

@@ -6,6 +6,7 @@ use thiserror::Error;
pub mod categories;
pub mod ids;
pub mod mod_item;
pub mod notification_item;
pub mod report_item;
pub mod team_item;
pub mod user_item;

View File

@@ -71,6 +71,7 @@ impl ModBuilder {
updated: chrono::Utc::now(),
status: self.status,
downloads: 0,
follows: 0,
icon_url: self.icon_url,
issues_url: self.issues_url,
source_url: self.source_url,
@@ -122,6 +123,7 @@ pub struct Mod {
pub updated: chrono::DateTime<chrono::Utc>,
pub status: StatusId,
pub downloads: i32,
pub follows: i32,
pub icon_url: Option<String>,
pub issues_url: Option<String>,
pub source_url: Option<String>,
@@ -153,7 +155,7 @@ impl Mod {
$6, $7, $8, $9,
$10, $11, $12, $13,
$14, $15, $16, $17,
$18
LOWER($18)
)
",
self.id as ModId,
@@ -187,7 +189,7 @@ impl Mod {
{
let result = sqlx::query!(
"
SELECT title, description, downloads,
SELECT title, description, downloads, follows,
icon_url, body, body_url, published,
updated, status,
issues_url, source_url, wiki_url, discord_url, license_url,
@@ -222,6 +224,7 @@ impl Mod {
license: LicenseId(row.license),
slug: row.slug,
body: row.body,
follows: row.follows,
}))
} else {
Ok(None)
@@ -237,7 +240,7 @@ impl Mod {
let mod_ids_parsed: Vec<i64> = mod_ids.into_iter().map(|x| x.0).collect();
let mods = sqlx::query!(
"
SELECT id, title, description, downloads,
SELECT id, title, description, downloads, follows,
icon_url, body, body_url, published,
updated, status,
issues_url, source_url, wiki_url, discord_url, license_url,
@@ -270,6 +273,7 @@ impl Mod {
license: LicenseId(m.license),
slug: m.slug,
body: m.body,
follows: m.follows,
}))
})
.try_collect::<Vec<Mod>>()
@@ -300,6 +304,26 @@ impl Mod {
return Ok(None);
};
sqlx::query!(
"
DELETE FROM mod_follows
WHERE mod_id = $1
",
id as ModId
)
.execute(exec)
.await?;
sqlx::query!(
"
DELETE FROM mod_follows
WHERE mod_id = $1
",
id as ModId,
)
.execute(exec)
.await?;
sqlx::query!(
"
DELETE FROM reports
@@ -390,7 +414,7 @@ impl Mod {
let id = sqlx::query!(
"
SELECT id FROM mods
WHERE slug = $1
WHERE LOWER(slug) = LOWER($1)
",
slug
)
@@ -413,7 +437,7 @@ impl Mod {
{
let result = sqlx::query!(
"
SELECT m.id id, m.title title, m.description description, m.downloads downloads,
SELECT m.id id, m.title title, m.description description, m.downloads downloads, m.follows follows,
m.icon_url icon_url, m.body body, m.body_url body_url, m.published published,
m.updated updated, m.status status,
m.issues_url issues_url, m.source_url source_url, m.wiki_url wiki_url, m.discord_url discord_url, m.license_url license_url,
@@ -459,6 +483,7 @@ impl Mod {
license: LicenseId(m.license),
slug: m.slug.clone(),
body: m.body.clone(),
follows: m.follows,
},
categories: m
.categories
@@ -496,7 +521,7 @@ impl Mod {
let mod_ids_parsed: Vec<i64> = mod_ids.into_iter().map(|x| x.0).collect();
sqlx::query!(
"
SELECT m.id id, m.title title, m.description description, m.downloads downloads,
SELECT m.id id, m.title title, m.description description, m.downloads downloads, m.follows follows,
m.icon_url icon_url, m.body body, m.body_url body_url, m.published published,
m.updated updated, m.status status,
m.issues_url issues_url, m.source_url source_url, m.wiki_url wiki_url, m.discord_url discord_url, m.license_url license_url,
@@ -540,6 +565,7 @@ impl Mod {
license: LicenseId(m.license),
slug: m.slug.clone(),
body: m.body.clone(),
follows: m.follows
},
categories: m.categories.unwrap_or_default().split(',').map(|x| x.to_string()).collect(),
versions: m.versions.unwrap_or_default().split(',').map(|x| VersionId(x.parse().unwrap_or_default())).collect(),

View File

@@ -0,0 +1,327 @@
use super::ids::*;
use crate::database::models::DatabaseError;
pub struct NotificationBuilder {
pub title: String,
pub text: String,
pub link: String,
pub actions: Vec<NotificationActionBuilder>,
}
pub struct NotificationActionBuilder {
pub title: String,
pub action_route: String,
}
pub struct Notification {
pub id: NotificationId,
pub user_id: UserId,
pub title: String,
pub text: String,
pub link: String,
pub read: bool,
pub created: chrono::DateTime<chrono::Utc>,
pub actions: Vec<NotificationAction>,
}
pub struct NotificationAction {
pub id: NotificationActionId,
pub notification_id: NotificationId,
pub title: String,
pub action_route: String,
}
impl NotificationBuilder {
pub async fn insert(
&self,
user: UserId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), DatabaseError> {
self.insert_many(vec![user], transaction).await
}
pub async fn insert_many(
&self,
users: Vec<UserId>,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), DatabaseError> {
for user in users {
let id = generate_notification_id(&mut *transaction).await?;
let mut actions = Vec::new();
for action in &self.actions {
actions.push(NotificationAction {
id: NotificationActionId(0),
notification_id: id,
title: action.title.clone(),
action_route: action.action_route.clone(),
})
}
Notification {
id,
user_id: user,
title: self.title.clone(),
text: self.text.clone(),
link: self.link.clone(),
read: false,
created: chrono::Utc::now(),
actions,
}
.insert(&mut *transaction)
.await?;
}
Ok(())
}
}
impl Notification {
pub async fn insert(
&self,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), sqlx::error::Error> {
sqlx::query!(
"
INSERT INTO notifications (
id, user_id, title, text, link
)
VALUES (
$1, $2, $3, $4, $5
)
",
self.id as NotificationId,
self.user_id as UserId,
&self.title,
&self.text,
&self.link
)
.execute(&mut *transaction)
.await?;
for action in &self.actions {
action.insert(&mut *transaction).await?;
}
Ok(())
}
pub async fn get<'a, 'b, E>(
id: NotificationId,
executor: E,
) -> Result<Option<Self>, sqlx::error::Error>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
let result = sqlx::query!(
"
SELECT n.user_id, n.title, n.text, n.link, n.created, n.read,
STRING_AGG(DISTINCT na.id || ', ' || na.title || ', ' || na.action_route, ' ,') actions
FROM notifications n
LEFT OUTER JOIN notifications_actions na on n.id = na.notification_id
WHERE n.id = $1
GROUP BY n.id, n.user_id;
",
id as NotificationId,
)
.fetch_optional(executor)
.await?;
if let Some(row) = result {
let mut actions: Vec<NotificationAction> = Vec::new();
row.actions.unwrap_or_default().split(" ,").for_each(|x| {
let action: Vec<&str> = x.split(", ").collect();
if action.len() >= 3 {
actions.push(NotificationAction {
id: NotificationActionId(action[0].parse().unwrap_or(0)),
notification_id: id,
title: action[1].to_string(),
action_route: action[2].to_string(),
});
}
});
Ok(Some(Notification {
id,
user_id: UserId(row.user_id),
title: row.title,
text: row.text,
link: row.link,
read: row.read,
created: row.created,
actions,
}))
} else {
Ok(None)
}
}
pub async fn get_many<'a, E>(
notification_ids: Vec<NotificationId>,
exec: E,
) -> Result<Vec<Notification>, sqlx::Error>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
{
use futures::stream::TryStreamExt;
let notification_ids_parsed: Vec<i64> = notification_ids.into_iter().map(|x| x.0).collect();
sqlx::query!(
"
SELECT n.id, n.user_id, n.title, n.text, n.link, n.created, n.read,
STRING_AGG(DISTINCT na.id || ', ' || na.title || ', ' || na.action_route, ' ,') actions
FROM notifications n
LEFT OUTER JOIN notifications_actions na on n.id = na.notification_id
WHERE n.id IN (SELECT * FROM UNNEST($1::bigint[]))
GROUP BY n.id, n.user_id;
",
&notification_ids_parsed
)
.fetch_many(exec)
.try_filter_map(|e| async {
Ok(e.right().map(|row| {
let id = NotificationId(row.id);
let mut actions: Vec<NotificationAction> = Vec::new();
row.actions.unwrap_or_default().split(" ,").for_each(|x| {
let action: Vec<&str> = x.split(", ").collect();
if action.len() >= 3 {
actions.push(NotificationAction {
id: NotificationActionId(action[0].parse().unwrap_or(0)),
notification_id: id,
title: action[1].to_string(),
action_route: action[2].to_string(),
});
}
});
Notification {
id,
user_id: UserId(row.user_id),
title: row.title,
text: row.text,
link: row.link,
read: row.read,
created: row.created,
actions,
}
}))
})
.try_collect::<Vec<Notification>>()
.await
}
pub async fn get_many_user<'a, E>(
user_id: UserId,
exec: E,
) -> Result<Vec<Notification>, sqlx::Error>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
{
use futures::stream::TryStreamExt;
sqlx::query!(
"
SELECT n.id, n.user_id, n.title, n.text, n.link, n.created, n.read,
STRING_AGG(DISTINCT na.id || ', ' || na.title || ', ' || na.action_route, ' ,') actions
FROM notifications n
LEFT OUTER JOIN notifications_actions na on n.id = na.notification_id
WHERE n.user_id = $1
GROUP BY n.id, n.user_id;
",
user_id as UserId
)
.fetch_many(exec)
.try_filter_map(|e| async {
Ok(e.right().map(|row| {
let id = NotificationId(row.id);
let mut actions: Vec<NotificationAction> = Vec::new();
row.actions.unwrap_or_default().split(" ,").for_each(|x| {
let action: Vec<&str> = x.split(", ").collect();
if action.len() >= 3 {
actions.push(NotificationAction {
id: NotificationActionId(action[0].parse().unwrap_or(0)),
notification_id: id,
title: action[1].to_string(),
action_route: action[2].to_string(),
});
}
});
Notification {
id,
user_id: UserId(row.user_id),
title: row.title,
text: row.text,
link: row.link,
read: row.read,
created: row.created,
actions,
}
}))
})
.try_collect::<Vec<Notification>>()
.await
}
pub async fn remove<'a, 'b, E>(
id: NotificationId,
exec: E,
) -> Result<Option<()>, sqlx::error::Error>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
{
sqlx::query!(
"
DELETE FROM notifications_actions
WHERE notification_id = $1
",
id as NotificationId,
)
.execute(exec)
.await?;
sqlx::query!(
"
DELETE FROM notifications
WHERE id = $1
",
id as NotificationId,
)
.execute(exec)
.await?;
Ok(Some(()))
}
}
impl NotificationAction {
pub async fn insert(
&self,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), sqlx::error::Error> {
sqlx::query!(
"
INSERT INTO notifications_actions (
notification_id, title, action_route
)
VALUES (
$1, $2, $3
)
",
self.notification_id as NotificationId,
&self.title,
&self.action_route,
)
.execute(&mut *transaction)
.await?;
Ok(())
}
}

View File

@@ -24,7 +24,7 @@ impl User {
avatar_url, bio, created
)
VALUES (
$1, $2, $3, $4, $5,
$1, $2, LOWER($3), $4, $5,
$6, $7, $8
)
",
@@ -126,7 +126,7 @@ impl User {
u.avatar_url, u.bio,
u.created, u.role
FROM users u
WHERE u.username = $1
WHERE LOWER(u.username) = LOWER($1)
",
username
)
@@ -239,6 +239,29 @@ impl User {
.execute(exec)
.await?;
use futures::TryStreamExt;
let notifications: Vec<i64> = sqlx::query!(
"
SELECT n.id FROM notifications n
WHERE n.user_id = $1
",
id as UserId,
)
.fetch_many(exec)
.try_filter_map(|e| async { Ok(e.right().map(|m| m.id as i64)) })
.try_collect::<Vec<i64>>()
.await?;
sqlx::query!(
"
DELETE FROM notifications
WHERE user_id = $1
",
id as UserId,
)
.execute(exec)
.await?;
sqlx::query!(
"
DELETE FROM reports
@@ -249,6 +272,26 @@ impl User {
.execute(exec)
.await?;
sqlx::query!(
"
DELETE FROM mod_follows
WHERE follower_id = $1
",
id as UserId,
)
.execute(exec)
.await?;
sqlx::query!(
"
DELETE FROM notifications_actions
WHERE notification_id IN (SELECT * FROM UNNEST($1::bigint[]))
",
&notifications
)
.execute(exec)
.await?;
sqlx::query!(
"
DELETE FROM team_members
@@ -298,6 +341,38 @@ impl User {
let _result = super::mod_item::Mod::remove_full(mod_id, exec).await?;
}
let notifications: Vec<i64> = sqlx::query!(
"
SELECT n.id FROM notifications n
WHERE n.user_id = $1
",
id as UserId,
)
.fetch_many(exec)
.try_filter_map(|e| async { Ok(e.right().map(|m| m.id as i64)) })
.try_collect::<Vec<i64>>()
.await?;
sqlx::query!(
"
DELETE FROM notifications
WHERE user_id = $1
",
id as UserId,
)
.execute(exec)
.await?;
sqlx::query!(
"
DELETE FROM notifications_actions
WHERE notification_id IN (SELECT * FROM UNNEST($1::bigint[]))
",
&notifications
)
.execute(exec)
.await?;
let deleted_user: UserId = crate::models::users::DELETED_USER.into();
sqlx::query!(