Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
Merge pull request #29 from pusher/api-v3
Browse files Browse the repository at this point in the history
Api v3
  • Loading branch information
elverkilde authored Mar 8, 2019
2 parents b7706df + 3e75d8a commit 36fd227
Show file tree
Hide file tree
Showing 4 changed files with 416 additions and 7 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/pusher/chatkit-server-ruby/compare/1.1.0...HEAD)
## [Unreleased](https://github.com/pusher/chatkit-server-ruby/compare/1.2.0...HEAD)

## [1.2.0](https://github.com/pusher/chatkit-server-ruby/compare/1.1.0...1.2.0)

### Added

- `send_multipart_message`, `send_simple_message` and `fetch_multipart_messages` using the new V3 endpoints

### Changed

- all methods except `sendMessage` and `getRoomMessages` uses new V3 endpoints

## [1.1.0](https://github.com/pusher/chatkit-server-ruby/compare/1.0.0...1.1.0) - 2018-11-07

Expand Down
154 changes: 149 additions & 5 deletions lib/chatkit/client.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
require 'pusher-platform'
require 'json'
require 'cgi'
require 'excon'

require_relative './error'
require_relative './missing_parameter_error'
require_relative './response_error'
require_relative './upload_error'

module Chatkit

ROOM_SCOPE = "room"
GLOBAL_SCOPE = "global"

class Client
attr_accessor :api_instance, :authorizer_instance, :cursors_instance
attr_accessor :api_instance,
:api_v2_instance,
:authorizer_instance,
:cursors_instance

def initialize(options)
base_options = {
Expand All @@ -27,13 +32,20 @@ def initialize(options)
})
}

@api_instance = PusherPlatform::Instance.new(
@api_v2_instance = PusherPlatform::Instance.new(
base_options.merge({
service_name: 'chatkit',
service_version: 'v2'
})
)

@api_instance = PusherPlatform::Instance.new(
base_options.merge({
service_name: 'chatkit',
service_version: 'v3'
})
)

@authorizer_instance = PusherPlatform::Instance.new(
base_options.merge({
service_name: 'chatkit_authorizer',
Expand Down Expand Up @@ -206,7 +218,7 @@ def create_room(options)
name: options[:name],
private: options[:private] || false
}

body[:custom_data] = options[:custom_data] unless options[:custom_data].nil?

unless options[:user_ids].nil?
Expand Down Expand Up @@ -328,6 +340,26 @@ def remove_users_from_room(options)

# Messages API

def fetch_multipart_messages(options)
verify({
room_id: "You must provide the ID of the room to send the message to",
}, options)

if !options[:limit].nil? and options[:limit] <= 0
raise Chatkit::MissingParameterError.new("Limit must be greater than 0")
end

optional_params = [:initial_id, :direction, :limit]
query_params = options.select { |key,_| optional_params.include? key }

api_request({
method: "GET",
path: "/rooms/#{CGI::escape options[:room_id]}/messages",
query: query_params,
jwt: generate_su_token[:token]
})
end

def get_room_messages(options)
if options[:room_id].nil?
raise Chatkit::MissingParameterError.new("You must provide the ID of the room to fetch messages from")
Expand All @@ -338,14 +370,73 @@ def get_room_messages(options)
query_params[:direction] = options[:direction] unless options[:direction].nil?
query_params[:limit] = options[:limit] unless options[:limit].nil?

api_request({
api_v2_request({
method: "GET",
path: "/rooms/#{CGI::escape options[:room_id]}/messages",
query: query_params,
jwt: generate_su_token[:token]
})
end

def send_simple_message(options)
verify({text: "You must provide some text for the message",
}, options)

options[:parts] = [{type: "text/plain",
content: options[:text]
}]

send_multipart_message(options)
end

def send_multipart_message(options)
verify({
room_id: "You must provide the ID of the room to send the message to",
sender_id: "You must provide the ID of the user sending the message",
parts: "You must provide a parts array",
}, options)

if not options[:parts].length > 0
raise Chatkit::MissingParameterError.new("parts array must have at least one item")
end

# this assumes the token lives long enough to finish all S3 uploads
token = generate_su_token({ user_id: options[:sender_id] })[:token]

request_parts = options[:parts].map { |part|
verify({type: "Each part must define a type"}, part)

if !part[:content].nil?
{
type: part[:type],
content: part[:content]
}
elsif !part[:url].nil?
{
type: part[:type],
url: part[:url]
}
elsif !part[:file].nil?
attachment_id = upload_attachment(token, options[:room_id], part)
{
type: part[:type],
attachment: {id: attachment_id},
name: part[:name],
customData: part[:customData]
}.reject{ |_,v| v.nil? }
else
raise Chatkit::MissingParameterError.new("Each part must have one of :file, :content or :url")
end
}

api_request({
method: "POST",
path: "/rooms/#{CGI::escape options[:room_id]}/messages",
body: {parts: request_parts},
jwt: token
})
end

def send_message(options)
if options[:room_id].nil?
raise Chatkit::MissingParameterError.new("You must provide the ID of the room to send the message to")
Expand Down Expand Up @@ -380,7 +471,7 @@ def send_message(options)
attachment: options[:attachment]
}

api_request({
api_v2_request({
method: "POST",
path: "/rooms/#{CGI::escape options[:room_id]}/messages",
body: payload,
Expand Down Expand Up @@ -551,6 +642,10 @@ def get_room_read_cursors(options)

# Service-specific helpers

def api_v2_request(options)
make_request(@api_v2_instance, options)
end

def api_request(options)
make_request(@api_instance, options)
end
Expand Down Expand Up @@ -713,5 +808,54 @@ def update_permissions_for_role(options)
jwt: generate_su_token[:token]
})
end

def upload_attachment(token, room_id, part)
body = part[:file]
content_length = body.length
content_type = part[:type]

if content_length <= 0
raise Chatkit::MissingParameterError.new("File contents size must be greater than 0")
end

attachment_req = {
content_type: content_type,
content_length: content_length
}

attachment_response = api_request({
method: "POST",
path: "/rooms/#{CGI::escape room_id}/attachments",
body: attachment_req,
jwt: token
})

url = attachment_response[:body][:upload_url]
connection = Excon.new(url, :omit_default_port => true)
upload_response = connection.put(
:body => body,
:headers => {
"Content-Type" => content_type,
"Content-Length" => content_length
}
)

if upload_response.status != 200
error = {message: "Failed to upload attachment",
response_object: upload_response
}
raise Chatkit::UploadError.new(error)
end

attachment_response[:body][:attachment_id]
end

def verify(required, options)
required.each { |field_name, message|
if options[field_name].nil?
raise Chatkit::MissingParameterError.new(message)
end
}
end
end
end
17 changes: 17 additions & 0 deletions lib/chatkit/upload_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative './error'

module Chatkit
class UploadError < Chatkit::Error
attr_accessor :message, :response_object

def initialize(error)
@message = error[:message]
@response_object = error[:response_object]
end

def to_s
"Chatkit::MissingParameterError - #{@message}"
end

end
end
Loading

0 comments on commit 36fd227

Please sign in to comment.