Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for telegram chat links opening #397

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion data/com.github.melix99.telegrand.desktop.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
Name=Telegrand
Comment=A Telegram client optimized for the GNOME desktop
Type=Application
Exec=telegrand
TryExec=telegrand
Exec=telegrand %U
melix99 marked this conversation as resolved.
Show resolved Hide resolved
Terminal=false
Categories=GNOME;GTK;
Keywords=Gnome;GTK;
DBusActivatable=true
MimeType=x-scheme-handler/tg;
# Translators: Do NOT translate or transliterate this text (this is an icon file name)!
Icon=@icon@
StartupNotify=true
Expand Down
3 changes: 3 additions & 0 deletions data/com.github.melix99.telegrand.service.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[D-BUS Service]
Name=@application_id@
Exec=@bindir@/telegrand --gapplication-service
12 changes: 12 additions & 0 deletions data/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,15 @@ if glib_compile_schemas.found()
],
)
endif

# D-bus service
krlade marked this conversation as resolved.
Show resolved Hide resolved
service_conf = configuration_data()
service_conf.set('application_id', application_id)
service_conf.set('bindir', bindir)
configure_file(
input: '@[email protected]'.format(base_id),
output: '@[email protected]'.format(application_id),
configuration: service_conf,
install: true,
install_dir: datadir / 'dbus-1' / 'services',
)
11 changes: 11 additions & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ mod imp {
obj.main_window().present();
}

fn open(&self, files: &[gio::File], _hint: &str) {
debug!("Application::open");

let app = self.obj();
let url = files.first().map(|f| f.uri().into()).unwrap();
app.main_window()
.session_manager()
.handle_telegram_link(url);
}

fn startup(&self) {
debug!("GtkApplication<Application>::startup");

Expand Down Expand Up @@ -88,6 +98,7 @@ impl Application {
glib::Object::builder()
.property("application-id", APP_ID)
.property("resource-base-path", "/com/github/melix99/telegrand/")
.property("flags", gio::ApplicationFlags::HANDLES_OPEN)
.build()
}

Expand Down
31 changes: 31 additions & 0 deletions src/session_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,37 @@ impl SessionManager {
}
}

pub(crate) fn handle_telegram_link(&self, link: String) {
let client_id = self.active_logged_in_client_id();
if let Some(client_id) = client_id {
spawn(clone!(@weak self as obj => async move {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having all these spawn calls, I think it's better to have this whole function (and open_chat_by_username) async and then having the callers of this function to spawn a future to execute it.

let result = functions::get_internal_link_type(link, client_id).await;
if let Ok(link_type) = result {
use enums::InternalLinkType::*;
// TODO: Support other link types
#[allow(clippy::single_match)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to do that, just follow what clippy says. I know that we'll need a match arm in the future, but I prefer not ignore clippy :)

match link_type {
PublicChat(data) => obj.open_chat_by_username(data.chat_username),
_ => (),
}
}
}));
}
}

pub(crate) fn open_chat_by_username(&self, username: String) {
let client_id = self.active_logged_in_client_id();
if let Some(client_id) = client_id {
spawn(clone!(@weak self as obj => async move {
let result = functions::search_public_chat(username, client_id).await;
if let Ok(chat) = result {
let enums::Chat::Chat(chat) = chat;
obj.select_chat(client_id, chat.id);
}
}))
}
}

pub(crate) fn begin_chats_search(&self) {
if let Some(client_id) = self.active_logged_in_client_id() {
let clients = self.imp().clients.borrow();
Expand Down