Commit e9cc5092 authored by Max Kostikov's avatar Max Kostikov

Merge branch 'dev' into 'dev'

Code reuse

See merge request hubzilla/addons!276
parents 11299571 a7cb4866
...@@ -168,16 +168,34 @@ function short_link ($url) { ...@@ -168,16 +168,34 @@ function short_link ($url) {
} }; } };
/**
* @brief Cut message and add link
*/
function twitter_short_message(&$msg, $link, $limit, $shortlink = true) {
if ($shortlink && strlen($link) > 20)
$link = short_link($link);
if (strlen($msg . " " . $link) > $limit) {
$msg = substr($msg, 0, ($limit - strlen($link)));
if (substr($msg, -1) != "\n")
$msg = rtrim(substr($msg, 0, strrpos($msg, " ")), "?.,:;!-") . "...";
}
$msg .= " " . $link;
}
/** /**
* @brief Shorten message intelligent using bulit-in functions * @brief Shorten message intelligent using bulit-in functions
*/ */
function twitter_shortenmsg($b, $shortlink = true) { function twitter_shortenmsg($b) {
require_once("include/api.php"); require_once("include/api.php");
require_once("include/bbcode.php"); require_once("include/bbcode.php");
require_once("include/html2plain.php"); require_once("include/html2plain.php");
// Give Twitter extra 3 chars to be sure that post will fit to their limits // Give Twitter extra 10 chars to be sure that post will fit to their limits
$max_char = get_pconfig($b['uid'], 'twitter', 'tweet_length', 280) - 3; $max_char = get_pconfig($b['uid'], 'twitter', 'tweet_length', 280) - 10;
// Add title at the top if exist // Add title at the top if exist
$body = $b["body"]; $body = $b["body"];
...@@ -227,17 +245,8 @@ function twitter_shortenmsg($b, $shortlink = true) { ...@@ -227,17 +245,8 @@ function twitter_shortenmsg($b, $shortlink = true) {
$msg = trim($msg); $msg = trim($msg);
// Add URL if exist and no image found // Add URL if exist and no image found
if (empty($image) && $link != '') { if (empty($image) && $link != '')
twitter_short_message($msg, $link, $max_char - 23);
if ($shortlink && strlen($link) > 20)
$link = short_link($link);
if (strlen($msg . " " . $link) > ($max_char - 23)) {
$msg = trim(substr($msg, 0, ($max_char - strlen($link))));
if (substr($msg, -1) != "\n")
$msg = rtrim(substr($msg, 0, strrpos($msg, " ")), "?.,:;!-") . "...";
}
$msg .= " " . $link;
}
$msglink = $b["plink"]; $msglink = $b["plink"];
...@@ -245,20 +254,10 @@ function twitter_shortenmsg($b, $shortlink = true) { ...@@ -245,20 +254,10 @@ function twitter_shortenmsg($b, $shortlink = true) {
if (strlen($msg . " " . $msglink) <= $max_char) if (strlen($msg . " " . $msglink) <= $max_char)
return([ "msg" => $msg . " " . $msglink, "image" => $image ]); return([ "msg" => $msg . " " . $msglink, "image" => $image ]);
// Shorten links if enabled
if ($shortlink && strlen($msglink) > 20)
$msglink = short_link($msglink);
// Shorten message // Shorten message
if (strlen($msg . " " . $msglink) > $max_char) { twitter_short_message($msg, $msglink, $max_char);
$msg = substr($msg, 0, ($max_char - strlen($msglink)));
$msg = substr($msg, 0, -1);
if (substr($msg, -1) != "\n")
$msg = rtrim(substr($msg, 0, strrpos($msg, " ")), "?.,:;!-") . "...";
}
return([ "msg" => $msg . " " . $msglink, "image" => $image ]); return([ "msg" => $msg, "image" => $image ]);
} }
...@@ -453,7 +452,7 @@ function twitter_post_hook(&$a,&$b) { ...@@ -453,7 +452,7 @@ function twitter_post_hook(&$a,&$b) {
if ($result->httpstatus != 200) { if ($result->httpstatus != 200) {
logger('Send to Twitter failed with HTTP status code ' . $result->httpstatus . '; error message: "' . print_r($result->errors, true) . '"'); logger('Send to Twitter failed with HTTP status code ' . $result->httpstatus . '; error message: "' . print_r($result->errors, true) . '"');
logger('twitter post completed', LOGGER_INFO); logger('twitter post completed');
// // Workaround: Remove the picture link so that the post can be reposted without it // // Workaround: Remove the picture link so that the post can be reposted without it
// $msg .= " ".$image; // $msg .= " ".$image;
...@@ -628,4 +627,3 @@ function twitter_expand_entities($a, $body, $item, $no_tags = false, $dontinclud ...@@ -628,4 +627,3 @@ function twitter_expand_entities($a, $body, $item, $no_tags = false, $dontinclud
} }
return(array("body" => $body, "tags" => $tags)); return(array("body" => $body, "tags" => $tags));
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment