Skip to content

Commit

Permalink
Move note fetching from gitnews to octokit in main process
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Sep 8, 2024
1 parent 573d759 commit e3a1f08
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 64 deletions.
9 changes: 9 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import debugFactory from 'debug';
import log from 'electron-log';
import AutoLaunch from 'easy-auto-launch';
import dotEnv from 'dotenv';
import { fetchNotificationsForAccount } from './lib/github-interface';
import type { AccountInfo } from '../shared-types';

// These are provided by electron forge
declare const MAIN_WINDOW_WEBPACK_ENTRY: string;
Expand Down Expand Up @@ -177,6 +179,13 @@ ipcMain.handle('is-demo-mode:get', async () => {
return Boolean(process.env.GITNEWS_DEMO_MODE);
});

ipcMain.handle(
'notifications-for-account:get',
async (_event, account: AccountInfo) => {
return fetchNotificationsForAccount(account);
}
);

function setIcon(type?: string) {
if (!type) {
type = lastIconState;
Expand Down
35 changes: 35 additions & 0 deletions src/main/lib/github-interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { AccountInfo, Note, NoteReason } from '../../shared-types';
import { Octokit } from '@octokit/rest';

export async function fetchNotificationsForAccount(
account: AccountInfo
): Promise<Note[]> {
const octokit = new Octokit({
auth: account.apiKey,
baseUrl: account.serverUrl,
userAgent: 'gitnews-menubar',
});
const notifications =
await octokit.rest.activity.listNotificationsForAuthenticatedUser({
all: false,
});
return notifications.data.map((notification) => {
return {
id: notification.id,
title: notification.subject.title,
unread: notification.unread,
repositoryFullName: notification.repository.full_name,
commentUrl: notification.subject.latest_comment_url,
updatedAt: notification.updated_at,
repositoryName: notification.repository.name,
type: notification.subject.type,
subjectUrl: notification.subject.url,
commentAvatar: notification.repository.owner.avatar_url, // FIXME I think this comes from the commentUrl
repositoryOwnerAvatar: notification.repository.owner.avatar_url,
api: {
subject: { state: undefined, merged: undefined }, // FIXME I think this comes from the subjectUrl
notification: { reason: notification.reason as NoteReason },
},
};
});
}
4 changes: 3 additions & 1 deletion src/preload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { contextBridge, ipcRenderer } from 'electron';
import { IconType, MainBridge } from './renderer/types';
import { AccountInfo, IconType, MainBridge } from './renderer/types';

const bridge: MainBridge = {
quitApp: () => ipcRenderer.send('quit-app'),
Expand All @@ -19,6 +19,8 @@ const bridge: MainBridge = {
getVersion: () => ipcRenderer.invoke('version:get'),
isDemoMode: () => ipcRenderer.invoke('is-demo-mode:get'),
isAutoLaunchEnabled: () => ipcRenderer.invoke('is-auto-launch:get'),
getNotificationsForAccount: (account: AccountInfo) =>
ipcRenderer.invoke('notifications-for-account:get', account),
};

contextBridge.exposeInMainWorld('electronApi', bridge);
27 changes: 17 additions & 10 deletions src/renderer/lib/gitnews-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
isInvalidJson,
isTokenInvalid,
} from '../lib/helpers';
import { createNoteGetter } from 'gitnews';
import {
changeToOffline,
fetchBegin,
Expand All @@ -19,7 +18,13 @@ import {
addConnectionError,
setIsTokenInvalid,
} from '../lib/reducer';
import { AppReduxState, Note, UnknownFetchError } from '../types';
import {
AccountInfo,
AppReduxState,
Note,
NoteReason,
UnknownFetchError,
} from '../types';
import { AppDispatch } from './store';
import { createDemoNotifications } from './demo-mode';

Expand Down Expand Up @@ -126,26 +131,28 @@ export function createFetcher(): Middleware<{}, AppReduxState> {
}
}

const getNotifications = createNoteGetter({
fetch: (url, options) => fetch(url, options),
log: (message) => {
console.log('Gitnews: ' + message);
},
});

function getFetcher(
token: string,
isDemoMode: boolean
): () => Promise<Note[]> {
if (isDemoMode) {
return () => getDemoNotifications();
}
return () => getNotifications(token);
return () =>
fetchNotifications({
id: 'main-github-api', // FIXME: use the account info from the state
serverUrl: 'https://api.github.com',
apiKey: token,
});
}

return fetcher;
}

async function fetchNotifications(account: AccountInfo): Promise<Note[]> {
return window.electronApi.getNotificationsForAccount(account);
}

async function getDemoNotifications(): Promise<Note[]> {
currentDemoNotifications = [
...currentDemoNotifications,
Expand Down
56 changes: 3 additions & 53 deletions src/renderer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,12 @@ import {
PANE_MUTED_REPOS,
PANE_ACCOUNTS,
} from './lib/constants';
import type { NoteReason, Note, AccountInfo } from '../shared-types';

export type NoteReason =
| 'assign'
| 'author'
| 'ci_activity'
| 'comment'
| 'manual'
| 'mention'
| 'push'
| 'review_requested'
| 'security_alert'
| 'state_change'
| 'subscribed'
| 'team_mention'
| 'your_activity';
export type { NoteReason, Note, AccountInfo };

export type FilterType = NoteReason | 'all';

export interface NoteApi {
subject?: { state?: string; merged?: boolean };
notification?: { reason?: NoteReason };
}

export interface Note {
id: string;
title: string;
unread: boolean;
repositoryFullName: string;
gitnewsMarkedUnread?: boolean;
gitnewsSeen?: boolean;

/**
* Number of milliseconds since the epoc (what Date.now() returns).
*/
gitnewsSeenAt?: number;

api: NoteApi;
commentUrl: string;

/**
* ISO 8601 formatted date string like `2017-08-23T18:20:00Z`.
*/
updatedAt: string;

repositoryName: string;
type: string;
subjectUrl: string;
commentAvatar?: string;
repositoryOwnerAvatar?: string;
}

export interface AppReduxState {
token: undefined | string;
notes: Note[];
Expand Down Expand Up @@ -190,16 +145,11 @@ export interface MainBridge {
onClick: (callback: () => void) => void;
getToken: () => Promise<string>;
getVersion: () => Promise<string>;
getNotificationsForAccount: (account: AccountInfo) => Promise<Note[]>;
isDemoMode: () => Promise<boolean>;
isAutoLaunchEnabled: () => Promise<boolean>;
}

export interface AccountInfo {
id: string;
apiKey: string;
serverUrl: string;
}

export interface FetchErrorObject {
code?: string;
name?: string;
Expand Down
53 changes: 53 additions & 0 deletions src/shared-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export type NoteReason =
| 'assign'
| 'author'
| 'ci_activity'
| 'comment'
| 'manual'
| 'mention'
| 'push'
| 'review_requested'
| 'security_alert'
| 'state_change'
| 'subscribed'
| 'team_mention'
| 'your_activity';

export interface NoteApi {
subject?: { state?: string; merged?: boolean };
notification?: { reason?: NoteReason };
}

export interface Note {
id: string;
title: string;
unread: boolean;
repositoryFullName: string;
gitnewsMarkedUnread?: boolean;
gitnewsSeen?: boolean;

/**
* Number of milliseconds since the epoc (what Date.now() returns).
*/
gitnewsSeenAt?: number;

api: NoteApi;
commentUrl: string;

/**
* ISO 8601 formatted date string like `2017-08-23T18:20:00Z`.
*/
updatedAt: string;

repositoryName: string;
type: string;
subjectUrl: string;
commentAvatar?: string;
repositoryOwnerAvatar?: string;
}

export interface AccountInfo {
id: string;
apiKey: string;
serverUrl: string;
}

0 comments on commit e3a1f08

Please sign in to comment.