diff --git a/packages/backend/src/core/activitypub/models/ApQuestionService.ts b/packages/backend/src/core/activitypub/models/ApQuestionService.ts index 73004d10b0..9246398fde 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 31f96e854f..c9a637261f 100644 --- a/packages/backend/src/server/api/EndpointsModule.ts +++ b/packages/backend/src/server/api/EndpointsModule.ts @@ -300,6 +300,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'; @@ -699,6 +700,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 }; @@ -1102,6 +1104,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, @@ -1498,6 +1501,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 8be9854b15..a88bed8e7a 100644 --- a/packages/backend/src/server/api/endpoints.ts +++ b/packages/backend/src/server/api/endpoints.ts @@ -306,6 +306,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'; @@ -703,6 +704,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..b96691f894 --- /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: 'read:federation', + + 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 refresh 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/MkNote.vue b/packages/frontend/src/components/MkNote.vue index edae1e91b2..beaf9bc52b 100644 --- a/packages/frontend/src/components/MkNote.vue +++ b/packages/frontend/src/components/MkNote.vue @@ -94,7 +94,7 @@ SPDX-License-Identifier: AGPL-3.0-only
- +
diff --git a/packages/frontend/src/components/MkNoteDetailed.vue b/packages/frontend/src/components/MkNoteDetailed.vue index 123e94c3e0..508ea18153 100644 --- a/packages/frontend/src/components/MkNoteDetailed.vue +++ b/packages/frontend/src/components/MkNoteDetailed.vue @@ -100,7 +100,7 @@ SPDX-License-Identifier: AGPL-3.0-only
- +
diff --git a/packages/frontend/src/components/MkPoll.vue b/packages/frontend/src/components/MkPoll.vue index 393ac4efba..4d893df39d 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 @@ -39,11 +41,13 @@ import { i18n } from '@/i18n.js'; import { host } from '@/config.js'; import { useInterval } from '@/scripts/use-interval.js'; import type { OpenOnRemoteOptions } from '@/scripts/please-login.js'; +import { $i } from '@/account.js'; const props = defineProps<{ noteId: string; poll: NonNullable; readOnly?: boolean; + local?: boolean; }>(); const remaining = ref(-1); @@ -108,6 +112,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 ); +};