Skip to content
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

[Feature addition] Added Version history and the ability to get the alt language from the selected page #519

Merged
merged 6 commits into from
Jan 30, 2025
Merged
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
4 changes: 3 additions & 1 deletion apps/api/src/pages/pages.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import { DbModule, DbService } from '@dua-upd/db';
import { hours } from '@dua-upd/utils-common';
import { FeedbackModule } from '@dua-upd/api/feedback';
import { FlowModule } from '@dua-upd/api/flow';
import { BlobStorageModule, BlobStorageService } from '@dua-upd/blob-storage';

@Module({
imports: [
CacheModule.register({ ttl: hours(12) }),
DbModule,
FeedbackModule,
FlowModule.register(),
BlobStorageModule,
],
controllers: [PagesController],
providers: [PagesService, DbService],
providers: [PagesService, DbService, BlobStorageService],
})
export class PagesModule {}
53 changes: 53 additions & 0 deletions apps/api/src/pages/pages.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
MetricsConfig,
PageDocument,
PageMetricsModel,
UrlDocument,
} from '@dua-upd/db';
import {
DbService,
Expand All @@ -27,17 +28,21 @@ import type {
IProject,
PageStatus,
Direction,
UrlHash,
} from '@dua-upd/types-common';
import {
arrayToDictionary,
dateRangeSplit,
parseDateRangeString,
percentChange,
wait,
} from '@dua-upd/utils-common';
import type { InternalSearchTerm } from '@dua-upd/types-common';
import { FeedbackService } from '@dua-upd/api/feedback';
import { compressString, decompressString } from '@dua-upd/node-utils';
import { FlowService } from '@dua-upd/api/flow';
import { BlobStorageService } from '@dua-upd/blob-storage';
import { format } from 'prettier';

@Injectable()
export class PagesService {
Expand All @@ -54,6 +59,7 @@ export class PagesService {
@Inject(CACHE_MANAGER) private cacheManager: Cache,
private feedbackService: FeedbackService,
private flowService: FlowService,
@Inject(BlobStorageService.name) private blob: BlobStorageService,
) {}

async listPages({ projection, populate }): Promise<Page[]> {
Expand Down Expand Up @@ -267,8 +273,39 @@ export class PagesService {
const readability = await this.readabilityModel
.find({ page: new Types.ObjectId(params.id) })
.sort({ date: -1 })
.lean()
.exec();

const urls = await this.db.collections.urls
.find({ page: new Types.ObjectId(params.id) })
.sort({ date: -1 })
.lean()
.exec();

const hash = urls.map((url) => url.hashes).flat();

const promises: Promise<UrlHash>[] = [];

for (const h of hash) {
promises.push(
this.blob.blobModels.urls
.blob(h.hash)
.downloadToString()
.then(async (blob) => ({
hash: h.hash,
date: h.date,
blob: await format(blob, {
parser: 'html',
}),
})),
);
await wait(30);
}

const hashes = (await Promise.all(promises)).sort(
(a, b) => b.date.getTime() - a.date.getTime(),
);

const mostRelevantCommentsAndWords =
await this.feedbackService.getMostRelevantCommentsAndWords({
dateRange: parseDateRangeString(params.dateRange),
Expand All @@ -295,6 +332,20 @@ export class PagesService {
? percentChange(numComments, numPreviousComments)
: null;

const alternateUrl = await this.pageModel.findById(
new Types.ObjectId(params.id),
{
altLangHref: 1,
},
);

const altLangPage = await this.pageModel.findOne(
{ url: alternateUrl.altLangHref },
{ _id: 1 },
);

const alternatePageId = altLangPage._id;

const results = {
...page,
is404: page.is_404,
Expand Down Expand Up @@ -336,6 +387,8 @@ export class PagesService {
mostRelevantCommentsAndWords,
numComments,
numCommentsPercentChange,
hashes,
alternatePageId,
} as PageDetailsData;

await this.cacheManager.set(cacheKey, results);
Expand Down
Loading