Skip to content

feat: send auth in task content #104

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
96 changes: 50 additions & 46 deletions src/api/backend/api/default.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export class DefaultService {
this.configuration = configuration;
this.basePath = basePath || configuration.basePath || this.basePath;
}

this.basePath = this.basePath.replace(/\/+$/, ''); // remove trailing slash
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately it is impossible for us to modify this file since it is auto generated by npm run gen.api and as such these changes would be overwritten. Maybe this needs to be changed on backend instead?

}

/**
Expand Down Expand Up @@ -3993,51 +3995,53 @@ export class DefaultService {
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
* @param reportProgress flag to report request and response progress.
*/
public taskContent2GetSingle(view: string, contentId: string, observe?: 'body', reportProgress?: boolean): Observable<Uint8Array>;
public taskContent2GetSingle(view: string, contentId: string, observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<Uint8Array>>;
public taskContent2GetSingle(view: string, contentId: string, observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<Uint8Array>>;
public taskContent2GetSingle(view: string, contentId: string, observe: any = 'body', reportProgress: boolean = false ): Observable<any> {

if (view === null || view === undefined) {
throw new Error('Required parameter view was null or undefined when calling taskContent2GetSingle.');
}

if (contentId === null || contentId === undefined) {
throw new Error('Required parameter contentId was null or undefined when calling taskContent2GetSingle.');
}

let headers = this.defaultHeaders;

// authentication (ksi) required
if (this.configuration.accessToken) {
const accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers = headers.set('Authorization', 'Bearer ' + accessToken);
}

// to determine the Accept header
let httpHeaderAccepts: string[] = [
'application/json'
];
const httpHeaderAcceptSelected: string | undefined = this.configuration.selectHeaderAccept(httpHeaderAccepts);
if (httpHeaderAcceptSelected != undefined) {
headers = headers.set('Accept', httpHeaderAcceptSelected);
}

// to determine the Content-Type header
const consumes: string[] = [
];

return this.httpClient.request<Uint8Array>('get',`${this.basePath}/taskContent/${encodeURIComponent(String(contentId))}/${encodeURIComponent(String(view))}`,
{
withCredentials: this.configuration.withCredentials,
headers: headers,
observe: observe,
reportProgress: reportProgress
}
);
}
public taskContent2GetSingle(view: string, contentId: string, observe?: 'body', reportProgress?: boolean): Observable<Blob>;
public taskContent2GetSingle(view: string, contentId: string, observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<Blob>>;
public taskContent2GetSingle(view: string, contentId: string, observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<Blob>>;
public taskContent2GetSingle(view: string, contentId: string, observe: any = 'body', reportProgress: boolean = false ): Observable<any> {

if (view === null || view === undefined) {
throw new Error('Required parameter view was null or undefined when calling taskContent2GetSingle.');
}

if (contentId === null || contentId === undefined) {
throw new Error('Required parameter contentId was null or undefined when calling taskContent2GetSingle.');
}

let headers = this.defaultHeaders;

// authentication (ksi) required
if (this.configuration.accessToken) {
const accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers = headers.set('Authorization', 'Bearer ' + accessToken);
}

// to determine the Accept header
let httpHeaderAccepts: string[] = [
'application/json'
];
const httpHeaderAcceptSelected: string | undefined = this.configuration.selectHeaderAccept(httpHeaderAccepts);
if (httpHeaderAcceptSelected != undefined) {
headers = headers.set('Accept', httpHeaderAcceptSelected);
}

// to determine the Content-Type header
const consumes: string[] = [
];

return this.httpClient.request<Blob>('get',`${this.basePath}/taskContent/${String(contentId)}/${String(view)}`,
{
//@ts-ignore
responseType: "blob",
withCredentials: this.configuration.withCredentials,
headers: headers,
observe: observe,
reportProgress: reportProgress
}
);
}

/**
*
Expand Down Expand Up @@ -5221,4 +5225,4 @@ export class DefaultService {
);
}

}
}
84 changes: 57 additions & 27 deletions src/app/services/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { BackendService, YearsService, UserService } from '../shared';
import { combineLatest, merge, Observable, of, Subject } from 'rxjs';
import { filter, map, mapTo, mergeMap, shareReplay, take, tap } from 'rxjs/operators';
Expand Down Expand Up @@ -38,13 +39,20 @@ export class TasksService {
private readonly taskUpdatesSubject: Subject<TaskWithIcon> = new Subject<TaskWithIcon>();
private readonly taskUpdates$ = this.taskUpdatesSubject.asObservable();

constructor(private backend: BackendService, private year: YearsService, private userService: UserService) {
constructor(
private backend: BackendService,
private year: YearsService,
private userService: UserService,
private sanitizer: DomSanitizer,
) {
this.tasks$ = merge(
backend.user$.pipe(map(() => this.year.selected)),
year.selected$
).pipe(
mergeMap((year) => this.backend.http.tasksGetAll(year?.id)),
map((response) => response.tasks.map((task) => TasksService.taskAddIcon(task))),
mergeMap((response) =>
combineLatest(response.tasks.map((task) => this.taskAddIcon(task))),
),
tap((tasks) => {
tasks.forEach((task) => this.updateTask(task));
}),
Expand Down Expand Up @@ -156,7 +164,8 @@ export class TasksService {
}

return this.backend.http.tasksGetSingle(taskId).pipe(
map((response) => TasksService.taskAddIcon(response.task)),
map((response) => this.taskAddIcon(response.task)),
mergeMap((taskWithIcon$) => taskWithIcon$),
tap((task) => this.updateTask(task, publishUpdate)),
take(1),
);
Expand All @@ -168,26 +177,32 @@ export class TasksService {
* @param publishUpdate if set to false then no task update subscribers are notified upon update
*/
public updateTask(task: Task, publishUpdate = true): void {
environment.logger.debug(`[TASK] updating task cache for task ${task.id}, publish update: ${publishUpdate}`);
const taskWithIcon = TasksService.taskAddIcon(task);

/*
Empty space in the cache
*/
const cachedKeys = Object.keys(this.cache);
let firstKey;
while (cachedKeys.length >= TasksService.CACHE_MAX_SIZE && (firstKey = cachedKeys.shift())) {
const key = Number(firstKey);
delete this.cache[key];
}
environment.logger.debug(
`[TASK] updating task cache for task ${task.id}, publish update: ${publishUpdate}`,
);

/*
Save the task into cache
*/
this.cache[taskWithIcon.id] = taskWithIcon;
if (publishUpdate) {
this.taskUpdatesSubject.next(taskWithIcon);
}
this.taskAddIcon(task).subscribe((taskWithIcon) => {
/*
Empty space in the cache
*/
const cachedKeys = Object.keys(this.cache);
let firstKey;
while (
cachedKeys.length >= TasksService.CACHE_MAX_SIZE &&
(firstKey = cachedKeys.shift())
) {
const key = Number(firstKey);
delete this.cache[key];
}

/*
Save the task into cache
*/
this.cache[taskWithIcon.id] = taskWithIcon;
if (publishUpdate) {
this.taskUpdatesSubject.next(taskWithIcon);
}
});
}

/**
Expand Down Expand Up @@ -236,6 +251,7 @@ export class TasksService {
mapTo(true));
}


public static sortTasks(tasks: TaskWithIcon[], lockedLast: boolean): TaskWithIcon[] {
const flatLevels = Utils.flatArray(TasksService.splitToLevels(tasks));
if (!lockedLast) {
Expand Down Expand Up @@ -302,10 +318,24 @@ export class TasksService {
return r;
}

private static taskAddIcon(task: Task): TaskWithIcon {
return {
...task,
icon: Utils.getTaskIconURL(task),
};

private taskAddIcon(task: Task): Observable<TaskWithIcon> {
return this.backend.http
.taskContent2GetSingle(
`icon/${task.state}${task.picture_suffix}`,
task.id.toString(),
)
.pipe(
mergeMap((icon) => {
const url = URL.createObjectURL(icon);
// icon is safe
const safeUrl = this.sanitizer.bypassSecurityTrustUrl(url);

return of({
...task,
icon: safeUrl as string,
});
}),
);
}
}
4 changes: 0 additions & 4 deletions src/app/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ export class Utils {
return r;
}

public static getTaskIconURL(task: Task): string {
return Utils.fixUrl(`${environment.backend}${task.picture_base}/${task.state}${task.picture_suffix}`);
}

public static fixUrl(url: string): string {
const replace = (x: string): string => x.replace(/([^:])\/\//g, '$1/');
return replace(replace(url));
Expand Down