mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-07 20:44:34 +00:00
overhaul trending polls
* Split into local, global, and completed sections * Don't require credential, but check for local/global timeline perms * Fix rate limit * Return polls where the current user has already voted * Return non-public polls if the user has permission to view them * Apply user/instance blocks * Fetch polls + notes + users in a single step to speed up pack
This commit is contained in:
parent
b05b4ec74d
commit
3c949f0b81
6 changed files with 140 additions and 24 deletions
12
locales/index.d.ts
vendored
12
locales/index.d.ts
vendored
|
@ -13069,6 +13069,18 @@ export interface Locale extends ILocale {
|
||||||
* Users popular on {name}
|
* Users popular on {name}
|
||||||
*/
|
*/
|
||||||
"popularUsersLocal": ParameterizedString<"name">;
|
"popularUsersLocal": ParameterizedString<"name">;
|
||||||
|
/**
|
||||||
|
* Polls trending on {host}
|
||||||
|
*/
|
||||||
|
"pollsOnLocal": ParameterizedString<"host">;
|
||||||
|
/**
|
||||||
|
* Polls trending on the global network
|
||||||
|
*/
|
||||||
|
"pollsOnRemote": string;
|
||||||
|
/**
|
||||||
|
* Polls that have ended recently
|
||||||
|
*/
|
||||||
|
"pollsExpired": string;
|
||||||
/**
|
/**
|
||||||
* Silenced
|
* Silenced
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -9,13 +9,13 @@ import type { NotesRepository, MutingsRepository, PollsRepository, PollVotesRepo
|
||||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||||
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
||||||
import { DI } from '@/di-symbols.js';
|
import { DI } from '@/di-symbols.js';
|
||||||
|
import { QueryService } from '@/core/QueryService.js';
|
||||||
|
import { RoleService } from '@/core/RoleService.js';
|
||||||
|
import { ApiError } from '@/server/api/error.js';
|
||||||
|
|
||||||
export const meta = {
|
export const meta = {
|
||||||
tags: ['notes'],
|
tags: ['notes'],
|
||||||
|
|
||||||
requireCredential: true,
|
|
||||||
kind: 'read:account',
|
|
||||||
|
|
||||||
res: {
|
res: {
|
||||||
type: 'array',
|
type: 'array',
|
||||||
optional: false, nullable: false,
|
optional: false, nullable: false,
|
||||||
|
@ -26,10 +26,24 @@ export const meta = {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
// 2 calls per second
|
errors: {
|
||||||
|
ltlDisabled: {
|
||||||
|
message: 'Local timeline has been disabled.',
|
||||||
|
code: 'LTL_DISABLED',
|
||||||
|
id: '45a6eb02-7695-4393-b023-dd3be9aaaefd',
|
||||||
|
},
|
||||||
|
gtlDisabled: {
|
||||||
|
message: 'Global timeline has been disabled.',
|
||||||
|
code: 'GTL_DISABLED',
|
||||||
|
id: '0332fc13-6ab2-4427-ae80-a9fadffd1a6b',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// Up to 10 calls, then 2 per second
|
||||||
limit: {
|
limit: {
|
||||||
duration: 1000,
|
type: 'bucket',
|
||||||
max: 2,
|
size: 10,
|
||||||
|
dripRate: 500,
|
||||||
},
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
|
@ -39,6 +53,8 @@ export const paramDef = {
|
||||||
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
||||||
offset: { type: 'integer', default: 0 },
|
offset: { type: 'integer', default: 0 },
|
||||||
excludeChannels: { type: 'boolean', default: false },
|
excludeChannels: { type: 'boolean', default: false },
|
||||||
|
local: { type: 'boolean', nullable: true, default: null },
|
||||||
|
expired: { type: 'boolean', default: false },
|
||||||
},
|
},
|
||||||
required: [],
|
required: [],
|
||||||
} as const;
|
} as const;
|
||||||
|
@ -59,18 +75,54 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
private mutingsRepository: MutingsRepository,
|
private mutingsRepository: MutingsRepository,
|
||||||
|
|
||||||
private noteEntityService: NoteEntityService,
|
private noteEntityService: NoteEntityService,
|
||||||
|
private readonly queryService: QueryService,
|
||||||
|
private readonly roleService: RoleService,
|
||||||
) {
|
) {
|
||||||
super(meta, paramDef, async (ps, me) => {
|
super(meta, paramDef, async (ps, me) => {
|
||||||
const query = this.pollsRepository.createQueryBuilder('poll')
|
const query = this.pollsRepository.createQueryBuilder('poll')
|
||||||
.where('poll.userHost IS NULL')
|
.innerJoinAndSelect('poll.note', 'note')
|
||||||
.andWhere('poll.userId != :meId', { meId: me.id })
|
.innerJoinAndSelect('note.user', 'user')
|
||||||
.andWhere('poll.noteVisibility = \'public\'')
|
.leftJoinAndSelect('note.renote', 'renote')
|
||||||
.andWhere(new Brackets(qb => {
|
.leftJoinAndSelect('note.reply', 'reply')
|
||||||
|
.leftJoinAndSelect('renote.user', 'renoteUser')
|
||||||
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
||||||
|
;
|
||||||
|
|
||||||
|
if (me) {
|
||||||
|
query.andWhere('poll.userId != :meId', { meId: me.id });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ps.expired) {
|
||||||
|
query.andWhere('poll.expiresAt IS NOT NULL');
|
||||||
|
query.andWhere('poll.expiresAt < :expiresMax', {
|
||||||
|
expiresMax: new Date(),
|
||||||
|
});
|
||||||
|
query.andWhere('poll.expiresAt >= :expiresMin', {
|
||||||
|
expiresMin: new Date(Date.now() - (1000 * 60 * 60 * 24 * 7)),
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
query.andWhere(new Brackets(qb => {
|
||||||
qb
|
qb
|
||||||
.where('poll.expiresAt IS NULL')
|
.where('poll.expiresAt IS NULL')
|
||||||
.orWhere('poll.expiresAt > :now', { now: new Date() });
|
.orWhere('poll.expiresAt > :now', { now: new Date() });
|
||||||
}));
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
const policies = await this.roleService.getUserPolicies(me?.id ?? null);
|
||||||
|
if (ps.local != null) {
|
||||||
|
if (ps.local) {
|
||||||
|
if (!policies.ltlAvailable) throw new ApiError(meta.errors.ltlDisabled);
|
||||||
|
query.andWhere('poll.userHost IS NULL');
|
||||||
|
} else {
|
||||||
|
if (!policies.gtlAvailable) throw new ApiError(meta.errors.gtlDisabled);
|
||||||
|
query.andWhere('poll.userHost IS NOT NULL');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!policies.ltlAvailable) throw new ApiError(meta.errors.ltlDisabled);
|
||||||
|
if (!policies.gtlAvailable) throw new ApiError(meta.errors.gtlDisabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
//#region exclude arleady voted polls
|
//#region exclude arleady voted polls
|
||||||
const votedQuery = this.pollVotesRepository.createQueryBuilder('vote')
|
const votedQuery = this.pollVotesRepository.createQueryBuilder('vote')
|
||||||
.select('vote.noteId')
|
.select('vote.noteId')
|
||||||
|
@ -81,16 +133,15 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
|
|
||||||
query.setParameters(votedQuery.getParameters());
|
query.setParameters(votedQuery.getParameters());
|
||||||
//#endregion
|
//#endregion
|
||||||
|
*/
|
||||||
|
|
||||||
//#region mute
|
//#region block/mute/vis
|
||||||
const mutingQuery = this.mutingsRepository.createQueryBuilder('muting')
|
this.queryService.generateVisibilityQuery(query, me);
|
||||||
.select('muting.muteeId')
|
this.queryService.generateBlockedHostQueryForNote(query);
|
||||||
.where('muting.muterId = :muterId', { muterId: me.id });
|
if (me) {
|
||||||
|
this.queryService.generateBlockedUserQueryForNotes(query, me);
|
||||||
query
|
this.queryService.generateMutedUserQueryForNotes(query, me);
|
||||||
.andWhere(`poll.userId NOT IN (${ mutingQuery.getQuery() })`);
|
}
|
||||||
|
|
||||||
query.setParameters(mutingQuery.getParameters());
|
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
//#region exclude channels
|
//#region exclude channels
|
||||||
|
@ -107,6 +158,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
|
|
||||||
if (polls.length === 0) return [];
|
if (polls.length === 0) return [];
|
||||||
|
|
||||||
|
/*
|
||||||
const notes = await this.notesRepository.find({
|
const notes = await this.notesRepository.find({
|
||||||
where: {
|
where: {
|
||||||
id: In(polls.map(poll => poll.noteId)),
|
id: In(polls.map(poll => poll.noteId)),
|
||||||
|
@ -115,6 +167,10 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
id: 'DESC',
|
id: 'DESC',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||||
|
const notes = polls.map(poll => poll.note!);
|
||||||
|
|
||||||
return await this.noteEntityService.packMany(notes, me, {
|
return await this.noteEntityService.packMany(notes, me, {
|
||||||
detail: true,
|
detail: true,
|
||||||
|
|
|
@ -10,27 +10,67 @@ SPDX-License-Identifier: AGPL-3.0-only
|
||||||
<option value="polls">{{ i18n.ts.poll }}</option>
|
<option value="polls">{{ i18n.ts.poll }}</option>
|
||||||
</MkTab>
|
</MkTab>
|
||||||
<MkNotes v-if="tab === 'notes'" :pagination="paginationForNotes"/>
|
<MkNotes v-if="tab === 'notes'" :pagination="paginationForNotes"/>
|
||||||
<MkNotes v-else-if="tab === 'polls'" :pagination="paginationForPolls"/>
|
<div v-else-if="tab === 'polls'">
|
||||||
|
<MkFoldableSection class="_margin">
|
||||||
|
<template #header><i class="ph-house ph-bold ph-lg" style="margin-right: 0.5em;"></i>{{ i18n.tsx.pollsOnLocal({ host: instance.name ?? host }) }}</template>
|
||||||
|
<MkNotes :pagination="paginationForPollsLocal" :disableAutoLoad="true"/>
|
||||||
|
</MkFoldableSection>
|
||||||
|
|
||||||
|
<MkFoldableSection class="_margin">
|
||||||
|
<template #header><i class="ph-globe ph-bold ph-lg" style="margin-right: 0.5em;"></i>{{ i18n.ts.pollsOnRemote }}</template>
|
||||||
|
<MkNotes :pagination="paginationForPollsRemote" :disableAutoLoad="true"/>
|
||||||
|
</MkFoldableSection>
|
||||||
|
|
||||||
|
<MkFoldableSection class="_margin">
|
||||||
|
<template #header><i class="ph-timer ph-bold ph-lg" style="margin-right: 0.5em;"></i>{{ i18n.ts.pollsExpired }}</template>
|
||||||
|
<MkNotes :pagination="paginationForPollsExpired" :disableAutoLoad="true"/>
|
||||||
|
</MkFoldableSection>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { ref } from 'vue';
|
import { ref } from 'vue';
|
||||||
|
import { host } from '@@/js/config.js';
|
||||||
import MkNotes from '@/components/MkNotes.vue';
|
import MkNotes from '@/components/MkNotes.vue';
|
||||||
import MkTab from '@/components/MkTab.vue';
|
import MkTab from '@/components/MkTab.vue';
|
||||||
import { i18n } from '@/i18n.js';
|
import { i18n } from '@/i18n.js';
|
||||||
|
import MkFoldableSection from '@/components/MkFoldableSection.vue';
|
||||||
|
import { instance } from '@/instance.js';
|
||||||
|
|
||||||
const paginationForNotes = {
|
const paginationForNotes = {
|
||||||
endpoint: 'notes/featured' as const,
|
endpoint: 'notes/featured' as const,
|
||||||
limit: 10,
|
limit: 10,
|
||||||
};
|
};
|
||||||
|
|
||||||
const paginationForPolls = {
|
const paginationForPollsLocal = {
|
||||||
endpoint: 'notes/polls/recommendation' as const,
|
endpoint: 'notes/polls/recommendation' as const,
|
||||||
limit: 10,
|
limit: 10,
|
||||||
offsetMode: true,
|
offsetMode: true,
|
||||||
params: {
|
params: {
|
||||||
excludeChannels: true,
|
excludeChannels: true,
|
||||||
|
local: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const paginationForPollsRemote = {
|
||||||
|
endpoint: 'notes/polls/recommendation' as const,
|
||||||
|
limit: 10,
|
||||||
|
offsetMode: true,
|
||||||
|
params: {
|
||||||
|
excludeChannels: true,
|
||||||
|
local: false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const paginationForPollsExpired = {
|
||||||
|
endpoint: 'notes/polls/recommendation' as const,
|
||||||
|
limit: 10,
|
||||||
|
offsetMode: true,
|
||||||
|
params: {
|
||||||
|
excludeChannels: true,
|
||||||
|
local: null,
|
||||||
|
expired: true,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3840,7 +3840,7 @@ declare module '../api.js' {
|
||||||
/**
|
/**
|
||||||
* No description provided.
|
* No description provided.
|
||||||
*
|
*
|
||||||
* **Credential required**: *Yes* / **Permission**: *read:account*
|
* **Credential required**: *No*
|
||||||
*/
|
*/
|
||||||
request<E extends 'notes/polls/recommendation', P extends Endpoints[E]['req']>(
|
request<E extends 'notes/polls/recommendation', P extends Endpoints[E]['req']>(
|
||||||
endpoint: E,
|
endpoint: E,
|
||||||
|
|
|
@ -3317,7 +3317,7 @@ export type paths = {
|
||||||
* notes/polls/recommendation
|
* notes/polls/recommendation
|
||||||
* @description No description provided.
|
* @description No description provided.
|
||||||
*
|
*
|
||||||
* **Credential required**: *Yes* / **Permission**: *read:account*
|
* **Credential required**: *No*
|
||||||
*/
|
*/
|
||||||
post: operations['notes___polls___recommendation'];
|
post: operations['notes___polls___recommendation'];
|
||||||
};
|
};
|
||||||
|
@ -27492,7 +27492,7 @@ export type operations = {
|
||||||
* notes/polls/recommendation
|
* notes/polls/recommendation
|
||||||
* @description No description provided.
|
* @description No description provided.
|
||||||
*
|
*
|
||||||
* **Credential required**: *Yes* / **Permission**: *read:account*
|
* **Credential required**: *No*
|
||||||
*/
|
*/
|
||||||
notes___polls___recommendation: {
|
notes___polls___recommendation: {
|
||||||
requestBody: {
|
requestBody: {
|
||||||
|
@ -27504,6 +27504,10 @@ export type operations = {
|
||||||
offset?: number;
|
offset?: number;
|
||||||
/** @default false */
|
/** @default false */
|
||||||
excludeChannels?: boolean;
|
excludeChannels?: boolean;
|
||||||
|
/** @default null */
|
||||||
|
local?: boolean | null;
|
||||||
|
/** @default false */
|
||||||
|
expired?: boolean;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -572,6 +572,10 @@ bubbleTimelineMustBeEnabled: "Note: the bubble timeline is hidden by default, an
|
||||||
popularUsersGlobal: "Users popular on the global network"
|
popularUsersGlobal: "Users popular on the global network"
|
||||||
popularUsersLocal: "Users popular on {name}"
|
popularUsersLocal: "Users popular on {name}"
|
||||||
|
|
||||||
|
pollsOnLocal: "Polls trending on {host}"
|
||||||
|
pollsOnRemote: "Polls trending on the global network"
|
||||||
|
pollsExpired: "Polls that have ended recently"
|
||||||
|
|
||||||
silenced: "Silenced"
|
silenced: "Silenced"
|
||||||
totalFollowers: "Total followers"
|
totalFollowers: "Total followers"
|
||||||
totalFollowing: "Total following"
|
totalFollowing: "Total following"
|
||||||
|
|
Loading…
Add table
Reference in a new issue