Compare commits
No commits in common. "b57720a2f875ea8766fbcbd555a5c31ca91c0247" and "30cb80559364784834b3cf770b42c167c201f892" have entirely different histories.
b57720a2f8
...
30cb805593
66
src/main.rs
66
src/main.rs
@ -15,7 +15,6 @@ use event::{GitlabEvent, GitlabEventExt};
|
||||
use http::StatusCode;
|
||||
use matrix_sdk::{
|
||||
config::SyncSettings,
|
||||
room::Joined,
|
||||
ruma::{events::room::message::RoomMessageEventContent, OwnedRoomOrAliasId},
|
||||
BaseRoom, Client,
|
||||
};
|
||||
@ -55,7 +54,29 @@ async fn matrix_connect(config: &config::Config) -> anyhow::Result<Client> {
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
async fn ensure_matrix_room_joined(matrix_client: &Client, room_id: &OwnedRoomOrAliasId) -> anyhow::Result<Joined> {
|
||||
fn build_gitlab_message(event: &GitlabEvent) -> String {
|
||||
let project = event.project();
|
||||
let refname = event
|
||||
.r#ref()
|
||||
.split('/')
|
||||
.last()
|
||||
.unwrap_or_else(|| event.r#ref())
|
||||
.to_string();
|
||||
format!(
|
||||
"*{}* {} **{}** [{}]({})",
|
||||
project.path_with_namespace,
|
||||
refname,
|
||||
event.user(),
|
||||
event.title(),
|
||||
event.url()
|
||||
)
|
||||
}
|
||||
|
||||
async fn handle_gitlab_event(
|
||||
event: GitlabEvent,
|
||||
room_id: &OwnedRoomOrAliasId,
|
||||
matrix_client: &Client,
|
||||
) -> anyhow::Result<()> {
|
||||
fn room_matches(a_room: &BaseRoom, our_room: &OwnedRoomOrAliasId) -> bool {
|
||||
let our_room_str = our_room.as_str();
|
||||
a_room.room_id().as_str() == our_room_str
|
||||
@ -95,38 +116,15 @@ async fn ensure_matrix_room_joined(matrix_client: &Client, room_id: &OwnedRoomOr
|
||||
.map(|room| room.clone());
|
||||
}
|
||||
|
||||
room.ok_or_else(|| anyhow!("Unable to join room {}", room_id))
|
||||
}
|
||||
|
||||
fn build_gitlab_message(event: &GitlabEvent) -> String {
|
||||
let project = event.project();
|
||||
let refname = event
|
||||
.r#ref()
|
||||
.split('/')
|
||||
.last()
|
||||
.unwrap_or_else(|| event.r#ref())
|
||||
.to_string();
|
||||
format!(
|
||||
"*{}* {} **{}** [{}]({})",
|
||||
project.path_with_namespace,
|
||||
refname,
|
||||
event.user(),
|
||||
event.title(),
|
||||
event.url()
|
||||
)
|
||||
}
|
||||
|
||||
async fn handle_gitlab_event(
|
||||
event: GitlabEvent,
|
||||
room_id: &OwnedRoomOrAliasId,
|
||||
matrix_client: &Client,
|
||||
) -> anyhow::Result<()> {
|
||||
let room = ensure_matrix_room_joined(matrix_client, room_id).await?;
|
||||
let msg = build_gitlab_message(&event);
|
||||
debug!("Sending message to {}: {}", room_id, msg);
|
||||
let msg_content = RoomMessageEventContent::text_markdown(&msg);
|
||||
room.send(msg_content, None).await?;
|
||||
Ok(())
|
||||
if let Some(room) = room {
|
||||
let msg = build_gitlab_message(&event);
|
||||
debug!("Sending message to {}: {}", room_id, msg);
|
||||
let msg_content = RoomMessageEventContent::text_markdown(&msg);
|
||||
room.send(msg_content, None).await?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("Unable to join room {}", room_id))
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
Loading…
Reference in New Issue
Block a user