Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User following (closes #308) #460

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions migrations/20221002212953_user_follows.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE user_follows(
follower_id bigint REFERENCES users NOT NULL,
user_id bigint REFERENCES mods NOT NULL,
created timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (follower_id, user_id)
);

ALTER TABLE users
ADD COLUMN follows integer NOT NULL default 0;
111 changes: 111 additions & 0 deletions sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@
},
"query": "\n SELECT f.version_id version_id\n FROM hashes h\n INNER JOIN files f ON h.file_id = f.id\n INNER JOIN versions v on f.version_id = v.id\n INNER JOIN mods m on v.mod_id = m.id\n INNER JOIN statuses s on m.status = s.id\n WHERE h.algorithm = $2 AND h.hash = $1 AND s.status != $3\n "
},
"09a8f55cd4c82c180d06a25638bc024f803f66afe5caa710934a6397dd8eea62": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int8"
]
}
},
"query": "\n UPDATE users\n SET follows = follows - 1\n WHERE id = $1\n "
},
"0a1a470c12b84c7e171f0f51e8e541e9abe8bbee17fc441a5054e1dfd5607c05": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -1570,6 +1582,19 @@
},
"query": "\n DELETE FROM mods_categories\n WHERE joining_mod_id = $1 AND is_additional = TRUE\n "
},
"42695e1cf12cdeafc37674b362a2e0a415a25bfbed798ee7faf992da42205602": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int8",
"Int8"
]
}
},
"query": "\n INSERT INTO user_follows (follower_id, user_id)\n VALUES ($1, $2)\n "
},
"4298552497a48adb9ace61c8dcf989c4d35866866b61c0cc4d45909b1d31c660": {
"describe": {
"columns": [
Expand Down Expand Up @@ -2806,6 +2831,19 @@
},
"query": "\n INSERT INTO loaders_versions (loader_id, version_id)\n VALUES ($1, $2)\n "
},
"6d09d3af4c7db4a631a3902d7e4c96996b62a6b2a23870f49920cec46811f823": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int8",
"Int8"
]
}
},
"query": "\n DELETE FROM user_follows\n WHERE follower_id = $1 AND user_id = $2\n "
},
"6d883ea05aead20f571a0f63bfd63f1d432717ec7a0fb9ab29e01fcb061b3afc": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -3550,6 +3588,27 @@
},
"query": "\n UPDATE mods\n SET icon_url = $1\n WHERE (id = $2)\n "
},
"9128932fe8689883b1ba6feeaf338f04e5d106e33d9c0e3ae49af6622039695d": {
"describe": {
"columns": [
{
"name": "exists",
"ordinal": 0,
"type_info": "Bool"
}
],
"nullable": [
null
],
"parameters": {
"Left": [
"Int8",
"Int8"
]
}
},
"query": "SELECT EXISTS(SELECT 1 FROM user_follows WHERE follower_id = $1 AND user_id = $2)"
},
"9348309884811e8b22f33786ae7c0f259f37f3c90e545f00761a641570107160": {
"describe": {
"columns": [
Expand Down Expand Up @@ -5049,6 +5108,26 @@
},
"query": "\n DELETE FROM reports\n WHERE mod_id = $1\n "
},
"cc3942fd0a640bd19720cab5b21d6f144d630ff0295cbee228822fc57aa59ba9": {
"describe": {
"columns": [
{
"name": "user_id",
"ordinal": 0,
"type_info": "Int8"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Int8"
]
}
},
"query": "\n SELECT user_id FROM user_follows\n WHERE follower_id = $1\n "
},
"ccd913bb2f3006ffe881ce2fc4ef1e721d18fe2eed6ac62627046c955129610c": {
"describe": {
"columns": [
Expand Down Expand Up @@ -5670,6 +5749,26 @@
},
"query": "\n DELETE FROM files\n WHERE files.id = $1\n "
},
"e44bc4fb044b6043eb29ea7c9d65d223ffa3c1732e04e7669d766448d24c9ca5": {
"describe": {
"columns": [
{
"name": "follower_id",
"ordinal": 0,
"type_info": "Int8"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Int8"
]
}
},
"query": "\n SELECT follower_id FROM user_follows\n WHERE user_id = $1\n "
},
"e48c85a2b2e11691afae3799aa126bdd8b7338a973308bbab2760c18bb9cb0b7": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -6178,6 +6277,18 @@
},
"query": "\n SELECT version FROM game_versions\n WHERE id = $1\n "
},
"f192786daf210522fd584d16a404b893ce37e6b3d4b6ddcc438f89b5f499aa82": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int8"
]
}
},
"query": "\n UPDATE users\n SET follows = follows + 1\n WHERE id = $1\n "
},
"f22e9aee090f9952cf795a3540c03b0a5036dab0b740847d05e03d4565756283": {
"describe": {
"columns": [],
Expand Down
5 changes: 4 additions & 1 deletion src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ pub fn users_config(cfg: &mut web::ServiceConfig) {
.service(users::user_edit)
.service(users::user_icon_edit)
.service(users::user_notifications)
.service(users::user_follows),
.service(users::user_follows)
.service(users::user_following)
.service(users::follow_user)
.service(users::unfollow_user),
);
}

Expand Down
76 changes: 75 additions & 1 deletion src/routes/projects.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::database;
use crate::database::models::notification_item::NotificationBuilder;
use crate::file_hosting::FileHost;
use crate::models;
use crate::models::projects::{
Expand All @@ -12,7 +13,7 @@ use crate::util::routes::read_from_payload;
use crate::util::validate::validation_errors_to_string;
use actix_web::{delete, get, patch, post, web, HttpRequest, HttpResponse};
use chrono::Utc;
use futures::StreamExt;
use futures::{StreamExt, TryStreamExt};
use serde::{Deserialize, Serialize};
use serde_json::json;
use sqlx::{PgPool, Row};
Expand Down Expand Up @@ -979,6 +980,79 @@ pub async fn project_edit(
.await?;
}

if new_project.status.as_ref().unwrap_or(&project_item.status)
== &ProjectStatus::Approved
{
let user = database::models::TeamMember::get_from_team(
project_item.inner.team_id,
&mut *transaction,
)
.await?
.into_iter()
.filter(|user| user.role == models::teams::OWNER_ROLE)
.collect::<Vec<database::models::TeamMember>>();

let user = user.first().ok_or_else(|| {
ApiError::InvalidInput(
"This project has no owner".to_string(),
)
})?;

let users = sqlx::query!(
"
SELECT follower_id FROM user_follows
WHERE user_id = $1
",
user.user_id as crate::database::models::ids::UserId
)
.fetch_many(&mut *transaction)
.try_filter_map(|e| async {
Ok(e.right()
.map(|m| database::models::ids::UserId(m.follower_id)))
})
.try_collect::<Vec<database::models::ids::UserId>>()
.await?;

let user = database::models::User::get(
user.user_id,
&mut *transaction,
)
.await?
.ok_or_else(|| {
ApiError::InvalidInput(
"This project has no owner".to_string(),
)
})?;

let project_id: ProjectId = project_item.inner.id.into();

NotificationBuilder {
notification_type: Some("project_created".to_string()),
title: format!(
"**{}** has been created!",
new_project
.title
.as_ref()
.unwrap_or(&project_item.inner.title)
),
text: format!(
"{}, has released a new project: {}",
user.name.unwrap_or(user.username),
new_project
.title
.as_ref()
.unwrap_or(&project_item.inner.title)
),
link: format!(
"/{}/{}",
project_item.project_type, project_id
),
actions: vec![],
}
.insert_many(users, &mut transaction)
.await?;
}

transaction.commit().await?;
Ok(HttpResponse::NoContent().body(""))
} else {
Expand Down
Loading