mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-02-13 02:42:45 +00:00
improve User documentation
This commit is contained in:
34
src/User.ts
34
src/User.ts
@@ -5,6 +5,9 @@ import { IUserDocument } from "./database/users/users.types";
|
|||||||
import Repository from "./Repository";
|
import Repository from "./Repository";
|
||||||
import { GitHubRepository } from "./source/GitHubRepository";
|
import { GitHubRepository } from "./source/GitHubRepository";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Model for a user
|
||||||
|
*/
|
||||||
export default class User {
|
export default class User {
|
||||||
private _model: IUserDocument;
|
private _model: IUserDocument;
|
||||||
constructor(model: IUserDocument) {
|
constructor(model: IUserDocument) {
|
||||||
@@ -31,10 +34,22 @@ export default class User {
|
|||||||
this._model.default = d;
|
this._model.default = d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the GitHub repositories of the user
|
||||||
|
* @param opt options
|
||||||
|
* @returns the list of github repositories
|
||||||
|
*/
|
||||||
async getGitHubRepositories(opt?: {
|
async getGitHubRepositories(opt?: {
|
||||||
|
/**
|
||||||
|
* Get the repository from GitHub
|
||||||
|
*/
|
||||||
force: boolean;
|
force: boolean;
|
||||||
}): Promise<GitHubRepository[]> {
|
}): Promise<GitHubRepository[]> {
|
||||||
if (!this._model.repositories || opt?.force === true) {
|
if (
|
||||||
|
!this._model.repositories ||
|
||||||
|
this._model.repositories.length == 0 ||
|
||||||
|
opt?.force === true
|
||||||
|
) {
|
||||||
// get the list of repo from github
|
// get the list of repo from github
|
||||||
const octokit = new Octokit({ auth: this.accessToken });
|
const octokit = new Octokit({ auth: this.accessToken });
|
||||||
const repositories = (
|
const repositories = (
|
||||||
@@ -53,6 +68,7 @@ export default class User {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// find the repositories that are already in the database
|
||||||
const finds = (
|
const finds = (
|
||||||
await RepositoryModel.find({
|
await RepositoryModel.find({
|
||||||
externalId: {
|
externalId: {
|
||||||
@@ -61,12 +77,14 @@ export default class User {
|
|||||||
}).select("externalId")
|
}).select("externalId")
|
||||||
).map((m) => m.externalId);
|
).map((m) => m.externalId);
|
||||||
|
|
||||||
|
// save all the new repositories
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
repositories
|
repositories
|
||||||
.filter((r) => finds.indexOf(r.externalId) == -1)
|
.filter((r) => finds.indexOf(r.externalId) == -1)
|
||||||
.map((r) => r.save())
|
.map((r) => r.save())
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// save only the if of the repositories in the user model
|
||||||
this._model.repositories = (
|
this._model.repositories = (
|
||||||
await RepositoryModel.find({
|
await RepositoryModel.find({
|
||||||
externalId: {
|
externalId: {
|
||||||
@@ -74,6 +92,8 @@ export default class User {
|
|||||||
},
|
},
|
||||||
}).select("id")
|
}).select("id")
|
||||||
).map((m) => m.id);
|
).map((m) => m.id);
|
||||||
|
|
||||||
|
// have the model
|
||||||
await this._model.save();
|
await this._model.save();
|
||||||
return repositories.map((r) => new GitHubRepository(r));
|
return repositories.map((r) => new GitHubRepository(r));
|
||||||
} else {
|
} else {
|
||||||
@@ -83,24 +103,28 @@ export default class User {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the lost of anonymized repositories
|
||||||
|
* @returns the list of anonymized repositories
|
||||||
|
*/
|
||||||
async getRepositories() {
|
async getRepositories() {
|
||||||
const repositories = (
|
const repositories = (
|
||||||
await AnonymizedRepositoryModel.find({
|
await AnonymizedRepositoryModel.find({
|
||||||
owner: this.username,
|
owner: this.username,
|
||||||
}).exec()
|
}).exec()
|
||||||
).map((d) => new Repository(d));
|
).map((d) => new Repository(d));
|
||||||
|
const promises = [];
|
||||||
for (let repo of repositories) {
|
for (let repo of repositories) {
|
||||||
if (repo.options.expirationDate) {
|
|
||||||
repo.options.expirationDate = new Date(repo.options.expirationDate);
|
|
||||||
}
|
|
||||||
if (
|
if (
|
||||||
repo.options.expirationMode != "never" &&
|
repo.options.expirationMode != "never" &&
|
||||||
repo.options.expirationDate != null &&
|
repo.options.expirationDate != null &&
|
||||||
repo.options.expirationDate < new Date()
|
repo.options.expirationDate < new Date()
|
||||||
) {
|
) {
|
||||||
await repo.expire();
|
// expire the repository
|
||||||
|
promises.push(repo.expire());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
await Promise.all(promises);
|
||||||
return repositories;
|
return repositories;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user