2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { NotesRepository, UserNotePiningsRepository, UsersRepository } from '@/models/_.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { IdentifiableError } from '@/misc/identifiable-error.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
import type { MiNote } from '@/models/Note.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { MiUserNotePining } from '@/models/UserNotePining.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { RelayService } from '@/core/RelayService.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { Config } from '@/config.js';
|
2022-12-04 02:16:03 +01:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { ApDeliverManagerService } from '@/core/activitypub/ApDeliverManagerService.js';
|
|
|
|
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-14 10:04:56 +01:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class NotePiningService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userNotePiningsRepository)
|
|
|
|
private userNotePiningsRepository: UserNotePiningsRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private idService: IdService,
|
2023-01-14 10:04:56 +01:00
|
|
|
private roleService: RoleService,
|
2022-09-17 20:27:08 +02:00
|
|
|
private relayService: RelayService,
|
|
|
|
private apDeliverManagerService: ApDeliverManagerService,
|
|
|
|
private apRendererService: ApRendererService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 指定した投稿をピン留めします
|
|
|
|
* @param user
|
|
|
|
* @param noteId
|
|
|
|
*/
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public async addPinned(user: { id: MiUser['id']; host: MiUser['host']; }, noteId: MiNote['id']) {
|
2022-09-17 20:27:08 +02:00
|
|
|
// Fetch pinee
|
|
|
|
const note = await this.notesRepository.findOneBy({
|
|
|
|
id: noteId,
|
|
|
|
userId: user.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (note == null) {
|
|
|
|
throw new IdentifiableError('70c4e51f-5bea-449c-a030-53bee3cce202', 'No such note.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const pinings = await this.userNotePiningsRepository.findBy({ userId: user.id });
|
|
|
|
|
2023-01-15 12:52:53 +01:00
|
|
|
if (pinings.length >= (await this.roleService.getUserPolicies(user.id)).pinLimit) {
|
2022-09-17 20:27:08 +02:00
|
|
|
throw new IdentifiableError('15a018eb-58e5-4da1-93be-330fcc5e4e1a', 'You can not pin notes any more.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pinings.some(pining => pining.noteId === note.id)) {
|
|
|
|
throw new IdentifiableError('23f0cf4e-59a3-4276-a91d-61a5891c1514', 'That note has already been pinned.');
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.userNotePiningsRepository.insert({
|
2023-10-16 03:45:22 +02:00
|
|
|
id: this.idService.gen(),
|
2022-09-17 20:27:08 +02:00
|
|
|
userId: user.id,
|
|
|
|
noteId: note.id,
|
2023-08-16 10:51:28 +02:00
|
|
|
} as MiUserNotePining);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
// Deliver to remote followers
|
2023-12-02 11:32:30 +01:00
|
|
|
if (this.userEntityService.isLocalUser(user) && !note.localOnly && ['public', 'home'].includes(note.visibility)) {
|
2022-09-17 20:27:08 +02:00
|
|
|
this.deliverPinnedChange(user.id, note.id, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 指定した投稿のピン留めを解除します
|
|
|
|
* @param user
|
|
|
|
* @param noteId
|
|
|
|
*/
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public async removePinned(user: { id: MiUser['id']; host: MiUser['host']; }, noteId: MiNote['id']) {
|
2022-09-17 20:27:08 +02:00
|
|
|
// Fetch unpinee
|
|
|
|
const note = await this.notesRepository.findOneBy({
|
|
|
|
id: noteId,
|
|
|
|
userId: user.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (note == null) {
|
|
|
|
throw new IdentifiableError('b302d4cf-c050-400a-bbb3-be208681f40c', 'No such note.');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.userNotePiningsRepository.delete({
|
|
|
|
userId: user.id,
|
|
|
|
noteId: note.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Deliver to remote followers
|
2023-12-02 11:32:30 +01:00
|
|
|
if (this.userEntityService.isLocalUser(user) && !note.localOnly && ['public', 'home'].includes(note.visibility)) {
|
2022-09-17 20:27:08 +02:00
|
|
|
this.deliverPinnedChange(user.id, noteId, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public async deliverPinnedChange(userId: MiUser['id'], noteId: MiNote['id'], isAddition: boolean) {
|
2022-09-17 20:27:08 +02:00
|
|
|
const user = await this.usersRepository.findOneBy({ id: userId });
|
|
|
|
if (user == null) throw new Error('user not found');
|
|
|
|
|
|
|
|
if (!this.userEntityService.isLocalUser(user)) return;
|
|
|
|
|
|
|
|
const target = `${this.config.url}/users/${user.id}/collections/featured`;
|
|
|
|
const item = `${this.config.url}/notes/${noteId}`;
|
2023-02-12 10:47:30 +01:00
|
|
|
const content = this.apRendererService.addContext(isAddition ? this.apRendererService.renderAdd(user, target, item) : this.apRendererService.renderRemove(user, target, item));
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
this.apDeliverManagerService.deliverToFollowers(user, content);
|
|
|
|
this.relayService.deliverToRelays(user, content);
|
|
|
|
}
|
|
|
|
}
|