Skip to content

Commit e75124a

Browse files
authored
Merge pull request #8 from codeccoop/feat/rocketchat
Feat/rocketchat
2 parents 7c5301a + 3457a80 commit e75124a

File tree

15 files changed

+852
-33
lines changed

15 files changed

+852
-33
lines changed
15.9 KB
Loading
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Class Rocketchat_Addon
4+
*
5+
* @package formsbridge
6+
*/
7+
8+
namespace FORMS_BRIDGE;
9+
10+
if ( ! defined( 'ABSPATH' ) ) {
11+
exit();
12+
}
13+
14+
require_once 'class-rocketchat-form-bridge.php';
15+
require_once 'hooks.php';
16+
17+
/**
18+
* RocketChat addon class
19+
*/
20+
class Rocketchat_Addon extends Addon {
21+
/**
22+
* Holds the addon's title.
23+
*
24+
* @var string
25+
*/
26+
public const TITLE = 'Rocket.Chat';
27+
28+
/**
29+
* Holds the addon's name.
30+
*
31+
* @var string
32+
*/
33+
public const NAME = 'rocketchat';
34+
35+
/**
36+
* Holds the addon's custom bridge class.
37+
*
38+
* @var string
39+
*/
40+
public const BRIDGE = '\FORMS_BRIDGE\Rocketchat_Form_Bridge';
41+
42+
/**
43+
* Performs a request against the backend to check the connexion status.
44+
*
45+
* @param string $backend Backend name.
46+
*
47+
* @return boolean
48+
*/
49+
public function ping( $backend ) {
50+
$bridge = new Rocketchat_Form_Bridge(
51+
array(
52+
'name' => '__rocketchat-' . time(),
53+
'endpoint' => '/api/v1/users.list',
54+
'method' => 'GET',
55+
'backend' => $backend,
56+
)
57+
);
58+
59+
$response = $bridge->submit( array( 'status' => 'active' ) );
60+
61+
if ( is_wp_error( $response ) ) {
62+
Logger::log( 'Rocket.Chat backend ping error response', Logger::ERROR );
63+
Logger::log( $response, Logger::ERROR );
64+
return false;
65+
}
66+
67+
return true;
68+
}
69+
70+
/**
71+
* Performs an introspection of the backend endpoint and returns API fields.
72+
*
73+
* @param string $endpoint API endpoint.
74+
* @param string $backend Backend name.
75+
* @param string|null $method HTTP method.
76+
*
77+
* @return array List of fields and content type of the endpoint.
78+
*/
79+
public function get_endpoint_schema( $endpoint, $backend, $method = null ) {
80+
return array();
81+
}
82+
}
83+
84+
Rocketchat_Addon::setup();
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Class Rocketchat_Form_Bridge
4+
*
5+
* @package formsbridge
6+
*/
7+
8+
namespace FORMS_BRIDGE;
9+
10+
use FBAPI;
11+
use WP_Error;
12+
13+
if ( ! defined( 'ABSPATH' ) ) {
14+
exit();
15+
}
16+
17+
/**
18+
* Form bridge implementation for the Rocket.Chat API
19+
*/
20+
class Rocketchat_Form_Bridge extends Form_Bridge {
21+
/**
22+
* Submits payload and attachments to the bridge's backend.
23+
*
24+
* @param array $payload Payload data.
25+
* @param array $attachments Submission's attached files.
26+
*
27+
* @return array|WP_Error Http request response.
28+
*/
29+
public function submit( $payload = array(), $attachments = array() ) {
30+
$uploads = FBAPI::get_uploads();
31+
$room_id = $payload['roomId'] ?? $payload['channel'] ?? null;
32+
33+
if ( ! empty( $uploads ) && $room_id ) {
34+
$attachments = Forms_Bridge::attachments( $uploads );
35+
$backend = $this->backend();
36+
37+
$message_attachments = array();
38+
foreach ( $attachments as $name => $path ) {
39+
$info = pathinfo( $path );
40+
$filename = $info['basename'];
41+
42+
$response = $backend->post(
43+
'/api/v1/rooms.media/' . $room_id,
44+
array(
45+
'msg' => $name,
46+
),
47+
array(
48+
'Content-Type' => 'multipart/form-data',
49+
),
50+
array(
51+
'file' => $path,
52+
)
53+
);
54+
55+
if ( is_wp_error( $response ) ) {
56+
return $response;
57+
}
58+
59+
if ( ! $response['data']['success'] ) {
60+
return new WP_Error( 'rocketchat_upload', __( 'Can not upload a file to Rocket.Chat', 'forms-bridge' ), $response['data'] );
61+
}
62+
63+
unset( $payload[ $name ] );
64+
unset( $payload[ $name . '_filename' ] );
65+
66+
$message_attachments[] = array(
67+
'title' => $filename,
68+
'title_link' => $response['data']['file']['url'],
69+
'title_link_download' => true,
70+
);
71+
}
72+
73+
$payload['attachments'] = $message_attachments;
74+
}
75+
76+
return parent::submit( $payload, array() );
77+
}
78+
}

0 commit comments

Comments
 (0)