Skip to content

Clean up realtime docs when they're no longer needed #3199

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
42 changes: 31 additions & 11 deletions src/SIL.XForge.Scripture/ClientApp/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ExternalUrlService } from 'xforge-common/external-url.service';
import { FileService } from 'xforge-common/file.service';
import { I18nService } from 'xforge-common/i18n.service';
import { LocationService } from 'xforge-common/location.service';
import { FETCH_WITHOUT_SUBSCRIBE } from 'xforge-common/models/realtime-doc';
import { UserDoc } from 'xforge-common/models/user-doc';
import { NoticeService } from 'xforge-common/notice.service';
import { OnlineStatusService } from 'xforge-common/online-status.service';
Expand Down Expand Up @@ -678,11 +679,12 @@ class TestEnvironment {
this.addProjectUserConfig('project01', 'user03');
this.addProjectUserConfig('project01', 'user04');

when(mockedSFProjectService.getProfile(anything())).thenCall(projectId =>
this.realtimeService.subscribe(SFProjectProfileDoc.COLLECTION, projectId)
when(mockedSFProjectService.subscribeProfile(anything(), anything())).thenCall((projectId, subscription) =>
this.realtimeService.subscribe(SFProjectProfileDoc.COLLECTION, projectId, subscription)
);
when(mockedSFProjectService.getUserConfig(anything(), anything())).thenCall((projectId, userId) =>
this.realtimeService.subscribe(SFProjectUserConfigDoc.COLLECTION, `${projectId}:${userId}`)
when(mockedSFProjectService.getUserConfig(anything(), anything(), anything())).thenCall(
(projectId, userId, subscriber) =>
this.realtimeService.subscribe(SFProjectUserConfigDoc.COLLECTION, `${projectId}:${userId}`, subscriber)
);
when(mockedLocationService.pathname).thenReturn('/projects/project01/checking');

Expand Down Expand Up @@ -793,12 +795,14 @@ class TestEnvironment {
}

get currentUserDoc(): UserDoc {
return this.realtimeService.get(UserDoc.COLLECTION, 'user01');
return this.realtimeService.get(UserDoc.COLLECTION, 'user01', FETCH_WITHOUT_SUBSCRIBE);
}

setCurrentUser(userId: string): void {
when(mockedUserService.currentUserId).thenReturn(userId);
when(mockedUserService.getCurrentUser()).thenCall(() => this.realtimeService.subscribe(UserDoc.COLLECTION, userId));
when(mockedUserService.subscribeCurrentUser(anything())).thenCall(subscriber =>
this.realtimeService.subscribe(UserDoc.COLLECTION, userId, subscriber)
);
}

triggerLogin(): void {
Expand Down Expand Up @@ -866,33 +870,49 @@ class TestEnvironment {
when(mockedUserService.currentProjectId(anything())).thenReturn(undefined);
}
this.ngZone.run(() => {
const projectDoc = this.realtimeService.get(SFProjectProfileDoc.COLLECTION, projectId);
const projectDoc = this.realtimeService.get(SFProjectProfileDoc.COLLECTION, projectId, FETCH_WITHOUT_SUBSCRIBE);
projectDoc.delete();
});
this.wait();
}

removeUserFromProject(projectId: string): void {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, projectId);
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
projectId,
FETCH_WITHOUT_SUBSCRIBE
);
projectDoc.submitJson0Op(op => op.unset<string>(p => p.userRoles['user01']), false);
this.wait();
}

updatePreTranslate(projectId: string): void {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, projectId);
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
projectId,
FETCH_WITHOUT_SUBSCRIBE
);
projectDoc.submitJson0Op(op => op.set<boolean>(p => p.translateConfig.preTranslate, true), false);
this.wait();
}

addUserToProject(projectId: string): void {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, projectId);
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
projectId,
FETCH_WITHOUT_SUBSCRIBE
);
projectDoc.submitJson0Op(op => op.set<string>(p => p.userRoles['user01'], SFProjectRole.CommunityChecker), false);
this.currentUserDoc.submitJson0Op(op => op.add<string>(u => u.sites['sf'].projects, 'project04'), false);
this.wait();
}

changeUserRole(projectId: string, userId: string, role: SFProjectRole): void {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, projectId);
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
projectId,
FETCH_WITHOUT_SUBSCRIBE
);
projectDoc.submitJson0Op(op => op.set<string>(p => p.userRoles[userId], role), false);
this.wait();
}
Expand Down
12 changes: 8 additions & 4 deletions src/SIL.XForge.Scripture/ClientApp/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BreakpointObserver, BreakpointState } from '@angular/cdk/layout';
import { Component, DestroyRef, OnDestroy, OnInit } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { NavigationEnd, Router } from '@angular/router';
import Bugsnag from '@bugsnag/js';
import { translate } from '@ngneat/transloco';
Expand All @@ -23,6 +24,7 @@ import { FileService } from 'xforge-common/file.service';
import { I18nService } from 'xforge-common/i18n.service';
import { LocationService } from 'xforge-common/location.service';
import { Breakpoint, MediaBreakpointService } from 'xforge-common/media-breakpoints/media-breakpoint.service';
import { DocSubscription } from 'xforge-common/models/realtime-doc';
import { UserDoc } from 'xforge-common/models/user-doc';
import { NoticeService } from 'xforge-common/notice.service';
import { OnlineStatusService } from 'xforge-common/online-status.service';
Expand All @@ -31,11 +33,10 @@ import {
BrowserIssue,
SupportedBrowsersDialogComponent
} from 'xforge-common/supported-browsers-dialog/supported-browsers-dialog.component';
import { ThemeService } from 'xforge-common/theme.service';
import { UserService } from 'xforge-common/user.service';
import { quietTakeUntilDestroyed } from 'xforge-common/util/rxjs-util';
import { issuesEmailTemplate, supportedBrowser } from 'xforge-common/utils';
import { ThemeService } from 'xforge-common/theme.service';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import versionData from '../../../version.json';
import { environment } from '../environments/environment';
import { SFProjectProfileDoc } from './core/models/sf-project-profile-doc';
Expand Down Expand Up @@ -241,7 +242,9 @@ export class AppComponent extends DataLoadingComponent implements OnInit, OnDest
this.themeService.setDarkMode(enabled);
});
this.loadingStarted();
this.currentUserDoc = await this.userService.getCurrentUser();
this.currentUserDoc = await this.userService.subscribeCurrentUser(
new DocSubscription('AppComponent', this.destroyRef)
);
const userData: User | undefined = cloneDeep(this.currentUserDoc.data);
if (userData != null) {
const userDataWithId = { ...userData, id: this.currentUserDoc.id };
Expand Down Expand Up @@ -280,7 +283,8 @@ export class AppComponent extends DataLoadingComponent implements OnInit, OnDest
this.userService.setCurrentProjectId(this.currentUserDoc!, this._selectedProjectDoc.id);
this.projectUserConfigDoc = await this.projectService.getUserConfig(
this._selectedProjectDoc.id,
this.currentUserDoc!.id
this.currentUserDoc!.id,
new DocSubscription('AppComponent', this.destroyRef)
);
if (this.selectedProjectDeleteSub != null) {
this.selectedProjectDeleteSub.unsubscribe();
Expand Down
16 changes: 14 additions & 2 deletions src/SIL.XForge.Scripture/ClientApp/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OverlayContainer } from '@angular/cdk/overlay';
import { DatePipe } from '@angular/common';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { APP_ID, ErrorHandler, NgModule } from '@angular/core';
import { APP_ID, APP_INITIALIZER, ErrorHandler, NgModule } from '@angular/core';
import { MatRipple } from '@angular/material/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ServiceWorkerModule } from '@angular/service-worker';
Expand Down Expand Up @@ -37,13 +37,19 @@ import { ProjectComponent } from './project/project.component';
import { ScriptureChooserDialogComponent } from './scripture-chooser-dialog/scripture-chooser-dialog.component';
import { DeleteProjectDialogComponent } from './settings/delete-project-dialog/delete-project-dialog.component';
import { SettingsComponent } from './settings/settings.component';
import { CacheService } from './shared/cache-service/cache.service';
import { GlobalNoticesComponent } from './shared/global-notices/global-notices.component';
import { SharedModule } from './shared/shared.module';
import { TextNoteDialogComponent } from './shared/text/text-note-dialog/text-note-dialog.component';
import { SyncComponent } from './sync/sync.component';
import { TranslateModule } from './translate/translate.module';
import { UsersModule } from './users/users.module';

/** Initialization function for any services that need to be run but are not depended on by any component. */
function initializeGlobalServicesFactor(_cacheService: CacheService): () => Promise<any> {
return () => Promise.resolve();
}

@NgModule({
declarations: [
AppComponent,
Expand Down Expand Up @@ -93,7 +99,13 @@ import { UsersModule } from './users/users.module';
defaultTranslocoMarkupTranspilers(),
{ provide: ErrorHandler, useClass: ExceptionHandlingService },
{ provide: OverlayContainer, useClass: InAppRootOverlayContainer },
provideHttpClient(withInterceptorsFromDi())
provideHttpClient(withInterceptorsFromDi()),
{
provide: APP_INITIALIZER,
useFactory: initializeGlobalServicesFactor,
deps: [CacheService],
multi: true
}
]
})
export class AppModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { TextInfo } from 'realtime-server/lib/esm/scriptureforge/models/text-inf
import { of } from 'rxjs';
import { anything, mock, resetCalls, verify, when } from 'ts-mockito';
import { DialogService } from 'xforge-common/dialog.service';
import { FETCH_WITHOUT_SUBSCRIBE } from 'xforge-common/models/realtime-doc';
import { NoticeService } from 'xforge-common/notice.service';
import { OnlineStatusService } from 'xforge-common/online-status.service';
import { noopDestroyRef } from 'xforge-common/realtime.service';
Expand Down Expand Up @@ -408,7 +409,8 @@ describe('CheckingOverviewComponent', () => {
const env = new TestEnvironment();
const questionDoc: QuestionDoc = env.realtimeService.get(
QuestionDoc.COLLECTION,
getQuestionDocId('project01', 'q7Id')
getQuestionDocId('project01', 'q7Id'),
FETCH_WITHOUT_SUBSCRIBE
);
await questionDoc.submitJson0Op(op => {
op.set(d => d.isArchived, false);
Expand Down Expand Up @@ -916,18 +918,26 @@ class TestEnvironment {
when(mockedActivatedRoute.params).thenReturn(of({ projectId: 'project01' }));
when(mockedQuestionDialogService.questionDialog(anything())).thenResolve();
when(mockedDialogService.confirm(anything(), anything())).thenResolve(true);
when(mockedProjectService.getProfile(anything())).thenCall(id =>
this.realtimeService.subscribe(SFProjectProfileDoc.COLLECTION, id)
when(mockedProjectService.subscribeProfile(anything(), anything())).thenCall((id, subscription) =>
this.realtimeService.subscribe(SFProjectProfileDoc.COLLECTION, id, subscription)
);
when(mockedProjectService.getUserConfig(anything(), anything())).thenCall((id, userId) =>
this.realtimeService.subscribe(SFProjectUserConfigDoc.COLLECTION, getSFProjectUserConfigDocId(id, userId))
when(mockedProjectService.getUserConfig(anything(), anything(), anything())).thenCall((id, userId, subscriber) =>
this.realtimeService.subscribe(
SFProjectUserConfigDoc.COLLECTION,
getSFProjectUserConfigDocId(id, userId),
subscriber
)
);
when(mockedQuestionsService.queryQuestions('project01', anything(), anything())).thenCall(() =>
this.realtimeService.subscribeQuery(QuestionDoc.COLLECTION, {}, noopDestroyRef)
);
when(mockedProjectService.onlineDeleteAudioTimingData(anything(), anything(), anything())).thenCall(
(projectId, book, chapter) => {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, projectId);
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
projectId,
FETCH_WITHOUT_SUBSCRIBE
);
const textIndex: number = projectDoc.data!.texts.findIndex(t => t.bookNum === book);
const chapterIndex: number = projectDoc.data!.texts[textIndex].chapters.findIndex(c => c.number === chapter);
projectDoc.submitJson0Op(op => op.set(p => p.texts[textIndex].chapters[chapterIndex].hasAudio, false), false);
Expand Down Expand Up @@ -1093,7 +1103,11 @@ class TestEnvironment {
}

setSeeOtherUserResponses(isEnabled: boolean): void {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, 'project01');
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
'project01',
FETCH_WITHOUT_SUBSCRIBE
);
projectDoc.submitJson0Op(
op => op.set<boolean>(p => p.checkingConfig.usersSeeEachOthersResponses, isEnabled),
false
Expand All @@ -1103,7 +1117,11 @@ class TestEnvironment {

setCheckingEnabled(isEnabled: boolean): void {
this.ngZone.run(() => {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, 'project01');
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
'project01',
FETCH_WITHOUT_SUBSCRIBE
);
projectDoc.submitJson0Op(op => op.set<boolean>(p => p.checkingConfig.checkingEnabled, isEnabled), false);
});
this.waitForProjectDocChanges();
Expand Down Expand Up @@ -1163,7 +1181,11 @@ class TestEnvironment {
],
permissions: {}
};
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, 'project01');
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
'project01',
FETCH_WITHOUT_SUBSCRIBE
);
const index: number = projectDoc.data!.texts.length - 1;
projectDoc.submitJson0Op(op => op.insert(p => p.texts, index, text), false);
this.addQuestion({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { DataLoadingComponent } from 'xforge-common/data-loading-component';
import { DialogService } from 'xforge-common/dialog.service';
import { I18nService } from 'xforge-common/i18n.service';
import { L10nNumberPipe } from 'xforge-common/l10n-number.pipe';
import { DocSubscription } from 'xforge-common/models/realtime-doc';
import { RealtimeQuery } from 'xforge-common/models/realtime-query';
import { NoticeService } from 'xforge-common/notice.service';
import { OnlineStatusService } from 'xforge-common/online-status.service';
Expand Down Expand Up @@ -178,7 +179,10 @@ export class CheckingOverviewComponent extends DataLoadingComponent implements O
const projectId$ = this.activatedRoute.params.pipe(
tap(params => {
this.loadingStarted();
projectDocPromise = this.projectService.getProfile(params['projectId']);
projectDocPromise = this.projectService.subscribeProfile(
params['projectId'],
new DocSubscription('CheckingOverviewComponent', this.destroyRef)
);
}),
map(params => params['projectId'] as string)
);
Expand All @@ -187,7 +191,11 @@ export class CheckingOverviewComponent extends DataLoadingComponent implements O
this.projectId = projectId;
try {
this.projectDoc = await projectDocPromise;
this.projectUserConfigDoc = await this.projectService.getUserConfig(projectId, this.userService.currentUserId);
this.projectUserConfigDoc = await this.projectService.getUserConfig(
projectId,
this.userService.currentUserId,
new DocSubscription('CheckingOverviewComponent', this.destroyRef)
);
this.questionsQuery?.dispose();
this.questionsQuery = await this.checkingQuestionsService.queryQuestions(
projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { DialogService } from 'xforge-common/dialog.service';
import { FileService } from 'xforge-common/file.service';
import { I18nService } from 'xforge-common/i18n.service';
import { FileType } from 'xforge-common/models/file-offline-data';
import { DocSubscription } from 'xforge-common/models/realtime-doc';
import { NoticeService } from 'xforge-common/notice.service';
import { OnlineStatusService } from 'xforge-common/online-status.service';
import { UserService } from 'xforge-common/user.service';
Expand Down Expand Up @@ -489,7 +490,9 @@ export class CheckingAnswersComponent implements OnInit {

async submit(response: CheckingInput): Promise<void> {
this.submittingAnswer = true;
const userDoc = await this.userService.getCurrentUser();
const userDoc = await this.userService.subscribeCurrentUser(
new DocSubscription('CheckingAnswersComponent', this.destroyRef)
);
if (this.onlineStatusService.isOnline && userDoc.data?.isDisplayNameConfirmed !== true) {
await this.userService.editDisplayName(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { expect, within } from '@storybook/test';
import { createTestUserProfile } from 'realtime-server/lib/esm/common/models/user-test-data';
import { Comment } from 'realtime-server/lib/esm/scriptureforge/models/comment';
import { createTestProject } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-test-data';
import { instance, mock, when } from 'ts-mockito';
import { anything, instance, mock, when } from 'ts-mockito';
import { DialogService } from 'xforge-common/dialog.service';
import { I18nStoryModule } from 'xforge-common/i18n-story.module';
import { UserProfileDoc } from 'xforge-common/models/user-profile-doc';
Expand All @@ -17,11 +17,11 @@ import { CheckingCommentsComponent } from './checking-comments.component';
const mockedDialogService = mock(DialogService);
const mockedUserService = mock(UserService);
when(mockedUserService.currentUserId).thenReturn('user01');
when(mockedUserService.getProfile('user01')).thenResolve({
when(mockedUserService.subscribeProfile('user01', anything())).thenResolve({
id: 'user01',
data: createTestUserProfile({}, 1)
} as UserProfileDoc);
when(mockedUserService.getProfile('user02')).thenResolve({
when(mockedUserService.subscribeProfile('user02', anything())).thenResolve({
id: 'user02',
data: createTestUserProfile({}, 2)
} as UserProfileDoc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { VerseRefData } from 'realtime-server/lib/esm/scriptureforge/models/vers
import { Subject } from 'rxjs';
import { FileService } from 'xforge-common/file.service';
import { FileType } from 'xforge-common/models/file-offline-data';
import { DocSubscriberInfo } from 'xforge-common/models/realtime-doc';
import { RealtimeQuery } from 'xforge-common/models/realtime-query';
import { ComparisonOperator, PropertyFilter, QueryParameters, Sort } from 'xforge-common/query-parameters';
import { RealtimeService } from 'xforge-common/realtime.service';
Expand Down Expand Up @@ -180,6 +181,7 @@ export class CheckingQuestionsService {
async createQuestion(
id: string,
question: Question,
subscriber: DocSubscriberInfo,
audioFileName?: string,
audioBlob?: Blob
): Promise<QuestionDoc | undefined> {
Expand Down Expand Up @@ -210,7 +212,7 @@ export class CheckingQuestionsService {
});

return this.realtimeService
.create<QuestionDoc>(QuestionDoc.COLLECTION, docId, question)
.create<QuestionDoc>(QuestionDoc.COLLECTION, docId, question, subscriber)
.then((questionDoc: QuestionDoc) => {
this.afterQuestionCreated$.next(questionDoc);
return questionDoc;
Expand Down
Loading
Loading