use GuzzleHttp\Subscriber\Oauth\Oauth1;
define('TUMBLR_DEFAULT_POLL_INTERVAL', 10); // given in minutes
+define('TUMBLR_DEFAULT_MAXIMUM_TAGS', 10);
function tumblr_install()
{
return;
}
- Logger::debug('Search for tumblr blog', ['url' => $hookData['uri']]);
-
$hookData['result'] = tumblr_get_contact_by_url($hookData['uri']);
}
Logger::debug('Got post', ['blog' => $matches[1], 'id' => $matches[2], 'result' => $result->response->posts]);
if (!empty($result->response->posts)) {
- $hookData['item_id'] = tumblr_process_post($result->response->posts[0], $hookData['uid']);
+ $hookData['item_id'] = tumblr_process_post($result->response->posts[0], $hookData['uid'], Item::PR_FETCHED);
}
}
'$submit' => DI::l10n()->t('Save Settings'),
'$consumer_key' => ['consumer_key', DI::l10n()->t('Consumer Key'), DI::config()->get('tumblr', 'consumer_key'), ''],
'$consumer_secret' => ['consumer_secret', DI::l10n()->t('Consumer Secret'), DI::config()->get('tumblr', 'consumer_secret'), ''],
+ '$max_tags' => ['max_tags', DI::l10n()->t('Maximum tags'), DI::config()->get('tumblr', 'max_tags', TUMBLR_DEFAULT_MAXIMUM_TAGS), DI::l10n()->t('Maximum number of tags that a user can follow. Enter 0 to deactivate the feature.')],
]);
}
{
DI::config()->set('tumblr', 'consumer_key', trim($_POST['consumer_key'] ?? ''));
DI::config()->set('tumblr', 'consumer_secret', trim($_POST['consumer_secret'] ?? ''));
+ DI::config()->set('tumblr', 'max_tags', max(0, intval($_POST['max_tags'] ?? '')));
}
function tumblr_settings(array &$data)
$enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post', false);
$def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post_by_default', false);
$import = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'import', false);
+ $tags = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'tags');
+
+ $max_tags = DI::config()->get('tumblr', 'max_tags', TUMBLR_DEFAULT_MAXIMUM_TAGS);
+ $tags_str = implode(', ', $tags ?? []);
$cachekey = 'tumblr-blogs-' . DI::userSession()->getLocalUserId();
$blogs = DI::cache()->get($cachekey);
if (empty($blogs)) {
'$enable' => ['tumblr', DI::l10n()->t('Enable Tumblr Post Addon'), $enabled],
'$bydefault' => ['tumblr_bydefault', DI::l10n()->t('Post to Tumblr by default'), $def_enabled],
'$import' => ['tumblr_import', DI::l10n()->t('Import the remote timeline'), $import],
+ '$tags' => ['tags', DI::l10n()->t('Subscribed tags'), $tags_str, DI::l10n()->t('Comma separated list of up to %d tags that will be imported additionally to the timeline', $max_tags)],
'$page_select' => $page_select ?? '',
]);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'tumblr', 'page', $_POST['tumblr_page']);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'tumblr', 'post_by_default', intval($_POST['tumblr_bydefault']));
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'tumblr', 'import', intval($_POST['tumblr_import']));
+
+ $max_tags = DI::config()->get('tumblr', 'max_tags', TUMBLR_DEFAULT_MAXIMUM_TAGS);
+ $tags = [];
+ foreach (explode(',', $_POST['tags']) as $tag) {
+ if (count($tags) < $max_tags) {
+ $tags[] = trim($tag, ' #');
+ }
+ }
+
+ DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'tumblr', 'tags', $tags);
}
}
if ($last) {
$next = $last + ($poll_interval * 60);
if ($next > time()) {
- Logger::notice('poll intervall not reached');
+ Logger::notice('poll interval not reached');
return;
}
}
Logger::notice('importing timeline - start', ['user' => $pconfig['uid']]);
tumblr_fetch_dashboard($pconfig['uid']);
+ tumblr_fetch_tags($pconfig['uid']);
Logger::notice('importing timeline - done', ['user' => $pconfig['uid']]);
}
return $post;
}
+/**
+ * Fetch posts for user defined hashtags for the given user
+ *
+ * @param integer $uid
+ * @return void
+ */
+function tumblr_fetch_tags(int $uid)
+{
+ if (!DI::config()->get('tumblr', 'max_tags', TUMBLR_DEFAULT_MAXIMUM_TAGS)) {
+ return;
+ }
+
+ foreach (DI::pConfig()->get($uid, 'tumblr', 'tags', []) as $tag) {
+ $data = tumblr_get($uid, 'tagged', ['tag' => $tag]);
+ foreach (array_reverse($data->response) as $post) {
+ $id = tumblr_process_post($post, $uid, Item::PR_TAG);
+ if (!empty($id)) {
+ Logger::debug('Tag post imported', ['tag' => $tag, 'id' => $id]);
+ }
+ }
+ }
+}
+
/**
* Fetch the dashboard (timeline) for the given user
*
*/
function tumblr_fetch_dashboard(int $uid)
{
- $page = tumblr_get_page($uid);
-
$parameters = ['reblog_info' => false, 'notes_info' => false, 'npf' => false];
$last = DI::pConfig()->get($uid, 'tumblr', 'last_id');
}
foreach (array_reverse($dashboard->response->posts) as $post) {
- $uri = 'tumblr::' . $post->id_string . ':' . $post->reblog_key;
-
if ($post->id > $last) {
$last = $post->id;
}
- Logger::debug('Importing post', ['uid' => $uid, 'created' => date(DateTimeFormat::MYSQL, $post->timestamp), 'uri' => $uri]);
-
- if (Post::exists(['uri' => $uri, 'uid' => $uid]) || ($post->blog->uuid == $page)) {
- DI::pConfig()->set($uid, 'tumblr', 'last_id', $last);
- continue;
- }
-
- tumblr_process_post($post, $uid, $uri);
+ Logger::debug('Importing post', ['uid' => $uid, 'created' => date(DateTimeFormat::MYSQL, $post->timestamp), 'id' => $post->id_string]);
+ tumblr_process_post($post, $uid, Item::PR_NONE);
DI::pConfig()->set($uid, 'tumblr', 'last_id', $last);
}
}
-function tumblr_process_post(stdClass $post, int $uid, string $uri = ''): int
+function tumblr_process_post(stdClass $post, int $uid, int $post_reason): int
{
- if (empty($uri)) {
- $uri = 'tumblr::' . $post->id_string . ':' . $post->reblog_key;
+ $uri = 'tumblr::' . $post->id_string . ':' . $post->reblog_key;
+
+ if (Post::exists(['uri' => $uri, 'uid' => $uid]) || ($post->blog->uuid == tumblr_get_page($uid))) {
+ return 0;
}
$item = tumblr_get_header($post, $uri, $uid);
$item = tumblr_get_content($item, $post);
+ $item['post-reason'] = $post_reason;
+
+ if (!empty($post->followed)) {
+ $item['post-reason'] = Item::PR_FOLLOWER;
+ }
+
$id = item::insert($item);
if ($id) {
'url' => $url,
'nurl' => Strings::normaliseLink($url),
'alias' => $blog->url,
- 'name' => $blog->title,
+ 'name' => $blog->title ?: $blog->name,
'nick' => $blog->name,
'addr' => $blog->name . '@tumblr.com',
'about' => HTML::toBBCode($blog->description),
'nurl' => Strings::normaliseLink($url),
'uri-id' => $uri_id,
'alias' => $info->response->blog->url,
- 'name' => $info->response->blog->title,
+ 'name' => $info->response->blog->title ?: $info->response->blog->name,
'nick' => $info->response->blog->name,
'addr' => $info->response->blog->name . '@tumblr.com',
'about' => HTML::toBBCode($info->response->blog->description),
return [];
}
+ Logger::debug('Update Tumblr blog data', ['url' => $url]);
+
$curlResult = DI::httpClient()->get('https://api.tumblr.com/v2/blog/' . $blog . '/info?api_key=' . $consumer_key);
$body = $curlResult->getBody();
$data = json_decode($body);
'notify' => '',
'poll' => 'tumblr::' . $data->response->blog->uuid,
'poco' => '',
- 'name' => $data->response->blog->title,
+ 'name' => $data->response->blog->title ?: $data->response->blog->name,
'nick' => $data->response->blog->name,
'network' => Protocol::TUMBLR,
'baseurl' => $baseurl,