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

Feat: Display amount of guarantees #171

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ <h3 class="card-title">Your guarantor</h3>

<div class="card">
<div class="card-header">
<h3 class="card-title">Guarantees that {{instance.name}} <strong>has given</strong></h3>
<h3 class="card-title">
Guarantees that {{instance.name}} <strong>has given</strong>
({{instancesGuaranteedByMe.length}}/{{maxGuarantees}})
</h3>
</div>
<div class="card-body">
<table class="table table-bordered">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {ApiResponseHelperService} from "../../../services/api-response-helper.se
import {toPromise} from "../../../types/resolvable";
import {CachedFediseerApiService} from "../../../services/cached-fediseer-api.service";
import {InstanceMoveEvent} from "../../../shared/components/instance-move-to-list/instance-move-to-list.component";
import {int} from "../../../types/number";

@Component({
selector: 'app-my-guarantees',
Expand All @@ -20,6 +21,7 @@ export class MyGuaranteesComponent implements OnInit {
public instancesGuaranteedByMe: InstanceDetailResponse[] = [];
public instance: Instance = this.authManager.currentInstanceSnapshot;
public loading: boolean = true;
public maxGuarantees: int = 0;

constructor(
private readonly titleService: TitleService,
Expand All @@ -37,6 +39,7 @@ export class MyGuaranteesComponent implements OnInit {
const responses = await Promise.all([
toPromise(this.cachedApi.getCurrentInstanceInfo()),
toPromise(this.cachedApi.getGuaranteesByInstance(this.authManager.currentInstanceSnapshot.name!)),
toPromise(this.cachedApi.getFediseerConfig()),
]);

if (this.apiResponseHelper.handleErrors(responses)) {
Expand All @@ -46,6 +49,7 @@ export class MyGuaranteesComponent implements OnInit {

this.guarantor = responses[0].successResponse!.guarantor ?? '';
this.instancesGuaranteedByMe = responses[1].successResponse!.instances;
this.maxGuarantees = responses[2].successResponse!.max_guarantees;

this.loading = false;
}
Expand Down
8 changes: 8 additions & 0 deletions src/app/response/fediseer-config.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {int} from "../types/number";

export interface FediseerConfigResponse {
max_guarantees: int;
max_guarantors: int;
max_config_actions_per_min: int;
max_tags: int;
}
17 changes: 17 additions & 0 deletions src/app/services/cached-fediseer-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {PermanentCacheService} from "./cache/permanent-cache.service";
import {Cache, CacheItem} from "./cache/cache";
import {InstanceListResponse} from "../response/instance-list.response";
import {SafelistFilter} from "../types/safelist-filter";
import {FediseerConfigResponse} from "../response/fediseer-config.response";

export enum CacheType {
Runtime,
Expand Down Expand Up @@ -318,6 +319,22 @@ export class CachedFediseerApiService {
)
}

public getFediseerConfig(cacheConfig: CacheConfiguration = {}): Observable<ApiResponse<FediseerConfigResponse>> {
cacheConfig.type ??= CacheType.Permanent;
cacheConfig.ttl ??= 60;

const cacheKey = `api.fediseer_config${cacheConfig.ttl}`;
const item = this.getCacheItem<FediseerConfigResponse>(cacheKey, cacheConfig)!;
if (item.isHit && !cacheConfig.clear) {
return this.getSuccessResponse(item);
}

return this.api.getFediseerConfig().pipe(
tap(this.storeResponse(item, cacheConfig)),
);
}


public clearCache(): void {
this.runtimeCache.clear();
this.permanentCache.clear();
Expand Down
5 changes: 5 additions & 0 deletions src/app/services/fediseer-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {PrivateMessageProxy} from "../types/private-message-proxy";
import {SolicitationInstanceDetailResponse} from "../response/solicitation-instance-detail.response";
import {ActionLogFilter} from "../types/action-log.filter";
import {SafelistFilter} from "../types/safelist-filter";
import {FediseerConfigResponse} from "../response/fediseer-config.response";

export interface ApiResponse<T> {
success: boolean;
Expand Down Expand Up @@ -497,6 +498,10 @@ export class FediseerApiService {
});
}

public getFediseerConfig(): Observable<ApiResponse<FediseerConfigResponse>> {
return this.sendRequest(HttpMethod.Get, `config`);
}

private sendRequest<T>(
method: HttpMethod,
endpoint: string,
Expand Down