Skip to content

Commit 89d68d7

Browse files
authored
Merge pull request #6 from codeccoop/feat/zulip
zulip addon
2 parents ca0abd5 + 6f1d014 commit 89d68d7

File tree

12 files changed

+775
-42
lines changed

12 files changed

+775
-42
lines changed

forms-bridge/addons/listmonk/class-listmonk-addon.php

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -159,43 +159,43 @@ public function get_endpoint_schema( $endpoint, $backend, $method = null ) {
159159
}
160160
}
161161

162-
if ( '/api/subscribers' === $endpoint ) {
163-
return array(
164-
array(
165-
'name' => 'email',
166-
'schema' => array( 'type' => 'string' ),
167-
'required' => true,
168-
),
169-
array(
170-
'name' => 'name',
171-
'schema' => array( 'type' => 'string' ),
172-
),
173-
array(
174-
'name' => 'status',
175-
'schema' => array( 'type' => 'string' ),
176-
),
177-
array(
178-
'name' => 'lists',
179-
'schema' => array(
180-
'type' => 'array',
181-
'items' => array( 'type' => 'number' ),
182-
),
183-
),
184-
array(
185-
'name' => 'preconfirm_subscriptions',
186-
'schema' => array( 'type' => 'boolean' ),
187-
),
188-
array(
189-
'name' => 'attribs',
190-
'schema' => array(
191-
'type' => 'object',
192-
'properties' => array(),
193-
),
194-
),
195-
);
162+
if ( '/api/subscribers' !== $endpoint ) {
163+
return array();
196164
}
197165

198-
return array();
166+
return array(
167+
array(
168+
'name' => 'email',
169+
'schema' => array( 'type' => 'string' ),
170+
'required' => true,
171+
),
172+
array(
173+
'name' => 'name',
174+
'schema' => array( 'type' => 'string' ),
175+
),
176+
array(
177+
'name' => 'status',
178+
'schema' => array( 'type' => 'string' ),
179+
),
180+
array(
181+
'name' => 'lists',
182+
'schema' => array(
183+
'type' => 'array',
184+
'items' => array( 'type' => 'number' ),
185+
),
186+
),
187+
array(
188+
'name' => 'preconfirm_subscriptions',
189+
'schema' => array( 'type' => 'boolean' ),
190+
),
191+
array(
192+
'name' => 'attribs',
193+
'schema' => array(
194+
'type' => 'object',
195+
'properties' => array(),
196+
),
197+
),
198+
);
199199
}
200200
}
201201

forms-bridge/addons/nextcloud/class-nextcloud-addon.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ static function ( $prune, $bridge ) {
6262
2
6363
);
6464
}
65+
6566
/**
6667
* Performs a request against the backend to check the connexion status.
6768
*
14.9 KB
Loading
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
/**
3+
* Class Zulip_Addon
4+
*
5+
* @package formsbridge
6+
*/
7+
8+
namespace FORMS_BRIDGE;
9+
10+
use Exception;
11+
12+
if ( ! defined( 'ABSPATH' ) ) {
13+
exit();
14+
}
15+
16+
require_once 'class-zulip-form-bridge.php';
17+
require_once 'hooks.php';
18+
19+
/**
20+
* Zulip addon class
21+
*/
22+
class Zulip_Addon extends Addon {
23+
24+
/**
25+
* Holds the addon's title.
26+
*
27+
* @var string
28+
*/
29+
public const TITLE = 'Zulip';
30+
31+
/**
32+
* Holds the addon's name.
33+
*
34+
* @var string
35+
*/
36+
public const NAME = 'zulip';
37+
38+
/**
39+
* Holds the addon's custom bridge class.
40+
*
41+
* @var string
42+
*/
43+
public const BRIDGE = '\FORMS_BRIDGE\Zulip_Form_Bridge';
44+
45+
/**
46+
* Holds the Zulip OAS public URL.
47+
*
48+
* @var string
49+
*/
50+
public const OAS_URL = 'https://raw.githubusercontent.com/zulip/zulip/refs/heads/main/zerver/openapi/zulip.yaml';
51+
52+
/**
53+
* Performs a request against the backend to check the connexion status.
54+
*
55+
* @param string $backend Backend name.
56+
*
57+
* @return boolean
58+
*/
59+
public function ping( $backend ) {
60+
$bridge = new Zulip_Form_Bridge(
61+
array(
62+
'name' => '__zulip-' . time(),
63+
'endpoint' => '/api/v1/streams',
64+
'method' => 'GET',
65+
'backend' => $backend,
66+
),
67+
'zulip'
68+
);
69+
70+
$response = $bridge->submit();
71+
72+
if ( is_wp_error( $response ) ) {
73+
Logger::log( 'Zulip backend ping error response', Logger::ERROR );
74+
Logger::log( $response, Logger::ERROR );
75+
return false;
76+
}
77+
78+
return true;
79+
}
80+
81+
/**
82+
* Performs a GET request against the backend endpoint and retrive the response data.
83+
*
84+
* @param string $endpoint API endpoint.
85+
* @param string $backend Backend name.
86+
*
87+
* @return array|WP_Error
88+
*/
89+
public function fetch( $endpoint, $backend ) {
90+
$bridge = new Zulip_Form_Bridge(
91+
array(
92+
'name' => '__zulip-' . time(),
93+
'endpoint' => $endpoint,
94+
'method' => 'GET',
95+
'backend' => $backend,
96+
),
97+
'zulip'
98+
);
99+
100+
return $bridge->submit();
101+
}
102+
103+
/**
104+
* Performs an introspection of the backend endpoint and returns API fields.
105+
*
106+
* @param string $endpoint API endpoint.
107+
* @param string $backend Backend name.
108+
* @param string|null $method HTTP method.
109+
*
110+
* @return array List of fields and content type of the endpoint.
111+
*/
112+
public function get_endpoint_schema( $endpoint, $backend, $method = null ) {
113+
if ( function_exists( 'yaml_parse' ) ) {
114+
$response = wp_remote_get( self::OAS_URL );
115+
116+
if ( ! is_wp_error( $response ) ) {
117+
$data = yaml_parse( $response['body'] );
118+
119+
if ( $data ) {
120+
try {
121+
$oa_explorer = new OpenAPI( $data );
122+
123+
$method = strtolower( $method ?? 'post' );
124+
$path = preg_replace( '', '', $endpoint );
125+
$source = in_array( $method, array( 'post', 'put', 'patch' ), true ) ? 'body' : 'query';
126+
$params = $oa_explorer->params( $path, $method, $source );
127+
128+
return $params ?: array();
129+
} catch ( Exception ) {
130+
// do nothing.
131+
}
132+
}
133+
}
134+
}
135+
136+
if ( '/api/v1/messages' !== $endpoint ) {
137+
return array();
138+
}
139+
140+
return array(
141+
array(
142+
'name' => 'type',
143+
'schema' => array(
144+
'type' => 'string',
145+
'enum' => array( 'direct', 'stream' ),
146+
),
147+
'required' => true,
148+
),
149+
array(
150+
'name' => 'to',
151+
'schema' => array(
152+
'type' => 'array',
153+
'items' => array(
154+
'type' => array( 'string', 'integer' ),
155+
),
156+
),
157+
'required' => true,
158+
),
159+
array(
160+
'name' => 'content',
161+
'schema' => array( 'type' => 'string' ),
162+
'required' => true,
163+
),
164+
array(
165+
'name' => 'topic',
166+
'schema' => array(
167+
'type' => 'string',
168+
'default' => '(no topic)',
169+
),
170+
),
171+
array(
172+
'name' => 'queue_id',
173+
'schema' => array( 'type' => 'string' ),
174+
),
175+
array(
176+
'name' => 'local_id',
177+
'schema' => array( 'type' => 'string' ),
178+
),
179+
array(
180+
'name' => 'read_by_sender',
181+
'schema' => array( 'type' => 'boolean' ),
182+
),
183+
);
184+
}
185+
}
186+
187+
Zulip_Addon::setup();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Class Zulip_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 implamentation for the Zulip API.
19+
*/
20+
class Zulip_Form_Bridge extends Form_Bridge {
21+
22+
/**
23+
* Submits payload and attachments to the bridge's backend.
24+
*
25+
* @param array $payload Payload data.
26+
* @param array $attachments Submission's attached files.
27+
*
28+
* @return array|WP_Error Http request response.
29+
*/
30+
public function submit( $payload = array(), $attachments = array() ) {
31+
$uploads = FBAPI::get_uploads();
32+
33+
if ( ! empty( $uploads ) ) {
34+
$backend = $this->backend()->clone(
35+
array(
36+
'headers' => array(
37+
array(
38+
'name' => 'Content-Type',
39+
'value' => 'multipart/form-data',
40+
),
41+
),
42+
)
43+
);
44+
45+
$annex = "\n\n----\n" . esc_html( __( 'Attachments', 'forms-bridge' ) ) . ":\n";
46+
47+
$attachments = Forms_Bridge::attachments( $uploads );
48+
49+
foreach ( $attachments as $name => $path ) {
50+
$response = $backend->post( '/api/v1/user_uploads', array(), array(), array( $name => $path ) );
51+
52+
if ( is_wp_error( $response ) ) {
53+
return $response;
54+
} elseif ( 'success' !== $response['data']['result'] ) {
55+
return new WP_Error( 'zulip_upload', __( 'Can not upload a file to Zulip', 'forms-bridge' ), $response['data'] );
56+
}
57+
58+
unset( $payload[ $name ] );
59+
unset( $payload[ $name . '_filename' ] );
60+
61+
$annex .= "* [{$name}]({$response['data']['url']})\n";
62+
}
63+
64+
$payload['content'] .= $annex;
65+
}
66+
67+
return parent::submit( $payload, array() );
68+
}
69+
}

0 commit comments

Comments
 (0)