diff --git a/src/main/java/org/jivesoftware/site/FeedItem.java b/src/main/java/org/jivesoftware/site/FeedItem.java index b36cabcc..99931e12 100644 --- a/src/main/java/org/jivesoftware/site/FeedItem.java +++ b/src/main/java/org/jivesoftware/site/FeedItem.java @@ -23,6 +23,8 @@ public FeedItem( JSONObject entry ) final JSONObject firstPost = entry.getJSONObject( "post_stream" ).getJSONArray( "posts" ).getJSONObject( 0 ); contents = firstPost.getString( "cooked" ); + contents = replaceVideo(contents); + publishedDate = javax.xml.bind.DatatypeConverter.parseDateTime( firstPost.getString( "created_at" ) ).getTime(); final JSONArray arrayElements = entry.getJSONArray("tags"); @@ -33,6 +35,64 @@ public FeedItem( JSONObject entry ) replyCount = entry.getInt( "posts_count" ); } + /** + * Replaces embedded video in Discourse markup with a HTML5 'video' element. + */ + public static String replaceVideo(final String data) { + if (data == null) { + return data; + } + + final int start = data.indexOf("
", start); + if (end < 0) { + return data; + } else { + end += "
".length(); + } + + // Video + int startVideoSrc = data.indexOf("data-video-src=\"", start); + if (startVideoSrc < 0) { + return data; + } else { + startVideoSrc += "data-video-src=\"".length(); + } + int endVideoSrc = data.indexOf('"', startVideoSrc); + final String videoSrc = data.substring(startVideoSrc, endVideoSrc); + + // Thumbnail ('poster') + final String videoPoster; + int startVideoPoster = data.indexOf("data-thumbnail-src=\"", start); + if (startVideoPoster >= 0) { + startVideoPoster += "data-thumbnail-src=\"".length(); + int endVideoPoster = data.indexOf('"', startVideoPoster); + if (endVideoPoster < 0) { + return data; // No closing quote? Sounds dodgy. Better abort. + } else { + videoPoster = data.substring(startVideoPoster, endVideoPoster); + } + } else { + videoPoster = null; + } + + // Build replacement data. + final StringBuilder replacement = new StringBuilder(); + replacement.append(""); + + // Switch the original with the replacement. + return data.substring(0, start) + replacement + data.substring(end); + } // public FeedItem( SyndEntry entry ) // { // super( entry ); diff --git a/src/test/java/org/jivesoftware/site/FeedItemTest.java b/src/test/java/org/jivesoftware/site/FeedItemTest.java new file mode 100644 index 00000000..4aea7dae --- /dev/null +++ b/src/test/java/org/jivesoftware/site/FeedItemTest.java @@ -0,0 +1,35 @@ +package org.jivesoftware.site; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class FeedItemTest +{ + @Test + public void testVideoReplacement() + { + // Setup test fixture. + final String input = "

We are excited to be able to announce the immediate availability of a new plugin for Openfire: XMPP Web!

\n" + + "

This new plugin for the real-time communications server provided by the Ignite Realtime community allows you to install the third-party webclient named ‘XMPP Web’ in mere seconds! By installing this new plugin, the web client is immediately ready for use.

\n" + + "

\n" + + "

\n" + + "

This new plugin compliments others that similarly allow to deploy a web client with great ease, like Candy, inVerse and JSXC! With the addition of XMPP Web, the selection of easy-to-install clients for your users to use becomes even larger!

\n" + + "

The XMPP Web plugin for Openfire is based on release 0.10.2 of the upstream project, which currently is the latest release. It will automatically become available for installation in the admin console of your Openfire server in the next few days. Alternatively, you can download it immediately from its archive page.

\n" + + "

Do you think this is a good addition to the suite of plugins? Do you have any questions or concerns? Do you just want to say hi? Please stop by our community forum or our live groupchat!

\n" + + "

For other release announcements and news follow us on Mastodon or X

\n"; + + // Execute system under test. + final String result = FeedItem.replaceVideo(input); + + // Verify results. + final String expected = "

We are excited to be able to announce the immediate availability of a new plugin for Openfire: XMPP Web!

\n" + + "

This new plugin for the real-time communications server provided by the Ignite Realtime community allows you to install the third-party webclient named ‘XMPP Web’ in mere seconds! By installing this new plugin, the web client is immediately ready for use.

\n" + + "

\n" + + "

This new plugin compliments others that similarly allow to deploy a web client with great ease, like Candy, inVerse and JSXC! With the addition of XMPP Web, the selection of easy-to-install clients for your users to use becomes even larger!

\n" + + "

The XMPP Web plugin for Openfire is based on release 0.10.2 of the upstream project, which currently is the latest release. It will automatically become available for installation in the admin console of your Openfire server in the next few days. Alternatively, you can download it immediately from its archive page.

\n" + + "

Do you think this is a good addition to the suite of plugins? Do you have any questions or concerns? Do you just want to say hi? Please stop by our community forum or our live groupchat!

\n" + + "

For other release announcements and news follow us on Mastodon or X

\n"; + assertEquals(expected, result); + } +}