]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Try rewriting with httpclient
authorshamoon <4887959+shamoon@users.noreply.github.com>
Sat, 26 Apr 2025 02:38:43 +0000 (19:38 -0700)
committershamoon <4887959+shamoon@users.noreply.github.com>
Wed, 2 Jul 2025 18:03:54 +0000 (11:03 -0700)
src-ui/src/app/interceptors/csrf.interceptor.ts
src-ui/src/app/services/chat.service.ts
src-ui/src/app/services/csrf.service.ts [deleted file]
src-ui/src/main.ts

index 0414f4433729a268a64a27077423543a5657ca5f..03a2fa7b3cf302a389d12121bb61c3887d585af4 100644 (file)
@@ -5,18 +5,24 @@ import {
   HttpRequest,
 } from '@angular/common/http'
 import { inject, Injectable } from '@angular/core'
+import { Meta } from '@angular/platform-browser'
+import { CookieService } from 'ngx-cookie-service'
 import { Observable } from 'rxjs'
-import { CsrfService } from '../services/csrf.service'
 
 @Injectable()
 export class CsrfInterceptor implements HttpInterceptor {
-  private csrfService = inject(CsrfService)
+  private cookieService: CookieService = inject(CookieService)
+  private meta: Meta = inject(Meta)
 
   intercept(
     request: HttpRequest<unknown>,
     next: HttpHandler
   ): Observable<HttpEvent<unknown>> {
-    const csrfToken = this.csrfService.getToken()
+    let prefix = ''
+    if (this.meta.getTag('name=cookie_prefix')) {
+      prefix = this.meta.getTag('name=cookie_prefix').content
+    }
+    let csrfToken = this.cookieService.get(`${prefix}csrftoken`)
     if (csrfToken) {
       request = request.clone({
         setHeaders: {
index b32efbc3955e284d7571284399e928c1fa1b6938..5e576ceba3f869f5150a128e6a4d6218a1433cd7 100644 (file)
@@ -1,7 +1,11 @@
+import {
+  HttpClient,
+  HttpDownloadProgressEvent,
+  HttpEventType,
+} from '@angular/common/http'
 import { Injectable } from '@angular/core'
-import { Observable } from 'rxjs'
+import { filter, map, Observable } from 'rxjs'
 import { environment } from 'src/environments/environment'
-import { CsrfService } from './csrf.service'
 
 export interface ChatMessage {
   role: 'user' | 'assistant'
@@ -13,48 +17,31 @@ export interface ChatMessage {
   providedIn: 'root',
 })
 export class ChatService {
-  constructor(private csrfService: CsrfService) {}
+  constructor(private http: HttpClient) {}
 
   streamChat(documentId: number, prompt: string): Observable<string> {
-    return new Observable<string>((observer) => {
-      const url = `${environment.apiBaseUrl}documents/chat/`
-      const xhr = new XMLHttpRequest()
-      let lastLength = 0
-
-      xhr.open('POST', url)
-      xhr.setRequestHeader('Content-Type', 'application/json')
-
-      xhr.withCredentials = true
-      let csrfToken = this.csrfService.getToken()
-      if (csrfToken) {
-        xhr.setRequestHeader('X-CSRFToken', csrfToken)
-      }
-
-      xhr.onreadystatechange = () => {
-        if (xhr.readyState === 3 || xhr.readyState === 4) {
-          const partial = xhr.responseText.slice(lastLength)
-          lastLength = xhr.responseText.length
-
-          if (partial) {
-            observer.next(partial)
-          }
+    // use httpclient as we have withFetch
+    return this.http
+      .post(
+        `${environment.apiBaseUrl}documents/chat/`,
+        {
+          document_id: documentId,
+          q: prompt,
+        },
+        {
+          observe: 'events',
+          reportProgress: true,
+          responseType: 'text',
+          withCredentials: true,
         }
-
-        if (xhr.readyState === 4) {
-          observer.complete()
-        }
-      }
-
-      xhr.onerror = () => {
-        observer.error(new Error('Streaming request failed.'))
-      }
-
-      const body = JSON.stringify({
-        document_id: documentId,
-        q: prompt,
-      })
-
-      xhr.send(body)
-    })
+      )
+      .pipe(
+        map((event) => {
+          if (event.type === HttpEventType.DownloadProgress) {
+            return (event as HttpDownloadProgressEvent).partialText!
+          }
+        }),
+        filter((chunk) => !!chunk)
+      )
   }
 }
diff --git a/src-ui/src/app/services/csrf.service.ts b/src-ui/src/app/services/csrf.service.ts
deleted file mode 100644 (file)
index 28f5094..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-import { Injectable } from '@angular/core'
-import { Meta } from '@angular/platform-browser'
-import { CookieService } from 'ngx-cookie-service' // Assuming you're using this
-
-@Injectable({ providedIn: 'root' })
-export class CsrfService {
-  constructor(
-    private cookieService: CookieService,
-    private meta: Meta
-  ) {}
-
-  public getCookiePrefix(): string {
-    let prefix = ''
-    if (this.meta.getTag('name=cookie_prefix')) {
-      prefix = this.meta.getTag('name=cookie_prefix').content
-    }
-    return prefix
-  }
-
-  public getToken(): string {
-    return this.cookieService.get(`${this.getCookiePrefix()}csrftoken`)
-  }
-}
index a571e1bf724eb5fb6da4b7ef97ae2817fcab34da..556b34fd99742c51e5421c1fbee4e72c796ffa0b 100644 (file)
@@ -9,6 +9,7 @@ import { DatePipe, registerLocaleData } from '@angular/common'
 import {
   HTTP_INTERCEPTORS,
   provideHttpClient,
+  withFetch,
   withInterceptorsFromDi,
 } from '@angular/common/http'
 import { FormsModule, ReactiveFormsModule } from '@angular/forms'
@@ -393,6 +394,6 @@ bootstrapApplication(AppComponent, {
     CorrespondentNamePipe,
     DocumentTypeNamePipe,
     StoragePathNamePipe,
-    provideHttpClient(withInterceptorsFromDi()),
+    provideHttpClient(withInterceptorsFromDi(), withFetch()),
   ],
 }).catch((err) => console.error(err))