add refresh button to poll
This commit is contained in:
parent
68cc264f5f
commit
4c9153fd14
|
@ -28,7 +28,7 @@ import {
|
|||
import { db } from "@/db/postgre.js";
|
||||
import { IdentifiableError } from "@/misc/identifiable-error.js";
|
||||
|
||||
async function populatePoll(note: Note, meId: User["id"] | null) {
|
||||
export async function populatePoll(note: Note, meId: User["id"] | null) {
|
||||
const poll = await Polls.findOneByOrFail({ noteId: note.id });
|
||||
const choices = poll.choices.map((c) => ({
|
||||
text: c,
|
||||
|
|
|
@ -13,7 +13,7 @@ import type {
|
|||
import { htmlToMfm } from "../misc/html-to-mfm.js";
|
||||
import { extractApHashtags } from "./tag.js";
|
||||
import { unique, toArray, toSingle } from "@/prelude/array.js";
|
||||
import { extractPollFromQuestion, updateQuestion } from "./question.js";
|
||||
import { extractPollFromQuestion } from "./question.js";
|
||||
import vote from "@/services/note/polls/vote.js";
|
||||
import { apLogger } from "../logger.js";
|
||||
import { DriveFile } from "@/models/entities/drive-file.js";
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import config from "@/config/index.js";
|
||||
import Resolver from "../resolver.js";
|
||||
import type { IObject, IQuestion } from "../type.js";
|
||||
import { isQuestion } from "../type.js";
|
||||
import { getApId, isQuestion } from "../type.js";
|
||||
import { apLogger } from "../logger.js";
|
||||
import { Notes, Polls } from "@/models/index.js";
|
||||
import type { IPoll } from "@/models/entities/poll.js";
|
||||
|
@ -47,11 +47,14 @@ export async function extractPollFromQuestion(
|
|||
|
||||
/**
|
||||
* Update votes of Question
|
||||
* @param uri URI of AP Question object
|
||||
* @param value URI of AP Question object or object itself
|
||||
* @returns true if updated
|
||||
*/
|
||||
export async function updateQuestion(value: any, resolver?: Resolver) {
|
||||
const uri = typeof value === "string" ? value : value.id;
|
||||
export async function updateQuestion(
|
||||
value: string | IQuestion,
|
||||
resolver?: Resolver,
|
||||
): Promise<boolean> {
|
||||
const uri = typeof value === "string" ? value : getApId(value);
|
||||
|
||||
// Skip if URI points to this server
|
||||
if (uri.startsWith(`${config.url}/`)) throw new Error("uri points local");
|
||||
|
@ -65,22 +68,23 @@ export async function updateQuestion(value: any, resolver?: Resolver) {
|
|||
//#endregion
|
||||
|
||||
// resolve new Question object
|
||||
if (resolver == null) resolver = new Resolver();
|
||||
const question = (await resolver.resolve(value)) as IQuestion;
|
||||
const _resolver = resolver ?? new Resolver();
|
||||
const question = (await _resolver.resolve(value)) as IQuestion;
|
||||
apLogger.debug(`fetched question: ${JSON.stringify(question, null, 2)}`);
|
||||
|
||||
if (question.type !== "Question") throw new Error("object is not a Question");
|
||||
|
||||
const apChoices = question.oneOf || question.anyOf;
|
||||
if (!apChoices) return false;
|
||||
|
||||
let changed = false;
|
||||
|
||||
for (const choice of poll.choices) {
|
||||
const oldCount = poll.votes[poll.choices.indexOf(choice)];
|
||||
const newCount = apChoices!.filter((ap) => ap.name === choice)[0].replies!
|
||||
.totalItems;
|
||||
const newCount = apChoices.filter((ap) => ap.name === choice)[0].replies
|
||||
?.totalItems;
|
||||
|
||||
if (oldCount !== newCount) {
|
||||
if (newCount !== undefined && oldCount !== newCount) {
|
||||
changed = true;
|
||||
poll.votes[poll.choices.indexOf(choice)] = newCount;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import define from "../../define.js";
|
||||
import config from "@/config/index.js";
|
||||
import { createPerson } from "@/remote/activitypub/models/person.js";
|
||||
import { createNote } from "@/remote/activitypub/models/note.js";
|
||||
import DbResolver from "@/remote/activitypub/db-resolver.js";
|
||||
|
@ -9,11 +8,13 @@ import { extractDbHost } from "@/misc/convert-host.js";
|
|||
import { Users, Notes } from "@/models/index.js";
|
||||
import type { Note } from "@/models/entities/note.js";
|
||||
import type { CacheableLocalUser, User } from "@/models/entities/user.js";
|
||||
import { fetchMeta } from "@/misc/fetch-meta.js";
|
||||
import { isActor, isPost, getApId } from "@/remote/activitypub/type.js";
|
||||
import type { SchemaType } from "@/misc/schema.js";
|
||||
import { HOUR } from "@/const.js";
|
||||
import { shouldBlockInstance } from "@/misc/should-block-instance.js";
|
||||
import { updateQuestion } from "@/remote/activitypub/models/question.js";
|
||||
import { populatePoll } from "@/models/repositories/note.js";
|
||||
import { redisClient } from "@/db/redis.js";
|
||||
|
||||
export const meta = {
|
||||
tags: ["federation"],
|
||||
|
@ -104,18 +105,29 @@ async function fetchAny(
|
|||
|
||||
const dbResolver = new DbResolver();
|
||||
|
||||
let local = await mergePack(
|
||||
me,
|
||||
...(await Promise.all([
|
||||
const [user, note] = await Promise.all([
|
||||
dbResolver.getUserFromApId(uri),
|
||||
dbResolver.getNoteFromApId(uri),
|
||||
])),
|
||||
);
|
||||
if (local != null) return local;
|
||||
]);
|
||||
let local = await mergePack(me, user, note);
|
||||
if (local) {
|
||||
if (local.type === "Note" && note?.uri && note.hasPoll) {
|
||||
// Update questions if the stored (remote) note contains the poll
|
||||
const key = `pollFetched:${note.uri}`;
|
||||
if ((await redisClient.exists(key)) === 0) {
|
||||
if (await updateQuestion(note.uri)) {
|
||||
local.object.poll = await populatePoll(note, me?.id ?? null);
|
||||
}
|
||||
// Allow refetching the poll after 2 minutes
|
||||
await redisClient.set(key, 1, "EX", 60 * 2);
|
||||
}
|
||||
}
|
||||
return local;
|
||||
}
|
||||
|
||||
// fetching Object once from remote
|
||||
const resolver = new Resolver();
|
||||
const object = (await resolver.resolve(uri)) as any;
|
||||
const object = await resolver.resolve(uri);
|
||||
|
||||
// /@user If a URI other than the id is specified,
|
||||
// the URI is determined here
|
||||
|
@ -123,8 +135,8 @@ async function fetchAny(
|
|||
local = await mergePack(
|
||||
me,
|
||||
...(await Promise.all([
|
||||
dbResolver.getUserFromApId(object.id),
|
||||
dbResolver.getNoteFromApId(object.id),
|
||||
dbResolver.getUserFromApId(getApId(object)),
|
||||
dbResolver.getNoteFromApId(getApId(object)),
|
||||
])),
|
||||
);
|
||||
if (local != null) return local;
|
||||
|
|
|
@ -34,23 +34,25 @@
|
|||
</ul>
|
||||
<p v-if="!readOnly">
|
||||
<span>{{ i18n.t("_poll.totalVotes", { n: total }) }}</span>
|
||||
<span v-if="!closed && !isVoted">
|
||||
<span> · </span>
|
||||
<a
|
||||
v-if="!closed && !isVoted"
|
||||
@click.stop="showResult = !showResult"
|
||||
>{{
|
||||
<a @click.stop="showResult = !showResult">{{
|
||||
showResult ? i18n.ts._poll.vote : i18n.ts._poll.showResult
|
||||
}}</a
|
||||
>
|
||||
<span v-if="isVoted">{{ i18n.ts._poll.voted }}</span>
|
||||
<span v-else-if="closed">{{ i18n.ts._poll.closed }}</span>
|
||||
}}</a>
|
||||
</span>
|
||||
<span v-if="!isLocal">
|
||||
<span> · </span>
|
||||
<a @click.stop="refresh">{{ i18n.ts.reload }}</a>
|
||||
</span>
|
||||
<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>
|
||||
</p>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { computed, onUnmounted, ref, toRef } from "vue";
|
||||
import { computed, ref } from "vue";
|
||||
import * as misskey from "calckey-js";
|
||||
import { sum } from "@/scripts/array";
|
||||
import { pleaseLogin } from "@/scripts/please-login";
|
||||
|
@ -67,6 +69,7 @@ const remaining = ref(-1);
|
|||
|
||||
const total = computed(() => sum(props.note.poll.choices.map((x) => x.votes)));
|
||||
const closed = computed(() => remaining.value === 0);
|
||||
const isLocal = computed(() => !props.note.uri);
|
||||
const isVoted = computed(
|
||||
() =>
|
||||
!props.note.poll.multiple &&
|
||||
|
@ -112,6 +115,14 @@ if (props.note.poll.expiresAt) {
|
|||
});
|
||||
}
|
||||
|
||||
async function refresh() {
|
||||
if (!props.note.uri) return;
|
||||
const obj = await os.apiWithDialog("ap/show", { uri: props.note.uri });
|
||||
if (obj.type === "Note" && obj.object.poll) {
|
||||
props.note.poll = obj.object.poll;
|
||||
}
|
||||
}
|
||||
|
||||
const vote = async (id) => {
|
||||
pleaseLogin();
|
||||
|
||||
|
|
Loading…
Reference in New Issue