藍#apiの型パラメータにReturnTypeを追加

This commit is contained in:
takejohn 2024-03-24 21:09:14 +09:00
parent f893fd3a3f
commit a2ebc9401c
5 changed files with 40 additions and 10 deletions

View file

@ -17,6 +17,7 @@ import Stream from '@/stream.js';
import log from '@/utils/log.js'; import log from '@/utils/log.js';
import { sleep } from './utils/sleep.js'; import { sleep } from './utils/sleep.js';
import pkg from '../package.json' assert { type: 'json' }; import pkg from '../package.json' assert { type: 'json' };
import { Note } from './misskey/note.js';
type MentionHook = (msg: Message) => Promise<boolean | HandlerResult>; type MentionHook = (msg: Message) => Promise<boolean | HandlerResult>;
type ContextHook = (key: any, msg: Message, data?: any) => Promise<void | boolean | HandlerResult>; type ContextHook = (key: any, msg: Message, data?: any) => Promise<void | boolean | HandlerResult>;
@ -361,7 +362,7 @@ export default class 藍 {
*/ */
@bindThis @bindThis
public async post(param: any) { public async post(param: any) {
const res = await this.api('notes/create', param); const res = await this.api<{ createdNote: Note }>('notes/create', param);
return res.createdNote; return res.createdNote;
} }
@ -380,13 +381,13 @@ export default class 藍 {
* APIを呼び出します * APIを呼び出します
*/ */
@bindThis @bindThis
public api(endpoint: string, param?: any) { public api<ReturnType = unknown>(endpoint: string, param?: any) {
this.log(`API: ${endpoint}`); this.log(`API: ${endpoint}`);
return got.post(`${config.apiUrl}/${endpoint}`, { return got.post(`${config.apiUrl}/${endpoint}`, {
json: Object.assign({ json: Object.assign({
i: config.i i: config.i
}, param) }, param)
}).json(); }).json<ReturnType>();
}; };
/** /**

View file

@ -34,6 +34,7 @@ import NotingModule from './modules/noting/index.js';
import PollModule from './modules/poll/index.js'; import PollModule from './modules/poll/index.js';
import ReminderModule from './modules/reminder/index.js'; import ReminderModule from './modules/reminder/index.js';
import CheckCustomEmojisModule from './modules/check-custom-emojis/index.js'; import CheckCustomEmojisModule from './modules/check-custom-emojis/index.js';
import { User } from './misskey/user.js';
console.log(' __ ____ _____ ___ '); console.log(' __ ____ _____ ___ ');
console.log(' /__\\ (_ _)( _ )/ __)'); console.log(' /__\\ (_ _)( _ )/ __)');
@ -61,7 +62,7 @@ promiseRetry(retry => {
json: { json: {
i: config.i i: config.i
} }
}).json().catch(retry); }).json<User>().catch(retry);
}, { }, {
retries: 3 retries: 3
}).then(account => { }).then(account => {

View file

@ -61,7 +61,7 @@ export default class Message {
this.friend = new Friend(ai, { user: this.user }); this.friend = new Friend(ai, { user: this.user });
// メッセージなどに付いているユーザー情報は省略されている場合があるので完全なユーザー情報を持ってくる // メッセージなどに付いているユーザー情報は省略されている場合があるので完全なユーザー情報を持ってくる
this.ai.api('users/show', { this.ai.api<User>('users/show', {
userId: this.userId userId: this.userId
}).then(user => { }).then(user => {
this.friend.updateUser(user); this.friend.updateUser(user);

View file

@ -6,6 +6,27 @@ import { renderChart } from './render-chart.js';
import { items } from '@/vocabulary.js'; import { items } from '@/vocabulary.js';
import config from '@/config.js'; import config from '@/config.js';
type UserNotes = {
diffs: {
normal: number[],
reply: number[],
renote: number[]
}
};
type LocalRemotePair<T> = {
local: T,
remote: T
};
type UserFollowing = LocalRemotePair<{
followers: {
total: number[]
}
}>;
type Notes = LocalRemotePair<UserNotes>
export default class extends Module { export default class extends Module {
public readonly name = 'chart'; public readonly name = 'chart';
@ -48,7 +69,7 @@ export default class extends Module {
let chart; let chart;
if (type === 'userNotes') { if (type === 'userNotes') {
const data = await this.ai.api('charts/user/notes', { const data = await this.ai.api<UserNotes>('charts/user/notes', {
span: 'day', span: 'day',
limit: 30, limit: 30,
userId: params.user.id userId: params.user.id
@ -65,7 +86,7 @@ export default class extends Module {
}] }]
}; };
} else if (type === 'followers') { } else if (type === 'followers') {
const data = await this.ai.api('charts/user/following', { const data = await this.ai.api<UserFollowing>('charts/user/following', {
span: 'day', span: 'day',
limit: 30, limit: 30,
userId: params.user.id userId: params.user.id
@ -80,7 +101,7 @@ export default class extends Module {
}] }]
}; };
} else if (type === 'notes') { } else if (type === 'notes') {
const data = await this.ai.api('charts/notes', { const data = await this.ai.api<Notes>('charts/notes', {
span: 'day', span: 'day',
limit: 30, limit: 30,
}); });

View file

@ -5,6 +5,12 @@ import config from '@/config.js';
import serifs from '@/serifs.js'; import serifs from '@/serifs.js';
import { mecab } from './mecab.js'; import { mecab } from './mecab.js';
type LocalTimeline = {
userId: string;
text: string | null;
cw: string | null;
}[];
function kanaToHira(str: string) { function kanaToHira(str: string) {
return str.replace(/[\u30a1-\u30f6]/g, match => { return str.replace(/[\u30a1-\u30f6]/g, match => {
const chr = match.charCodeAt(0) - 0x60; const chr = match.charCodeAt(0) - 0x60;
@ -35,7 +41,7 @@ export default class extends Module {
@bindThis @bindThis
private async learn() { private async learn() {
const tl = await this.ai.api('notes/local-timeline', { const tl = await this.ai.api<LocalTimeline>('notes/local-timeline', {
limit: 30 limit: 30
}); });
@ -47,7 +53,8 @@ export default class extends Module {
let keywords: string[][] = []; let keywords: string[][] = [];
for (const note of interestedNotes) { for (const note of interestedNotes) {
const tokens = await mecab(note.text, config.mecab, config.mecabDic); // TODO: note.text に null チェックが必要?
const tokens = await mecab(note.text as string, config.mecab, config.mecabDic);
const keywordsInThisNote = tokens.filter(token => token[2] == '固有名詞' && token[8] != null); const keywordsInThisNote = tokens.filter(token => token[2] == '固有名詞' && token[8] != null);
keywords = keywords.concat(keywordsInThisNote); keywords = keywords.concat(keywordsInThisNote);
} }