Module: Keybase::Local::Chat

Defined in:
lib/keybase/local/chat.rb

Overview

Represents Keybase's JSON chat API.

Constant Summary collapse

CHAT_EXEC_ARGS =

The initial arguments to pass when executing Keybase for chatting.

%w[keybase chat api].freeze

Class Method Summary collapse

Class Method Details

.chat_call(meth, options: {}) ⇒ OpenStruct

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Makes chat API calls.

Parameters:

  • meth (String, Symbol)

    the team method

  • options (Hash) (defaults to: {})

    the options hash

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



45
46
47
48
49
50
51
52
53
# File 'lib/keybase/local/chat.rb', line 45

def chat_call(meth, options: {})
  response = Open3.popen3(*CHAT_EXEC_ARGS) do |stdin, stdout, _, _|
    stdin.write envelope meth, options: options
    stdin.close
    stdout.read
  end

  unwrap JSON.parse response, object_class: OpenStruct
end

.conversation(users, peek: false, unread_only: false) ⇒ OpenStruct

Read a conversation.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • peek (Boolean) (defaults to: false)

    whether to mark the conversation read

  • unread_only (Boolean) (defaults to: false)

    whether to fetch unread messages only

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



71
72
73
74
75
76
77
78
79
# File 'lib/keybase/local/chat.rb', line 71

def conversation(users, peek: false, unread_only: false)
  chat_call :read, options: {
    channel: {
      name: Core::U[*users],
    },
    peek: peek,
    unread_only: unread_only,
  }
end

.delete_message(users, id) ⇒ OpenStruct

Delete a message from a conversation.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • id (Integer)

    the id of the message to delete

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



104
105
106
107
108
109
110
111
# File 'lib/keybase/local/chat.rb', line 104

def delete_message(users, id)
  chat_call :delete, options: {
    channel: {
      name: Core::U[*users],
    },
    message_id: id,
  }
end

.download_attachment(users, id, path) ⇒ OpenStruct

Download a file from a conversation.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • id (Integer)

    the id of the message to download from

  • path (String)

    the pathname to download to

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



153
154
155
156
157
158
159
160
161
# File 'lib/keybase/local/chat.rb', line 153

def download_attachment(users, id, path)
  chat_call :download, options: {
    channel: {
      name: Core::U[*users],
    },
    message_id: id,
    output: path,
  }
end

.edit_message(users, id, message) ⇒ OpenStruct

Edit a message in a conversation.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • id (Integer)

    the id of the message to delete

  • message (String)

    the message to send

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/keybase/local/chat.rb', line 119

def edit_message(users, id, message)
  chat_call :edit, options: {
    channel: {
      name: Core::U[*users],
    },
    message_id: id,
    message: {
      body: message,
    },
  }
end

.envelope(meth, options: {}) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the JSON serialized envelope.

Parameters:

  • meth (Symbol)

    the chat method

  • options (Hash) (defaults to: {})

    the options hash

Returns:

  • (String)

    the JSON serialized envelope



19
20
21
22
23
24
25
26
# File 'lib/keybase/local/chat.rb', line 19

def envelope(meth, options: {})
  {
    method: meth,
    params: {
      options: options,
    },
  }.to_json
end

.list_inbox(topic_type: nil) ⇒ OpenStruct

List the current user's inbox.

Parameters:

  • topic_type (String) (defaults to: nil)

    the topic type to list by

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



59
60
61
62
63
# File 'lib/keybase/local/chat.rb', line 59

def list_inbox(topic_type: nil)
  chat_call :list, options: {
    topic_type: topic_type,
  }
end

.mark_conversation(users, id) ⇒ OpenStruct

Make a conversation as read up to a specific ID.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • id (Integer)

    the id of the message to mark up to

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



168
169
170
171
172
173
174
175
# File 'lib/keybase/local/chat.rb', line 168

def mark_conversation(users, id)
  chat_call :mark, options: {
    channel: {
      name: Core::U[*users],
    },
    message_id: id,
  }
end

.mute_conversation(users) ⇒ OpenStruct

Mute a conversation.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



181
182
183
184
185
186
187
188
# File 'lib/keybase/local/chat.rb', line 181

def mute_conversation(users)
  chat_call :setstatus, options: {
    channel: {
      name: Core::U[*users],
    },
    status: "muted",
  }
end

.send_message(users, message, public: false) ⇒ OpenStruct

Send a message to a conversation.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • message (String)

    the message to send

  • public (Boolean) (defaults to: false)

    whether to send the message to a public channel

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/keybase/local/chat.rb', line 87

def send_message(users, message, public: false)
  chat_call :send, options: {
    channel: {
      name: Core::U[*users],
      public: public,
    },
    message: {
      body: message,
    },
  }
end

.unwrap(struct) ⇒ OpenStruct

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Cleans up the object returned by chat_call.

Parameters:

  • struct (OpenStruct)

    a structified response from the Keybase chat API

Returns:

  • (OpenStruct)

    an unwrapped version of the response

Raises:



33
34
35
36
37
# File 'lib/keybase/local/chat.rb', line 33

def unwrap(struct)
  raise Exceptions::ChatError, struct.error.message if struct.error

  struct.result
end

.upload_attachment(users, path, title) ⇒ OpenStruct

Upload a file to a conversation.

Parameters:

  • users (Array<String>)

    a list of the users in the conversation

  • path (String)

    the pathname of the file to upload

  • title (String)

    the uploaded file's title

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



137
138
139
140
141
142
143
144
145
# File 'lib/keybase/local/chat.rb', line 137

def upload_attachment(users, path, title)
  chat_call :attach, options: {
    channel: {
      name: Core::U[*users],
    },
    filename: path,
    title: title,
  }
end