use Friendica\Model\Conversation;
use Friendica\Model\Group;
use Friendica\Model\Item;
-use Friendica\Model\ItemContent;
use Friendica\Model\ItemURI;
use Friendica\Model\Post;
use Friendica\Model\Tag;
if (DI::pConfig()->get($post['uid'], 'twitter', 'import')) {
// Don't fork if it isn't a reply to a twitter post
- if (($post['parent'] != $post['id']) && !Item::exists(['id' => $post['parent'], 'network' => Protocol::TWITTER])) {
+ if (($post['parent'] != $post['id']) && !Post::exists(['id' => $post['parent'], 'network' => Protocol::TWITTER])) {
Logger::notice('No twitter parent found', ['item' => $post['id']]);
$b['execute'] = false;
return;
return;
}
+ $thr_parent = null;
+
if ($b['parent'] != $b['id']) {
Logger::debug('Got comment', ['item' => $b]);
}
$condition = ['uri' => $b["thr-parent"], 'uid' => $b["uid"]];
- $orig_post = Item::selectFirst([], $condition);
- if (!DBA::isResult($orig_post)) {
+ $thr_parent = Post::selectFirst(['uri', 'extid', 'author-link', 'author-nick', 'author-network'], $condition);
+ if (!DBA::isResult($thr_parent)) {
Logger::warning('No parent found', ['thr-parent' => $b["thr-parent"]]);
return;
- } else {
- $iscomment = true;
}
+ if ($thr_parent['author-network'] == Protocol::TWITTER) {
+ $nickname = '@[url=' . $thr_parent['author-link'] . ']' . $thr_parent['author-nick'] . '[/url]';
+ $nicknameplain = '@' . $thr_parent['author-nick'];
- $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
- $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
- $nicknameplain = "@" . $nicknameplain;
-
- Logger::info('Comparing', ['nickname' => $nickname, 'nicknameplain' => $nicknameplain, 'body' => $b["body"]]);
- if ((strpos($b["body"], $nickname) === false) && (strpos($b["body"], $nicknameplain) === false)) {
- $b["body"] = $nickname . " " . $b["body"];
+ Logger::info('Comparing', ['nickname' => $nickname, 'nicknameplain' => $nicknameplain, 'body' => $b["body"]]);
+ if ((strpos($b["body"], $nickname) === false) && (strpos($b["body"], $nicknameplain) === false)) {
+ $b["body"] = $nickname . " " . $b["body"];
+ }
}
- Logger::debug('Parent found', ['parent' => $orig_post]);
+ Logger::debug('Parent found', ['parent' => $thr_parent]);
} else {
- $iscomment = false;
-
if ($b['private'] || !strstr($b['postopts'], 'twitter')) {
return;
}
}
if (($b['verb'] == Activity::POST) && $b['deleted']) {
- twitter_action($a, $b["uid"], twitter_get_id($orig_post["uri"]), "delete");
+ twitter_action($a, $b['uid'], twitter_get_id($thr_parent['uri']), 'delete');
}
if ($b['verb'] == Activity::LIKE) {
if ($b['verb'] == Activity::ANNOUNCE) {
Logger::info('Retweet', ['uid' => $b['uid'], 'id' => twitter_get_id($b["thr-parent"])]);
if ($b['deleted']) {
- twitter_action($a, $b["uid"], twitter_get_id($orig_post["extid"]), "delete");
+ twitter_action($a, $b['uid'], twitter_get_id($thr_parent['extid']), 'delete');
} else {
twitter_retweet($b["uid"], twitter_get_id($b["thr-parent"]));
}
$b['body'] = twitter_update_mentions($b['body']);
- $msgarr = ItemContent::getPlaintextPost($b, $max_char, true, BBCode::TWITTER);
+ $msgarr = Plaintext::getPost($b, $max_char, true, BBCode::TWITTER);
Logger::info('Got plaintext', ['id' => $b['id'], 'message' => $msgarr]);
$msg = $msgarr["text"];
$post['status'] = $msg;
- if ($iscomment) {
- $post["in_reply_to_status_id"] = twitter_get_id($orig_post["uri"]);
+ if ($thr_parent) {
+ $post['in_reply_to_status_id'] = twitter_get_id($thr_parent['uri']);
}
$url = 'statuses/update';
if (!empty($result->errors)) {
Logger::error('Send to Twitter failed', ['id' => $b['id'], 'error' => $result->errors]);
Worker::defer();
- } elseif ($iscomment) {
+ } elseif ($thr_parent) {
Logger::notice('Post send, updating extid', ['id' => $b['id'], 'extid' => $result->id_str]);
Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
}
Logger::notice('Start deleting expired posts');
- $r = Item::select(['id', 'guid'], ['deleted' => true, 'network' => Protocol::TWITTER]);
- while ($row = DBA::fetch($r)) {
+ $r = Post::select(['id', 'guid'], ['deleted' => true, 'network' => Protocol::TWITTER]);
+ while ($row = Post::fetch($r)) {
Logger::info('[twitter] Delete expired item', ['id' => $row['id'], 'guid' => $row['guid'], 'callstack' => \Friendica\Core\System::callstack()]);
DBA::delete('item', ['id' => $row['id']]);
}
$item["plink"] = DI::baseUrl()->get() . "/display/" . $item["guid"];
$condition = ['uri' => $item["thr-parent"], 'uid' => local_user()];
- $orig_post = Item::selectFirst(['author-link'], $condition);
+ $orig_post = Post::selectFirst(['author-link'], $condition);
if (DBA::isResult($orig_post)) {
$nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
$nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
}
}
- $msgarr = ItemContent::getPlaintextPost($item, $max_char, true, BBCode::TWITTER);
+ $msgarr = Plaintext::getPost($item, $max_char, true, BBCode::TWITTER);
$msg = $msgarr["text"];
if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
*/
function twitter_do_mirrorpost(App $a, $uid, $post)
{
+ $datarray['uid'] = $uid;
$datarray['extid'] = 'twitter::' . $post->id;
$datarray['title'] = '';
$connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
+ // Ensure to have the own contact
+ try {
+ twitter_fetch_own_contact($a, $uid);
+ } catch (TwitterOAuthException $e) {
+ Logger::warning('Error fetching own contact', ['uid' => $uid, 'message' => $e->getMessage()]);
+ return;
+ }
+
$parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended", "include_ext_alt_text" => true];
$first_time = ($lastid == "");
if (!empty($cid)) {
DBA::update('contact', $fields, ['id' => $cid]);
Contact::updateAvatar($cid, $avatar);
+ } else {
+ Logger::warning('No contact found', ['fields' => $fields]);
}
$contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'alias' => "twitter::" . $data->id_str]);
- if (!DBA::isResult($contact) && !$create_user) {
+ if (!DBA::isResult($contact) && empty($cid)) {
Logger::warning('User contact not found', ['uid' => $uid, 'twitter-id' => $data->id_str]);
return 0;
+ } elseif (!$create_user) {
+ return $cid;
}
if (!DBA::isResult($contact)) {
$postarray['uri'] = "twitter::" . $post->id_str;
$postarray['protocol'] = Conversation::PARCEL_TWITTER;
$postarray['source'] = json_encode($post);
+ $postarray['direction'] = Conversation::PULL;
if (empty($uriid)) {
$uriid = $postarray['uri-id'] = ItemURI::insert(['uri' => $postarray['uri']]);
}
// Don't import our own comments
- if (Item::exists(['extid' => $postarray['uri'], 'uid' => $uid])) {
+ if (Post::exists(['extid' => $postarray['uri'], 'uid' => $uid])) {
Logger::info('Item found', ['extid' => $postarray['uri']]);
return [];
}
if ($post->in_reply_to_status_id_str != "") {
$thr_parent = "twitter::" . $post->in_reply_to_status_id_str;
- $item = Item::selectFirst(['uri'], ['uri' => $thr_parent, 'uid' => $uid]);
+ $item = Post::selectFirst(['uri'], ['uri' => $thr_parent, 'uid' => $uid]);
if (!DBA::isResult($item)) {
- $item = Item::selectFirst(['uri'], ['extid' => $thr_parent, 'uid' => $uid]);
+ $item = Post::selectFirst(['uri'], ['extid' => $thr_parent, 'uid' => $uid]);
}
if (DBA::isResult($item)) {
$postarray['thr-parent'] = $retweet['uri'];
} else {
$retweet['source'] = $postarray['source'];
+ $retweet['direction'] = $postarray['direction'];
$retweet['private'] = $postarray['private'];
$retweet['allow_cid'] = $postarray['allow_cid'];
$retweet['contact-id'] = $postarray['contact-id'];
break;
}
- if (Item::exists(['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
+ if (Post::exists(['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
break;
}