2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-08-16 17:51:28 +09:00
|
|
|
import type { NotesRepository, UsersRepository, PollsRepository, PollVotesRepository, MiUser } from '@/models/index.js';
|
|
|
|
import type { MiNote } from '@/models/entities/Note.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { RelayService } from '@/core/RelayService.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2022-12-04 10:16:03 +09:00
|
|
|
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { ApDeliverManagerService } from '@/core/activitypub/ApDeliverManagerService.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-02-04 12:40:40 +09:00
|
|
|
import { UserBlockingService } from '@/core/UserBlockingService.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class PollService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.pollsRepository)
|
|
|
|
private pollsRepository: PollsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.pollVotesRepository)
|
|
|
|
private pollVotesRepository: PollVotesRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private idService: IdService,
|
|
|
|
private relayService: RelayService,
|
2023-02-04 10:02:03 +09:00
|
|
|
private globalEventService: GlobalEventService,
|
2023-02-04 12:40:40 +09:00
|
|
|
private userBlockingService: UserBlockingService,
|
2022-09-18 03:27:08 +09:00
|
|
|
private apRendererService: ApRendererService,
|
|
|
|
private apDeliverManagerService: ApDeliverManagerService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2023-08-16 17:51:28 +09:00
|
|
|
public async vote(user: MiUser, note: MiNote, choice: number) {
|
2022-09-18 03:27:08 +09:00
|
|
|
const poll = await this.pollsRepository.findOneBy({ noteId: note.id });
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (poll == null) throw new Error('poll not found');
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Check whether is valid choice
|
|
|
|
if (poll.choices[choice] == null) throw new Error('invalid choice param');
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Check blocking
|
|
|
|
if (note.userId !== user.id) {
|
2023-02-04 12:40:40 +09:00
|
|
|
const blocked = await this.userBlockingService.checkBlocked(note.userId, user.id);
|
|
|
|
if (blocked) {
|
2022-09-18 03:27:08 +09:00
|
|
|
throw new Error('blocked');
|
|
|
|
}
|
|
|
|
}
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// if already voted
|
|
|
|
const exist = await this.pollVotesRepository.findBy({
|
|
|
|
noteId: note.id,
|
|
|
|
userId: user.id,
|
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (poll.multiple) {
|
|
|
|
if (exist.some(x => x.choice === choice)) {
|
|
|
|
throw new Error('already voted');
|
|
|
|
}
|
|
|
|
} else if (exist.length !== 0) {
|
|
|
|
throw new Error('already voted');
|
|
|
|
}
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Create vote
|
|
|
|
await this.pollVotesRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
noteId: note.id,
|
|
|
|
userId: user.id,
|
|
|
|
choice: choice,
|
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Increment votes count
|
|
|
|
const index = choice + 1; // In SQL, array index is 1 based
|
|
|
|
await this.pollsRepository.query(`UPDATE poll SET votes[${index}] = votes[${index}] + 1 WHERE "noteId" = '${poll.noteId}'`);
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2023-02-04 10:02:03 +09:00
|
|
|
this.globalEventService.publishNoteStream(note.id, 'pollVoted', {
|
2022-09-18 03:27:08 +09:00
|
|
|
choice: choice,
|
|
|
|
userId: user.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2023-08-16 17:51:28 +09:00
|
|
|
public async deliverQuestionUpdate(noteId: MiNote['id']) {
|
2022-09-18 03:27:08 +09:00
|
|
|
const note = await this.notesRepository.findOneBy({ id: noteId });
|
|
|
|
if (note == null) throw new Error('note not found');
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const user = await this.usersRepository.findOneBy({ id: note.userId });
|
|
|
|
if (user == null) throw new Error('note not found');
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (this.userEntityService.isLocalUser(user)) {
|
2023-02-12 18:47:30 +09:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderUpdate(await this.apRendererService.renderNote(note, false), user));
|
2022-09-18 03:27:08 +09:00
|
|
|
this.apDeliverManagerService.deliverToFollowers(user, content);
|
|
|
|
this.relayService.deliverToRelays(user, content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|