2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 18:27:08 +00: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 05:28:29 +00:00
|
|
|
import type { DriveFilesRepository, NotesRepository, UserProfilesRepository } from '@/models/_.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { Config } from '@/config.js';
|
2023-09-20 02:33:36 +00:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-10-16 01:45:22 +00:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2022-09-17 18:27:08 +00: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,
|
2023-10-16 01:45:22 +00:00
|
|
|
private idService: IdService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async packFeed(user: MiUser) {
|
2022-09-17 18:27:08 +00:00
|
|
|
const author = {
|
|
|
|
link: `${this.config.url}/@${user.username}`,
|
|
|
|
name: user.name ?? user.username,
|
|
|
|
};
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id });
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const notes = await this.notesRepository.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
renoteId: IsNull(),
|
|
|
|
visibility: In(['public', 'home']),
|
|
|
|
},
|
2023-10-16 01:45:22 +00:00
|
|
|
order: { id: -1 },
|
2022-09-17 18:27:08 +00:00
|
|
|
take: 20,
|
|
|
|
});
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const feed = new Feed({
|
|
|
|
id: author.link,
|
|
|
|
title: `${author.name} (@${user.username}@${this.config.host})`,
|
2023-10-16 01:45:22 +00:00
|
|
|
updated: this.idService.parse(notes[0].id).date,
|
2023-10-17 21:55:53 +00:00
|
|
|
generator: 'Sharkey',
|
2022-09-17 18:27:08 +00:00
|
|
|
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 10:48:24 +00:00
|
|
|
image: user.avatarUrl ?? this.userEntityService.getIdenticonUrl(user),
|
2022-09-17 18:27:08 +00:00
|
|
|
feedLinks: {
|
|
|
|
json: `${author.link}.json`,
|
|
|
|
atom: `${author.link}.atom`,
|
|
|
|
},
|
|
|
|
author,
|
|
|
|
copyright: user.name ?? user.username,
|
|
|
|
});
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2022-09-17 18:27:08 +00: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-07 22:08:16 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
feed.addItem({
|
|
|
|
title: `New note by ${author.name}`,
|
|
|
|
link: `${this.config.url}/notes/${note.id}`,
|
2023-10-16 01:45:22 +00:00
|
|
|
date: this.idService.parse(note.id).date,
|
2022-09-17 18:27:08 +00:00
|
|
|
description: note.cw ?? undefined,
|
|
|
|
content: note.text ?? undefined,
|
2023-10-18 00:54:18 +00:00
|
|
|
image: file ? this.driveFileEntityService.getPublicUrl(file) : undefined,
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
}
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
return feed;
|
|
|
|
}
|
|
|
|
}
|