From 5fc375bed2c89b4d04fe2c8e7a27748c1337d5a4 Mon Sep 17 00:00:00 2001 From: kamecha Date: Sun, 27 Oct 2024 16:47:55 +0900 Subject: [PATCH 1/6] =?UTF-8?q?stamp=E5=91=A8=E3=82=8A=E3=81=AEmodel?= =?UTF-8?q?=E3=82=92=E6=95=B4=E5=82=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- denops/traqvim/model.ts | 57 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/denops/traqvim/model.ts b/denops/traqvim/model.ts index 3462cae..19dfe6d 100644 --- a/denops/traqvim/model.ts +++ b/denops/traqvim/model.ts @@ -275,13 +275,6 @@ export const activity = async (): Promise => { return activitiesConverted; }; -// stamp情報の取得 -export const getStamps = async (): Promise => { - const stampsRes = await api.api.getStamps(); - const stamps = stampsRes.data; - return stamps; -}; - export const sendMessage = async ( channelUUID: string, content: string, @@ -358,6 +351,15 @@ export const removePin = async ( } }; +export const getStamps = async (): Promise => { + const stampsRes = await api.api.getStamps(); + const stamps = stampsRes.data; + stamps.forEach((stamp: traq.Stamp) => { + StampMapCache.set(stamp.id, stamp); + }); + return stamps; +}; + export const getStamp = async ( stampId: string, ): Promise => { @@ -385,4 +387,45 @@ export const downloadFile = async ( console.error(e); } return new Uint8Array(); +} + +export const getStampId = async ( + stampName: string, +): Promise => { + const stamps = await getStamps(); + const stamp = stamps.find((stamp: traq.Stamp) => { + return stamp.name === stampName; + }); + return stamp?.id; +} + +export const getMessageStamps = async ( + messageId: string, +): Promise => { + const stmpasRes = await api.api.getMessageStamps(messageId); + return stmpasRes.data; +}; + +export const addMessageStamp = async ( + messageId: string, + stampId: string, +): Promise => { + try { + // TODO: postだから失敗するかも + await api.api.addMessageStamp(messageId, stampId); + } catch (e) { + console.error(e); + } +}; + +export const removeMessageStamp = async ( + messageId: string, + stampId: string, +): Promise => { + try { + // TODO: postだから失敗するかも + await api.api.removeMessageStamp(messageId, stampId); + } catch (e) { + console.error(e); + } }; From e8b666aad8cd6274266e24cff14e78a130d96dc8 Mon Sep 17 00:00:00 2001 From: kamecha Date: Sun, 18 Aug 2024 20:42:42 +0900 Subject: [PATCH 2/6] =?UTF-8?q?action=E3=82=92=E6=95=B4=E5=82=99(=E7=94=BB?= =?UTF-8?q?=E9=9D=A2=E6=9B=B4=E6=96=B0=E3=81=86=E3=81=BE=E3=81=8F=E3=81=84?= =?UTF-8?q?=E3=81=A3=E3=81=A6=E3=81=AA=E3=81=84)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- denops/traqvim/action.ts | 49 ++++++++++++++++++++++++++++++++++++++++ denops/traqvim/main.ts | 25 ++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/denops/traqvim/action.ts b/denops/traqvim/action.ts index f642b9c..611465f 100644 --- a/denops/traqvim/action.ts +++ b/denops/traqvim/action.ts @@ -2,12 +2,14 @@ import { assert, bufname, Denops, fn, helper, is, vars } from "./deps.ts"; import { ChannelBuffer, Message } from "./type.d.ts"; import { activity, + addMessageStamp, channelMessageOptions, channelPath, channelTimeline, createPin, deleteMessage, editMessage, + getMessageStamps, removePin, } from "./model.ts"; import { isMessage } from "./type_check.ts"; @@ -272,3 +274,50 @@ export const actionRemovePin = async ( ); await denops.call("traqvim#view#draw_message_pin", bufNum, message); }; + +export const actionAddMessageStamp = async ( + denops: Denops, + message: Message, + stamp: string, + bufNum: number, +): Promise => { + try { + await addMessageStamp(message.id, stamp); + } catch (e) { + console.error(e); + return; + } + // 既存メッセージの取得 + const timeline = await vars.buffers.get(denops, "channelTimeline"); + assert(timeline, is.ArrayOf(isMessage)); + // 一旦対象メッセージを削除する + await vars.buffers.set( + denops, + "channelTimeline", + timeline.filter((m) => m.id !== message.id), + ); + await denops.call("traqvim#view#draw_delete_message", bufNum, message); + // 更新されたメッセージにあるスタンプを取得 + const updatedMessage = await getMessageStamps(message.id); + const editedTimeline = timeline.map((m) => { + if (m.id === message.id) { + return { + ...m, + stamps: updatedMessage + }; + } else { + return m; + } + }); + // 編集したものをセット + await vars.buffers.set( + denops, + "channelTimeline", + editedTimeline, + ); + await denops.call( + "traqvim#view#draw_append_message", + bufNum, + editedTimeline.find((m) => m.id === message.id), + ); +} diff --git a/denops/traqvim/main.ts b/denops/traqvim/main.ts index 73a3100..6c1a3d4 100644 --- a/denops/traqvim/main.ts +++ b/denops/traqvim/main.ts @@ -6,6 +6,8 @@ import { channelUUID, downloadFile, getStamp, + getStampId, + getStamps, getUser, homeChannelId, homeChannelPath, @@ -23,6 +25,7 @@ import { vars, } from "./deps.ts"; import { + actionAddMessageStamp, actionBackChannelMessage, actionCreatePin, actionDeleteMessage, @@ -403,6 +406,25 @@ export async function main(denops: Denops) { await denops.cmd(":bdelete"); return; }, + async messageAddStamps( + bufNum: unknown, + message: unknown, + stampNames: unknown, + ): Promise { + assert(bufNum, is.Number); + assert(message, isMessage); + assert(stampNames, is.ArrayOf(is.String)); + for (const stampName of stampNames) { + const stampId = await getStampId(stampName); + if (stampId === undefined) { + helper.echo(denops, `Stamp not found: ${stampName}`); + continue; + } + const stamp = await getStamp(stampId); + await actionAddMessageStamp(denops, message, stamp.id, bufNum); + } + return; + }, async createPin( bufNum: unknown, message: unknown, @@ -437,6 +459,9 @@ export async function main(denops: Denops) { assert(userId, is.String); return getUser(userId); }; + denops.dispatcher["getStamps"] = (): Promise => { + return getStamps(); + }; denops.dispatcher["getStamp"] = (stampId: unknown): Promise => { assert(stampId, is.String); return getStamp(stampId); From 79169a79d938653fa80c09ecaa395026cfe3dc3d Mon Sep 17 00:00:00 2001 From: kamecha Date: Sun, 18 Aug 2024 20:43:16 +0900 Subject: [PATCH 3/6] =?UTF-8?q?vim=E3=81=AEcmdline=E8=A3=9C=E5=AE=8C?= =?UTF-8?q?=E3=82=92=E6=95=B4=E5=82=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autoload/traqvim/command.vim | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/autoload/traqvim/command.vim b/autoload/traqvim/command.vim index 655495a..dd419eb 100644 --- a/autoload/traqvim/command.vim +++ b/autoload/traqvim/command.vim @@ -20,6 +20,11 @@ let g:traqvim#command#subcommands = #{ \ impl: function('traqvim#command#message'), \ comp: function('traqvim#command#messageComplete'), \ }, + \ stamp: #{ + \ args: ['add', 'remove'], + \ impl: function('traqvim#command#stamp'), + \ comp: function('traqvim#command#stampComplete'), + \ }, \ pin: #{ \ args: ['create', 'remove'], \ impl: function('traqvim#command#pin'), @@ -114,6 +119,39 @@ function traqvim#command#messageComplete(arglead, cmdline) abort return g:traqvim#command#subcommands.message.args->copy()->filter({_, v -> v =~? '^' . a:arglead}) endfunction +function traqvim#command#stamp(args) abort + if a:args[0] ==# 'add' + call denops#request('traqvim', 'messageAddStamps', [bufnr(), traqvim#message#get_message(), a:args[1:]]) + elseif a:args[0] ==# 'remove' + call denops#request('traqvim', 'removeStamps', [bufnr(), traqvim#message#get_message(), a:args[1:]]) + endif +endfunction + +function traqvim#command#stampComplete(arglead, cmdline) abort + let cmds = split(a:cmdline) + " addの場合はスタンプ名を補完 + if len(cmds) >= 3 && cmds[2] ==# 'add' + let stamps = denops#request('traqvim', 'getStamps', []) + return stamps->map({_, v -> v['name']})->matchfuzzy(a:arglead) + endif + " removeの場合はスタンプに押されてるスタンプを補完 + if len(cmds) >= 3 && cmds[2] ==# 'remove' + let message = traqvim#message#get_message() + let stamps = message->get('stamps', []) + let ret = [] + " TODO: userIDを取得して自分が押したスタンプだけを補完する + for stamp in stamps + let s = denops#request('traqvim', 'getStamp', [stamp['stampId']]) + let ret += [s['name']] + endfor + return ret->matchfuzzy(a:arglead) + endif + if a:cmdline[strlen(a:cmdline)-1] ==# ' ' && len(split(a:cmdline)) >= 3 + return [] + endif + return g:traqvim#command#subcommands.stamp.args->copy()->filter({_, v -> v =~? '^' . a:arglead}) +endfunction + function traqvim#command#pin(args) abort if a:args[0] ==# 'create' call denops#request('traqvim', 'createPin', [bufnr(), traqvim#message#get_message()]) From de30e5a56aa84d56ad4b3618e309693de9080cb8 Mon Sep 17 00:00:00 2001 From: kamecha Date: Sun, 18 Aug 2024 20:43:37 +0900 Subject: [PATCH 4/6] =?UTF-8?q?todo=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autoload/traqvim/view.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/autoload/traqvim/view.vim b/autoload/traqvim/view.vim index b9299ae..e92cf01 100644 --- a/autoload/traqvim/view.vim +++ b/autoload/traqvim/view.vim @@ -190,6 +190,7 @@ endfunction function traqvim#view#draw_delete_message(bufNum, message) abort call setbufvar(a:bufNum, "&modifiable", 1) + " TODO: このstart, endがスタンプの行をどのように考慮してるか確認しとく let start = a:message.position["start"] let end = a:message.position["end"] if a:message->get('pinned') From 7bc6b69a9b43c5af8eecf5c3a24bb5a9b947c3d7 Mon Sep 17 00:00:00 2001 From: kamecha Date: Sun, 27 Oct 2024 18:41:16 +0900 Subject: [PATCH 5/6] =?UTF-8?q?=E3=82=B9=E3=82=BF=E3=83=B3=E3=83=97?= =?UTF-8?q?=E3=81=AE=E5=89=8A=E9=99=A4=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autoload/traqvim/command.vim | 2 +- denops/traqvim/action.ts | 48 ++++++++++++++++++++++++++++++++++++ denops/traqvim/main.ts | 20 +++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/autoload/traqvim/command.vim b/autoload/traqvim/command.vim index dd419eb..c774b97 100644 --- a/autoload/traqvim/command.vim +++ b/autoload/traqvim/command.vim @@ -123,7 +123,7 @@ function traqvim#command#stamp(args) abort if a:args[0] ==# 'add' call denops#request('traqvim', 'messageAddStamps', [bufnr(), traqvim#message#get_message(), a:args[1:]]) elseif a:args[0] ==# 'remove' - call denops#request('traqvim', 'removeStamps', [bufnr(), traqvim#message#get_message(), a:args[1:]]) + call denops#request('traqvim', 'messageRemoveStamps', [bufnr(), traqvim#message#get_message(), a:args[1:]]) endif endfunction diff --git a/denops/traqvim/action.ts b/denops/traqvim/action.ts index 611465f..6311fa1 100644 --- a/denops/traqvim/action.ts +++ b/denops/traqvim/action.ts @@ -10,6 +10,7 @@ import { deleteMessage, editMessage, getMessageStamps, + removeMessageStamp, removePin, } from "./model.ts"; import { isMessage } from "./type_check.ts"; @@ -321,3 +322,50 @@ export const actionAddMessageStamp = async ( editedTimeline.find((m) => m.id === message.id), ); } + +export const actionRemoveMessageStamp = async ( + denops: Denops, + message: Message, + stamp: string, + bufNum: number, +): Promise => { + try { + await removeMessageStamp(message.id, stamp); + } catch (e) { + console.error(e); + return; + } + // 既存メッセージの取得 + const timeline = await vars.buffers.get(denops, "channelTimeline"); + assert(timeline, is.ArrayOf(isMessage)); + // 一旦対象メッセージを削除する + await vars.buffers.set( + denops, + "channelTimeline", + timeline.filter((m) => m.id !== message.id), + ); + await denops.call("traqvim#view#draw_delete_message", bufNum, message); + // 更新されたメッセージにあるスタンプを取得 + const updatedMessage = await getMessageStamps(message.id); + const editedTimeline = timeline.map((m) => { + if (m.id === message.id) { + return { + ...m, + stamps: updatedMessage, + }; + } else { + return m; + } + }); + // 編集したものをセット + await vars.buffers.set( + denops, + "channelTimeline", + editedTimeline, + ); + await denops.call( + "traqvim#view#draw_append_message", + bufNum, + editedTimeline.find((m) => m.id === message.id), + ); +}; diff --git a/denops/traqvim/main.ts b/denops/traqvim/main.ts index 6c1a3d4..584a254 100644 --- a/denops/traqvim/main.ts +++ b/denops/traqvim/main.ts @@ -33,6 +33,7 @@ import { actionForwardChannelMessage, actionOpenActivity, actionOpenChannel, + actionRemoveMessageStamp, actionRemovePin, actionYankMessageLink, actionYankMessageMarkdown, @@ -425,6 +426,25 @@ export async function main(denops: Denops) { } return; }, + async messageRemoveStamps( + bufNum: unknown, + message: unknown, + stampNames: unknown, + ): Promise { + assert(bufNum, is.Number); + assert(message, isMessage); + assert(stampNames, is.ArrayOf(is.String)); + for (const stampName of stampNames) { + const stampId = await getStampId(stampName); + if (stampId === undefined) { + helper.echo(denops, `Stamp not found: ${stampName}`); + continue; + } + const stamp = await getStamp(stampId); + await actionRemoveMessageStamp(denops, message, stamp.id, bufNum); + } + return; + }, async createPin( bufNum: unknown, message: unknown, From 1c325c206453c9b9b10b199cc263cb250de6a44b Mon Sep 17 00:00:00 2001 From: kamecha Date: Sun, 27 Oct 2024 18:41:52 +0900 Subject: [PATCH 6/6] format --- denops/traqvim/action.ts | 4 ++-- denops/traqvim/model.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/denops/traqvim/action.ts b/denops/traqvim/action.ts index 6311fa1..bda1f2e 100644 --- a/denops/traqvim/action.ts +++ b/denops/traqvim/action.ts @@ -304,7 +304,7 @@ export const actionAddMessageStamp = async ( if (m.id === message.id) { return { ...m, - stamps: updatedMessage + stamps: updatedMessage, }; } else { return m; @@ -321,7 +321,7 @@ export const actionAddMessageStamp = async ( bufNum, editedTimeline.find((m) => m.id === message.id), ); -} +}; export const actionRemoveMessageStamp = async ( denops: Denops, diff --git a/denops/traqvim/model.ts b/denops/traqvim/model.ts index 19dfe6d..6ce54fa 100644 --- a/denops/traqvim/model.ts +++ b/denops/traqvim/model.ts @@ -387,7 +387,7 @@ export const downloadFile = async ( console.error(e); } return new Uint8Array(); -} +}; export const getStampId = async ( stampName: string, @@ -397,7 +397,7 @@ export const getStampId = async ( return stamp.name === stampName; }); return stamp?.id; -} +}; export const getMessageStamps = async ( messageId: string,