Skip to content

Commit 968295d

Browse files
authored
merge: pull request #2 (website-expansion)
Website structure refactor
2 parents 83b2ad8 + 4dc2cc6 commit 968295d

28 files changed

+1248
-1030
lines changed

.idea/jsLibraryMappings.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The main technologies implemented in this project are:
1414
* **Protobuf:** powered by `prost` and `protobuf.js`
1515
* **Serialization:** powered by `serde`
1616
* **Authentication:** powered by `jwt`
17-
* **Hashing:** powered by the `argon2`
17+
* **Hashing:** powered by `argon2`
1818
* **Async:** powered by `tokio`
1919
* **Frontend:** powered by `Svelte`
2020

server/src/entity/reactions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use diesel::{allow_tables_to_appear_in_same_query, Identifiable, Insertable, Queryable, Selectable};
22
use crate::entity::message::messages;
33
use crate::entity::user::users;
4-
use diesel::sql_types::{BigInt, Text, Varchar, Integer, Bool};
4+
use diesel::sql_types::{BigInt, Text, Varchar, Integer, Bool, Serial};
55
use diesel::prelude::*;
66
use serde::{Deserialize, Serialize};
77

@@ -18,7 +18,7 @@ pub struct Reaction {
1818

1919
diesel::table! {
2020
reactions (reaction_id) {
21-
reaction_id -> Integer,
21+
reaction_id -> Serial,
2222
message_id -> BigInt,
2323
emoji -> Varchar,
2424
reaction_count -> Integer
@@ -39,7 +39,7 @@ pub struct ReactionUser {
3939

4040
diesel::table! {
4141
reaction_users (id) {
42-
id -> Integer,
42+
id -> Serial,
4343
reaction_id -> Integer,
4444
user_id -> BigInt
4545
}
@@ -77,7 +77,7 @@ pub struct ReactionSummary {
7777
pub count: i32,
7878
#[sql_type = "Bool"]
7979
pub me: bool,
80-
#[sql_type = "Integer"]
80+
#[sql_type = "Serial"]
8181
pub reaction_id: i32
8282
}
8383

server/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async fn main() {
6060
tracing_subscriber::registry()
6161
.with(
6262
tracing_subscriber::EnvFilter::try_from_default_env()
63-
.unwrap_or_else(|_| "example_websockets=debug,tower_http=info,diesel=debug".into()),
63+
.unwrap_or_else(|_| "example_websockets=debug,tower_http=debug ,diesel=debug".into()),
6464
)
6565
.with(tracing_subscriber::fmt::layer())
6666
.init();
@@ -69,6 +69,7 @@ async fn main() {
6969
.route("/ws", get(server::subscribe_chat_handshake))
7070
.route("/api/users/@me", get(server::rest::user::get_self))
7171
.route("/api/contacts/@me", get(server::rest::contacts::get_contacts))
72+
.route("/api/contacts/:contact_id", get(server::rest::contacts::get_contact))
7273
.route("/api/channels/:context_id/messages", post(server::rest::messages::create_message))
7374
.route("/api/channels/:context_id/messages", get(server::rest::messages::get_messages))
7475
.route("/api/channels/:context_id/messages/:message_id", put(server::rest::messages::edit_message))

server/src/server/gateway/context.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,17 @@ pub async fn send_packet_to_context(packet_queue: &mut DashMap<i64, Sender<Box<d
1414
futures_util::future::ready(())
1515
}).await;
1616
}
17+
}
18+
19+
pub async fn send_packet_to_user(packet_queue: &mut DashMap<i64, Sender<Box<dyn Packet + Send>>>, user: i64, packet: Box<dyn Packet + Send>) {
20+
println!("Sending packet to user: {}", user);
21+
if let Some(tx) = packet_queue.get(&user) {
22+
println!("Context was found, now sending packet");
23+
tx.send(packet).then(|result| {
24+
if let Err(e) = result {
25+
eprintln!("Failed to send message: {:?}", e);
26+
}
27+
futures_util::future::ready(())
28+
}).await;
29+
}
1730
}

server/src/server/rest/contacts.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use axum::body::Body;
22
use axum::Extension;
3+
use axum::extract::Path;
34
use axum::http::Request;
45
use diesel::prelude::*;
56
use diesel::row::NamedRow;
@@ -62,5 +63,59 @@ pub async fn get_contacts(
6263
}
6364
}).collect();
6465

66+
ok(result)
67+
}
68+
69+
pub async fn get_contact(
70+
Path(contact_id): Path<i64>,
71+
Extension(state): Extension<SharedState>,
72+
request: Request<Body>
73+
) -> IrisResponse<ContactResponse> {
74+
let user = request.extensions().get::<User>().cloned().expect("User not found");
75+
let conn = &mut state.write().await.database;
76+
77+
let query = sql_query("
78+
WITH recent_messages AS (
79+
SELECT u.*, m.id AS message_id, m.content, m.reception_status,
80+
(SELECT COUNT(*)
81+
FROM messages
82+
WHERE (user_id = $2 AND context = $1)
83+
AND reception_status = 0) AS reception_status_count
84+
FROM users u
85+
LEFT JOIN LATERAL (
86+
SELECT id, content, reception_status
87+
FROM messages
88+
WHERE (user_id = $2 AND context = $1)
89+
OR (context = $2 AND user_id = $1)
90+
ORDER BY id DESC
91+
LIMIT 1
92+
) m ON true
93+
where u.id = $1
94+
)
95+
SELECT *
96+
FROM recent_messages
97+
ORDER BY COALESCE(message_id, -1) DESC;
98+
").bind::<BigInt, _>(contact_id).bind::<BigInt, _>(user.id);
99+
let results = query
100+
.load::<ContactWithLastMessage>(conn)
101+
.expect("Failed to load contact");
102+
103+
let contact = results.into_iter().next().expect("Contact not found");
104+
105+
let result = ContactResponse {
106+
id: contact.id,
107+
name: contact.name,
108+
username: contact.username.clone(),
109+
last_message: match contact.message_id {
110+
Some(_) => Some(PrimordialMessage {
111+
id: contact.message_id.unwrap(),
112+
content: contact.content.unwrap(),
113+
receipt: contact.reception_status.unwrap(),
114+
}),
115+
None => None
116+
},
117+
unread_count: contact.reception_status_count
118+
};
119+
65120
ok(result)
66121
}

server/src/server/rest/middlewares.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ pub struct IrisAuth;
2222
pub async fn authorize(mut req: Request, next: Next) -> Response {
2323
let headers = req.headers().clone();
2424
let auth = headers.get("Authorization").or(headers.get("Sec-Websocket-Protocol"));
25-
println!("Now parsing: {}", req.uri().path());
2625
if auth.is_none() {
2726
return error::<String>(StatusCode::UNAUTHORIZED, "No authorization header provided").into_response();
2827
}

web/package-lock.json

Lines changed: 0 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/src/lib/components/Alert.svelte

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,17 @@
6565

6666
{#if alert}
6767
{@const alertId = Math.random()}
68-
<div id="alert" class="alert" style="background-color: {typeToColor(alert.type)}" in:receive={{key: alertId}} out:send={{key: alertId}}>
68+
{@const color = typeToColor(alert.type)}
69+
{@const contrast = typeToColorContrast(alert.type)}
70+
<div
71+
id="alert"
72+
class="alert"
73+
style="background-color: {color}; box-shadow: 0 0 20px {color}"
74+
in:receive={{key: alertId}}
75+
out:send={{key: alertId}}
76+
>
6977
<span class="message">{alert.message}</span>
70-
<div class="fill" style="background-color: {typeToColorContrast(alert.type)}"></div>
78+
<div class="fill" style="background-color: {contrast}"></div>
7179
</div>
7280
{/if}
7381

@@ -87,6 +95,9 @@
8795
font-size: 14px;
8896
font-family: 'DM Sans', sans-serif;
8997
overflow: hidden;
98+
user-select: none;
99+
-webkit-user-select: none;
100+
-moz-user-select: none;
90101
}
91102
92103
.message {

0 commit comments

Comments
 (0)