upd: add ability to refresh poll

This commit is contained in:
Marie 2024-10-13 03:44:07 +02:00 committed by Hazelnoot
parent 68b90df00b
commit dd58a4aa92
10 changed files with 199 additions and 2 deletions

View file

@ -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;
}

View file

@ -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,

View file

@ -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],

View file

@ -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<typeof meta, typeof paramDef> { // 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,
});
});
}
}

View file

@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<template>
<div :class="{ [$style.done]: closed || isVoted }">
<ul :class="$style.choices">
<li v-for="(choice, i) in poll.choices" :key="i" :class="$style.choice" @click="vote(i)">
<li v-for="(choice, i) in props.poll.choices" :key="i" :class="$style.choice" @click="vote(i)">
<div :class="$style.bg" :style="{ 'width': `${showResult ? (choice.votes / total * 100) : 0}%` }"></div>
<span :class="$style.fg">
<template v-if="choice.isVoted"><i class="ti ti-check" style="margin-right: 4px; color: var(--accent);"></i></template>
@ -24,6 +24,8 @@ SPDX-License-Identifier: AGPL-3.0-only
<span v-if="isVoted">{{ i18n.ts._poll.voted }}</span>
<span v-else-if="closed">{{ i18n.ts._poll.closed }}</span>
<span v-if="remaining > 0"> · {{ timer }}</span>
<span v-if="!closed"> · </span>
<a v-if="!closed" style="color: inherit;" @click="refreshVotes()">Refresh</a>
</p>
</div>
</template>
@ -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 );
};
</script>
<style lang="scss" module>

View file

@ -1659,6 +1659,7 @@ declare namespace entities {
NotesPollsRecommendationRequest,
NotesPollsRecommendationResponse,
NotesPollsVoteRequest,
NotesPollsRefreshRequest,
NotesReactionsRequest,
NotesReactionsResponse,
NotesReactionsCreateRequest,
@ -2697,6 +2698,9 @@ type NotesPollsRecommendationRequest = operations['notes___polls___recommendatio
// @public (undocumented)
type NotesPollsRecommendationResponse = operations['notes___polls___recommendation']['responses']['200']['content']['application/json'];
// @public (undocumented)
type NotesPollsRefreshRequest = operations['notes___polls___refresh']['requestBody']['content']['application/json'];
// @public (undocumented)
type NotesPollsVoteRequest = operations['notes___polls___vote']['requestBody']['content']['application/json'];

View file

@ -3273,6 +3273,17 @@ declare module '../api.js' {
credential?: string | null,
): Promise<SwitchCaseResponseType<E, P>>;
/**
* No description provided.
*
* **Credential required**: *Yes* / **Permission**: *write:votes*
*/
request<E extends 'notes/polls/refresh', P extends Endpoints[E]['req']>(
endpoint: E,
params: P,
credential?: string | null,
): Promise<SwitchCaseResponseType<E, P>>;
/**
* No description provided.
*

View file

@ -437,6 +437,7 @@ import type {
NotesPollsRecommendationRequest,
NotesPollsRecommendationResponse,
NotesPollsVoteRequest,
NotesPollsRefreshRequest,
NotesReactionsRequest,
NotesReactionsResponse,
NotesReactionsCreateRequest,
@ -886,6 +887,7 @@ export type Endpoints = {
'notes/mentions': { req: NotesMentionsRequest; res: NotesMentionsResponse };
'notes/polls/recommendation': { req: NotesPollsRecommendationRequest; res: NotesPollsRecommendationResponse };
'notes/polls/vote': { req: NotesPollsVoteRequest; res: EmptyResponse };
'notes/polls/refresh': { req: NotesPollsRefreshRequest; res: EmptyResponse };
'notes/reactions': { req: NotesReactionsRequest; res: NotesReactionsResponse };
'notes/reactions/create': { req: NotesReactionsCreateRequest; res: EmptyResponse };
'notes/reactions/delete': { req: NotesReactionsDeleteRequest; res: EmptyResponse };
@ -1283,6 +1285,7 @@ export const endpointReqTypes: Record<keyof Endpoints, 'application/json' | 'mul
'notes/mentions': 'application/json',
'notes/polls/recommendation': 'application/json',
'notes/polls/vote': 'application/json',
'notes/polls/refresh': 'application/json',
'notes/reactions': 'application/json',
'notes/reactions/create': 'application/json',
'notes/reactions/delete': 'application/json',

View file

@ -440,6 +440,7 @@ export type NotesMentionsResponse = operations['notes___mentions']['responses'][
export type NotesPollsRecommendationRequest = operations['notes___polls___recommendation']['requestBody']['content']['application/json'];
export type NotesPollsRecommendationResponse = operations['notes___polls___recommendation']['responses']['200']['content']['application/json'];
export type NotesPollsVoteRequest = operations['notes___polls___vote']['requestBody']['content']['application/json'];
export type NotesPollsRefreshRequest = operations['notes___polls___refresh']['requestBody']['content']['application/json'];
export type NotesReactionsRequest = operations['notes___reactions']['requestBody']['content']['application/json'];
export type NotesReactionsResponse = operations['notes___reactions']['responses']['200']['content']['application/json'];
export type NotesReactionsCreateRequest = operations['notes___reactions___create']['requestBody']['content']['application/json'];

View file

@ -2836,6 +2836,15 @@ export type paths = {
*/
post: operations['notes___polls___vote'];
};
'/notes/polls/refresh': {
/**
* notes/polls/refresh
* @description No description provided.
*
* **Credential required**: *Yes* / **Permission**: *write:votes*
*/
post: operations['notes___polls___refresh'];
};
'/notes/reactions': {
/**
* notes/reactions
@ -22806,6 +22815,58 @@ export type operations = {
};
};
};
/**
* notes/polls/refresh
* @description No description provided.
*
* **Credential required**: *Yes* / **Permission**: *write:votes*
*/
notes___polls___refresh: {
requestBody: {
content: {
'application/json': {
/** Format: misskey:id */
noteId: string;
};
};
};
responses: {
/** @description OK (without any results) */
204: {
content: never;
};
/** @description Client error */
400: {
content: {
'application/json': components['schemas']['Error'];
};
};
/** @description Authentication error */
401: {
content: {
'application/json': components['schemas']['Error'];
};
};
/** @description Forbidden error */
403: {
content: {
'application/json': components['schemas']['Error'];
};
};
/** @description I'm Ai */
418: {
content: {
'application/json': components['schemas']['Error'];
};
};
/** @description Internal server error */
500: {
content: {
'application/json': components['schemas']['Error'];
};
};
};
};
/**
* notes/reactions
* @description No description provided.