Payouts finish (#470)

* Almost done

* More work on midas

* Finish payouts backend

* Update Cargo.lock

* Run fmt + prepare
This commit is contained in:
Geometrically 2022-10-30 23:34:56 -07:00 committed by GitHub
parent 6e72be54cb
commit 2ca6e67b37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 2267 additions and 1050 deletions

5
.env
View File

@ -42,7 +42,10 @@ RATE_LIMIT_IGNORE_IPS='["127.0.0.1"]'
WHITELISTED_MODPACK_DOMAINS='["cdn.modrinth.com", "edge.forgecdn.net", "github.com", "raw.githubusercontent.com"]'
ALLOWED_CALLBACK_URLS='["localhost", ".modrinth.com", "-modrinth.vercel.app"]'
ALLOWED_CALLBACK_URLS='["localhost", ".modrinth.com"]'
ARIADNE_ADMIN_KEY=feedbeef
ARIADNE_URL=https://staging-ariadne.modrinth.com/v1/
STRIPE_TOKEN=none
STRIPE_WEBHOOK_SECRET=none

View File

@ -12,32 +12,15 @@ jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
- name: Checkout
uses: actions/checkout@v2
-
name: Docker meta
- name: Fetch docker metadata
id: docker_meta
uses: crazy-max/ghaction-docker-meta@v1
uses: docker/metadata-action@v3
with:
images: ghcr.io/modrinth/labrinth
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
-
name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
-
name: Login to GHCR
if: github.event_name != 'pull_request'
name: Login to GitHub Images
uses: docker/login-action@v1
with:
registry: ghcr.io
@ -45,6 +28,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
context: .
@ -52,13 +36,3 @@ jobs:
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
-
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache

911
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -15,51 +15,55 @@ path = "src/main.rs"
actix = "0.13.0"
actix-web = "4.2.1"
actix-rt = "2.7.0"
tokio = { version = "1.19.0", features = ["sync"] }
tokio-stream = "0.1.8"
actix-multipart = "0.4.0"
actix-cors = "0.6.3"
actix-cors = "0.6.4"
tokio = { version = "1.21.2", features = ["sync"] }
tokio-stream = "0.1.10"
futures = "0.3.24"
futures-timer = "3.0.2"
async-trait = "0.1.57"
dashmap = "5.4.0"
lazy_static = "1.4.0"
meilisearch-sdk = "0.15.0"
reqwest = { version = "0.11.10", features = ["json"] }
rust-s3 = "0.32.3"
reqwest = { version = "0.11.12", features = ["json"] }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_with = "2.0.1"
chrono = { version = "0.4.22", features = ["serde"]}
yaserde = "0.8.0"
yaserde_derive = "0.8.0"
xml-rs = "0.8.4"
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_with = "1.12.0"
chrono = { version = "0.4.22", default-features = false, features = ["clock", "serde", "std"] }
rand = "0.8.5"
bytes = "1.2.1"
base64 = "0.13.0"
sha1 = { version = "0.6.1", features = ["std"] }
sha2 = "0.9.9"
hmac = "0.11.0"
bitflags = "1.3.2"
zip = "0.6.0"
itertools = "0.10.3"
hex = "0.4.3"
validator = { version = "0.14.0", features = ["derive"] }
regex = "1.5.5"
url = "2.2.2"
urlencoding = "2.1.0"
url = "2.3.1"
urlencoding = "2.1.2"
gumdrop = "0.8.1"
dotenvy = "0.15.6"
log = "0.4.16"
env_logger = "0.9.0"
thiserror = "1.0.30"
lazy_static = "1.4.0"
# Temporary - to fix zstd conflict
zip = { git = "https://github.com/zip-rs/zip", rev = "bb230ef56adc13436d1fcdfaa489249d119c498f" }
futures = "0.3.21"
futures-timer = "3.0.2"
rust-s3 = "0.32.3"
async-trait = "0.1.53"
sqlx = { version = "0.6.0", features = ["runtime-actix-rustls", "postgres", "chrono", "offline", "macros", "migrate"] }
bytes = "1.1.0"
dashmap = "5.2.0"
itertools = "0.10.5"
validator = { version = "0.16.0", features = ["derive"] }
regex = "1.6.0"
censor = "0.2.0"
dotenvy = "0.15.6"
log = "0.4.17"
env_logger = "0.9.1"
thiserror = "1.0.37"
sqlx = { version = "0.6.2", features = ["runtime-actix-rustls", "postgres", "chrono", "offline", "macros", "migrate", "decimal"] }
rust_decimal = { version = "1.26", features = ["serde-with-float"] }

View File

@ -1,4 +1,4 @@
FROM rust:1.59.0 as build
FROM rust:1.64.0 as build
ENV PKG_CONFIG_ALLOW_CROSS=1
WORKDIR /usr/src/labrinth

View File

@ -0,0 +1,29 @@
ALTER TABLE team_members DROP COLUMN payouts_split;
ALTER TABLE team_members ADD COLUMN payouts_split numeric(96, 48) NOT NULL DEFAULT 0;
UPDATE team_members
SET payouts_split = 100
WHERE role = 'Owner';
CREATE TABLE payouts_values (
id bigserial PRIMARY KEY,
user_id bigint REFERENCES users NOT NULL,
mod_id bigint REFERENCES mods NULL,
amount numeric(96, 48) NOT NULL,
created timestamptz NOT NULL,
claimed BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE INDEX payouts_values_user_id
ON payouts_values (user_id);
CREATE INDEX payouts_values_mod_id
ON payouts_values (mod_id);
CREATE INDEX payouts_values_created
ON payouts_values (created);
ALTER TABLE users ADD COLUMN midas_expires timestamptz NULL;
ALTER TABLE users ADD COLUMN is_overdue BOOLEAN NULL;
ALTER TABLE users ADD COLUMN stripe_customer_id varchar(255) NULL;
ALTER TABLE users ADD COLUMN paypal_email varchar(128) NULL;

File diff suppressed because it is too large Load Diff

View File

@ -474,6 +474,17 @@ impl Project {
.execute(&mut *transaction)
.await?;
sqlx::query!(
"
UPDATE payouts_values
SET mod_id = NULL
WHERE (mod_id = $1)
",
id as ProjectId,
)
.execute(&mut *transaction)
.await?;
sqlx::query!(
"
DELETE FROM mods
@ -968,6 +979,7 @@ impl Project {
.await
}
}
#[derive(Clone, Debug)]
pub struct QueryProject {
pub inner: Project,

View File

@ -2,6 +2,7 @@ use super::ids::*;
use crate::database::models::User;
use crate::models::teams::Permissions;
use crate::models::users::Badges;
use rust_decimal::Decimal;
pub struct TeamBuilder {
pub members: Vec<TeamMemberBuilder>,
@ -11,7 +12,7 @@ pub struct TeamMemberBuilder {
pub role: String,
pub permissions: Permissions,
pub accepted: bool,
pub payouts_split: f32,
pub payouts_split: Decimal,
}
impl TeamBuilder {
@ -81,7 +82,7 @@ pub struct TeamMember {
pub role: String,
pub permissions: Permissions,
pub accepted: bool,
pub payouts_split: f32,
pub payouts_split: Decimal,
}
/// A member of a team
@ -93,7 +94,7 @@ pub struct QueryTeamMember {
pub role: String,
pub permissions: Permissions,
pub accepted: bool,
pub payouts_split: f32,
pub payouts_split: Decimal,
}
impl TeamMember {
@ -157,7 +158,7 @@ impl TeamMember {
SELECT tm.id id, tm.role member_role, tm.permissions permissions, tm.accepted accepted, tm.payouts_split payouts_split,
u.id user_id, u.github_id github_id, u.name user_name, u.email email,
u.avatar_url avatar_url, u.username username, u.bio bio,
u.created created, u.role user_role, u.badges badges
u.created created, u.role user_role, u.badges badges, u.paypal_email paypal_email
FROM team_members tm
INNER JOIN users u ON u.id = tm.user_id
WHERE tm.team_id = $1
@ -185,6 +186,7 @@ impl TeamMember {
created: m.created,
role: m.user_role,
badges: Badges::from_bits(m.badges as u64).unwrap_or_default(),
paypal_email: m.paypal_email
},
payouts_split: m.payouts_split
})))
@ -219,7 +221,7 @@ impl TeamMember {
SELECT tm.id id, tm.team_id team_id, tm.role member_role, tm.permissions permissions, tm.accepted accepted, tm.payouts_split payouts_split,
u.id user_id, u.github_id github_id, u.name user_name, u.email email,
u.avatar_url avatar_url, u.username username, u.bio bio,
u.created created, u.role user_role, u.badges badges
u.created created, u.role user_role, u.badges badges, u.paypal_email paypal_email
FROM team_members tm
INNER JOIN users u ON u.id = tm.user_id
WHERE tm.team_id = ANY($1)
@ -248,6 +250,7 @@ impl TeamMember {
created: m.created,
role: m.user_role,
badges: Badges::from_bits(m.badges as u64).unwrap_or_default(),
paypal_email: m.paypal_email
},
payouts_split: m.payouts_split
})))
@ -540,7 +543,7 @@ impl TeamMember {
new_permissions: Option<Permissions>,
new_role: Option<String>,
new_accepted: Option<bool>,
new_payouts_split: Option<f32>,
new_payouts_split: Option<Decimal>,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), super::DatabaseError> {
if let Some(permissions) = new_permissions {

View File

@ -13,6 +13,7 @@ pub struct User {
pub created: DateTime<Utc>,
pub role: String,
pub badges: Badges,
pub paypal_email: Option<String>,
}
impl User {
@ -56,7 +57,7 @@ impl User {
"
SELECT u.github_id, u.name, u.email,
u.avatar_url, u.username, u.bio,
u.created, u.role, u.badges
u.created, u.role, u.badges, u.paypal_email
FROM users u
WHERE u.id = $1
",
@ -78,6 +79,7 @@ impl User {
role: row.role,
badges: Badges::from_bits(row.badges as u64)
.unwrap_or_default(),
paypal_email: row.paypal_email,
}))
} else {
Ok(None)
@ -95,7 +97,7 @@ impl User {
"
SELECT u.id, u.name, u.email,
u.avatar_url, u.username, u.bio,
u.created, u.role, u.badges
u.created, u.role, u.badges, u.paypal_email
FROM users u
WHERE u.github_id = $1
",
@ -117,6 +119,7 @@ impl User {
role: row.role,
badges: Badges::from_bits(row.badges as u64)
.unwrap_or_default(),
paypal_email: row.paypal_email,
}))
} else {
Ok(None)
@ -134,7 +137,7 @@ impl User {
"
SELECT u.id, u.github_id, u.name, u.email,
u.avatar_url, u.username, u.bio,
u.created, u.role, u.badges
u.created, u.role, u.badges, u.paypal_email
FROM users u
WHERE LOWER(u.username) = LOWER($1)
",
@ -156,6 +159,7 @@ impl User {
role: row.role,
badges: Badges::from_bits(row.badges as u64)
.unwrap_or_default(),
paypal_email: row.paypal_email,
}))
} else {
Ok(None)
@ -177,7 +181,7 @@ impl User {
"
SELECT u.id, u.github_id, u.name, u.email,
u.avatar_url, u.username, u.bio,
u.created, u.role, u.badges
u.created, u.role, u.badges, u.paypal_email
FROM users u
WHERE u.id = ANY($1)
",
@ -196,6 +200,7 @@ impl User {
created: u.created,
role: u.role,
badges: Badges::from_bits(u.badges as u64).unwrap_or_default(),
paypal_email: u.paypal_email,
}))
})
.try_collect::<Vec<User>>()
@ -352,6 +357,16 @@ impl User {
.execute(&mut *transaction)
.await?;
sqlx::query!(
"
DELETE FROM payouts_values
WHERE user_id = $1
",
id as UserId,
)
.execute(&mut *transaction)
.await?;
sqlx::query!(
"
DELETE FROM users

View File

@ -21,7 +21,7 @@ impl S3Host {
access_token: &str,
secret: &str,
) -> Result<S3Host, FileHostingError> {
let mut bucket = Bucket::new(
let bucket = Bucket::new(
bucket_name,
if bucket_region == "r2" {
Region::R2 {

View File

@ -7,7 +7,6 @@ use crate::util::env::{parse_strings_from_var, parse_var};
use actix_cors::Cors;
use actix_web::{web, App, HttpServer};
use env_logger::Env;
use gumdrop::Options;
use log::{error, info, warn};
use search::indexing::index_projects;
use search::indexing::IndexingSettings;
@ -25,23 +24,6 @@ mod search;
mod util;
mod validate;
#[derive(Debug, Options)]
struct Config {
#[options(help = "Print help message")]
help: bool,
#[options(no_short, help = "Skip indexing on startup")]
skip_first_index: bool,
#[options(no_short, help = "Reset the documents in the indices")]
reset_indices: bool,
#[options(
no_short,
help = "Allow missing environment variables on startup. This is a bad idea, but it may work in some cases."
)]
allow_missing_vars: bool,
}
#[derive(Clone)]
pub struct Pepper {
pub pepper: String,
@ -53,40 +35,20 @@ async fn main() -> std::io::Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info"))
.init();
let config = Config::parse_args_default_or_exit();
if check_env_vars() {
error!("Some environment variables are missing!");
if !config.allow_missing_vars {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Missing required environment variables",
));
}
}
info!("Starting Labrinth on {}", dotenvy::var("BIND_ADDR").unwrap());
info!(
"Starting Labrinth on {}",
dotenvy::var("BIND_ADDR").unwrap()
);
let search_config = search::SearchConfig {
address: dotenvy::var("MEILISEARCH_ADDR").unwrap(),
key: dotenvy::var("MEILISEARCH_KEY").unwrap(),
};
if config.reset_indices {
info!("Resetting indices");
search::indexing::reset_indices(&search_config)
.await
.unwrap();
return Ok(());
}
// Allow manually skipping the initial indexing for quicker iteration
// and startup times.
let skip_initial = config.skip_first_index;
if skip_initial {
info!("Skipping initial indexing");
}
database::check_for_migrations()
.await
.expect("An error occurred while running migrations.");
@ -131,20 +93,12 @@ async fn main() -> std::io::Result<()> {
parse_var("LOCAL_INDEX_INTERVAL").unwrap_or(3600),
);
let mut skip = skip_initial;
let pool_ref = pool.clone();
let search_config_ref = search_config.clone();
scheduler.run(local_index_interval, move || {
let pool_ref = pool_ref.clone();
let search_config_ref = search_config_ref.clone();
let local_skip = skip;
if skip {
skip = false;
}
async move {
if local_skip {
return;
}
info!("Indexing local database");
let settings = IndexingSettings { index_local: true };
let result =
@ -183,7 +137,7 @@ async fn main() -> std::io::Result<()> {
}
});
scheduler::schedule_versions(&mut scheduler, pool.clone(), skip_initial);
scheduler::schedule_versions(&mut scheduler, pool.clone());
let download_queue = Arc::new(DownloadQueue::new());
@ -253,7 +207,9 @@ async fn main() -> std::io::Result<()> {
})
.with_interval(std::time::Duration::from_secs(60))
.with_max_requests(300)
.with_ignore_key(dotenvy::var("RATE_LIMIT_IGNORE_KEY").ok()),
.with_ignore_key(
dotenvy::var("RATE_LIMIT_IGNORE_KEY").ok(),
),
)
.app_data(web::Data::new(pool.clone()))
.app_data(web::Data::new(file_host.clone()))
@ -346,5 +302,8 @@ fn check_env_vars() -> bool {
failed |= check_var::<String>("ARIADNE_ADMIN_KEY");
failed |= check_var::<String>("ARIADNE_URL");
failed |= check_var::<String>("STRIPE_TOKEN");
failed |= check_var::<String>("STRIPE_WEBHOOK_SECRET");
failed
}

View File

@ -1,6 +1,7 @@
use super::ids::Base62Id;
use crate::database::models::team_item::QueryTeamMember;
use crate::models::users::User;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
/// The ID of a team
@ -59,9 +60,11 @@ pub struct TeamMember {
pub permissions: Option<Permissions>,
/// Whether the user has joined the team or is just invited to it
pub accepted: bool,
#[serde(with = "rust_decimal::serde::float_option")]
/// Payouts split. This is a weighted average. For example. if a team has two members with this
/// value set to 25.0 for both members, they split revenue 50/50
pub payouts_split: Option<f32>,
pub payouts_split: Option<Decimal>,
}
impl TeamMember {

View File

@ -13,6 +13,7 @@ bitflags::bitflags! {
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
pub struct Badges: u64 {
// 1 << 0 unused - ignore + replace with something later
const MIDAS = 1 << 0;
const EARLY_MODPACK_ADOPTER = 1 << 1;
const EARLY_RESPACK_ADOPTER = 1 << 2;
@ -44,6 +45,7 @@ pub struct User {
pub created: DateTime<Utc>,
pub role: Role,
pub badges: Badges,
pub paypal_email: Option<String>,
}
use crate::database::models::user_item::User as DBUser;
@ -60,6 +62,7 @@ impl From<DBUser> for User {
created: data.created,
role: Role::from_string(&*data.role),
badges: data.badges,
paypal_email: None,
}
}
}

View File

@ -1,11 +1,16 @@
use crate::models::ids::ProjectId;
use crate::routes::ApiError;
use crate::util::auth::check_is_admin_from_headers;
use crate::util::guards::admin_key_guard;
use crate::util::payout_calc::get_claimable_time;
use crate::DownloadQueue;
use actix_web::{patch, web, HttpResponse};
use actix_web::{get, patch, post, web, HttpRequest, HttpResponse};
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::Deserialize;
use serde_json::json;
use sqlx::PgPool;
use std::collections::HashMap;
use std::sync::Arc;
#[derive(Deserialize)]
@ -80,5 +85,294 @@ pub async fn count_download(
.await
.ok();
Ok(HttpResponse::Ok().body(""))
Ok(HttpResponse::NoContent().body(""))
}
#[derive(Deserialize)]
pub struct PayoutData {
amount: Decimal,
date: DateTime<Utc>,
}
#[post("/_process_payout", guard = "admin_key_guard")]
pub async fn process_payout(
pool: web::Data<PgPool>,
data: web::Json<PayoutData>,
) -> Result<HttpResponse, ApiError> {
let start = data.date.date().and_hms(0, 0, 0);
let client = reqwest::Client::new();
let mut transaction = pool.begin().await?;
#[derive(Deserialize)]
struct PayoutMultipliers {
sum: u64,
values: HashMap<i64, u64>,
}
let multipliers: PayoutMultipliers = client
.get(format!(
"{}multipliers?start_date=\"{}\"",
dotenvy::var("ARIADNE_URL")?,
start.to_rfc3339(),
))
.header("Modrinth-Admin", dotenvy::var("ARIADNE_ADMIN_KEY")?)
.send()
.await
.map_err(|_| {
ApiError::Analytics(
"Error while fetching payout multipliers!".to_string(),
)
})?
.json()
.await
.map_err(|_| {
ApiError::Analytics(
"Error while deserializing payout multipliers!".to_string(),
)
})?;
sqlx::query!(
"
DELETE FROM payouts_values
WHERE created = $1
",
start
)
.execute(&mut *transaction)
.await?;
struct Project {
project_type: String,
// user_id, payouts_split
team_members: Vec<(i64, Decimal)>,
// user_id, payouts_split
split_team_members: Vec<(i64, Decimal)>,
}
let mut projects_map: HashMap<i64, Project> = HashMap::new();
use futures::TryStreamExt;
sqlx::query!(
"
SELECT m.id id, tm.user_id user_id, tm.payouts_split payouts_split, pt.name project_type
FROM mods m
INNER JOIN team_members tm on m.team_id = tm.team_id
INNER JOIN project_types pt ON pt.id = m.project_type
WHERE m.id = ANY($1)
",
&multipliers.values.keys().map(|x| *x).collect::<Vec<i64>>()
)
.fetch_many(&mut *transaction)
.try_for_each(|e| {
if let Some(row) = e.right() {
if let Some(project) = projects_map.get_mut(&row.id) {
project.team_members.push((row.user_id, row.payouts_split));
} else {
projects_map.insert(row.id, Project {
project_type: row.project_type,
team_members: vec![(row.user_id, row.payouts_split)],
split_team_members: Default::default()
});
}
}
futures::future::ready(Ok(()))
})
.await?;
// Specific Payout Conditions (ex: modpack payout split)
let mut projects_split_dependencies = Vec::new();
for (id, project) in &projects_map {
if project.project_type == "modpack" {
projects_split_dependencies.push(*id);
}
}
if !projects_split_dependencies.is_empty() {
// (dependent_id, (dependency_id, times_depended))
let mut project_dependencies: HashMap<i64, Vec<(i64, i64)>> =
HashMap::new();
// dependency_ids to fetch team members from
let mut fetch_team_members: Vec<i64> = Vec::new();
sqlx::query!(
"
SELECT mv.mod_id, m.id, COUNT(m.id) times_depended FROM versions mv
INNER JOIN dependencies d ON d.dependent_id = mv.id
INNER JOIN versions v ON d.dependency_id = v.id
INNER JOIN mods m ON v.mod_id = m.id OR d.mod_dependency_id = m.id
WHERE mv.mod_id = ANY($1)
group by mv.mod_id, m.id;
",
&projects_split_dependencies
)
.fetch_many(&mut *transaction)
.try_for_each(|e| {
if let Some(row) = e.right() {
fetch_team_members.push(row.id);
if let Some(project) = project_dependencies.get_mut(&row.mod_id)
{
project.push((row.id, row.times_depended.unwrap_or(0)));
} else {
project_dependencies.insert(
row.mod_id,
vec![(row.id, row.times_depended.unwrap_or(0))],
);
}
}
futures::future::ready(Ok(()))
})
.await?;
// (project_id, (user_id, payouts_split))
let mut team_members: HashMap<i64, Vec<(i64, Decimal)>> =
HashMap::new();
sqlx::query!(
"
SELECT m.id id, tm.user_id user_id, tm.payouts_split payouts_split
FROM mods m
INNER JOIN team_members tm on m.team_id = tm.team_id
WHERE m.id = ANY($1)
",
&*fetch_team_members
)
.fetch_many(&mut *transaction)
.try_for_each(|e| {
if let Some(row) = e.right() {
if let Some(project) = team_members.get_mut(&row.id) {
project.push((row.user_id, row.payouts_split));
} else {
team_members
.insert(row.id, vec![(row.user_id, row.payouts_split)]);
}
}
futures::future::ready(Ok(()))
})
.await?;
for (project, dependencies) in project_dependencies {
let dep_sum: i64 = dependencies.iter().map(|x| x.1).sum();
let project = projects_map.get_mut(&project);
if let Some(project) = project {
for dependency in dependencies {
let project_multiplier: Decimal =
Decimal::from(dependency.1) / Decimal::from(dep_sum);
if let Some(members) = team_members.get(&dependency.0) {
let members_sum: Decimal =
members.iter().map(|x| x.1).sum();
for member in members {
let member_multiplier: Decimal =
member.1 / members_sum;
project.split_team_members.push((
member.0,
member_multiplier * project_multiplier,
));
}
}
}
}
}
}
for (id, project) in projects_map {
if let Some(value) = &multipliers.values.get(&id) {
let project_multiplier: Decimal =
Decimal::from(**value) / Decimal::from(multipliers.sum);
let default_split_given = Decimal::from(1);
let split_given = Decimal::from(1) / Decimal::from(5);
let split_retention = Decimal::from(4) / Decimal::from(5);
let sum_splits: Decimal =
project.team_members.iter().map(|x| x.1).sum();
let sum_tm_splits: Decimal =
project.split_team_members.iter().map(|x| x.1).sum();
for (user_id, split) in project.team_members {
let payout: Decimal = data.amount
* project_multiplier
* (split / sum_splits)
* (if !project.split_team_members.is_empty() {
&split_given
} else {
&default_split_given
});
sqlx::query!(
"
INSERT INTO payouts_values (user_id, mod_id, amount, created)
VALUES ($1, $2, $3, $4)
",
user_id,
id,
payout,
start
)
.execute(&mut *transaction)
.await?;
}
for (user_id, split) in project.split_team_members {
let payout: Decimal = data.amount
* project_multiplier
* (split / sum_tm_splits)
* split_retention;
sqlx::query!(
"
INSERT INTO payouts_values (user_id, amount, created)
VALUES ($1, $2, $3)
",
user_id,
payout,
start
)
.execute(&mut *transaction)
.await?;
}
}
}
transaction.commit().await?;
Ok(HttpResponse::NoContent().body(""))
}
#[get("/_get-payout-data")]
pub async fn get_payout_data(
req: HttpRequest,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, ApiError> {
check_is_admin_from_headers(req.headers(), &**pool).await?;
use futures::stream::TryStreamExt;
let payouts = sqlx::query!(
"
SELECT u.paypal_email, SUM(pv.amount) amount
FROM payouts_values pv
INNER JOIN users u ON pv.user_id = u.id AND u.paypal_email IS NOT NULL
WHERE pv.created <= $1 AND pv.claimed = FALSE
GROUP BY u.paypal_email
",
get_claimable_time(Utc::now(), false)
)
.fetch_many(&**pool)
.try_filter_map(|e| async {
Ok(e.right().map(|r| (r.paypal_email.unwrap_or_default(), r.amount.unwrap_or_default())))
})
.try_collect::<HashMap<String, Decimal>>()
.await?;
Ok(HttpResponse::Ok().json(payouts))
}

View File

@ -273,6 +273,7 @@ pub async fn auth_callback(
created: Utc::now(),
role: Role::Developer.to_string(),
badges: Badges::default(),
paypal_email: None,
}
.insert(&mut transaction)
.await?;

336
src/routes/midas.rs Normal file
View File

@ -0,0 +1,336 @@
use crate::models::users::UserId;
use crate::routes::ApiError;
use crate::util::auth::get_user_from_headers;
use actix_web::{post, web, HttpRequest, HttpResponse};
use chrono::{DateTime, Duration, NaiveDateTime, Utc};
use hmac::{Hmac, Mac, NewMac};
use itertools::Itertools;
use serde::Deserialize;
use serde_json::{json, Value};
use sqlx::PgPool;
#[derive(Deserialize)]
pub struct CheckoutData {
pub price_id: String,
}
#[post("/_stripe-init-checkout")]
pub async fn init_checkout(
req: HttpRequest,
pool: web::Data<PgPool>,
data: web::Json<CheckoutData>,
) -> Result<HttpResponse, ApiError> {
let user = get_user_from_headers(req.headers(), &**pool).await?;
let client = reqwest::Client::new();
#[derive(Deserialize)]
struct Session {
url: Option<String>,
}
let session = client
.post("https://api.stripe.com/v1/checkout/sessions")
.header(
"Authorization",
format!("Bearer {}", dotenvy::var("STRIPE_TOKEN")?),
)
.form(&[
("mode", "subscription"),
("line_items[0][price]", &*data.price_id),
("line_items[0][quantity]", "1"),
("success_url", "https://modrinth.com/welcome-to-midas"),
("cancel_url", "https://modrinth.com/midas"),
("metadata[user_id]", &user.id.to_string()),
])
.send()
.await
.map_err(|_| {
ApiError::Payments(
"Error while creating checkout session!".to_string(),
)
})?
.json::<Session>()
.await
.map_err(|_| {
ApiError::Payments(
"Error while deserializing checkout response!".to_string(),
)
})?;
Ok(HttpResponse::Ok().json(json!(
{
"url": session.url
}
)))
}
#[post("/_stripe-init-portal")]
pub async fn init_customer_portal(
req: HttpRequest,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, ApiError> {
let user = get_user_from_headers(req.headers(), &**pool).await?;
let customer_id = sqlx::query!(
"
SELECT u.stripe_customer_id
FROM users u
WHERE u.id = $1
",
user.id.0 as i64,
)
.fetch_optional(&**pool)
.await?
.and_then(|x| x.stripe_customer_id)
.ok_or_else(|| {
ApiError::InvalidInput(
"User is not linked to stripe account!".to_string(),
)
})?;
let client = reqwest::Client::new();
#[derive(Deserialize)]
struct Session {
url: Option<String>,
}
let session = client
.post("https://api.stripe.com/v1/billing_portal/sessions")
.header(
"Authorization",
format!("Bearer {}", dotenvy::var("STRIPE_TOKEN")?),
)
.form(&[
("customer", &*customer_id),
("return_url", "https://modrinth.com/settings/billing"),
])
.send()
.await
.map_err(|_| {
ApiError::Payments(
"Error while creating billing session!".to_string(),
)
})?
.json::<Session>()
.await
.map_err(|_| {
ApiError::Payments(
"Error while deserializing billing response!".to_string(),
)
})?;
Ok(HttpResponse::Ok().json(json!(
{
"url": session.url
}
)))
}
#[post("/_stripe-webook")]
pub async fn handle_stripe_webhook(
body: String,
req: HttpRequest,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, ApiError> {
if let Some(signature_raw) = req
.headers()
.get("Stripe-Signature")
.and_then(|x| x.to_str().ok())
{
let mut timestamp = None;
let mut signature = None;
for val in signature_raw.split(',') {
let key_val = val.split('=').collect_vec();
if key_val.len() == 2 {
if key_val[0] == "v1" {
signature = hex::decode(key_val[1]).ok()
} else if key_val[0] == "t" {
timestamp = key_val[1].parse::<i64>().ok()
}
}
}
if let Some(timestamp) = timestamp {
if let Some(signature) = signature {
type HmacSha256 = Hmac<sha2::Sha256>;
let mut key = HmacSha256::new_from_slice(dotenvy::var("STRIPE_WEBHOOK_SECRET")?.as_bytes()).map_err(|_| {
ApiError::Crypto(
"Unable to initialize HMAC instance due to invalid key length!".to_string(),
)
})?;
key.update(format!("{}.{}", timestamp, body).as_bytes());
key.verify(&signature).map_err(|_| {
ApiError::Crypto(
"Unable to verify webhook signature!".to_string(),
)
})?;
if timestamp < (Utc::now() - Duration::minutes(5)).timestamp()
|| timestamp
> (Utc::now() + Duration::minutes(5)).timestamp()
{
return Err(ApiError::Crypto(
"Webhook signature expired!".to_string(),
));
}
} else {
return Err(ApiError::Crypto("Missing signature!".to_string()));
}
} else {
return Err(ApiError::Crypto("Missing timestamp!".to_string()));
}
} else {
return Err(ApiError::Crypto("Missing signature header!".to_string()));
}
#[derive(Deserialize)]
struct StripeWebhookBody {
#[serde(rename = "type")]
type_: String,
data: StripeWebhookObject,
}
#[derive(Deserialize)]
struct StripeWebhookObject {
object: Value,
}
let webhook: StripeWebhookBody = serde_json::from_str(&*body)?;
#[derive(Deserialize)]
struct CheckoutSession {
customer: String,
metadata: SessionMetadata,
}
#[derive(Deserialize)]
struct SessionMetadata {
user_id: UserId,
}
#[derive(Deserialize)]
struct Invoice {
customer: String,
// paid: bool,
lines: InvoiceLineItems,
}
#[derive(Deserialize)]
struct InvoiceLineItems {
pub data: Vec<InvoiceLineItem>,
}
#[derive(Deserialize)]
struct InvoiceLineItem {
period: Period,
}
#[derive(Deserialize)]
struct Period {
// start: i64,
end: i64,
}
#[derive(Deserialize)]
struct Subscription {
customer: String,
}
let mut transaction = pool.begin().await?;
// TODO: Currently hardcoded to midas-only. When we add more stuff should include price IDs
match &*webhook.type_ {
"checkout.session.completed" => {
let session: CheckoutSession =
serde_json::from_value(webhook.data.object)?;
sqlx::query!(
"
UPDATE users
SET stripe_customer_id = $1
WHERE (id = $2)
",
session.customer,
session.metadata.user_id.0 as i64,
)
.execute(&mut *transaction)
.await?;
}
"invoice.paid" => {
let invoice: Invoice = serde_json::from_value(webhook.data.object)?;
if let Some(item) = invoice.lines.data.first() {
let expires: DateTime<Utc> = DateTime::from_utc(
NaiveDateTime::from_timestamp(item.period.end, 0),
Utc,
) + Duration::days(1);
sqlx::query!(
"
UPDATE users
SET midas_expires = $1, is_overdue = FALSE
WHERE (stripe_customer_id = $2)
",
expires,
invoice.customer,
)
.execute(&mut *transaction)
.await?;
}
}
"invoice.payment_failed" => {
let invoice: Invoice = serde_json::from_value(webhook.data.object)?;
let customer_id = sqlx::query!(
"
SELECT u.id
FROM users u
WHERE u.stripe_customer_id = $1
",
invoice.customer,
)
.fetch_optional(&**pool)
.await?
.map(|x| x.id);
if let Some(user_id) = customer_id {
sqlx::query!(
"
UPDATE users
SET is_overdue = TRUE
WHERE (id = $1)
",
user_id,
)
.execute(&mut *transaction)
.await?;
}
}
"customer.subscription.deleted" => {
let session: Subscription =
serde_json::from_value(webhook.data.object)?;
sqlx::query!(
"
UPDATE users
SET stripe_customer_id = NULL, midas_expires = NULL, is_overdue = NULL
WHERE (stripe_customer_id = $1)
",
session.customer,
)
.execute(&mut *transaction)
.await?;
}
_ => {}
};
transaction.commit().await?;
Ok(HttpResponse::NoContent().body(""))
}

View File

@ -6,6 +6,7 @@ mod auth;
mod health;
mod index;
mod maven;
mod midas;
mod moderation;
mod not_found;
mod notifications;
@ -41,7 +42,8 @@ pub fn v2_config(cfg: &mut web::ServiceConfig) {
.configure(moderation_config)
.configure(reports_config)
.configure(notifications_config)
.configure(admin_config),
.configure(admin_config)
.configure(midas_config),
);
}
@ -169,6 +171,15 @@ pub fn admin_config(cfg: &mut web::ServiceConfig) {
cfg.service(web::scope("admin").service(admin::count_download));
}
pub fn midas_config(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("midas")
.service(midas::init_checkout)
.service(midas::init_customer_portal)
.service(midas::handle_stripe_webhook),
);
}
#[derive(thiserror::Error, Debug)]
pub enum ApiError {
#[error("Environment Error")]
@ -195,6 +206,12 @@ pub enum ApiError {
Search(#[from] meilisearch_sdk::errors::Error),
#[error("Indexing Error: {0}")]
Indexing(#[from] crate::search::indexing::IndexingError),
#[error("Ariadne Error: {0}")]
Analytics(String),
#[error("Crypto Error: {0}")]
Crypto(String),
#[error("Payments Error: {0}")]
Payments(String),
}
impl actix_web::ResponseError for ApiError {
@ -234,6 +251,13 @@ impl actix_web::ResponseError for ApiError {
ApiError::Validation(..) => {
actix_web::http::StatusCode::BAD_REQUEST
}
ApiError::Analytics(..) => {
actix_web::http::StatusCode::FAILED_DEPENDENCY
}
ApiError::Crypto(..) => actix_web::http::StatusCode::FORBIDDEN,
ApiError::Payments(..) => {
actix_web::http::StatusCode::FAILED_DEPENDENCY
}
}
}
@ -253,6 +277,9 @@ impl actix_web::ResponseError for ApiError {
ApiError::FileHosting(..) => "file_hosting_error",
ApiError::InvalidInput(..) => "invalid_input",
ApiError::Validation(..) => "invalid_input",
ApiError::Analytics(..) => "analytics_error",
ApiError::Crypto(..) => "crypto_error",
ApiError::Payments(..) => "payments_error",
},
description: &self.to_string(),
},

View File

@ -17,6 +17,7 @@ use actix_web::web::Data;
use actix_web::{post, HttpRequest, HttpResponse};
use chrono::Utc;
use futures::stream::StreamExt;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use sqlx::postgres::PgPool;
use std::sync::Arc;
@ -274,9 +275,7 @@ pub async fn project_create(
// fix multipart error bug:
payload.for_each(|_| ready(())).await;
if let Err(e) = undo_result {
return Err(e);
}
undo_result?;
if let Err(e) = rollback_result {
return Err(e.into());
}
@ -628,7 +627,7 @@ pub async fn project_create_inner(
role: crate::models::teams::OWNER_ROLE.to_owned(),
permissions: crate::models::teams::Permissions::ALL,
accepted: true,
payouts_split: 100.0,
payouts_split: Decimal::from(100),
}],
};
@ -793,7 +792,8 @@ pub async fn project_create_inner(
let _project_id = project_builder.insert(&mut *transaction).await?;
if status == ProjectStatus::Processing {
if let Ok(webhook_url) = dotenvy::var("MODERATION_DISCORD_WEBHOOK") {
if let Ok(webhook_url) = dotenvy::var("MODERATION_DISCORD_WEBHOOK")
{
crate::util::webhook::send_discord_webhook(
response.clone(),
webhook_url,

View File

@ -8,6 +8,7 @@ use crate::models::users::UserId;
use crate::routes::ApiError;
use crate::util::auth::get_user_from_headers;
use actix_web::{delete, get, patch, post, web, HttpRequest, HttpResponse};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
@ -216,7 +217,7 @@ pub struct NewTeamMember {
#[serde(default = "Permissions::default")]
pub permissions: Permissions,
#[serde(default)]
pub payouts_split: f32,
pub payouts_split: Decimal,
}
#[post("{id}/members")]
@ -259,7 +260,9 @@ pub async fn add_team_member(
));
}
if !(0.0..=5000.0).contains(&new_member.payouts_split) {
if new_member.payouts_split < Decimal::from(0)
|| new_member.payouts_split > Decimal::from(5000)
{
return Err(ApiError::InvalidInput(
"Payouts split must be between 0 and 5000!".to_string(),
));
@ -360,7 +363,7 @@ pub async fn add_team_member(
pub struct EditTeamMember {
pub permissions: Option<Permissions>,
pub role: Option<String>,
pub payouts_split: Option<f32>,
pub payouts_split: Option<Decimal>,
}
#[patch("{id}/members/{user_id}")]
@ -419,7 +422,9 @@ pub async fn edit_team_member(
}
if let Some(payouts_split) = edit_member.payouts_split {
if !(0.0..=5000.0).contains(&payouts_split) {
if payouts_split < Decimal::from(0)
|| payouts_split > Decimal::from(5000)
{
return Err(ApiError::InvalidInput(
"Payouts split must be between 0 and 5000!".to_string(),
));

View File

@ -1,16 +1,20 @@
use crate::database::models::User;
use crate::file_hosting::FileHost;
use crate::models::notifications::Notification;
use crate::models::projects::{Project, ProjectStatus};
use crate::models::projects::{Project, ProjectId, ProjectStatus};
use crate::models::users::{Badges, Role, UserId};
use crate::routes::ApiError;
use crate::util::auth::get_user_from_headers;
use crate::util::auth::{check_is_admin_from_headers, get_user_from_headers};
use crate::util::payout_calc::get_claimable_time;
use crate::util::routes::read_from_payload;
use crate::util::validate::validation_errors_to_string;
use actix_web::{delete, get, patch, web, HttpRequest, HttpResponse};
use chrono::{DateTime, Utc};
use lazy_static::lazy_static;
use regex::Regex;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use serde_json::json;
use sqlx::PgPool;
use std::sync::Arc;
use validator::Validate;
@ -20,10 +24,8 @@ pub async fn user_auth_get(
req: HttpRequest,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, ApiError> {
Ok(HttpResponse::Ok().json(
get_user_from_headers(req.headers(), &mut *pool.acquire().await?)
.await?,
))
Ok(HttpResponse::Ok()
.json(get_user_from_headers(req.headers(), &**pool).await?))
}
#[derive(Serialize, Deserialize)]
@ -155,6 +157,8 @@ pub struct EditUser {
pub bio: Option<Option<String>>,
pub role: Option<Role>,
pub badges: Option<Badges>,
#[validate(email, length(max = 128))]
pub paypal_email: Option<Option<String>>,
}
#[patch("{id}")]
@ -299,6 +303,20 @@ pub async fn user_edit(
.await?;
}
if let Some(paypal_email) = &new_user.paypal_email {
sqlx::query!(
"
UPDATE users
SET paypal_email = $1
WHERE (id = $2)
",
paypal_email.as_deref(),
id as crate::database::models::ids::UserId,
)
.execute(&mut *transaction)
.await?;
}
transaction.commit().await?;
Ok(HttpResponse::NoContent().body(""))
} else {
@ -542,3 +560,102 @@ pub async fn user_notifications(
Ok(HttpResponse::NotFound().body(""))
}
}
#[derive(Serialize)]
pub struct Payout {
pub claimed: bool,
pub claimable: bool,
pub created: DateTime<Utc>,
pub project: Option<ProjectId>,
pub amount: Decimal,
}
#[get("{id}/payouts")]
pub async fn user_payouts(
req: HttpRequest,
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, ApiError> {
let user = get_user_from_headers(req.headers(), &**pool).await?;
let id_option =
User::get_id_from_username_or_id(&*info.into_inner().0, &**pool)
.await?;
if let Some(id) = id_option {
if !user.role.is_admin() && user.id != id.into() {
return Err(ApiError::CustomAuthentication(
"You do not have permission to see the payouts of this user!"
.to_string(),
));
}
use futures::TryStreamExt;
let payouts: Vec<Payout> = sqlx::query!(
"
SELECT pv.mod_id, pv.created, pv.claimed, pv.amount
FROM payouts_values pv
WHERE pv.user_id = $1
ORDER BY pv.created DESC
",
id as crate::database::models::UserId
)
.fetch_many(&**pool)
.try_filter_map(|e| async {
Ok(e.right().map(|row| {
let claimable_time: DateTime<Utc> =
get_claimable_time(row.created, true);
Payout {
claimed: row.claimed,
claimable: Utc::now() > claimable_time,
created: row.created,
project: row.mod_id.map(|x| ProjectId(x as u64)),
amount: row.amount,
}
}))
})
.try_collect::<Vec<Payout>>()
.await?;
Ok(HttpResponse::Ok().json(json!({
"all_time": payouts.iter().map(|x| x.amount).sum::<Decimal>(),
"current_period": payouts.iter().filter(|x| !x.claimed && !x.claimable).map(|x| x.amount).sum::<Decimal>(),
"withdrawable": payouts.iter().filter(|x| x.claimable && !x.claimed).map(|x| x.amount).sum::<Decimal>(),
"payouts": payouts,
})))
} else {
Ok(HttpResponse::NotFound().body(""))
}
}
#[get("{id}/payouts")]
pub async fn finish_user_payout(
req: HttpRequest,
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, ApiError> {
check_is_admin_from_headers(req.headers(), &**pool).await?;
let id_option =
User::get_id_from_username_or_id(&*info.into_inner().0, &**pool)
.await?;
if let Some(id) = id_option {
sqlx::query!(
"
UPDATE payouts_values
SET claimed = TRUE
WHERE (claimed = FALSE AND user_id = $1 AND created <= $2)
",
id as crate::database::models::ids::UserId,
get_claimable_time(Utc::now(), false)
)
.execute(&**pool)
.await?;
Ok(HttpResponse::NoContent().body(""))
} else {
Ok(HttpResponse::NotFound().body(""))
}
}

View File

@ -136,9 +136,7 @@ pub async fn mod_create(
let undo_result = undo_uploads(&***file_host, &uploaded_files).await;
let rollback_result = transaction.rollback().await;
if let Err(e) = undo_result {
return Err(e);
}
undo_result?;
if let Err(e) = rollback_result {
return Err(e.into());
}

View File

@ -1,4 +1,4 @@
use crate::file_hosting::FileHost;
use crate::database;
use crate::models::ids::{ProjectId, UserId, VersionId};
use crate::models::projects::{
Dependency, GameVersion, Loader, Version, VersionFile, VersionType,
@ -7,12 +7,10 @@ use crate::models::teams::Permissions;
use crate::routes::versions::{VersionIds, VersionListFilters};
use crate::routes::ApiError;
use crate::util::auth::get_user_from_headers;
use crate::{database, models};
use actix_web::{delete, get, web, HttpRequest, HttpResponse};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use std::sync::Arc;
/// A specific version of a mod
#[derive(Serialize, Deserialize)]
@ -286,7 +284,6 @@ pub async fn delete_file(
req: HttpRequest,
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
file_host: web::Data<Arc<dyn FileHost + Send + Sync>>,
algorithm: web::Query<Algorithm>,
) -> Result<HttpResponse, ApiError> {
let user = get_user_from_headers(req.headers(), &**pool).await?;

View File

@ -92,9 +92,7 @@ pub async fn version_create(
payload.for_each(|_| ready(())).await;
if let Err(e) = undo_result {
return Err(e);
}
undo_result?;
if let Err(e) = rollback_result {
return Err(e.into());
}
@ -461,9 +459,7 @@ pub async fn upload_file_to_version(
payload.for_each(|_| ready(())).await;
if let Err(e) = undo_result {
return Err(e);
}
undo_result?;
if let Err(e) = rollback_result {
return Err(e.into());
}

View File

@ -1,6 +1,5 @@
use super::ApiError;
use crate::database::models::{version_item::QueryVersion, DatabaseError};
use crate::file_hosting::FileHost;
use crate::models::projects::{GameVersion, Loader, Version};
use crate::models::teams::Permissions;
use crate::util::auth::get_user_from_headers;
@ -10,7 +9,6 @@ use actix_web::{delete, get, post, web, HttpRequest, HttpResponse};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Deserialize)]
@ -114,7 +112,6 @@ pub async fn delete_file(
req: HttpRequest,
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
file_host: web::Data<Arc<dyn FileHost + Send + Sync>>,
algorithm: web::Query<Algorithm>,
) -> Result<HttpResponse, ApiError> {
let user = get_user_from_headers(req.headers(), &**pool).await?;

View File

@ -35,23 +35,14 @@ use log::{info, warn};
pub fn schedule_versions(
scheduler: &mut Scheduler,
pool: sqlx::Pool<sqlx::Postgres>,
skip_initial: bool,
) {
let version_index_interval = std::time::Duration::from_secs(
parse_var("VERSION_INDEX_INTERVAL").unwrap_or(1800),
);
let mut skip = skip_initial;
scheduler.run(version_index_interval, move || {
let pool_ref = pool.clone();
let local_skip = skip;
if skip {
skip = false;
}
async move {
if local_skip {
return;
}
info!("Indexing game versions list from Mojang");
let result = update_versions(&pool_ref).await;
if let Err(e) = result {

View File

@ -62,15 +62,6 @@ pub async fn index_projects(
Ok(())
}
pub async fn reset_indices(config: &SearchConfig) -> Result<(), IndexingError> {
let client = config.make_client();
client.delete_index("projects").await?;
client.delete_index("projects_filtered").await?;
Ok(())
}
async fn create_index(
client: &Client,
name: &'static str,

View File

@ -73,6 +73,7 @@ where
created: result.created,
role: Role::from_string(&result.role),
badges: result.badges,
paypal_email: result.paypal_email,
}),
None => Err(AuthenticationError::InvalidCredentials),
}

View File

@ -2,6 +2,7 @@ pub mod auth;
pub mod env;
pub mod ext;
pub mod guards;
pub mod payout_calc;
pub mod routes;
pub mod validate;
pub mod webhook;

22
src/util/payout_calc.rs Normal file
View File

@ -0,0 +1,22 @@
use chrono::{DateTime, Datelike, NaiveDate, NaiveDateTime, NaiveTime, Utc};
pub fn get_claimable_time(
current: DateTime<Utc>,
future: bool,
) -> DateTime<Utc> {
let adder = if current.month() == 1 && !future {
(-1, 12)
} else if current.month() == 12 && future {
(1, 1)
} else {
(0, current.month())
};
DateTime::from_utc(
NaiveDateTime::new(
NaiveDate::from_ymd(current.year() + adder.0, adder.1, 16),
NaiveTime::default(),
),
Utc,
)
}

View File

@ -89,7 +89,7 @@ pub fn validate_deps(
Ok(())
}
pub fn validate_url(value: &String) -> Result<(), validator::ValidationError> {
pub fn validate_url(value: &str) -> Result<(), validator::ValidationError> {
let url = url::Url::parse(value)
.ok()
.ok_or_else(|| validator::ValidationError::new("invalid URL"))?;