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

Refactor repository #149

Merged
merged 59 commits into from
Aug 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
3c82f2f
fix: nits
h13i32maru Jul 31, 2020
238bfc2
refactor(StreamRepo):
h13i32maru Jul 31, 2020
138cd93
refactor(StreamRepo):
h13i32maru Jul 31, 2020
7dfa9c6
refactor(StreamRepo):
h13i32maru Aug 1, 2020
adcbd23
refactor(StreamRepo):
h13i32maru Aug 1, 2020
2bad1d3
refactor(StreamRepo):
h13i32maru Aug 1, 2020
50c1584
refactor(StreamRepo):
h13i32maru Aug 1, 2020
dc07165
refactor(StreamRepo):
h13i32maru Aug 1, 2020
1ca4bf6
refactor(StreamRepo):
h13i32maru Aug 1, 2020
c88fea0
refactor(StreamRepo):
h13i32maru Aug 1, 2020
f5081be
refactor(StreamRepo):
h13i32maru Aug 1, 2020
e57bb00
refactor(StreamRepo):
h13i32maru Aug 1, 2020
eb726e5
refactor(StreamRepo):
h13i32maru Aug 1, 2020
ae12f7d
refactor(StreamRepo):
h13i32maru Aug 1, 2020
ca4b6bb
refactor(StreamRepo):
h13i32maru Aug 1, 2020
c6c26fd
refactor(FilterHistoryRepo):
h13i32maru Aug 1, 2020
9c37bed
refactor(FilterHistoryRepo):
h13i32maru Aug 1, 2020
0df5a99
refactor(StreamRepo):
h13i32maru Aug 1, 2020
de9adf3
refactor(StreamIssueRepo):
h13i32maru Aug 1, 2020
498447d
refactor:
h13i32maru Aug 1, 2020
67f7e7c
refactor(StreamIssueRepo):
h13i32maru Aug 1, 2020
5d17553
refactor(StreamIssueRepo):
h13i32maru Aug 1, 2020
a9e1f5e
refactor(SubscriptionIssueRepo):
h13i32maru Aug 1, 2020
c9496ac
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
1ff6872
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
933087b
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
c3eee22
fix(Stream): query index calc
h13i32maru Aug 1, 2020
2e76678
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
8a9af6d
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
5b4678d
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
be72387
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
54b1f49
refactor(StreamPolling):
h13i32maru Aug 1, 2020
8bb448b
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
9d10f56
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
e135662
refactor(IssueRepo):
h13i32maru Aug 1, 2020
efceb1f
refactor(IssueRepo):
h13i32maru Aug 1, 2020
4e4059d
refactor(IssueRepo):
h13i32maru Aug 1, 2020
2b7e4ce
refactor(IssueRepo):
h13i32maru Aug 1, 2020
30f82e5
refactor(IssueRepo):
h13i32maru Aug 1, 2020
8d245b9
refactor(IssueRepo):
h13i32maru Aug 1, 2020
f0d0d98
refactor(IssueRepo):
h13i32maru Aug 1, 2020
0730d1e
refactor(IssueRepo):
h13i32maru Aug 1, 2020
457a361
refactor(IssueRepo):
h13i32maru Aug 1, 2020
59168df
refactor(VersionRepo):
h13i32maru Aug 1, 2020
8e2c123
refactor(IssueRepo):
h13i32maru Aug 1, 2020
3d9d5d6
refactor(IssueRepo):
h13i32maru Aug 1, 2020
19e50f8
refactor(IssueRepo):
h13i32maru Aug 1, 2020
bff9d34
refactor(IssueRepo):
h13i32maru Aug 1, 2020
b44e56c
refactor: nits
h13i32maru Aug 2, 2020
cab2570
refactor(IssueRepo):
h13i32maru Aug 2, 2020
8bcf72c
refactor(IssueRepo):
h13i32maru Aug 2, 2020
5166b9f
refactor(IssueRepo):
h13i32maru Aug 2, 2020
c0852ca
refactor(IssueRepo):
h13i32maru Aug 2, 2020
d71e3e7
refactor(IssueRepo):
h13i32maru Aug 2, 2020
eedb2d0
refactor(IssueRepo):
h13i32maru Aug 2, 2020
e45dc8d
refactor(StreamIssueRepo):
h13i32maru Aug 2, 2020
992dac7
refactor(DBIPC): show slow query
h13i32maru Aug 2, 2020
13aa0f8
refactor(Repo):
h13i32maru Aug 2, 2020
f9e5f9a
refactor(IssueRepo):
h13i32maru Aug 2, 2020
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
Prev Previous commit
Next Next commit
refactor(SystemStreamRepo):
  • Loading branch information
h13i32maru committed Aug 1, 2020
commit 9d10f566e5092c4d7ba667c30af81dced59b07a9
9 changes: 7 additions & 2 deletions src/Renderer/Fragment/Issues/IssuesFragment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import {StreamEvent} from '../../Event/StreamEvent';
import {LibraryStreamEvent} from '../../Event/LibraryStreamEvent';
import {IssueRepo} from '../../Repository/IssueRepo';
import {IssueEvent} from '../../Event/IssueEvent';
import {SystemStreamId, SystemStreamRepo} from '../../Repository/SystemStreamRepo';
import {SystemStreamId} from '../../Repository/SystemStreamRepo';
import {StreamRepo} from '../../Repository/StreamRepo';
import {WebViewEvent} from '../../Event/WebViewEvent';
import {FilterHistoryRepo} from '../../Repository/FilterHistoryRepo';
import {ColorUtil} from '../../Util/ColorUtil';
import {GARepo} from '../../Repository/GARepo';
import {ConfigRepo} from '../../Repository/ConfigRepo';
import {StreamPolling} from '../../Infra/StreamPolling';
import {SubscriptionIssuesRepo} from '../../Repository/SubscriptionIssuesRepo';

const remote = electron.remote;

Expand Down Expand Up @@ -236,7 +238,10 @@ export class IssuesFragment extends React.Component<any, State> {

async _unsubscribe(issue) {
const url = issue.html_url;
await SystemStreamRepo.unsubscribe(url);
const {error} = await SubscriptionIssuesRepo.unsubscribe(url);
if (error) return console.error(error);
await StreamPolling.refreshSystemStream(SystemStreamId.subscription);
SystemStreamEvent.emitRestartAllStreams();
await this._loadIssues();
}

Expand Down
8 changes: 7 additions & 1 deletion src/Renderer/Fragment/Stream/SystemStreamsFragment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {IssueRepo} from '../../Repository/IssueRepo';
import {ModalSystemStreamSettingFragment} from './ModalSystemStreamSettingFragment'
import {GARepo} from '../../Repository/GARepo';
import {ConfigRepo} from '../../Repository/ConfigRepo';
import {StreamPolling} from '../../Infra/StreamPolling';
import {SubscriptionIssuesRepo} from '../../Repository/SubscriptionIssuesRepo';

const remote = electron.remote;
const MenuItem = remote.MenuItem;
Expand Down Expand Up @@ -122,7 +124,11 @@ export class SystemStreamsFragment extends React.Component<any, State> {
dialog.close();
SystemStreamEvent.emitCloseSubscriptionSetting();

await SystemStreamRepo.subscribe(url);
const {error} = await SubscriptionIssuesRepo.subscribe(url);
if (error) return console.error(error);

await StreamPolling.refreshSystemStream(SystemStreamId.subscription);
SystemStreamEvent.emitRestartAllStreams();
await this._loadStreams();

const stream = this.state.streams.find((stream)=> stream.id === SystemStreamId.subscription);
Expand Down
70 changes: 70 additions & 0 deletions src/Renderer/Repository/SubscriptionIssuesRepo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import {DBIPC} from '../../IPC/DBIPC';
import {SubscriptionIssueEntity} from '../Type/SubscriptionIssueEntity';
import {ConfigRepo} from './ConfigRepo';
import {GitHubClient} from '../Infra/GitHubClient';
import {IssueRepo} from './IssueRepo';
import {StreamIssueRepo} from './StreamIssueRepo';
import {StreamPolling} from '../Infra/StreamPolling';
import {SystemStreamEvent} from '../Event/SystemStreamEvent';
import {SystemStreamId} from './SystemStreamRepo';
import {DateUtil} from '../Util/DateUtil';

class _SubscriptionIssuesRepo {
async getAllSubscriptionIssues(): Promise<{error?: Error; subscriptionIssues?: SubscriptionIssueEntity[]}>{
Expand All @@ -8,6 +16,68 @@ class _SubscriptionIssuesRepo {

return {subscriptionIssues: rows};
}

private async getSubscriptionIssue(url: string): Promise<{error?: Error; subscriptionIssue?: SubscriptionIssueEntity}> {
const {error, row} = await DBIPC.selectSingle<SubscriptionIssueEntity>('select * from subscription_issues where url = ?', [url]);
if (error) return {error};

return {subscriptionIssue: row};
}

async subscribe(url): Promise<{error?: Error}> {
// check already
const {error: e1, subscriptionIssue} = await this.getSubscriptionIssue(url);
if (e1) return {error: e1};
if (subscriptionIssue) return;

// get issue
const {repo, issueNumber} = this.getRepoAndIssueNumber(url);
const github = ConfigRepo.getConfig().github;
const client = new GitHubClient(github.accessToken, github.host, github.pathPrefix, github.https);
const res = await client.request(`/repos/${repo}/issues/${issueNumber}`);
if (res.error) return {error: res.error};
const issue = res.body;

// create
await IssueRepo.import([issue]);
const {error} = await StreamIssueRepo.createBulk(SystemStreamId.subscription, [issue]);
if (error) return {error};

const createdAt = DateUtil.localToUTCString(new Date());
const {error: e2} = await DBIPC.exec(
`insert into subscription_issues (issue_id, repo, url, created_at) values (?, ?, ?, ?) `,
[issue.id, repo, url, createdAt]
);
if (e2) return {error: e2};

await StreamPolling.refreshSystemStream(SystemStreamId.subscription);
SystemStreamEvent.emitRestartAllStreams();
}

async unsubscribe(url: string): Promise<{error?: Error}> {
// check not already
const {error: e1, subscriptionIssue} = await this.getSubscriptionIssue(url);
if (e1) return {error: e1};
if (!subscriptionIssue) return;

const {error: e2} = await DBIPC.exec('delete from subscription_issues where url = ?', [url]);
if (e2) return {error: e2};

const {error: e3} = await DBIPC.exec(
'delete from streams_issues where stream_id = ? and issue_id = ?',
[SystemStreamId.subscription, subscriptionIssue.issue_id]
);
if (e3) return {error: e3};

return {};
}

private getRepoAndIssueNumber(url: string): {repo: string; issueNumber: number} {
const urlPaths = url.split('/').reverse();
const repo = `${urlPaths[3]}/${urlPaths[2]}`;
const issueNumber = parseInt(urlPaths[0], 10);
return {repo, issueNumber};
}
}

export const SubscriptionIssuesRepo = new _SubscriptionIssuesRepo();
91 changes: 42 additions & 49 deletions src/Renderer/Repository/SystemStreamRepo.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {DBIPC} from '../../IPC/DBIPC';
import {StreamPolling} from '../Infra/StreamPolling';
import {SystemStreamEvent} from '../Event/SystemStreamEvent';
import {ConfigRepo} from './ConfigRepo';
import {GitHubClient} from '../Infra/GitHubClient';
import {IssueRepo} from './IssueRepo';
import {StreamIssueRepo} from './StreamIssueRepo';
import moment from 'moment';
import {SystemStreamEntity} from '../Type/SystemStreamEntity';

export enum SystemStreamId {
Expand Down Expand Up @@ -142,49 +135,49 @@ class _SystemStreamRepo {
return !!res.row;
}

async subscribe(url) {
const already = await this.isSubscription(url);
if (already) return;

const urlPaths = url.split('/').reverse();
const repo = `${urlPaths[3]}/${urlPaths[2]}`;
const number = urlPaths[0];

const github = ConfigRepo.getConfig().github;
const client = new GitHubClient(github.accessToken, github.host, github.pathPrefix, github.https);
const res = await client.request(`/repos/${repo}/issues/${number}`);
const issue = res.body;

await IssueRepo.import([issue]);
const {error} = await StreamIssueRepo.createBulk(SystemStreamId.subscription, [issue]);
if (error) return console.error(error);

const createdAt = moment(new Date()).utc().format('YYYY-MM-DDTHH:mm:ss[Z]');
await DBIPC.exec(`
insert into
subscription_issues
(issue_id, repo, url, created_at)
values
(?, ?, ?, ?)
`, [issue.id, repo, url, createdAt]);

await StreamPolling.refreshSystemStream(SystemStreamId.subscription);
SystemStreamEvent.emitRestartAllStreams();
}

async unsubscribe(url) {
const already = await this.isSubscription(url);
if (!already) return;

const {row: subscriptionIssue} = await DBIPC.selectSingle('select * from subscription_issues where url = ?', [url]);
await DBIPC.exec('delete from subscription_issues where url = ?', [url]);

await DBIPC.exec('delete from streams_issues where stream_id = ? and issue_id = ?', [SystemStreamId.subscription, subscriptionIssue.issue_id]);
// async subscribe(url) {
// const already = await this.isSubscription(url);
// if (already) return;
//
// const urlPaths = url.split('/').reverse();
// const repo = `${urlPaths[3]}/${urlPaths[2]}`;
// const number = urlPaths[0];
//
// const github = ConfigRepo.getConfig().github;
// const client = new GitHubClient(github.accessToken, github.host, github.pathPrefix, github.https);
// const res = await client.request(`/repos/${repo}/issues/${number}`);
// const issue = res.body;
//
// await IssueRepo.import([issue]);
// const {error} = await StreamIssueRepo.createBulk(SystemStreamId.subscription, [issue]);
// if (error) return console.error(error);
//
// const createdAt = moment(new Date()).utc().format('YYYY-MM-DDTHH:mm:ss[Z]');
// await DBIPC.exec(`
// insert into
// subscription_issues
// (issue_id, repo, url, created_at)
// values
// (?, ?, ?, ?)
// `, [issue.id, repo, url, createdAt]);
//
// await StreamPolling.refreshSystemStream(SystemStreamId.subscription);
// SystemStreamEvent.emitRestartAllStreams();
// }

// SystemStreamLauncher.restartAll();
await StreamPolling.refreshSystemStream(SystemStreamId.subscription);
SystemStreamEvent.emitRestartAllStreams();
}
// async unsubscribe(url) {
// const already = await this.isSubscription(url);
// if (!already) return;
//
// const {row: subscriptionIssue} = await DBIPC.selectSingle('select * from subscription_issues where url = ?', [url]);
// await DBIPC.exec('delete from subscription_issues where url = ?', [url]);
//
// await DBIPC.exec('delete from streams_issues where stream_id = ? and issue_id = ?', [SystemStreamId.subscription, subscriptionIssue.issue_id]);
//
// // SystemStreamLauncher.restartAll();
// await StreamPolling.refreshSystemStream(SystemStreamId.subscription);
// SystemStreamEvent.emitRestartAllStreams();
// }
}

export const SystemStreamRepo = new _SystemStreamRepo();