| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | #![allow(clippy::similar_names)] |
| 10 | + | |
| 11 | + | use model::User; |
| 12 | + | use sqlx::{AssertSqlSafe, Row}; |
| 13 | + | |
| 14 | + | use crate::users::USER_COLUMNS; |
| 15 | + | use crate::{Store, StoreError, new_id, now_ms}; |
| 16 | + | |
| 17 | + | impl Store { |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | pub async fn follow(&self, follower_id: &str, followee_id: &str) -> Result<(), StoreError> { |
| 24 | + | if follower_id == followee_id { |
| 25 | + | return Ok(()); |
| 26 | + | } |
| 27 | + | sqlx::query( |
| 28 | + | "INSERT INTO follows (id, follower_id, followee_id, created_at) VALUES ($1, $2, $3, $4) \ |
| 29 | + | ON CONFLICT (follower_id, followee_id) DO NOTHING", |
| 30 | + | ) |
| 31 | + | .bind(new_id()) |
| 32 | + | .bind(follower_id) |
| 33 | + | .bind(followee_id) |
| 34 | + | .bind(now_ms()) |
| 35 | + | .execute(&self.pool) |
| 36 | + | .await?; |
| 37 | + | Ok(()) |
| 38 | + | } |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | pub async fn unfollow(&self, follower_id: &str, followee_id: &str) -> Result<(), StoreError> { |
| 46 | + | sqlx::query("DELETE FROM follows WHERE follower_id = $1 AND followee_id = $2") |
| 47 | + | .bind(follower_id) |
| 48 | + | .bind(followee_id) |
| 49 | + | .execute(&self.pool) |
| 50 | + | .await?; |
| 51 | + | Ok(()) |
| 52 | + | } |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | pub async fn is_following( |
| 60 | + | &self, |
| 61 | + | follower_id: &str, |
| 62 | + | followee_id: &str, |
| 63 | + | ) -> Result<bool, StoreError> { |
| 64 | + | let row = |
| 65 | + | sqlx::query("SELECT 1 AS n FROM follows WHERE follower_id = $1 AND followee_id = $2") |
| 66 | + | .bind(follower_id) |
| 67 | + | .bind(followee_id) |
| 68 | + | .fetch_optional(&self.pool) |
| 69 | + | .await?; |
| 70 | + | Ok(row.is_some()) |
| 71 | + | } |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | pub async fn count_followers(&self, user_id: &str) -> Result<i64, StoreError> { |
| 79 | + | let n: i64 = sqlx::query("SELECT COUNT(*) AS n FROM follows WHERE followee_id = $1") |
| 80 | + | .bind(user_id) |
| 81 | + | .fetch_one(&self.pool) |
| 82 | + | .await? |
| 83 | + | .try_get("n")?; |
| 84 | + | Ok(n) |
| 85 | + | } |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | pub async fn count_following(&self, user_id: &str) -> Result<i64, StoreError> { |
| 93 | + | let n: i64 = sqlx::query("SELECT COUNT(*) AS n FROM follows WHERE follower_id = $1") |
| 94 | + | .bind(user_id) |
| 95 | + | .fetch_one(&self.pool) |
| 96 | + | .await? |
| 97 | + | .try_get("n")?; |
| 98 | + | Ok(n) |
| 99 | + | } |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | pub async fn followers(&self, user_id: &str) -> Result<Vec<User>, StoreError> { |
| 107 | + | let cols = format!("u.{}", USER_COLUMNS.replace(", ", ", u.")); |
| 108 | + | let sql = format!( |
| 109 | + | "SELECT {cols} FROM users u JOIN follows f ON f.follower_id = u.id \ |
| 110 | + | WHERE f.followee_id = $1 ORDER BY f.created_at DESC" |
| 111 | + | ); |
| 112 | + | let rows = sqlx::query(AssertSqlSafe(sql)) |
| 113 | + | .bind(user_id) |
| 114 | + | .fetch_all(&self.pool) |
| 115 | + | .await?; |
| 116 | + | rows.iter() |
| 117 | + | .map(|r| self.map_user(r)) |
| 118 | + | .collect::<Result<_, _>>() |
| 119 | + | .map_err(Into::into) |
| 120 | + | } |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | pub async fn following(&self, user_id: &str) -> Result<Vec<User>, StoreError> { |
| 128 | + | let cols = format!("u.{}", USER_COLUMNS.replace(", ", ", u.")); |
| 129 | + | let sql = format!( |
| 130 | + | "SELECT {cols} FROM users u JOIN follows f ON f.followee_id = u.id \ |
| 131 | + | WHERE f.follower_id = $1 ORDER BY f.created_at DESC" |
| 132 | + | ); |
| 133 | + | let rows = sqlx::query(AssertSqlSafe(sql)) |
| 134 | + | .bind(user_id) |
| 135 | + | .fetch_all(&self.pool) |
| 136 | + | .await?; |
| 137 | + | rows.iter() |
| 138 | + | .map(|r| self.map_user(r)) |
| 139 | + | .collect::<Result<_, _>>() |
| 140 | + | .map_err(Into::into) |
| 141 | + | } |
| 142 | + | } |