Skip to content

Commit

Permalink
Only request push token if script is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoran committed Mar 20, 2024
1 parent bda0b10 commit 85eecf7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/lib/weechat/action_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
fetchVersionAction,
lastReadLinesAction,
nicklistUpdatedAction,
fetchScriptsAction,
upgradeAction
} from '../../store/actions';

Expand Down Expand Up @@ -207,6 +208,11 @@ export const transformToReduxAction = (data: WeechatResponse<unknown>) => {

return lastReadLinesAction(object.content);
}
case 'scripts': {
const object = data.objects[0] as WeechatObject<{ name: string }[]>;

return fetchScriptsAction(object.content.map(({ name }) => name));
}
default:
console.log('unhandled event!', data.id, data);
return undefined;
Expand Down
2 changes: 2 additions & 0 deletions src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ export const nicklistUpdatedAction = createAction<{
added: WeechatNicklist[];
removed: WeechatNicklist[];
}>('NICKLIST_UPDATED');

export const fetchScriptsAction = createAction<string[]>('FETCH_SCRIPTS');
23 changes: 18 additions & 5 deletions src/usecase/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { PersistGate } from 'redux-persist/integration/react';
import WeechatConnection, { ConnectionError } from '../lib/weechat/connection';
import { persistor, store } from '../store';

import { addListener } from '@reduxjs/toolkit';
import { UnsubscribeListener, addListener } from '@reduxjs/toolkit';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { getPushNotificationStatusAsync } from '../lib/helpers/push-notifications';
import { upgradeAction } from '../store/actions';
import { fetchScriptsAction, upgradeAction } from '../store/actions';
import App from './App';
import ConnectionGate from './ConnectionGate';
import Buffer from './buffers/ui/Buffer';
Expand All @@ -35,20 +35,34 @@ export default class WeechatNative extends React.Component<null, State> {
}
});

unsubscribeUpgradeAction: UnsubscribeListener;
unsubscribeFetchScriptsAction: UnsubscribeListener;

constructor(props: null) {
super(props);
store.dispatch(
this.unsubscribeUpgradeAction = store.dispatch(
addListener({
actionCreator: upgradeAction,
effect: () => {
this.disconnect();
}
})
);
this.unsubscribeFetchScriptsAction = store.dispatch(
addListener({
actionCreator: fetchScriptsAction,
effect: (scripts) => {
if (scripts.payload.includes('WeechatRN'))
this.setNotificationToken();
}
})
);
}

componentWillUnmount(): void {
this.appStateListener.remove();
this.unsubscribeUpgradeAction();
this.unsubscribeFetchScriptsAction();
}

setNotificationToken = async (): Promise<void> => {
Expand All @@ -67,9 +81,8 @@ export default class WeechatNative extends React.Component<null, State> {
this.connection.send(
'(last_read_lines) hdata buffer:gui_buffers(*)/own_lines/last_read_line/data buffer'
);
// connection.send("(nicklist) nicklist");
connection.send('(scripts) hdata python_script:scripts(*) name');
connection.send('sync');
this.setNotificationToken();
};

onConnectionError = (
Expand Down

0 comments on commit 85eecf7

Please sign in to comment.