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

Fix(#162): DisconnectOnIdle doesn't restore presence #319

Merged
merged 2 commits into from
Aug 21, 2024
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
19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,30 +198,35 @@
],
"description": "List of problem level to count for `problems_count`."
},
"vscord.status.idle.enabled": {
"vscord.status.idle.check": {
"type": "boolean",
"default": true,
"description": "Should we enable the idle status?"
"description": "Should we check when you are idling?",
"order": 1
},
"vscord.status.idle.check": {
"vscord.status.idle.enabled": {
"type": "boolean",
"default": true,
"description": "Should we check when you are idling?"
"description": "Should we enable the idle status? \nIf disabled, the extension will only show a plain card with VS Code logo when idling.",
"order": 2
},
"vscord.status.idle.disconnectOnIdle": {
"type": "boolean",
"default": false,
"description": "Should going idle disconnect you from discord gateway?."
"description": "Should going idle disconnect you from Discord gateway? \nIf enabled, VS Code activity card will disappear from Discord at all when idling.",
"order": 3
},
"vscord.status.idle.resetElapsedTime": {
"type": "boolean",
"default": false,
"description": "Should going idle reset the elapsed time?."
"description": "Should going idle reset the elapsed time on the last active file? Only applies if \"Disconnect on Idle\" is enabled.",
"order": 4
},
"vscord.status.idle.timeout": {
"type": "number",
"default": 300,
"description": "Time in seconds before the user is considered idle."
"description": "Time in seconds before the user is considered idle.",
"order": 5
}
}
},
Expand Down
9 changes: 9 additions & 0 deletions src/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ export const activity = async (
const config = getConfig();
const presence = previous;

if (
isIdling &&
config.get(CONFIG_KEYS.Status.Idle.DisconnectOnIdle) &&
config.get(CONFIG_KEYS.Status.Idle.ResetElapsedTime)
) {
delete presence.startTimestamp;
return {};
}

if (isIdling && !config.get(CONFIG_KEYS.Status.Idle.Enabled)) return {};

if (config.get(CONFIG_KEYS.Status.ShowElapsedTime)) {
Expand Down
18 changes: 11 additions & 7 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,20 @@ export class RPCController {
const changeWindowState = window.onDidChangeWindowState((e: WindowState) => this.checkIdle(e));
const gitListener = dataClass.onUpdate(() => this.activityThrottle.callable());

// fire checkIdle at least once after loading
this.checkIdle(window.state);

if (config.get(CONFIG_KEYS.Status.Problems.Enabled)) this.listeners.push(diagnosticsChange);
if (config.get(CONFIG_KEYS.Status.Idle.Check)) this.listeners.push(changeWindowState);

this.listeners.push(fileSwitch, fileEdit, fileSelectionChanged, debugStart, debugEnd, gitListener);
}

private checkCanSend(): boolean {
private checkCanSend(isIdling: boolean): boolean {
const config = getConfig();
let userId = this.client.user?.id;
if (!userId) return false;
if (isIdling && config.get(CONFIG_KEYS.Status.Idle.DisconnectOnIdle)) return (this.canSendActivity = false);
let whitelistEnabled = config.get(CONFIG_KEYS.App.WhitelistEnabled);
if (whitelistEnabled) {
let whitelist = config.get(CONFIG_KEYS.App.Whitelist);
Expand All @@ -155,11 +159,11 @@ export class RPCController {
async () => {
if (!config.get(CONFIG_KEYS.Status.Idle.Check)) return;

if (config.get(CONFIG_KEYS.Status.Idle.DisconnectOnIdle)) {
await this.disable();
if (config.get(CONFIG_KEYS.Status.Idle.ResetElapsedTime))
this.state.startTimestamp = undefined;
return;
if (
config.get(CONFIG_KEYS.Status.Idle.DisconnectOnIdle) &&
config.get(CONFIG_KEYS.Status.Idle.ResetElapsedTime)
) {
delete this.state.startTimestamp;
}

if (!this.enabled) return;
Expand Down Expand Up @@ -190,7 +194,7 @@ export class RPCController {

async sendActivity(isViewing = false, isIdling = false): Promise<SetActivityResponse | undefined> {
if (!this.enabled) return;
this.checkCanSend();
this.checkCanSend(isIdling);
this.state = await activity(this.state, isViewing, isIdling);
this.state.instance = true;
if (!this.state || Object.keys(this.state).length === 0 || !this.canSendActivity)
Expand Down