add: connect to socket by community
direct messages/groups connect by channel id, everything else should connect by channel with the "is_channel" header set to true
This commit is contained in:
parent
c1c8cdbfcd
commit
0304461389
20 changed files with 241 additions and 160 deletions
|
@ -27,6 +27,7 @@
|
|||
<a
|
||||
href="/chats/0/0"
|
||||
class="button quaternary channel_icon {% if selected_community == 0 %}selected{% endif %}"
|
||||
data-turbo="false"
|
||||
>
|
||||
{{ icon "message-circle" }}
|
||||
</a>
|
||||
|
@ -35,6 +36,7 @@
|
|||
<a
|
||||
href="/chats/{{ community.id }}/0"
|
||||
class="button quaternary channel_icon {% if selected_community == community.id %}selected{% endif %}"
|
||||
data-turbo="false"
|
||||
>
|
||||
{{ components::community_avatar(id=community.id,
|
||||
community=community, size="48px") }}
|
||||
|
@ -440,64 +442,76 @@
|
|||
};
|
||||
</script>
|
||||
|
||||
{% if selected_channel %}
|
||||
<script>
|
||||
setTimeout(() => {
|
||||
<script id="socket_init" data-turbo-permanent="true">
|
||||
window.SUBSCRIBE_CHANNEL = "{{ selected_community }}" === "0";
|
||||
globalThis.socket_init = () => {
|
||||
if (window.socket) {
|
||||
if (window.socket_id === "{{ selected_channel }}") {
|
||||
console.log("cannot open; already in session");
|
||||
return;
|
||||
} else {
|
||||
window.socket.close();
|
||||
window.socket = undefined;
|
||||
console.log("closed lingering");
|
||||
}
|
||||
window.socket.send("Close");
|
||||
window.socket.close();
|
||||
window.socket = undefined;
|
||||
console.log("closed lingering");
|
||||
}
|
||||
|
||||
for (const anchor of document.querySelectorAll("a")) {
|
||||
if (anchor.href.includes("{{ selected_channel }}")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
anchor.addEventListener("click", () => {
|
||||
window.socket.close();
|
||||
window.socket = undefined;
|
||||
console.log("force abandon socket");
|
||||
});
|
||||
if ("{{ selected_community }}" !== "0") {
|
||||
const endpoint = `${window.location.origin.replace("http", "ws")}/api/v1/_connect/{{ selected_community }}`;
|
||||
const socket = new WebSocket(endpoint);
|
||||
window.socket = socket;
|
||||
window.socket_id = "{{ selected_community }}";
|
||||
} else {
|
||||
const endpoint = `${window.location.origin.replace("http", "ws")}/api/v1/_connect/{{ selected_channel }}`;
|
||||
const socket = new WebSocket(endpoint);
|
||||
window.socket = socket;
|
||||
window.socket_id = "{{ selected_channel }}";
|
||||
}
|
||||
|
||||
const endpoint = `${window.location.origin.replace("http", "ws")}/api/v1/channels/{{ selected_channel }}/ws`;
|
||||
const socket = new WebSocket(endpoint);
|
||||
window.socket = socket;
|
||||
window.socket_id = "{{ selected_channel }}";
|
||||
|
||||
socket.addEventListener("close", () => {
|
||||
return socket.send("Close");
|
||||
});
|
||||
|
||||
socket.addEventListener("open", () => {
|
||||
window.socket.addEventListener("open", () => {
|
||||
// auth
|
||||
socket.send(
|
||||
window.socket.send(
|
||||
JSON.stringify({
|
||||
method: "Headers",
|
||||
data: JSON.stringify({
|
||||
// SocketHeaders
|
||||
channel: "{{ selected_channel }}",
|
||||
user: "{{ user.id }}",
|
||||
is_channel: window.SUBSCRIBE_CHANNEL,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
};
|
||||
</script>
|
||||
|
||||
socket.addEventListener("message", async (event) => {
|
||||
{% if selected_channel %}
|
||||
<script>
|
||||
setTimeout(() => {
|
||||
if (!window.SUBSCRIBE_CHANNEL) {
|
||||
// sub community
|
||||
if (window.socket_id !== "{{ selected_community }}") {
|
||||
socket_init();
|
||||
}
|
||||
} else {
|
||||
// sub channel
|
||||
if (window.socket_id !== "{{ selected_channel }}") {
|
||||
socket_init();
|
||||
}
|
||||
}
|
||||
}, 50);
|
||||
|
||||
setTimeout(() => {
|
||||
window.socket.addEventListener("message", async (event) => {
|
||||
if (event.data === "Ping") {
|
||||
return socket.send("Pong");
|
||||
}
|
||||
|
||||
const msg = JSON.parse(event.data);
|
||||
const data = JSON.parse(msg.data);
|
||||
const [channel_id, data] = JSON.parse(msg.data);
|
||||
|
||||
if (msg.method === "Message" && window.CURRENT_PAGE === 0) {
|
||||
if (channel_id !== "{{ selected_channel }}") {
|
||||
// message not for us... maybe send notification later
|
||||
// something like /api/v1/messages/{id}/mark_unread
|
||||
return;
|
||||
}
|
||||
|
||||
if (document.getElementById("stream_body")) {
|
||||
const element = document.createElement("div");
|
||||
element.style.display = "contents";
|
||||
|
|
|
@ -83,7 +83,7 @@ pub async fn proxy_request(
|
|||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
if let None = user.connections.get(&ConnectionService::LastFm) {
|
||||
if user.connections.get(&ConnectionService::LastFm).is_none() {
|
||||
// connection doesn't exist
|
||||
return Json(Error::GeneralNotFound("connection".to_string()).into());
|
||||
};
|
||||
|
|
|
@ -66,11 +66,11 @@ pub async fn me_request(jar: CookieJar, Extension(data): Extension<State>) -> im
|
|||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
return Json(ApiReturn {
|
||||
Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "User exists".to_string(),
|
||||
payload: Some(user),
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
/// Update the settings of the given user.
|
||||
|
|
|
@ -63,7 +63,7 @@ pub async fn create_group_request(
|
|||
|
||||
// check for existing
|
||||
if members.len() == 1 {
|
||||
let other_user = members.get(0).unwrap().to_owned();
|
||||
let other_user = members.first().unwrap().to_owned();
|
||||
if let Ok(channel) = data.get_channel_by_owner_member(user.id, other_user).await {
|
||||
return Json(ApiReturn {
|
||||
ok: true,
|
||||
|
@ -80,21 +80,18 @@ pub async fn create_group_request(
|
|||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
if other_user.settings.private_chats {
|
||||
if data
|
||||
if other_user.settings.private_chats && data
|
||||
.get_userfollow_by_initiator_receiver(other_user.id, user.id)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
return Json(Error::NotAllowed.into());
|
||||
}
|
||||
.is_err() {
|
||||
return Json(Error::NotAllowed.into());
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
let mut props = Channel::new(0, user.id, 0, req.title);
|
||||
props.members = members;
|
||||
let id = props.id.clone();
|
||||
let id = props.id;
|
||||
|
||||
match data.create_channel(props).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use std::{collections::HashMap, time::Duration};
|
||||
use redis::Commands;
|
||||
use axum::{
|
||||
extract::{
|
||||
ws::{Message as WsMessage, WebSocket, WebSocketUpgrade},
|
||||
|
@ -12,7 +14,7 @@ use tetratto_core::{
|
|||
model::{
|
||||
auth::User,
|
||||
channels::Message,
|
||||
socket::{SocketMessage, SocketMethod},
|
||||
socket::{PacketType, SocketMessage, SocketMethod},
|
||||
ApiReturn, Error,
|
||||
},
|
||||
DataManager,
|
||||
|
@ -21,10 +23,10 @@ use crate::{get_user_from_token, routes::api::v1::CreateMessage, State};
|
|||
use serde::Deserialize;
|
||||
use futures_util::{sink::SinkExt, stream::StreamExt};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Clone, Deserialize)]
|
||||
pub struct SocketHeaders {
|
||||
pub channel: String,
|
||||
pub user: String,
|
||||
pub is_channel: bool,
|
||||
}
|
||||
|
||||
/// Handle a subscription to the websocket.
|
||||
|
@ -43,11 +45,11 @@ pub async fn subscription_handler(
|
|||
})
|
||||
}
|
||||
|
||||
pub async fn handle_socket(socket: WebSocket, db: DataManager, channel_id: String) {
|
||||
pub async fn handle_socket(socket: WebSocket, db: DataManager, community_id: String) {
|
||||
let (mut sink, mut stream) = socket.split();
|
||||
|
||||
let mut user: Option<User> = None;
|
||||
let mut con = db.2.clone().get_con().await;
|
||||
let mut headers: Option<SocketHeaders> = None;
|
||||
|
||||
// handle incoming messages on socket
|
||||
let dbc = db.clone();
|
||||
|
@ -60,7 +62,7 @@ pub async fn handle_socket(socket: WebSocket, db: DataManager, channel_id: Strin
|
|||
}
|
||||
};
|
||||
|
||||
if data.method != SocketMethod::Headers && user.is_none() {
|
||||
if data.method != SocketMethod::Headers && user.is_none() && headers.is_none() {
|
||||
// we've sent something else before authenticating... that's not right
|
||||
let _ = sink.close().await;
|
||||
return;
|
||||
|
@ -70,6 +72,7 @@ pub async fn handle_socket(socket: WebSocket, db: DataManager, channel_id: Strin
|
|||
SocketMethod::Headers => {
|
||||
let data: SocketHeaders = data.data();
|
||||
|
||||
headers = Some(data.clone());
|
||||
user = Some(
|
||||
match dbc
|
||||
.get_user_by_id(match data.user.parse::<usize>() {
|
||||
|
@ -89,39 +92,42 @@ pub async fn handle_socket(socket: WebSocket, db: DataManager, channel_id: Strin
|
|||
},
|
||||
);
|
||||
|
||||
let channel = match dbc
|
||||
.get_channel_by_id(match data.channel.parse::<usize>() {
|
||||
if data.is_channel {
|
||||
// verify permissions for single channel
|
||||
let channel = match dbc
|
||||
.get_channel_by_id(match community_id.parse::<usize>() {
|
||||
Ok(c) => c,
|
||||
Err(_) => {
|
||||
let _ = sink.close().await;
|
||||
return;
|
||||
}
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(c) => c,
|
||||
Err(_) => {
|
||||
let _ = sink.close().await;
|
||||
return;
|
||||
}
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(c) => c,
|
||||
Err(_) => {
|
||||
};
|
||||
|
||||
let user = user.as_ref().unwrap();
|
||||
|
||||
let membership = match dbc
|
||||
.get_membership_by_owner_community(user.id, channel.id)
|
||||
.await
|
||||
{
|
||||
Ok(ua) => ua,
|
||||
Err(_) => {
|
||||
let _ = sink.close().await;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if !channel.check_read(user.id, Some(membership.role)) {
|
||||
let _ = sink.close().await;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let user = user.as_ref().unwrap();
|
||||
|
||||
let membership = match dbc
|
||||
.get_membership_by_owner_community(user.id, channel.id)
|
||||
.await
|
||||
{
|
||||
Ok(ua) => ua,
|
||||
Err(_) => {
|
||||
let _ = sink.close().await;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if !channel.check_read(user.id, Some(membership.role)) {
|
||||
let _ = sink.close().await;
|
||||
return;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
|
@ -134,6 +140,37 @@ pub async fn handle_socket(socket: WebSocket, db: DataManager, channel_id: Strin
|
|||
return;
|
||||
}
|
||||
|
||||
// get channel permissions
|
||||
let user = user.unwrap();
|
||||
let headers = headers.unwrap();
|
||||
|
||||
let mut channel_read_statuses: HashMap<usize, bool> = HashMap::new();
|
||||
if !headers.is_channel {
|
||||
// check permissions for every channel in community
|
||||
let community_id = match community_id.parse::<usize>() {
|
||||
Ok(c) => c,
|
||||
Err(_) => return,
|
||||
};
|
||||
|
||||
let membership = match dbc
|
||||
.get_membership_by_owner_community(user.id, community_id)
|
||||
.await
|
||||
{
|
||||
Ok(ua) => ua,
|
||||
Err(_) => {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
for channel in dbc.get_channels_by_community(community_id).await.unwrap() {
|
||||
channel_read_statuses.insert(
|
||||
channel.id,
|
||||
channel.check_read(user.id, Some(membership.role)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
let mut recv_task = tokio::spawn(async move {
|
||||
while let Some(Ok(WsMessage::Text(text))) = stream.next().await {
|
||||
if text != "Close" {
|
||||
|
@ -147,28 +184,86 @@ pub async fn handle_socket(socket: WebSocket, db: DataManager, channel_id: Strin
|
|||
}
|
||||
});
|
||||
|
||||
let dbc = db.clone();
|
||||
let mut redis_task = tokio::spawn(async move {
|
||||
// forward messages from redis to the mpsc
|
||||
let mut con = dbc.2.get_con().await;
|
||||
let mut pubsub = con.as_pubsub();
|
||||
pubsub.subscribe(channel_id).unwrap();
|
||||
|
||||
pubsub.subscribe(user.id).unwrap();
|
||||
pubsub.subscribe(community_id.clone()).unwrap();
|
||||
|
||||
// listen for pubsub messages
|
||||
while let Ok(msg) = pubsub.get_message() {
|
||||
// payload is a stringified SocketMessage
|
||||
if sink
|
||||
.send(WsMessage::Text(msg.get_payload::<String>().unwrap().into()))
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
return;
|
||||
let smsg = msg.get_payload::<String>().unwrap();
|
||||
let packet: SocketMessage = serde_json::from_str(&smsg).unwrap();
|
||||
|
||||
if packet.method == SocketMethod::Forward(PacketType::Ping) {
|
||||
// forward with custom message
|
||||
if sink.send(WsMessage::Text("Ping".into())).await.is_err() {
|
||||
drop(sink);
|
||||
break;
|
||||
}
|
||||
} else if packet.method == SocketMethod::Message {
|
||||
// check perms and then forward
|
||||
let d: (String, Message) = packet.data();
|
||||
|
||||
if let Some(cs) = channel_read_statuses.get(&d.1.channel) {
|
||||
if !cs {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if !headers.is_channel {
|
||||
// since we didn't select by just a channel, there HAS to be
|
||||
// an entry for the channel for us to check this message
|
||||
continue;
|
||||
// we don't need to check messages when we're subscribed to
|
||||
// a channel, since that is checked on headers submission when
|
||||
// we subscribe to a channel
|
||||
}
|
||||
}
|
||||
|
||||
if sink.send(WsMessage::Text(smsg.into())).await.is_err() {
|
||||
drop(sink);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// forward to client
|
||||
if sink.send(WsMessage::Text(smsg.into())).await.is_err() {
|
||||
drop(sink);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let db2c = db.2.clone();
|
||||
let heartbeat_task = tokio::spawn(async move {
|
||||
let mut con = db2c.get_con().await;
|
||||
let mut heartbeat = tokio::time::interval(Duration::from_secs(10));
|
||||
|
||||
loop {
|
||||
con.publish::<usize, String, ()>(
|
||||
user.id,
|
||||
serde_json::to_string(&SocketMessage {
|
||||
method: SocketMethod::Forward(PacketType::Ping),
|
||||
data: "Ping".to_string(),
|
||||
})
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
heartbeat.tick().await;
|
||||
}
|
||||
});
|
||||
|
||||
tokio::select! {
|
||||
_ = (&mut recv_task) => redis_task.abort(),
|
||||
_ = (&mut redis_task) => recv_task.abort()
|
||||
}
|
||||
|
||||
heartbeat_task.abort(); // kill
|
||||
tracing::info!("socket terminate");
|
||||
}
|
||||
|
||||
|
|
|
@ -290,7 +290,7 @@ pub fn routes() -> Router {
|
|||
)
|
||||
// messages
|
||||
.route(
|
||||
"/channels/{id}/ws",
|
||||
"/_connect/{id}",
|
||||
any(channels::messages::subscription_handler),
|
||||
)
|
||||
.route("/messages", post(channels::messages::create_request))
|
||||
|
|
|
@ -211,7 +211,7 @@ pub async fn message_request(
|
|||
}
|
||||
};
|
||||
|
||||
let message: Message = match serde_json::from_str(&req.data) {
|
||||
let message: (String, Message) = match serde_json::from_str(&req.data) {
|
||||
Ok(m) => m,
|
||||
Err(e) => {
|
||||
return Err(Html(
|
||||
|
@ -220,6 +220,8 @@ pub async fn message_request(
|
|||
}
|
||||
};
|
||||
|
||||
let message = message.1;
|
||||
|
||||
let membership = match data
|
||||
.0
|
||||
.get_membership_by_owner_community(user.id, community)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue