merge: Allow logged in users to refresh polls (!686)

View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/686

Closes #743

Approved-by: Hazelnoot <acomputerdog@gmail.com>
Approved-by: dakkar <dakkar@thenautilus.net>
This commit is contained in:
Marie 2024-10-18 21:03:07 +00:00
commit 290bfd2075
14 changed files with 205 additions and 6 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

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

View file

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

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: '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<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

@ -94,7 +94,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<div v-if="appearNote.files && appearNote.files.length > 0">
<MkMediaList ref="galleryEl" :mediaList="appearNote.files" @click.stop/>
</div>
<MkPoll v-if="appearNote.poll" :noteId="appearNote.id" :poll="appearNote.poll" :class="$style.poll" @click.stop/>
<MkPoll v-if="appearNote.poll" :noteId="appearNote.id" :poll="appearNote.poll" :local="!appearNote.user.host" :class="$style.poll" @click.stop/>
<div v-if="isEnabledUrlPreview">
<MkUrlPreview v-for="url in urls" :key="url" :url="url" :compact="true" :detail="false" :class="$style.urlPreview" @click.stop/>
</div>

View file

@ -100,7 +100,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<div v-if="appearNote.files && appearNote.files.length > 0">
<MkMediaList ref="galleryEl" :mediaList="appearNote.files"/>
</div>
<MkPoll v-if="appearNote.poll" ref="pollViewer" :noteId="appearNote.id" :poll="appearNote.poll" :class="$style.poll"/>
<MkPoll v-if="appearNote.poll" ref="pollViewer" :noteId="appearNote.id" :poll="appearNote.poll" :local="!appearNote.user.host" :class="$style.poll"/>
<div v-if="isEnabledUrlPreview">
<MkUrlPreview v-for="url in urls" :key="url" :url="url" :compact="true" :detail="true" style="margin-top: 6px;"/>
</div>

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 && $i && !props.local"> · </span>
<a v-if="!closed && $i && !props.local" style="color: inherit;" @click="refreshVotes()">{{ i18n.ts.reload }}</a>
</p>
</div>
</template>
@ -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<Misskey.entities.Note['poll']>;
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 );
};
</script>
<style lang="scss" module>

View file

@ -96,7 +96,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<div v-if="appearNote.files && appearNote.files.length > 0">
<MkMediaList ref="galleryEl" :mediaList="appearNote.files" @click.stop/>
</div>
<MkPoll v-if="appearNote.poll" :noteId="appearNote.id" :poll="appearNote.poll" :class="$style.poll" @click.stop/>
<MkPoll v-if="appearNote.poll" :noteId="appearNote.id" :poll="appearNote.poll" :local="!appearNote.user.host" :class="$style.poll" @click.stop/>
<div v-if="isEnabledUrlPreview">
<MkUrlPreview v-for="url in urls" :key="url" :url="url" :compact="true" :detail="false" :class="$style.urlPreview" @click.stop/>
</div>

View file

@ -108,7 +108,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<div v-if="appearNote.files && appearNote.files.length > 0">
<MkMediaList ref="galleryEl" :mediaList="appearNote.files"/>
</div>
<MkPoll v-if="appearNote.poll" ref="pollViewer" :noteId="appearNote.id" :poll="appearNote.poll" :class="$style.poll"/>
<MkPoll v-if="appearNote.poll" ref="pollViewer" :noteId="appearNote.id" :poll="appearNote.poll" :local="!appearNote.user.host" :class="$style.poll"/>
<div v-if="isEnabledUrlPreview">
<MkUrlPreview v-for="url in urls" :key="url" :url="url" :compact="true" :detail="true" style="margin-top: 6px;"/>
</div>

View file

@ -1663,6 +1663,7 @@ declare namespace entities {
NotesPollsRecommendationRequest,
NotesPollsRecommendationResponse,
NotesPollsVoteRequest,
NotesPollsRefreshRequest,
NotesReactionsRequest,
NotesReactionsResponse,
NotesReactionsCreateRequest,
@ -2704,6 +2705,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

@ -3284,6 +3284,17 @@ declare module '../api.js' {
credential?: string | null,
): Promise<SwitchCaseResponseType<E, P>>;
/**
* No description provided.
*
* **Credential required**: *Yes* / **Permission**: *read:federation*
*/
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

@ -438,6 +438,7 @@ import type {
NotesPollsRecommendationRequest,
NotesPollsRecommendationResponse,
NotesPollsVoteRequest,
NotesPollsRefreshRequest,
NotesReactionsRequest,
NotesReactionsResponse,
NotesReactionsCreateRequest,
@ -888,6 +889,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 };
@ -1286,6 +1288,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

@ -441,6 +441,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

@ -2845,6 +2845,15 @@ export type paths = {
*/
post: operations['notes___polls___vote'];
};
'/notes/polls/refresh': {
/**
* notes/polls/refresh
* @description No description provided.
*
* **Credential required**: *Yes* / **Permission**: *read:federation*
*/
post: operations['notes___polls___refresh'];
};
'/notes/reactions': {
/**
* notes/reactions
@ -22867,6 +22876,58 @@ export type operations = {
};
};
};
/**
* notes/polls/refresh
* @description No description provided.
*
* **Credential required**: *Yes* / **Permission**: *read:federation*
*/
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.