Skip to content

Commit

Permalink
Pleroma support.
Browse files Browse the repository at this point in the history
- Added support for Pleroma posts.
- Fixed some toots URLs badly parsed (the ones with more than two “folders” in their paths).
  • Loading branch information
AmauryCarrade committed Jul 11, 2018
1 parent 84c88fa commit bef866a
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 16 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
#v1.1.0
## 11-07-2018

1. [](#new)
* Added [Pleroma](https://pleroma.social) support (using the `toot` shortcode).

1. [](#bugfix)
* Fixed some Mastodon URLs badly parsed.

# v1.0.1
## 07-07-2018

Expand Down
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The **Static Social Embeds** Plugin is for [Grav CMS](http://github.com/getgrav/grav). It embeds social status (like tweets, instagram posts, toots, etc.) in articles without using their embed iframe, but rather statically without any dependency to the service.

The following networks are supported ([see below](#usage) for details):

- [Twitter](https://twitter.com);
- [Mastodon](https://joinmastodon.org) and [Pleroma](https://pleroma.social);
- [Instagram](https://www.instagram.com).

## Installation

Installing the Static Social Embeds plugin can be done in one of two ways. The GPM (Grav Package Manager) installation method enables you to quickly and easily install the plugin with a simple terminal command, while the manual method enables you to do so via a zip file.
Expand Down Expand Up @@ -32,6 +38,8 @@ If you use the admin plugin, you can install directly through the admin plugin b

Before configuring this plugin, you should copy the `user/plugins/static-social-embeds/static-social-embeds.yaml` to `user/config/plugins/static-social-embeds.yaml` and only edit that copy.

Note that if you use the admin plugin, a file with your configuration, and named static-social-embeds.yaml will be saved in the `user/config/plugins/` folder once the configuration is saved in the admin.

Here is the default configuration and a short explanation of available options. Extensive explanations are available in the admin page of the plugin.

```yaml
Expand Down Expand Up @@ -86,17 +94,15 @@ To use Twitter embeds, you'll need to register an application. This is done in a
2. Then, click the **Keys and access tokens** tab and at the bottom of the page, click **Create my Access Token**.
3. Finally, copy the credentials in the configuration file (or in the admin).

Note that if you use the admin plugin, a file with your configuration, and named static-social-embeds.yaml will be saved in the `user/config/plugins/` folder once the configuration is saved in the admin.

### Customization

If you want to customize the embeds' HTML code, you can override the `partials/static-social-embeds` templates. The template name is the same as the shortcode name (see below), e.g. `toot.html.twig` for Mastodon embeds template. Templates context vary for different social networks; checkout built-in templates or shortcode classes to know which variables you can use.

## Usage

The plugin supports static embeds for Twitter, Mastodon and Instagram posts. It tries to match original embeds styles, so it's close to the real embeds, wut without any request to the social networks: independence!
The plugin supports static embeds for Twitter, Mastodon (including Pleroma) and Instagram posts. It tries to match original embeds styles, so it's close to the real embeds, but without any request to the social networks: independence!

Three shortcodes are declared to embed status: `tweet`, `toot` and `instagram`, for respectively Twitter, Mastodon and Instagram. They accept one parameter: the status URL to embed. Use them in your articles like this:
Three shortcodes are declared to embed status: `tweet`, `toot` and `instagram`, for respectively Twitter, Mastodon/Pleroma, and Instagram. They accept one parameter: the status URL to embed. Use them in your articles like this:

```markdown
[tweet="https://twitter.com/INSVideos/status/1013375197984980992"]
Expand All @@ -110,7 +116,7 @@ The following shortcodes will be rendered as below (for light and dark themes, w

![](assets/docs/embeds-preview.png)

Texts, images (single or multiples), videos (single or multiples), GIFs, are supported for all networks above. Mastodon toots supports CW and sensitive images.
Texts, images (single or multiples), videos (single or multiples), GIFs, are supported for all networks above. Mastodon/Pleroma toots supports CW and sensitive images, account privacy badges, and instances' custom emojis. Twitter and Instagram supports verified and protected accounts badges.

Extensive caching is used to retrieve data and medias only once per embed. The first page load may be slow (especially if there are a lot of embeds in the page), but subsequent calls will be lightning-fast, as all data will be read from the cache, without any request to the social networks (no tracking, yay!). This also means that if an embed status is deleted, it will be kept on your site if the cache is not deleted. Custom locations are used for the cache files.

Expand All @@ -121,7 +127,7 @@ The plugin is translated in English and French.

## Technical considerations regarding Instagram

While Twitter and Mastodon data are retrieved using their respective API, there is no way to do that with Instagram (or there is, but deprecated and soon-to-be-removed in december 2018). We scrap an internal JSON embed in the post pages to retrieve data (thanks to [RSSBridge](https://github.com/RSS-Bridge/rss-bridge/blob/master/bridges/InstagramBridge.php) for the tip). As it isn't official, it may break anytime.
While Twitter and Mastodon/Pleroma data are retrieved using their respective API, there is no way to do that with Instagram (or there is, but deprecated and soon-to-be-removed in december 2018). We scrap an internal JSON embed in the post pages to retrieve data (thanks to [RSSBridge](https://github.com/RSS-Bridge/rss-bridge/blob/master/bridges/InstagramBridge.php) for the tip). As it isn't official, it may break anytime.

Data is cached without expiration, so as long as you don't delete the cache, existing embeds will never break. New ones will. Feel free to open an issue if such problem occurs (but this is pretty stable for a few years now and I hope it will remains as such).

Expand All @@ -132,5 +138,5 @@ Data is cached without expiration, so as long as you don't delete the cache, exi
## To Do

- [ ] Support for Twitter pools.
- [ ] Support for Pleroma (should be simple as it's compatible with the Mastodon API) and maybe other networks (by request).
- [ ] Support for other networks (by request).
- [ ] Maybe option to proxy medias, allowing not to store them but still serve them from the website's domain.
17 changes: 14 additions & 3 deletions languages/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ PLUGIN_SSE:
DIRECT: Direct message
REBLOGS: $rbls reblogs
FAVOURITES: $favs favourites
PLEROMA:
CANNOT_RENDER: Unable to render post.
VIEW_ON_MASTODON: (Click to view on Pleroma.)
REBLOGS: $rbls repeats
FAVOURITES: $favs likes
VISIBILITIES:
PUBLIC: Public post
UNLISTED: Unlisted post
PRIVATE: Private post
DIRECT: Direct message
INSTAGRAM:
CANNOT_RENDER: Unable to render post
VIEW_ON_INSTAGRAM: (Click to view on Instagram.)
Expand Down Expand Up @@ -119,12 +129,13 @@ PLUGIN_SSE:
MASTODON:
TITLE: Mastodon
HELP: |
Embed toots from <a href="https://joinmastodon.org">Mastodon</a> in articles using
<code>[toot="tootURL"]</code> with blanks lines before and after.<br />
Embed toots from <a href="https://joinmastodon.org">Mastodon</a> or posts from
<a href="https://pleroma.social/">Pleroma</a> in articles using <code>[toot="tootURL"]</code> with blanks lines
before and after.<br />
As toots are retrieved using non-authenticated requests, you cannot embed private or direct toots (but
unlisted and public ones are fine).
THEME:
LABEL: Mastodon theme
LABEL: Mastodon/Pleroma theme
HELP: The embeds theme. This option is only guaranteed to be followed with the built-in CSS.
INSTAGRAM:
TITLE: Instagram
Expand Down
12 changes: 11 additions & 1 deletion languages/fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,20 @@ PLUGIN_SSE:
VISIBILITIES:
PUBLIC: Toot public
UNLISTED: Toot non listé
PRIVATE: Toot privé
PRIVATE: Toot privé, à destination de vos abonné⋅e⋅s uniquement
DIRECT: Message direct
REBLOGS: $rbls boosts
FAVOURITES: $favs favoris
PLEROMA:
CANNOT_RENDER: Impossible d'afficher le statut.
VIEW_ON_MASTODON: (Cliquez pour le voir sur Pleroma.)
REBLOGS: $rbls partages
FAVOURITES: $favs mentions j'aime
VISIBILITIES:
PUBLIC: Statut public
UNLISTED: Statut non listé
PRIVATE: Statut à destination de vos abonné⋅e⋅s uniquement
DIRECT: Message direct
INSTAGRAM:
CANNOT_RENDER: Impossible d'afficher la publication.
VIEW_ON_INSTAGRAM: (Cliquez pour le voir sur Instagram.)
Expand Down
43 changes: 42 additions & 1 deletion shortcodes/MastodonShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ protected function getShortcodeName()
*/
protected function getData($url)
{
// First, we fetch the final URL after redirections.
// Typically, Pleroma features objects links that will redirect to URLs with the status ID we need to
// call the API.

$ch = curl_init();

curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 256,
CURLOPT_TIMEOUT => 3600,

// Required because Pleroma only redirects if we accept HTML content (else, we get an ActivityPub XML).
CURLOPT_HTTPHEADER => [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
]
]);

curl_exec($ch);

$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$purl = parse_url($url);

if ($purl === false)
Expand All @@ -47,8 +71,14 @@ protected function getData($url)

// From: https://mamot.fr/@ploum/100244825435451499
// To: https://mamot.fr/api/v1/statuses/100244825435451499
// - or -
// From: https://weeaboo.space/notice/908080
// To: https://weeaboo.space/api/v1/statuses/908080
// - or -
// From: https://mstdn.jp/users/Alumi/statuses/100355954289843928
// To: https://mstdn.jp/api/v1/statuses/100355954289843928
$api_endpoint = $purl['scheme'] . '://' . $purl['host'] . (isset($purl['port']) && $purl['port'] != 80 ? ':' . $purl['port'] : '');
$api_endpoint .= '/api/v1/statuses/' . $path_parts[2];
$api_endpoint .= '/api/v1/statuses/' . end($path_parts);

$ch = curl_init();

Expand Down Expand Up @@ -76,6 +106,17 @@ protected function getData($url)
$toot['account']['avatar'] = $this->fetchImage($toot['account']['avatar']);
$toot['account']['fully_qualified_name'] = $toot['account']['username'] . '@' . $purl['host'] . (isset($purl['port']) && $purl['port'] != 80 ? ':' . $purl['port'] : '');

// Oh and Pleroma does not add <p> tags.

if (strpos($toot['content'], '<p>') !== 0)
{
$toot['content'] = '<p>' . $toot['content'] . '</p>';
}

// While we're at it, let's store which federation this is from

$toot['federation_software'] = strpos($toot['uri'], 'objects') !== false ? 'pleroma' : 'mastodon';

// Process & store medias

if (isset($toot['media_attachments']))
Expand Down
8 changes: 4 additions & 4 deletions templates/partials/static-social-embeds/toot.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<article class="static-social-embed sse-theme-{{ config.mastodon.theme }} sse-status sse-toot">
{% if errors %}
<div class="sse-status-error">
<p>{{ 'PLUGIN_SSE.MASTODON.CANNOT_RENDER'|t }} <a href="{{ url }}">{{ 'PLUGIN_SSE.MASTODON.VIEW_ON_MASTODON'|t }}</a></p>
<p>{{ ('PLUGIN_SSE.' ~ federation_software|upper ~ '.CANNOT_RENDER')|t }} <a href="{{ url }}">{{ ('PLUGIN_SSE.' ~ federation_software|upper ~ '.VIEW_ON_MASTODON')|t }}</a></p>
<ul>
{% for error in errors %}
<li>{{ error.code }}: {{ error.message }}</li>
Expand Down Expand Up @@ -105,7 +105,7 @@
<li class="sse-mastodon-stats-date">
<time datetime="{{ created_at|date('c') }}"><a href="{{ url }}">{{ created_at|date('PLUGIN_SSE.EMBEDS.DATE_FORMAT'|t) }}</a></time>
</li>
<li title="{{ ('PLUGIN_SSE.MASTODON.VISIBILITIES.' ~ visibility|upper)|t }}" class="sse-mastodon-stats-visibility">
<li title="{{ ('PLUGIN_SSE.' ~ federation_software|upper ~ '.VISIBILITIES.' ~ visibility|upper)|t }}" class="sse-mastodon-stats-visibility">
{%- set visibility_icons = {
'public': 'fas fa-globe-africa',
'unlisted': 'fas fa-unlock',
Expand All @@ -121,10 +121,10 @@
{{- application.name -}}
{%- endif -%}
</li>
<li aria-label="{{ 'PLUGIN_SSE.MASTODON.REBLOGS'|t|replace({'$rbls': reblogs_count}) }}" class="sse-mastodon-stats-reblogs">
<li title="{{ ('PLUGIN_SSE.' ~ federation_software|upper ~ '.REBLOGS')|t|replace({'$rbls': reblogs_count}) }}" class="sse-mastodon-stats-reblogs">
<span class="fas fa-retweet" aria-hidden="true"></span> {{ reblogs_count -}}
</li>
<li aria-label="{{ 'PLUGIN_SSE.MASTODON.FAVOURITES'|t|replace({'$favs': favourites_count}) }}" class="sse-mastodon-stats-favourites">
<li title="{{ ('PLUGIN_SSE.' ~ federation_software|upper ~ '.FAVOURITES')|t|replace({'$favs': favourites_count}) }}" class="sse-mastodon-stats-favourites">
<span class="fas fa-heart" aria-hidden="true"></span> {{ favourites_count -}}
</li>
</ul>
Expand Down

0 comments on commit bef866a

Please sign in to comment.