Skip to content

Commit

Permalink
Feat: Add synchronization from Mastodon to Fediseer (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage authored Oct 4, 2023
1 parent 2bf95cd commit 343b572
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ <h3>Preview</h3>
[newList]="sourceBlockedInstances"
[newToStringCallback]="lemmyToFediseerSyncNewListCallback"
[purgeMode]="false"
[highlightRemoved]="false"
addedText="This domain is only on your instance blocklist and will be added to your Fediseer censures"
/>
</ng-template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h3 class="card-title">Setup OAuth</h3>
<ng-template #synchronizeForm>
<div class="card">
<div class="card-header">
<h3 class="card-title">Synchronization</h3>
<h3 class="card-title">Synchronization from Fediseer to Mastodon</h3>
</div>
<div class="card-body">
<app-filter-form
Expand Down Expand Up @@ -121,6 +121,44 @@ <h3>Preview</h3>
</div>
</div>
</div>

<div class="card">
<div class="card-header">
<h3 class="card-title">Synchronization from Mastodon to Fediseer</h3>
</div>
<div class="card-body">
<p>
Note that no domains will be removed from Fediseer, only domains that are present on
<strong>{{currentInstance}}</strong> but not here on <strong>Fediseer</strong>
will be added to your Fediseer censure list.
</p>
<form [formGroup]="form" (submit)="synchronizeFromMastodon()">
<button type="submit" class="btn-primary btn">Synchronize</button>
</form>

<div class="row mt-4">
<div class="col-md-12 minimum-height">
<h3>Preview</h3>

<div class="position-relative">
<app-loader *ngIf="loadingPreviewMastodonToFediseer else previewMastodonToFediseer" />
</div>

<ng-template #previewMastodonToFediseer>
<app-blacklist-diff
[originalList]="myCensuredInstances"
[newList]="sourceBlockedInstances"
[newToStringCallback]="mastodonToFediseerSyncNewListCallback"
[purgeMode]="false"
[highlightRemoved]="false"
addedText="This domain is only on your instance blocklist and will be added to your Fediseer censures"
/>
</ng-template>
</div>
</div>
</div>
</div>

<div class="card">
<div class="card-header">
<h3 class="card-title">Original blacklist</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ import {
} from "../../components/filter-form/filter-form.component";
import {InstanceDetailResponse} from "../../../response/instance-detail.response";
import {SynchronizationMode} from "../../../types/synchronization-mode";
import {OriginalToStringCallback} from "../../components/blacklist-diff/blacklist-diff.component";
import {NewToStringCallback, OriginalToStringCallback} from "../../components/blacklist-diff/blacklist-diff.component";
import {NormalizedInstanceDetailResponse} from "../../../response/normalized-instance-detail.response";
import {CachedFediseerApiService} from "../../../services/cached-fediseer-api.service";
import {ApiResponseHelperService} from "../../../services/api-response-helper.service";
import {ApiResponse, FediseerApiService} from "../../../services/fediseer-api.service";
import {SuccessResponse} from "../../../response/success.response";

@Component({
selector: 'app-synchronize-mastodon',
Expand All @@ -27,6 +31,7 @@ import {NormalizedInstanceDetailResponse} from "../../../response/normalized-ins
export class SynchronizeMastodonComponent implements OnInit {
protected readonly MastodonBlacklistSeverity = MastodonBlacklistSeverity;
protected readonly currentInstance = this.authManager.currentInstanceSnapshot.name;
protected readonly mastodonToFediseerSyncNewListCallback: NewToStringCallback<MastodonBlacklistItem> = instance => instance.domain;

private syncSettings: MastodonSynchronizationSettings = this.database.mastodonSynchronizationSettings;

Expand All @@ -51,6 +56,7 @@ export class SynchronizeMastodonComponent implements OnInit {
public loadingPreview: boolean = true;
public purgeMode: boolean | null = null;
public instancesToBanPreview: InstanceDetailResponse[] | null = null;
public myCensuredInstances: string[] = [];

public saveSettingsCallback: SaveSettingsCallback<MastodonSynchronizationSettings> = (database, settings) => {
database.mastodonSynchronizationSettings = settings;
Expand All @@ -59,13 +65,17 @@ export class SynchronizeMastodonComponent implements OnInit {
return database.mastodonSynchronizationSettings;
}
public instanceToStringCallback: OriginalToStringCallback<MastodonBlacklistItem> = instance => instance.domain;
public loadingPreviewMastodonToFediseer: boolean = true;

constructor(
private readonly database: DatabaseService,
private readonly titleService: TitleService,
private readonly authManager: AuthenticationManagerService,
private readonly mastodonApi: MastodonApiService,
private readonly messageService: MessageService,
private readonly cachedFediseerApi: CachedFediseerApiService,
private readonly fediseerApi: FediseerApiService,
private readonly apiResponseHelper: ApiResponseHelperService,
) {
}

Expand Down Expand Up @@ -123,6 +133,15 @@ export class SynchronizeMastodonComponent implements OnInit {
}
this.originallyBlockedInstances = instances;
this.sourceBlockedInstances = instances;

const myCensures = await toPromise(this.cachedFediseerApi.getCensuresByInstances([
this.authManager.currentInstanceSnapshot.name,
], {ttl: 10}));
if (this.apiResponseHelper.handleErrors([myCensures])) {
return;
}
this.myCensuredInstances = myCensures.successResponse!.instances.map(instance => instance.domain);
this.loadingPreviewMastodonToFediseer = false;
}

public async saveOauth(): Promise<void> {
Expand Down Expand Up @@ -263,4 +282,47 @@ export class SynchronizeMastodonComponent implements OnInit {
this.instancesToBanPreview = [...instancesToBan.censured, ...instancesToBan.hesitated];
this.loadingPreview = false;
}

public async synchronizeFromMastodon() {
this.loading = true;

const instances = this.sourceBlockedInstances.filter(
instance => !this.myCensuredInstances.includes(instance.domain),
);

const promises: Promise<ApiResponse<SuccessResponse>>[] = [];
for (const instance of instances) {
let reasons: string[] = [];
if (instance.private_comment) {
reasons.push(instance.private_comment);
}
if (instance.public_comment) {
reasons.push(instance.public_comment);
}

reasons = reasons.join(',').split(',').map(reason => reason.trim().toLowerCase());
promises.push(toPromise(this.fediseerApi.censureInstance(instance.domain, reasons.length ? reasons.join(',') : null)));
}

const responses = await Promise.all(promises);
if (this.apiResponseHelper.handleErrors(responses)) {
this.loading = false;
return;
}

this.cachedFediseerApi.getCensuresByInstances(
[this.authManager.currentInstanceSnapshot.name],
{clear: true, ttl: 10},
).subscribe(response => {
if (!response.success) {
this.messageService.createWarning(`Couldn't fetch new list of your censured instances, please reload the page to get fresh data.`);
return;
}

this.myCensuredInstances = response.successResponse!.instances.map(instance => instance.domain);
});

this.messageService.createSuccess(`Your Mastodon blocklist was successfully synchronized to Fediseer. Please add reasons to the newly imported censures (if you haven't done so on Mastodon) to help your fellow admins.`);
this.loading = false;
}
}

0 comments on commit 343b572

Please sign in to comment.