Open6

misskeyクライアント制作記

nekobatonekobato

Web Socketについて
https://misskey-hub.net/docs/api/streaming

接続

const host = [インスタンスのドメイン名]
const token = [配られたtoken]

const ws = new WebSocket(`wss://${host}/streaming?i=${token}`);

タイムラインを購読

type MisskeyStreamChannel = "globalTimeline" | "homeTimeline" | "hybridTimeline" | "localTimeline" | "main";

type Params = {
  local?: boolean;
  media?: boolean;
  reply?: boolean;
  renote?: boolean;
  poll?: boolean;
  myRenote?: boolean;
  myReaction?: boolean;
  following?: boolean;
}

ws.send(JSON.stringify({
  type: "connect",
  body: {
    channel : "homeTimeline", // MisskeyStreamChannel
    id: uuid(),
    params: null // Params
  }
})

タイムラインの購読だけでは取得したNoteにreactionが付いたことに気づけない。
API docsには書いてないが、sでそれぞれのNoteの更新を購読する

TODO: これunsubscribeどうするんだろうね

ws.send(JSON.stringify({
  type: "s",
  body: {
    id: [NoteId]
  }
})

TODO: 公式Webもそうだが、放っておくと確実にWebSocketが切断されるので切断を検出して再接続する仕組みが必要

受け取り

connectで購読しているTLのNoteがやってくるよ

type MisskeyStreamMessage = {
  id: string;
  type: "note";
  body: MisskeyNote;
};

MisskeyNote: https://misskey-hub.net/docs/api/entity/note.html

nekobatonekobato

reaction

Reactionする

https://misskey-hub.net/docs/api/endpoints/notes/reactions/create.html

reaction済みのnoteにはnote.myReactionにリアクションが入る

reactionを取り消す

https://misskey-hub.net/docs/api/endpoints/notes/reactions/delete.html

既にmyReactionがあるNoteにReactionする

一度既存のreactionをdeleteしてからcreateする必要がある

nekobatonekobato

APIへの接続はBrowserWindow内でやるとセキュリティ上良くないので、https://github.com/arantes555/electron-fetchを導入してみた

ipc.invokeでElectron内のスレッドにAPI呼び出しを頼み、fetchコードでは何も考えず結果を返す

TODO: WebSocketへの接続どうするの

nekobatonekobato

お気に入りに入れてるchannelsを取得するAPIはエンドポイント一覧にないぽい (2023-10-25)

/api/channels/my-favorites

typeは以下

export type MisskeyChannel = {
  bannerUrl: string;
  color: string;
  createdAt: string;
  description: string;
  hasUnreadNote: boolean;
  id: string;
  isArchived: boolean;
  isFavorited: boolean;
  isFollowing: boolean;
  lastNotedAt: string;
  name: string;
  notesCount: number;
  pinnedNoteIds: string[];
  userId: string;
  usersCount: number;
}