Skip to content

Commit

Permalink
Fix: Use pagination when fetching Mastodon blocks (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage authored Sep 26, 2023
1 parent 9472649 commit e7cced8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/app/services/mastodon-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Injectable } from '@angular/core';
import {Observable} from "rxjs";
import {Injectable} from '@angular/core';
import {EMPTY, expand, Observable, reduce} from "rxjs";
import {HttpClient} from "@angular/common/http";
import {AccessTokenResponse} from "../response/access-token.response";
import {MastodonBlacklistItem, MastodonBlacklistResponse} from "../response/mastodon-blacklist.response";
import {MastodonDomainBlacklistRequest} from "../types/mastodon-domain-blacklist-request";
import {MastodonLinkParserService} from "./mastodon-link-parser.service";

export interface GetTokenOptions {
code: string;
Expand All @@ -19,6 +20,7 @@ export interface GetTokenOptions {
export class MastodonApiService {
constructor(
private readonly httpClient: HttpClient,
private readonly mastodonLinkParser: MastodonLinkParserService,
) {
}

Expand All @@ -42,7 +44,27 @@ export class MastodonApiService {
headers: {
Authorization: `Bearer ${token}`,
},
});
observe: 'response'
}).pipe(
expand(response => {
if (response.headers.has('Link')) {
const links = this.mastodonLinkParser.getLinks(response.headers.get('Link')!);
if (links.next === undefined) {
return EMPTY;
}

return this.httpClient.get<MastodonBlacklistResponse>(links.next, {
headers: {
Authorization: `Bearer ${token}`,
},
observe: 'response',
});
}

return EMPTY;
}),
reduce((acc, value) => acc.concat(value.body!), <MastodonBlacklistResponse>[]),
)
}

public blacklistInstance(
Expand Down
26 changes: 26 additions & 0 deletions src/app/services/mastodon-link-parser.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {Injectable} from '@angular/core';

interface Links {
next?: string;
prev?: string;
}

@Injectable({
providedIn: 'root'
})
export class MastodonLinkParserService {
public getLinks(rawHeader: string): Links {
const result: Links = {};

const links = rawHeader.split(", ");
for (const link of links) {
const parts = link.split("; ");
const url = parts[0].substring(1, parts[0].length - 1);
const relation: 'next' | 'prev' = <'next' | 'prev'>parts[1].substring('rel="'.length, parts[1].length - 1);

result[relation] = url;
}

return result;
}
}

0 comments on commit e7cced8

Please sign in to comment.