Skip to content
Merged
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
27 changes: 26 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ url = "2.5.8"
dirs = "6.0.0"
base64 = "0.22.1"
libc = "0.2.189"
discord-rich-presence = "1.1.0"

[build-dependencies]
anyhow = "1.0.103"
Expand Down
2 changes: 2 additions & 0 deletions flatpak/com.stremio.Stremio.Devel.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"--persist=.stremio-server",
"--filesystem=xdg-download",
"--filesystem=xdg-run/pipewire-0:ro",
"--filesystem=xdg-run/discord-ipc-0",
"--filesystem=xdg-run/app/com.discordapp.Discord:create",
"--talk-name=org.kde.StatusNotifierWatcher",
"--own-name=com.stremio.Stremio.Devel",
"--own-name=org.mpris.MediaPlayer2.Stremio",
Expand Down
1 change: 1 addition & 0 deletions src/app/discord/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const CLIENT_ID: &str = "1452620752263319665";
62 changes: 62 additions & 0 deletions src/app/discord/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
mod config;

use std::cell::RefCell;

use discord_rich_presence::{
DiscordIpc, DiscordIpcClient,
activity::{Activity, ActivityType, Assets},
};
use tracing::error;

use config::CLIENT_ID;

pub struct Discord {
client: RefCell<DiscordIpcClient>,
}

impl Discord {
pub fn new() -> Self {
let client = RefCell::new(DiscordIpcClient::new(CLIENT_ID));

Self { client }
}

pub fn connect(&self) -> bool {
if let Err(e) = self.client.borrow_mut().connect() {
error!("Failed to connect: {e}");
return false;
}

true
}

pub fn disconnect(&self) {
if let Err(e) = self.client.borrow_mut().close() {
error!("Failed to disconnect: {e}");
}
}

pub fn set_activity(&self, details: String, state: String, image: Option<String>) {
let mut assets = Assets::new().large_text("Stremio");

if let Some(image) = image {
assets = assets.large_image(image);
}

let activity = Activity::default()
.activity_type(ActivityType::Watching)
.details(details)
.state(state)
.assets(assets);

if let Err(e) = self.client.borrow_mut().set_activity(activity) {
error!("Failed to set activity: {e}");
}
}

pub fn clear_activity(&self) {
if let Err(e) = self.client.borrow_mut().clear_activity() {
error!("Failed to clear activity: {e}");
}
}
}
19 changes: 18 additions & 1 deletion src/app/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use tracing::error;
use crate::{
app::{
config::{APP_ID, APP_NAME, URI_SCHEME},
discord::Discord,
ipc::{
self,
event::{IpcEvent, IpcEventMpv},
event::{IpcEvent, IpcEventDiscord, IpcEventMpv},
},
mpris::Mpris,
tray::Tray,
Expand Down Expand Up @@ -71,6 +72,7 @@ impl ApplicationImpl for Application {
let tray = Tray::default();
let video = Video::default();
let mpris = Mpris::default();
let discord = Discord::new();

let startup_url = self.startup_url.borrow();
let dev_mode = self.dev_mode.get();
Expand Down Expand Up @@ -159,6 +161,21 @@ impl ApplicationImpl for Application {
IpcEvent::Quit => {
app.quit();
}
IpcEvent::Discord(event) => match event {
IpcEventDiscord::Connect => {
let connected = discord.connect();
let message = ipc::create_response(IpcEvent::Discord(
IpcEventDiscord::Status(connected),
));
webview.send(&message);
}
IpcEventDiscord::Disconnect => discord.disconnect(),
IpcEventDiscord::SetActivity((details, state, image)) => {
discord.set_activity(details, state, image)
}
IpcEventDiscord::ClearActivity => discord.clear_activity(),
_ => {}
},
IpcEvent::Mpv(event) => match event {
IpcEventMpv::Observe(name) => video.observe_mpv_property(name),
IpcEventMpv::Command((name, args)) => {
Expand Down
10 changes: 10 additions & 0 deletions src/app/ipc/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ pub enum IpcEventMpv {
Ended((String, Option<String>)),
}

#[derive(Deserialize, Debug)]
pub enum IpcEventDiscord {
Status(bool),
Connect,
Disconnect,
SetActivity((String, String, Option<String>)),
ClearActivity,
}

#[derive(Deserialize, Debug)]
pub enum IpcEvent {
Init,
Expand All @@ -23,6 +32,7 @@ pub enum IpcEvent {
Mpv(IpcEventMpv),
MediaMetadata((String, Option<String>, Option<String>)),
MediaStatus(bool),
Discord(IpcEventDiscord),
}

impl TryFrom<&str> for IpcEvent {
Expand Down
28 changes: 28 additions & 0 deletions src/app/ipc/request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::Deserialize;
use serde_json::Value;

use crate::app::ipc::event::IpcEventDiscord;

use super::event::{IpcEvent, IpcEventMpv};

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -28,6 +30,14 @@ pub struct IpcMessageRequestMediaStatus {
paused: bool,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct IpcMessageRequestDiscordSetActivity {
details: String,
state: String,
image: Option<String>,
}

impl TryFrom<IpcMessageRequest> for IpcEvent {
type Error = String;

Expand Down Expand Up @@ -100,6 +110,24 @@ impl TryFrom<IpcMessageRequest> for IpcEvent {

Ok(IpcEvent::MediaStatus(data.paused))
}
"discord-connect" => Ok(IpcEvent::Discord(IpcEventDiscord::Connect)),
"discord-disconnect" => {
Ok(IpcEvent::Discord(IpcEventDiscord::Disconnect))
}
"discord-set-activity" => {
let data: IpcMessageRequestDiscordSetActivity =
serde_json::from_value(data)
.map_err(|_| "Invalid discord-set-activity object")?;

Ok(IpcEvent::Discord(IpcEventDiscord::SetActivity((
data.details,
data.state,
data.image,
))))
}
"discord-clear-activity" => {
Ok(IpcEvent::Discord(IpcEventDiscord::ClearActivity))
}
method => Err(format!("Invalid method: {method}")),
},
None => match name {
Expand Down
11 changes: 11 additions & 0 deletions src/app/ipc/response.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::Serialize;
use serde_json::json;

use crate::app::ipc::event::IpcEventDiscord;

use super::{
TRANSPORT_NAME, VERSION,
event::{IpcEvent, IpcEventMpv},
Expand Down Expand Up @@ -102,6 +104,15 @@ impl TryFrom<IpcEvent> for IpcMessageResponse {
"paused": paused
}])),
}),
IpcEvent::Discord(IpcEventDiscord::Status(connected)) => Ok(IpcMessageResponse {
id: 1,
r#type: 1,
object: TRANSPORT_NAME.to_owned(),
data: None,
args: Some(json!(["discord-status", {
"connected": connected
}])),
}),
_ => Err("Failed to convert IpcEvent to IpcMessageResponse"),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod config;
mod dialogs;
mod discord;
mod imp;
mod ipc;
mod mpris;
Expand Down