Skip to content

Commit

Permalink
enhance: ミュート期限を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
hideki0403 committed Jun 29, 2024
1 parent 8f9f93e commit c29bfa5
Showing 1 changed file with 58 additions and 70 deletions.
128 changes: 58 additions & 70 deletions packages/frontend/src/scripts/get-user-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,58 @@ import { IRouter } from '@/nirax.js';
import { antennasCache, rolesCache, userListsCache } from '@/cache.js';
import { mainRouter } from '@/router/main.js';

type PeriodType = {
key: string;
time: number | null;
text: string;
};

const period: PeriodType[] = [{
key: 'indefinitely',
time: null,
text: i18n.ts.indefinitely,
}, {
key: 'tenMinutes',
time: 1000 * 60 * 10,
text: i18n.tsx._timeIn.minutes({ n: (10).toString() }),
}, {
key: 'oneHour',
time: 1000 * 60 * 60,
text: i18n.tsx._timeIn.hours({ n: (1).toString() }),
}, {
key: 'sixHours',
time: 1000 * 60 * 60 * 6,
text: i18n.tsx._timeIn.hours({ n: (6).toString() }),
}, {
key: 'twelveHours',
time: 1000 * 60 * 60 * 12,
text: i18n.tsx._timeIn.hours({ n: (12).toString() }),
}, {
key: 'oneDay',
time: 1000 * 60 * 60 * 24,
text: i18n.tsx._timeIn.days({ n: (1).toString() }),
}, {
key: 'oneWeek',
time: 1000 * 60 * 60 * 24 * 7,
text: i18n.tsx._timeIn.weeks({ n: (1).toString() }),
}];

async function getPeriod(title: string, text?: string) {
const { canceled, result } = await os.select({
title,
text,
items: period.map(x => ({
value: x.key,
text: x.text,
})),
default: 'indefinitely',
});
if (canceled) return false;

const periodTime = period.find(x => x.key === result)?.time;
return periodTime == null ? null : Date.now() + periodTime;
}

export function getUserMenu(user: Misskey.entities.UserDetailed, router: IRouter = mainRouter) {
const meId = $i ? $i.id : null;

Expand All @@ -30,29 +82,8 @@ export function getUserMenu(user: Misskey.entities.UserDetailed, router: IRouter
user.isMuted = false;
});
} else {
const { canceled, result: period } = await os.select({
title: i18n.ts.mutePeriod,
items: [{
value: 'indefinitely', text: i18n.ts.indefinitely,
}, {
value: 'tenMinutes', text: i18n.ts.tenMinutes,
}, {
value: 'oneHour', text: i18n.ts.oneHour,
}, {
value: 'oneDay', text: i18n.ts.oneDay,
}, {
value: 'oneWeek', text: i18n.ts.oneWeek,
}],
default: 'indefinitely',
});
if (canceled) return;

const expiresAt = period === 'indefinitely' ? null
: period === 'tenMinutes' ? Date.now() + (1000 * 60 * 10)
: period === 'oneHour' ? Date.now() + (1000 * 60 * 60)
: period === 'oneDay' ? Date.now() + (1000 * 60 * 60 * 24)
: period === 'oneWeek' ? Date.now() + (1000 * 60 * 60 * 24 * 7)
: null;
const expiresAt = await getPeriod(i18n.ts.mutePeriod);
if (expiresAt === false) return;

os.apiWithDialog('mute/create', {
userId: user.id,
Expand Down Expand Up @@ -103,30 +134,8 @@ export function getUserMenu(user: Misskey.entities.UserDetailed, router: IRouter
if (user.isMuted) return toggleBlock();
if (user.isBlocking) return toggleMute();

const { canceled, result: period } = await os.select({
title: i18n.ts.muteAndBlockConfirm,
text: i18n.ts.mutePeriod,
items: [{
value: 'indefinitely', text: i18n.ts.indefinitely,
}, {
value: 'tenMinutes', text: i18n.ts.tenMinutes,
}, {
value: 'oneHour', text: i18n.ts.oneHour,
}, {
value: 'oneDay', text: i18n.ts.oneDay,
}, {
value: 'oneWeek', text: i18n.ts.oneWeek,
}],
default: 'indefinitely',
});
if (canceled) return;

const expiresAt = period === 'indefinitely' ? null
: period === 'tenMinutes' ? Date.now() + (1000 * 60 * 10)
: period === 'oneHour' ? Date.now() + (1000 * 60 * 60)
: period === 'oneDay' ? Date.now() + (1000 * 60 * 60 * 24)
: period === 'oneWeek' ? Date.now() + (1000 * 60 * 60 * 24 * 7)
: null;
const expiresAt = await getPeriod(i18n.ts.muteAndBlockConfirm, i18n.ts.mutePeriod);
if (expiresAt === false) return;

await os.apiWithDialog('mute/create', {
userId: user.id,
Expand Down Expand Up @@ -314,29 +323,8 @@ export function getUserMenu(user: Misskey.entities.UserDetailed, router: IRouter
return roles.filter(r => r.target === 'manual').map(r => ({
text: r.name,
action: async () => {
const { canceled, result: period } = await os.select({
title: i18n.ts.period + ': ' + r.name,
items: [{
value: 'indefinitely', text: i18n.ts.indefinitely,
}, {
value: 'oneHour', text: i18n.ts.oneHour,
}, {
value: 'oneDay', text: i18n.ts.oneDay,
}, {
value: 'oneWeek', text: i18n.ts.oneWeek,
}, {
value: 'oneMonth', text: i18n.ts.oneMonth,
}],
default: 'indefinitely',
});
if (canceled) return;

const expiresAt = period === 'indefinitely' ? null
: period === 'oneHour' ? Date.now() + (1000 * 60 * 60)
: period === 'oneDay' ? Date.now() + (1000 * 60 * 60 * 24)
: period === 'oneWeek' ? Date.now() + (1000 * 60 * 60 * 24 * 7)
: period === 'oneMonth' ? Date.now() + (1000 * 60 * 60 * 24 * 30)
: null;
const expiresAt = await getPeriod(i18n.ts.period + ': ' + r.name);
if (expiresAt === false) return;

os.apiWithDialog('admin/roles/assign', { roleId: r.id, userId: user.id, expiresAt });
},
Expand Down

0 comments on commit c29bfa5

Please sign in to comment.