From dd58a4aa92edcad34605173f7852cd34f9599238 Mon Sep 17 00:00:00 2001 From: Marie Date: Sun, 13 Oct 2024 03:44:07 +0200 Subject: [PATCH 1/6] upd: add ability to refresh poll --- .../activitypub/models/ApQuestionService.ts | 2 +- .../backend/src/server/api/EndpointsModule.ts | 4 + packages/backend/src/server/api/endpoints.ts | 2 + .../api/endpoints/notes/polls/refresh.ts | 98 +++++++++++++++++++ packages/frontend/src/components/MkPoll.vue | 15 ++- packages/misskey-js/etc/misskey-js.api.md | 4 + .../misskey-js/src/autogen/apiClientJSDoc.ts | 11 +++ packages/misskey-js/src/autogen/endpoint.ts | 3 + packages/misskey-js/src/autogen/entities.ts | 1 + packages/misskey-js/src/autogen/types.ts | 61 ++++++++++++ 10 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 packages/backend/src/server/api/endpoints/notes/polls/refresh.ts diff --git a/packages/backend/src/core/activitypub/models/ApQuestionService.ts b/packages/backend/src/core/activitypub/models/ApQuestionService.ts index 73004d10b0..e3bff8a561 100644 --- a/packages/backend/src/core/activitypub/models/ApQuestionService.ts +++ b/packages/backend/src/core/activitypub/models/ApQuestionService.ts @@ -98,7 +98,7 @@ export class ApQuestionService { const newCount = apChoices.filter(ap => ap.name === choice).at(0)?.replies?.totalItems; if (newCount == null) throw new Error('invalid newCount: ' + newCount); - if (oldCount !== newCount) { + if (!(oldCount > newCount)) { changed = true; poll.votes[poll.choices.indexOf(choice)] = newCount; } diff --git a/packages/backend/src/server/api/EndpointsModule.ts b/packages/backend/src/server/api/EndpointsModule.ts index 8434e7e8e3..4b072fd707 100644 --- a/packages/backend/src/server/api/EndpointsModule.ts +++ b/packages/backend/src/server/api/EndpointsModule.ts @@ -299,6 +299,7 @@ import * as ep___notes_localTimeline from './endpoints/notes/local-timeline.js'; import * as ep___notes_mentions from './endpoints/notes/mentions.js'; import * as ep___notes_polls_recommendation from './endpoints/notes/polls/recommendation.js'; import * as ep___notes_polls_vote from './endpoints/notes/polls/vote.js'; +import * as ep___notes_polls_refresh from './endpoints/notes/polls/refresh.js'; import * as ep___notes_reactions from './endpoints/notes/reactions.js'; import * as ep___notes_reactions_create from './endpoints/notes/reactions/create.js'; import * as ep___notes_reactions_delete from './endpoints/notes/reactions/delete.js'; @@ -697,6 +698,7 @@ const $notes_localTimeline: Provider = { provide: 'ep:notes/local-timeline', use const $notes_mentions: Provider = { provide: 'ep:notes/mentions', useClass: ep___notes_mentions.default }; const $notes_polls_recommendation: Provider = { provide: 'ep:notes/polls/recommendation', useClass: ep___notes_polls_recommendation.default }; const $notes_polls_vote: Provider = { provide: 'ep:notes/polls/vote', useClass: ep___notes_polls_vote.default }; +const $notes_polls_refresh: Provider = { provide: 'ep:notes/polls/refresh', useClass: ep___notes_polls_refresh.default }; const $notes_reactions: Provider = { provide: 'ep:notes/reactions', useClass: ep___notes_reactions.default }; const $notes_reactions_create: Provider = { provide: 'ep:notes/reactions/create', useClass: ep___notes_reactions_create.default }; const $notes_reactions_delete: Provider = { provide: 'ep:notes/reactions/delete', useClass: ep___notes_reactions_delete.default }; @@ -1099,6 +1101,7 @@ const $reversi_verify: Provider = { provide: 'ep:reversi/verify', useClass: ep__ $notes_mentions, $notes_polls_recommendation, $notes_polls_vote, + $notes_polls_refresh, $notes_reactions, $notes_reactions_create, $notes_reactions_delete, @@ -1494,6 +1497,7 @@ const $reversi_verify: Provider = { provide: 'ep:reversi/verify', useClass: ep__ $notes_mentions, $notes_polls_recommendation, $notes_polls_vote, + $notes_polls_refresh, $notes_reactions, $notes_reactions_create, $notes_reactions_delete, diff --git a/packages/backend/src/server/api/endpoints.ts b/packages/backend/src/server/api/endpoints.ts index bb5c71d78c..66a7ac1740 100644 --- a/packages/backend/src/server/api/endpoints.ts +++ b/packages/backend/src/server/api/endpoints.ts @@ -305,6 +305,7 @@ import * as ep___notes_localTimeline from './endpoints/notes/local-timeline.js'; import * as ep___notes_mentions from './endpoints/notes/mentions.js'; import * as ep___notes_polls_recommendation from './endpoints/notes/polls/recommendation.js'; import * as ep___notes_polls_vote from './endpoints/notes/polls/vote.js'; +import * as ep___notes_polls_refresh from './endpoints/notes/polls/refresh.js'; import * as ep___notes_reactions from './endpoints/notes/reactions.js'; import * as ep___notes_reactions_create from './endpoints/notes/reactions/create.js'; import * as ep___notes_reactions_delete from './endpoints/notes/reactions/delete.js'; @@ -701,6 +702,7 @@ const eps = [ ['notes/mentions', ep___notes_mentions], ['notes/polls/recommendation', ep___notes_polls_recommendation], ['notes/polls/vote', ep___notes_polls_vote], + ['notes/polls/refresh', ep___notes_polls_refresh], ['notes/reactions', ep___notes_reactions], ['notes/reactions/create', ep___notes_reactions_create], ['notes/reactions/delete', ep___notes_reactions_delete], diff --git a/packages/backend/src/server/api/endpoints/notes/polls/refresh.ts b/packages/backend/src/server/api/endpoints/notes/polls/refresh.ts new file mode 100644 index 0000000000..c43387689e --- /dev/null +++ b/packages/backend/src/server/api/endpoints/notes/polls/refresh.ts @@ -0,0 +1,98 @@ +/* + * SPDX-FileCopyrightText: marie and sharkey-project + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { Injectable } from '@nestjs/common'; +import { Endpoint } from '@/server/api/endpoint-base.js'; +import { GetterService } from '@/server/api/GetterService.js'; +import { UserBlockingService } from '@/core/UserBlockingService.js'; +import { ApQuestionService } from '@/core/activitypub/models/ApQuestionService.js'; +import { NoteEntityService } from '@/core/entities/NoteEntityService.js'; +import { ApiError } from '../../../error.js'; + +export const meta = { + tags: ['notes'], + + requireCredential: true, + + prohibitMoved: true, + + kind: 'write:votes', + + errors: { + noSuchNote: { + message: 'No such note.', + code: 'NO_SUCH_NOTE', + id: 'ecafbd2e-c283-4d6d-aecb-1a0a33b75396', + }, + + noPoll: { + message: 'The note does not attach a poll.', + code: 'NO_POLL', + id: '5f979967-52d9-4314-a911-1c673727f92f', + }, + + noUri: { + message: 'The note has no URI defined.', + code: 'INVALID_URI', + id: 'e0cc9a04-f2e8-41e4-a5f1-4127293260ca', + }, + + youHaveBeenBlocked: { + message: 'You cannot vote this poll because you have been blocked by this user.', + code: 'YOU_HAVE_BEEN_BLOCKED', + id: '85a5377e-b1e9-4617-b0b9-5bea73331e49', + }, + }, +} as const; + +export const paramDef = { + type: 'object', + properties: { + noteId: { type: 'string', format: 'misskey:id' }, + }, + required: ['noteId'], +} as const; + +// TODO: ロジックをサービスに切り出す + +@Injectable() +export default class extends Endpoint { // eslint-disable-line import/no-default-export + constructor( + private getterService: GetterService, + private userBlockingService: UserBlockingService, + private apQuestionService: ApQuestionService, + private noteEntityService: NoteEntityService, + ) { + super(meta, paramDef, async (ps, me) => { + // Get note + const note = await this.getterService.getNote(ps.noteId).catch(err => { + if (err.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote); + throw err; + }); + + if (!note.hasPoll) { + throw new ApiError(meta.errors.noPoll); + } + + // Check blocking + if (note.userId !== me.id) { + const blocked = await this.userBlockingService.checkBlocked(note.userId, me.id); + if (blocked) { + throw new ApiError(meta.errors.youHaveBeenBlocked); + } + } + + if (!note.uri) { + throw new ApiError(meta.errors.noUri); + } + + await this.apQuestionService.updateQuestion(note.uri); + + return await this.noteEntityService.pack(note, me, { + detail: true, + }); + }); + } +} diff --git a/packages/frontend/src/components/MkPoll.vue b/packages/frontend/src/components/MkPoll.vue index 393ac4efba..d594348bfa 100644 --- a/packages/frontend/src/components/MkPoll.vue +++ b/packages/frontend/src/components/MkPoll.vue @@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only @@ -108,6 +110,17 @@ const vote = async (id) => { }); if (!showResult.value) showResult.value = !props.poll.multiple; }; + +const refreshVotes = async () => { + pleaseLogin(undefined, pleaseLoginContext.value); + + if (props.readOnly || closed.value) return; + await misskeyApi('notes/polls/refresh', { + noteId: props.noteId, + // Sadly due to being in the same component and the poll being a prop we require to break Vue's recommendation of not mutating the prop to update it. + // eslint-disable-next-line vue/no-mutating-props + }).then((res: any) => res.poll ? props.poll.choices = res.poll.choices : null ); +};