Skip to content

Commit

Permalink
Merge pull request #104 from kamecha/feature/stamp-view
Browse files Browse the repository at this point in the history
メッセージについてるスタンプを表示
  • Loading branch information
kamecha authored Aug 9, 2024
2 parents 312a86c + 3f1160f commit bbbca38
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 3 deletions.
22 changes: 21 additions & 1 deletion autoload/traqvim/view.vim
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,31 @@ function traqvim#view#make_message_body(message, width) abort
endfor
endif
endif
let stamps = []
if !empty(a:message.stamps)
let stamps += [ "{{{" ]
endif
for stamp in a:message.stamps
let s = denops#request('traqvim', 'getStamp', [stamp.stampId])
let user = denops#request('traqvim', 'getUser', [stamp.userId])
let stamps += [ ":" . s.name . ":" . user.displayName . "(" . stamp.count . ")" ]
endfor
if !empty(a:message.stamps)
let stamps += [ "}}}" ]
endif
let footer = [ "", repeat("", a:width - 2) ] " 2はsigncolumnの分
let messageBody = header + rows + quotes->flatten() + footer
let messageBody = header + rows + quotes->flatten() + stamps + footer
return #{ body: messageBody, position: #{ quote: quotePos }}
endfunction

function traqvim#view#folded_stamp_text() abort
let stamps = []
for l in getline(v:foldstart + 1, v:foldend - 1)
let stamps += [ matchstr(l, "^:[^:]*:") ]
endfor
return stamps->uniq()->join(" ")
endfunction

function traqvim#view#update_message_position(timeline, key, value) abort
if a:key == 0
let start = 1
Expand Down
11 changes: 11 additions & 0 deletions denops/traqvim/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
channelsRecursive,
channelTimeline,
channelUUID,
getStamp,
getUser,
homeChannelId,
homeChannelPath,
sendMessage,
Expand All @@ -16,6 +18,7 @@ import {
fn,
helper,
is,
traq,
vars,
} from "./deps.ts";
import {
Expand Down Expand Up @@ -399,4 +402,12 @@ export async function main(denops: Denops) {
const d = new Date(date);
return Promise.resolve(d.toLocaleString("ja-JP"));
};
denops.dispatcher["getUser"] = (userId: unknown): Promise<traq.User> => {
assert(userId, is.String);
return getUser(userId);
};
denops.dispatcher["getStamp"] = (stampId: unknown): Promise<traq.Stamp> => {
assert(stampId, is.String);
return getStamp(stampId);
};
}
98 changes: 98 additions & 0 deletions denops/traqvim/message_bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { traq } from "./deps.ts";
import {
channelMessageOptions,
channelTimeline,
channelUUID,
getStamp,
getUser,
homeChannelId,
} from "./model.ts";
import { Message } from "./type.d.ts";
import { api } from "./api.ts";

api.tokenFilePath = "";

let channelId: string;

// $ deno bench --allow-read --allow-net -- --#gps/times/kamecha
if (Deno.args.length > 0) {
// --#gps/times/kamecha
const channelPath = Deno.args[0];
// --#gps/times/kamecha -> ["gps", "times", "kamecha"]
const channelPathArray = channelPath.slice(3).split("/");
channelId = await channelUUID(channelPathArray) ?? "";
} else {
channelId = await homeChannelId();
}

const timelineOption: channelMessageOptions = {
id: channelId,
limit: 10,
until: new Date().toISOString(),
inclusive: true,
order: "desc",
};

const messages: Message[] = await channelTimeline(timelineOption);

console.log("messages length:", messages.length);

for (const message of messages) {
console.log("stamps length:", message.stamps.length);
}

Deno.bench("user & stamp cache", async (b: Deno.BenchContext) => {
const userCache: Record<string, traq.User> = {};
const stampCache: Record<string, traq.Stamp> = {};
b.start();
for (const message of messages) {
for (const stamp of message.stamps) {
if (!stampCache[stamp.stampId]) {
stampCache[stamp.stampId] = await getStamp(stamp.stampId);
}
if (!userCache[stamp.userId]) {
userCache[stamp.userId] = await getUser(stamp.userId);
}
}
}
b.end();
});

Deno.bench("stamp cache", async (b: Deno.BenchContext) => {
const stampCache: Record<string, traq.Stamp> = {};
b.start();
for (const message of messages) {
for (const stamp of message.stamps) {
if (!stampCache[stamp.stampId]) {
stampCache[stamp.stampId] = await getStamp(stamp.stampId);
}
await getUser(stamp.userId);
}
}
b.end();
});

Deno.bench("user cache", async (b: Deno.BenchContext) => {
const userCache: Record<string, traq.User> = {};
b.start();
for (const message of messages) {
for (const stamp of message.stamps) {
await getStamp(stamp.stampId);
if (!userCache[stamp.userId]) {
userCache[stamp.userId] = await getUser(stamp.userId);
}
}
}
b.end();
});

Deno.bench("no cache", async (b: Deno.BenchContext) => {
b.start();
for (const message of messages) {
for (const stamp of message.stamps) {
await getStamp(stamp.stampId);
await getUser(stamp.userId);
}
}
b.end();
});
24 changes: 22 additions & 2 deletions denops/traqvim/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export type channelMessageOptions = {
// TODO: ↓チャンネル周りはclassに分離しても良いかも
const channelMapCache: Map<string, traq.Channel> = new Map();

const UserMapCache = new Map<string, traq.UserDetail>();

const StampMapCache = new Map<string, traq.Stamp>();

const getChannelMapCache = () => {
return channelMapCache;
};
Expand Down Expand Up @@ -138,8 +142,12 @@ export const homeChannelId = async (): Promise<string> => {

// userIdからユーザー情報を取得する
export const getUser = async (userId: string): Promise<traq.User> => {
const userRes = await api.api.getUser(userId);
const userDetail: traq.UserDetail = userRes.data;
let userDetail: traq.UserDetail | undefined = UserMapCache.get(userId);
if (userDetail === undefined) {
const userRes = await api.api.getUser(userId);
userDetail = userRes.data;
UserMapCache.set(userId, userDetail);
}
// TODO: もっと良い変換方法ありそうなんで、見つけたらやっとく
const user: traq.User = {
id: userDetail.id,
Expand Down Expand Up @@ -349,3 +357,15 @@ export const removePin = async (
console.error(e);
}
};

export const getStamp = async (
stampId: string,
): Promise<traq.Stamp> => {
let stamp: traq.Stamp | undefined = StampMapCache.get(stampId);
if (stamp === undefined) {
const stampRes = await api.api.getStamp(stampId);
stamp = stampRes.data;
StampMapCache.set(stampId, stamp);
}
return stamp;
};
2 changes: 2 additions & 0 deletions ftplugin/traqvim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
setlocal noswapfile
setlocal signcolumn=yes
setlocal nolist
setlocal foldmethod=marker
setlocal foldtext=traqvim#view#folded_stamp_text()

" TODO: nerd font導入してるかの確認とかしたいな
" call sign_define("pin", #{ text: "📌"})
Expand Down

0 comments on commit bbbca38

Please sign in to comment.