2023-07-27 08:31:52 +03:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { In, IsNull } from 'typeorm';
|
|
|
|
import { Feed } from 'feed';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 08:28:29 +03:00
|
|
|
import type { DriveFilesRepository, NotesRepository, UserProfilesRepository } from '@/models/_.js';
|
2022-09-20 23:33:11 +03:00
|
|
|
import type { Config } from '@/config.js';
|
2023-09-20 05:33:36 +03:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.js';
|
2022-12-04 08:03:09 +02:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class FeedService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private driveFileEntityService: DriveFileEntityService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2023-08-16 11:51:28 +03:00
|
|
|
public async packFeed(user: MiUser) {
|
2022-09-17 21:27:08 +03:00
|
|
|
const author = {
|
|
|
|
link: `${this.config.url}/@${user.username}`,
|
|
|
|
name: user.name ?? user.username,
|
|
|
|
};
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id });
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
const notes = await this.notesRepository.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
renoteId: IsNull(),
|
|
|
|
visibility: In(['public', 'home']),
|
|
|
|
},
|
|
|
|
order: { createdAt: -1 },
|
|
|
|
take: 20,
|
|
|
|
});
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
const feed = new Feed({
|
|
|
|
id: author.link,
|
|
|
|
title: `${author.name} (@${user.username}@${this.config.host})`,
|
|
|
|
updated: notes[0].createdAt,
|
|
|
|
generator: 'Misskey',
|
|
|
|
description: `${user.notesCount} Notes, ${profile.ffVisibility === 'public' ? user.followingCount : '?'} Following, ${profile.ffVisibility === 'public' ? user.followersCount : '?'} Followers${profile.description ? ` · ${profile.description}` : ''}`,
|
|
|
|
link: author.link,
|
2023-04-06 13:48:24 +03:00
|
|
|
image: user.avatarUrl ?? this.userEntityService.getIdenticonUrl(user),
|
2022-09-17 21:27:08 +03:00
|
|
|
feedLinks: {
|
|
|
|
json: `${author.link}.json`,
|
|
|
|
atom: `${author.link}.atom`,
|
|
|
|
},
|
|
|
|
author,
|
|
|
|
copyright: user.name ?? user.username,
|
|
|
|
});
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
for (const note of notes) {
|
|
|
|
const files = note.fileIds.length > 0 ? await this.driveFilesRepository.findBy({
|
|
|
|
id: In(note.fileIds),
|
|
|
|
}) : [];
|
|
|
|
const file = files.find(file => file.type.startsWith('image/'));
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
feed.addItem({
|
|
|
|
title: `New note by ${author.name}`,
|
|
|
|
link: `${this.config.url}/notes/${note.id}`,
|
|
|
|
date: note.createdAt,
|
|
|
|
description: note.cw ?? undefined,
|
|
|
|
content: note.text ?? undefined,
|
|
|
|
image: file ? this.driveFileEntityService.getPublicUrl(file) ?? undefined : undefined,
|
|
|
|
});
|
|
|
|
}
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
return feed;
|
|
|
|
}
|
|
|
|
}
|