Skip to content

Commit a904a8f

Browse files
committed
Add video attachment support
1 parent 31558f6 commit a904a8f

File tree

1 file changed

+71
-7
lines changed

1 file changed

+71
-7
lines changed

feed-parsers/class-feed-parser-activitypub.php

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public function __construct( Feed $friends_feed ) {
101101
add_filter( 'mastodon_api_timelines_args', array( $this, 'mastodon_api_timelines_args' ) );
102102
add_filter( 'mastodon_api_account', array( $this, 'mastodon_api_account_augment_friend_posts' ), 9, 4 );
103103
add_filter( 'mastodon_api_status', array( $this, 'mastodon_api_status_add_reblogs' ), 30, 3 );
104-
add_filter( 'mastodon_api_status', array( $this, 'mastodon_api_status_add_attachments' ), 20, 3 );
104+
add_filter( 'mastodon_api_status', array( $this, 'mastodon_api_status_add_image_attachments' ), 20, 3 );
105+
add_filter( 'mastodon_api_status', array( $this, 'mastodon_api_status_add_video_attachments' ), 20, 3 );
105106
add_filter( 'mastodon_api_canonical_user_id', array( $this, 'mastodon_api_canonical_user_id' ), 20, 3 );
106107
}
107108

@@ -228,7 +229,7 @@ public function mastodon_api_account_augment_friend_posts( $account, $user_id, $
228229
return $account;
229230
}
230231

231-
public function mastodon_api_status_add_attachments( $status, $post_id, $request ) {
232+
public function mastodon_api_status_add_image_attachments( $status, $post_id, $request ) {
232233
if ( Friends::CPT !== get_post_type( $post_id ) ) {
233234
return $status;
234235
}
@@ -242,16 +243,79 @@ public function mastodon_api_status_add_attachments( $status, $post_id, $request
242243

243244
foreach ( $matches as $match ) {
244245
$status->content = str_replace( $match[0], '', $status->content );
245-
if ( ! preg_match( '/<img src="(?P<url>[^"]+)" width="(?P<width>[^"]*)" height="(?P<height>[^"]*)"/', $match[2], $block ) ) {
246+
if ( ! preg_match( '/<img\b([^>]+)>/', $match[2], $img ) ) {
247+
continue;
248+
}
249+
$block = array();
250+
foreach ( array( 'src', 'width', 'height' ) as $attr ) {
251+
if ( preg_match( '/\s' . $attr . '="(?P<' . $attr . '>[^"]+)"/', $img[1], $m ) ) {
252+
$block[ $attr ] = $m[ $attr ];
253+
}
254+
}
255+
if ( ! isset( $block['src'] ) ) {
246256
continue;
247257
}
248258

249259
$attachment = new \Enable_Mastodon_Apps\Entity\Media_Attachment();
250-
$attachment->id = strval( 2e10 + crc32( $block['url'] ) );
260+
$attachment->id = strval( 2e10 + crc32( $block['src'] ) );
251261
$attachment->type = 'image';
252-
$attachment->url = $block['url'];
253-
$attachment->preview_url = $block['url'];
254-
$attachment->remote_url = $block['url'];
262+
$attachment->url = $block['src'];
263+
$attachment->preview_url = $block['src'];
264+
$attachment->remote_url = $block['src'];
265+
if ( isset( $block['width'] ) && $block['width'] > 0 && isset( $block['height'] ) && $block['height'] > 0 ) {
266+
$attachment->meta = array(
267+
'width' => intval( $block['width'] ),
268+
'height' => intval( $block['height'] ),
269+
'size' => $block['width'] . 'x' . $block['height'],
270+
'aspect' => $block['width'] / $block['height'],
271+
);
272+
} else {
273+
$attachment->meta = array(
274+
'width' => 0,
275+
'height' => 0,
276+
'size' => 0x0,
277+
'aspect' => 1,
278+
);
279+
}
280+
$attachment->description = '';
281+
$status->media_attachments[] = $attachment;
282+
}
283+
return $status;
284+
}
285+
286+
public function mastodon_api_status_add_video_attachments( $status, $post_id, $request ) {
287+
if ( Friends::CPT !== get_post_type( $post_id ) ) {
288+
return $status;
289+
}
290+
if ( false === strpos( $status->content, '<video' ) ) {
291+
return $status;
292+
}
293+
preg_match_all( '/<video\b([^>]+)>/', $status->content, $matches, PREG_SET_ORDER );
294+
if ( empty( $matches ) ) {
295+
return $status;
296+
}
297+
298+
foreach ( $matches as $match ) {
299+
$status->content = str_replace( $match[0], '', $status->content );
300+
$block = array();
301+
foreach ( array( 'src', 'width', 'height', 'poster' ) as $attr ) {
302+
if ( preg_match( '/\s' . $attr . '="(?P<' . $attr . '>[^"]+)"/', $match[1], $m ) ) {
303+
$block[ $attr ] = $m[ $attr ];
304+
}
305+
}
306+
307+
if ( ! isset( $block['src'] ) ) {
308+
continue;
309+
}
310+
311+
$attachment = new \Enable_Mastodon_Apps\Entity\Media_Attachment();
312+
$attachment->id = strval( 2e10 + crc32( $block['src'] ) );
313+
$attachment->type = 'video';
314+
$attachment->url = $block['src'];
315+
if ( isset( $block['poster'] ) ) {
316+
$attachment->preview_url = $block['poster'];
317+
}
318+
$attachment->remote_url = $block['src'];
255319
if ( isset( $block['width'] ) && $block['width'] > 0 && isset( $block['height'] ) && $block['height'] > 0 ) {
256320
$attachment->meta = array(
257321
'width' => intval( $block['width'] ),

0 commit comments

Comments
 (0)