Support Flatpak and Snap Discord; keep the timer when a game changes process
Look for discord-ipc sockets in the Flatpak and Snap runtime folders too. When a game moves to another process (e.g. at Steam launch), keep its first start time and don't resend the activity. Bump version to 0.2.
This commit is contained in:
@@ -2,6 +2,7 @@ import json
|
||||
import os
|
||||
import socket
|
||||
import struct
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
from collections.abc import Iterator
|
||||
@@ -158,6 +159,9 @@ def test_choose() -> None:
|
||||
assert dgp.choose([], None) is None
|
||||
assert dgp.choose([a, b], None) == a
|
||||
assert dgp.choose([a, b], b) == b
|
||||
# same app on a newer process: keep the first start so the timer doesn't restart
|
||||
moved = dgp.Game(dgp.App("2", "B"), 3, 205)
|
||||
assert dgp.choose([a, moved], b) == dgp.Game(dgp.App("2", "B"), 3, 200)
|
||||
assert dgp.choose([a], b) == a
|
||||
|
||||
|
||||
@@ -291,9 +295,10 @@ class FakeDiscord:
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def runtime_dir(tmp_path: Path) -> Iterator[Path]:
|
||||
# AF_UNIX paths are limited to ~108 bytes, so don't nest deeply
|
||||
yield tmp_path
|
||||
def runtime_dir() -> Iterator[Path]:
|
||||
# AF_UNIX paths are limited to 107 bytes: tmp_path plus the Flatpak subdir can exceed it
|
||||
with tempfile.TemporaryDirectory(prefix="dgp-", dir="/tmp") as d:
|
||||
yield Path(d)
|
||||
|
||||
|
||||
def test_ipc_set_activity(runtime_dir: Path) -> None:
|
||||
@@ -316,6 +321,15 @@ def test_ipc_later_socket(runtime_dir: Path) -> None:
|
||||
dgp.DiscordIPC.connect("123", runtime_dir).close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("subdir", ["app/com.discordapp.Discord", "snap.discord"])
|
||||
def test_ipc_sandbox_socket(runtime_dir: Path, subdir: str) -> None:
|
||||
(runtime_dir / subdir).mkdir(parents=True)
|
||||
discord = FakeDiscord(runtime_dir / subdir / "discord-ipc-0")
|
||||
dgp.DiscordIPC.connect("123", runtime_dir).close()
|
||||
assert discord.received[0] == (dgp.OP_HANDSHAKE, {"v": 1, "client_id": "123"})
|
||||
discord.stop()
|
||||
|
||||
|
||||
def test_ipc_no_socket(runtime_dir: Path) -> None:
|
||||
with pytest.raises(FileNotFoundError):
|
||||
dgp.DiscordIPC.connect("123", runtime_dir)
|
||||
@@ -352,6 +366,7 @@ def test_presence(runtime_dir: Path) -> None:
|
||||
presence.update(genshin)
|
||||
assert presence.shown == genshin
|
||||
presence.update(genshin) # unchanged: nothing sent
|
||||
presence.update(dgp.Game(genshin.app, 11, 100)) # another process, same timer: nothing sent
|
||||
assert len(discord.received) == 2
|
||||
|
||||
presence.update(neverwinter) # another app needs a new connection
|
||||
|
||||
Reference in New Issue
Block a user