Skip to content

Commit

Permalink
Support more indieweb post types
Browse files Browse the repository at this point in the history
Apply the logic from [post type
discovery](https://indieweb.org/post-type-discovery) to handle RSVPs,
liks, bookmarks, etc.

This changes the function `reply_or_repost()` into
`silo_post_type_function()`, to handle all the post types with any
source URL.  I assume that **most** of the time we'll be interacting
with silos that have APIs of some sort when making replies, reposts,
likes, etc; but this will allow users to define custom functions for any
domain with which they may be interacting.
  • Loading branch information
Scott Merrill committed May 22, 2018
1 parent f6a1ea6 commit 66c29b2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
82 changes: 47 additions & 35 deletions inc/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,21 @@ function normalize_properties($properties) {
return $props;
}

function reply_or_repost($properties, $content) {
# a post is either a reply OR a repost OR neither; but never more than one.
# so we can safely loop through each of repost and reply and update
# the properties and content as needed.
foreach ( ['repost-of', 'in-reply-to'] as $type ) {
if (isset($properties[$type])) {
# replace all hyphens with underscores, for later use
$t = str_replace('-', '_', $type);
# get the domain of the site to which we are replying, and convert
# all dots to underscores.
$target = str_replace('.', '_', parse_url($properties[$type], PHP_URL_HOST));
# if a function exists for this type + target combo, call it
if (function_exists("${target}_${t}")) {
list($properties, $content) = call_user_func("${target}_${t}", $properties, $content);
}
}
# this function is a router to other functions that can operate on the source
# URLs of reposts, replies, bookmarks, etc.
# $type = the indieweb type (https://indieweb.org/post-type-discovery)
# $properties = array of front-matter properties for this post
# $content = the content of this post (which may be an empty string)
#
function silo_post_type_function($type, $properties, $content) {
# replace all hyphens with underscores, for later use
$t = str_replace('-', '_', $type);
# get the domain of the site to which we are replying, and convert
# all dots to underscores.
$target = str_replace('.', '_', parse_url($properties[$type], PHP_URL_HOST));
# if a function exists for this type + target combo, call it
if (function_exists("${target}_${t}")) {
list($properties, $content) = call_user_func("${target}_${t}", $properties, $content);
}
return [$properties, $content];
}
Expand Down Expand Up @@ -221,11 +220,26 @@ function create($request, $photos = []) {
# what type of post this is. We'll start with the assumption that
# everything is an article, and then revise as we discover otherwise.
$properties['posttype'] = 'article';

# figure out if this is a reply or a repost. Invoke silo-specific
# methods to obtain source content, and alter this post's properties
# accordingly.
list($properties, $content) = reply_or_repost($properties, $content);
if (isset($properties['rsvp'])) {
$properties['posttype'] = 'rsvp';
list($properties, $content) = silo_post_type_function('rsvp', $properties, $content);
}
if (isset($properties['in-reply-to'])) {
$properties['posttype'] = 'reply';
list($properties, $content) = silo_post_type_function('in-reply-to', $properties, $content);
}
if (isset($properties['repost-of'])) {
$properties['posttype'] = 'repost';
list($properties, $content) = silo_post_type_function('repost-of', $properties, $content);
}
if (isset($properties['like-of'])) {
$properties['posttype'] = 'like';
list($properties, $content) = silo_post_type_function('like-of', $properties, $content);
}
if (isset($properties['bookmark-of'])) {
$properties['posttype'] = 'bookmark';
list($properties, $content) = silo_post_type_function('bookmark-of', $properties, $content);
}

if (!empty($photos)) {
# add uploaded photos to the front matter.
Expand All @@ -234,6 +248,11 @@ function create($request, $photos = []) {
} else {
array_merge($properties['photo'], $photos);
}
if (strlen($content) < 50) {
# we have one or more photos and less than 50 characters worth
# of content. Let's call this a photo post.
$properties['posttype'] = 'photo';
}
}

# all items need a date
Expand All @@ -253,23 +272,16 @@ function create($request, $photos = []) {
$properties['published'] = true;
}

if ($type == 'entry') {
# we need either a title, or a slug.
# NOTE: MF2 defines "name" as the title value.
if (!isset($properties['name']) && !isset($properties['slug'])) {
# entries with neither a title nor a slug are "notes".
$type = 'note';
# We will assign this a slug.
$properties['slug'] = date('Hms');
if ($properties['posttype'] == 'article' ) {
# this is not a repost or a reply, so it must be a note.
$properties['posttype'] = 'note';
}
}
# we need either a title, or a slug.
# NOTE: MF2 defines "name" as the title value.
if (!isset($properties['name']) && !isset($properties['slug'])) {
# We will assign this a slug.
$properties['slug'] = date('Hms');
}

# if we have a title but not a slug, generate a slug
if (isset($properties['name']) && !isset($properties['slug'])) {
$properties['slug'] = slugify($properties['name']);
$properties['slug'] = $properties['name'];
}
# make sure the slugs are safe.
if (isset($properties['slug'])) {
Expand Down
1 change: 0 additions & 1 deletion inc/twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ function twitter_reply_or_repost( $type, $properties, $content) {
return [$properties, $content];
}

$properties['posttype'] = $type;
$tweet = get_tweet($config['syndication']['twitter'], $properties[$type]);
if ( false !== $tweet ) {
$properties["$type-name"] = $tweet->user->name;
Expand Down

0 comments on commit 66c29b2

Please sign in to comment.