Skip to content

Commit d4a388a

Browse files
committed
Working on fetch data from cloud.
1 parent 387bdd3 commit d4a388a

File tree

10 files changed

+82
-16
lines changed

10 files changed

+82
-16
lines changed

electron/src/book.backend.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ export class BookBackend {
169169
this.book.downloaded = true;
170170

171171
this.book.commit = (msg.data as IBookDownloaded).commit;
172-
this.book.headBranch = (msg.data as IBookDownloaded).branch;
173172

174173
query = {
175174
table: 'Book',

electron/src/git-ops.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ export const clone = async (bookDir: string, bookUri: string) => {
3737
Clone.clone(bookUri, bookDir, opts)
3838
.then(async (repo) => {
3939
const commit = await repo.getHeadCommit();
40-
const branch = await repo.getCurrentBranch();
40+
//const branch = await repo.getCurrentBranch();
4141

4242
const msg: IBookDownloaded = {
4343
commit: commit.sha(),
44-
branch: await Branch.name(branch)
44+
//branch: await Branch.name(branch)
4545
}
4646
if(process.send){
4747
const message: IIpcMessage = {title: 'book-downloaded', data: msg};

electron/src/models.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ export class Website extends Meta{
4848

4949
@Entity()
5050
export class Writer extends Meta{
51+
@Column({default: 0})
52+
platformId: number;
53+
54+
@Column({default: ''})
55+
htmlUrl: string;
56+
5157
@Column({default: ''})
5258
name: string;
5359

electron/src/vendor.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export interface IProgressMessage {
2424

2525
export interface IBookDownloaded {
2626
commit: string;
27-
branch: string;
2827
}
2928

3029
export interface IError {

src/app/home/components/book-list.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
(click)="openReadingRecordDialog(book)">
3636
<ion-icon name="list-outline"></ion-icon>阅读记录
3737
</button>
38-
<button mat-button *ngIf="book.downloaded" (click)="openReadmeDialog(book)">
38+
<button mat-button (click)="openReadmeDialog(book)">
3939
<ion-icon name="compass-outline"></ion-icon>README
4040
</button>
4141
<button mat-button *ngIf="book.downloaded" (click)="openBook(book)">

src/app/home/components/readme-dialog.component.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
MAT_DIALOG_DATA
1111
} from '@angular/material/dialog';
1212

13+
import { join } from 'path';
14+
1315
import { Book } from '../../models';
1416

1517
@Component({
@@ -23,11 +25,23 @@ export class ReadmeDialog implements OnInit{
2325
) {}
2426

2527
get readmeUrl () {
26-
const giteeReadmeUrl = "https://gitee.com/hainanwu/18.06-linalg-notes/raw/master/README.md";
27-
const gitlabReadmeUrl = 'https://gitlab.com/aviman1109/devops/-/raw/master/README.md';
28-
const githubReadmeUrl = 'https://raw.githubusercontent.com/gnu4cn/ccna60d/main/README.md';
28+
if(/github/.test(this.data.website.uri)) {
29+
return this.data.downloaded
30+
? join(this.data.website.uri, this.data.writer.name, this.data.name, 'README.md')
31+
: join('https://raw.githubusercontent.com', this.data.writer.name, this.data.name, this.data.defaultBranch, 'README.md');
32+
}
33+
34+
if(/gitee/.test(this.data.website.uri)) {
35+
return this.data.downloaded
36+
? join(this.data.website.uri, this.data.writer.name, this.data.name, 'README.md')
37+
: join('https://gitee.com', this.data.writer.name, this.data.name, 'raw', this.data.defaultBranch, 'README.md');
38+
}
2939

30-
return '';
40+
if(/gitlab/.test(this.data.website.uri)) {
41+
return this.data.downloaded
42+
? join(this.data.website.uri, this.data.writer.name, this.data.name, 'README.md')
43+
: join('https://gitlab.com', this.data.writer.name, this.data.name, '-', 'raw', this.data.defaultBranch, 'README.md');
44+
}
3145
}
3246

3347
ngOnInit() {}

src/app/home/services/book.service.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,37 @@ export class BookService {
7474
newBook.name = re.test(name) ? name.replace(re, '') : name;
7575

7676
newBook.website = await this.website.newWebsit(site);
77-
newBook.writer = await this.writer.newWriter(writerName, newBook.website);
77+
78+
if(/gitee/.test(site)){
79+
await this.fetchService.getRepoProfile(site, newBook.name, writerName).subscribe(async (res) => {
80+
newBook.writer = await this.writer.newWriter(writerName, newBook.website, res['owner']['login']);
81+
});
82+
}
83+
else {
84+
newBook.writer = await this.writer.newWriter(writerName, newBook.website);
85+
}
86+
7887
newBook.cateList = await this.cate.saveList(res.cateList);
7988
newBook.recordList = [];
8089

90+
// 这里要获取到 Repository 的更多信息
91+
if(/gitlab/.test(site)) {
92+
await this.fetchService.getRepoProfile(site, newBook.name, writerName, newBook.writer.platformId)
93+
.subscribe(res => {
94+
const repo = (res as object[]).find(repo => repo['path'] === newBook.name)
95+
96+
newBook.desc = repo['description'];
97+
newBook.defaultBranch = repo['default_branch'];
98+
});
99+
}
100+
else {
101+
await this.fetchService.getRepoProfile(site, newBook.name, writerName)
102+
.subscribe(res => {
103+
newBook.desc = res['description'];
104+
newBook.defaultBranch = res['default_branch'];
105+
});
106+
}
107+
81108
const query: IQuery = {
82109
table: 'Book',
83110
item: newBook

src/app/home/services/fetch.service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { PrivateTokensService } from './private-tokens.service';
99
providedIn: 'root'
1010
})
1111
export class FetchService {
12-
private inFlight = new Map<string, Observable<JSON>>();
12+
private inFlight = new Map<string, Observable<object|object[]>>();
1313

1414
constructor(
1515
private http: HttpClient,
@@ -20,7 +20,7 @@ export class FetchService {
2020
*
2121
* @param url {string} Full path relative to root
2222
*/
23-
get = (url: string, header: string): Observable<JSON> => {
23+
get = (url: string, header: string): Observable<object|object[]> => {
2424
if (!url) {
2525
return of(JSON.parse(''));
2626
}
@@ -47,7 +47,7 @@ export class FetchService {
4747
return obs;
4848
}
4949

50-
fetchWriterProfile = (writerName: string, websiteUri: string): Observable<JSON> => {
50+
getWriterProfile = (writerName: string, websiteUri: string): Observable<object|object[]> => {
5151
let url: string;
5252
let header: string;
5353

@@ -71,7 +71,7 @@ export class FetchService {
7171
return this.get(url, header);
7272
}
7373

74-
getRepoProfile = (website: string, repo: string, owner?: string, ownerId?: number): Observable<JSON> => {
74+
getRepoProfile = (website: string, repo: string, owner: string, ownerId?: number): Observable<object|object[]> => {
7575
let url: string;
7676
let header: string;
7777

@@ -95,7 +95,7 @@ export class FetchService {
9595
return this.get(url, header);
9696
}
9797

98-
searchBooks = (websiteUri: string, keywords: string): Observable<JSON> => {
98+
searchBooks = (websiteUri: string, keywords: string): Observable<object|object[]> => {
9999
let url: string;
100100
let header: string;
101101

src/app/home/services/writer.service.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { CrudService } from '../../services/crud.service';
44
import { Writer, Website } from '../../models';
55

66
import { OpMessageService } from './op-message.service';
7+
import { FetchService } from './fetch.service';
8+
79
import {
810
IQuery,
911
IQueryResult,
@@ -17,6 +19,7 @@ export class WriterService {
1719

1820
constructor(
1921
private crud: CrudService,
22+
private fetchService: FetchService,
2023
private opMessage: OpMessageService,
2124
) {
2225
this.crud.getItems({table: 'Writer'})
@@ -27,7 +30,7 @@ export class WriterService {
2730
});
2831
}
2932

30-
newWriter = async (writerName: string, website: Website) => {
33+
newWriter = async (writerName: string, website: Website, platformName?: string) => {
3134
const writer = await this.list.find(w => w.name === writerName);
3235

3336
if (writer){
@@ -62,6 +65,18 @@ export class WriterService {
6265
_writer.websiteList = [];
6366
_writer.websiteList.push(website);
6467

68+
// 获取writer的更多信息
69+
await this.fetchService.getWriterProfile(platformName ? platformName : writerName, website.uri).subscribe(res => {
70+
console.log(res);
71+
72+
_writer.platformId = res['id'];
73+
_writer.avatar_url = res['avatar_url'];
74+
_writer.fullName = res['name'];
75+
_writer.htmlUrl = res['html_url'];
76+
_writer.desc = res['bio'];
77+
if(/github/.test(website.uri))_writer.location = res['location'];
78+
});
79+
6580
const query: IQuery = {
6681
table: "Writer",
6782
item: _writer

src/app/models.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ export class Website extends Meta{
4848

4949
@Entity()
5050
export class Writer extends Meta{
51+
@Column({default: 0})
52+
platformId: number;
53+
54+
@Column({default: ''})
55+
htmlUrl: string;
56+
5157
@Column({default: ''})
5258
name: string;
5359

0 commit comments

Comments
 (0)