Skip to content

Commit

Permalink
Merge pull request #99 from kamecha/feature/open-channel-via-message
Browse files Browse the repository at this point in the history
メッセージに応じたチャンネルを開けるようにした
  • Loading branch information
kamecha authored Jul 29, 2024
2 parents 54bc745 + 2474802 commit 4021326
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 22 deletions.
8 changes: 8 additions & 0 deletions autoload/traqvim/message.vim
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ function traqvim#message#message_next() abort
call cursor([next.position["start"], 0])
endfunction

function traqvim#message#goto_message() abort
let message = traqvim#message#get_message()
if empty(message)
return
endif
call denops#request('traqvim', 'timelineMessage', [message])
endfunction

function traqvim#message#registerYankMessageLink() abort
let &opfunc = function('traqvim#message#yankMessageLink')
return 'g@'
Expand Down
6 changes: 4 additions & 2 deletions autoload/traqvim/view.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

" Message { user : {id, name, displayName}, content, createdAt }
function! traqvim#view#make_message_body(message, width) abort
let header = [ a:message["user"]["displayName"] . " @" . a:message["user"]["name"] . " " . a:message["createdAt"], "" ]
let createdAt = denops#request('traqvim', 'convertDate', [a:message["createdAt"]])
let header = [ a:message["user"]["displayName"] . " @" . a:message["user"]["name"] . " " . createdAt, "" ]
let rows = split(a:message["content"], "\n")
let quote = []
if a:message->has_key("quote")
if type(a:message["quote"]) == type([])
for q in a:message["quote"]
let createdAt = denops#request('traqvim', 'convertDate', [q["createdAt"]])
let quote += [ "", ">"]
let quote += [ "\t". q["user"]["displayName"] . " @" . q["user"]["name"] . " " . q["createdAt"], "" ]
let quote += [ "\t". q["user"]["displayName"] . " @" . q["user"]["name"] . " " . createdAt, "" ]
let quote += map(split(q["content"], "\n"), { _, v -> "\t" . v })
let quote += [ "", "<"]
endfor
Expand Down
8 changes: 6 additions & 2 deletions denops/traqvim/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ChannelBuffer, Message } from "./type.d.ts";
import {
activity,
channelMessageOptions,
channelPath,
channelTimeline,
createPin,
deleteMessage,
Expand All @@ -14,17 +15,20 @@ import { isMessage } from "./type_check.ts";
export const actionOpenChannel = async (
denops: Denops,
channelMessageOptions: channelMessageOptions,
message?: Message,
bufNum?: number,
): Promise<void> => {
helper.echo(denops, "actionOpenChannel");
const timeline: Message[] = await channelTimeline(channelMessageOptions);
if (channelMessageOptions.channelPath === undefined) {
helper.echoerr(denops, "channelPath is undefined");
return;
channelMessageOptions.channelPath = await channelPath(
channelMessageOptions.id,
);
}
const escapedChannelPath = bufname.format({
scheme: "VtraQ",
expr: "/Channel",
params: message ? { message: message.id } : undefined,
fragment: channelMessageOptions.channelPath.replace("#", ""),
});
const channelBufferVars: ChannelBuffer = {
Expand Down
40 changes: 27 additions & 13 deletions denops/traqvim/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export async function main(denops: Denops) {
channelPath: homePath,
limit: limit,
until: new Date().toISOString(),
inclusive: true,
order: "desc",
};
await actionOpenChannel(denops, timelineOption);
Expand All @@ -117,11 +118,26 @@ export async function main(denops: Denops) {
channelPath: channelPath,
limit: limit,
until: new Date().toISOString(),
inclusive: true,
order: "desc",
};
await actionOpenChannel(denops, timelineOption);
return;
},
async timelineMessage(message: unknown): Promise<unknown> {
assert(message, isMessage);
const limit = await vars.globals.get(denops, "traqvim#fetch_limit");
assert(limit, is.Number);
const timelineOption: channelMessageOptions = {
id: message.channelId,
limit: limit,
until: message.createdAt,
inclusive: true,
order: "desc",
};
await actionOpenChannel(denops, timelineOption, message);
return;
},
async activity(): Promise<unknown> {
await actionOpenActivity(denops);
return;
Expand All @@ -147,9 +163,10 @@ export async function main(denops: Denops) {
channelPath: bufNameWithoutNumber,
limit: limit,
until: new Date().toISOString(),
inclusive: true,
order: "desc",
};
actionOpenChannel(denops, timelineOption, bufNum);
actionOpenChannel(denops, timelineOption, undefined, bufNum);
}
return;
},
Expand All @@ -170,19 +187,14 @@ export async function main(denops: Denops) {
id: channelID,
channelPath: bufNameWithoutNumber,
limit: limit,
since: new Date(timeline[timeline.length - 1].createdAt)
.toISOString(),
since: timeline[timeline.length - 1].createdAt,
};
const forwardTimeline: Message[] = await channelTimeline(
timelineOption,
);
await actionForwardChannelMessage(
denops,
// 一番古いメッセージを削除
forwardTimeline.filter((message: Message) => {
return message.createdAt !==
timeline[timeline.length - 1].createdAt;
}),
forwardTimeline,
bufNum,
);
} catch (e) {
Expand All @@ -207,17 +219,14 @@ export async function main(denops: Denops) {
id: channelID,
channelPath: bufNameWithoutNumber,
limit: limit,
until: new Date(timeline[0].createdAt).toISOString(),
until: timeline[0].createdAt,
};
const backTimeline: Message[] = await channelTimeline(
timelineOption,
);
await actionBackChannelMessage(
denops,
// 受け取ったメッセージの中で一番新しい重複メッセージを削除
backTimeline.filter((message: Message) => {
return message.createdAt !== timeline[0].createdAt;
}),
backTimeline,
bufNum,
);
} catch (e) {
Expand Down Expand Up @@ -385,4 +394,9 @@ export async function main(denops: Denops) {
const channels = await channelsRecursive();
return channels;
};
denops.dispatcher["convertDate"] = (date: unknown): Promise<string> => {
assert(date, is.String);
const d = new Date(date);
return Promise.resolve(d.toLocaleString("ja-JP"));
};
}
5 changes: 0 additions & 5 deletions denops/traqvim/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ export const channelTimeline = async (
const ret: Message = {
...message,
user: user,
createdAt: new Date(message.createdAt).toLocaleString("ja-JP"),
};
// quotedMessageUUIDsが存在しなかった場合はundefinedを返す
let quotedMessages: Message[] | undefined = undefined;
Expand All @@ -199,9 +198,6 @@ export const channelTimeline = async (
const ret: Message = {
...quotedMessage,
user: user,
createdAt: new Date(quotedMessage.createdAt).toLocaleString(
"ja-JP",
),
};
return ret;
}),
Expand Down Expand Up @@ -265,7 +261,6 @@ export const activity = async (): Promise<Message[]> => {
return {
...message,
user: user,
createdAt: new Date(activity.createdAt).toLocaleString("ja-JP"),
};
}),
);
Expand Down
3 changes: 3 additions & 0 deletions doc/traqvim.jax
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ g:traqvim#token_file_path
<Plug>(traqvim-prev) *<Plug>(traqvim-prev)*
前のメッセージへカーソルを移動させます

<Plug>(traqvim-goto) *<Plug>(traqvim-goto)*
メッセージに対応するチャンネルへ移動します

<Plug>(traqvim-motion-message) *<Plug>(traqvim-motion-message)*
カーソル下の該当メッセージの始まり・終わりを選択します
レイアウトで表示されるものが選択されます
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/traqvim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ nnoremap <buffer><silent> <Plug>(traqvim-next)
\ <Cmd>call traqvim#message#message_next()<CR>
nnoremap <buffer><silent> <Plug>(traqvim-prev)
\ <Cmd>call traqvim#message#message_prev()<CR>
nnoremap <buffer><silent> <Plug>(traqvim-goto)
\ <Cmd>call traqvim#message#goto_message()<CR>
nnoremap <buffer><expr> <Plug>(traqvim-operator-message-yank-link)
\ traqvim#message#registerYankMessageLink()
Expand Down

0 comments on commit 4021326

Please sign in to comment.