+ if (!$item['uid']) {
+ return;
+ }
+ if (!$item['contact-id']) {
+ return;
+ }
+ if (!$item['uri']) {
+ return;
+ }
+
+ q("INSERT INTO `mailstream_item` (`uid`, `contact-id`, `uri`, `message-id`) " .
+ "VALUES (%d, '%s', '%s', '%s')", intval($item['uid']),
+ intval($item['contact-id']), dbesc($item['uri']), dbesc(mailstream_generate_id($a, $item['uri'])));
+ $r = q('SELECT * FROM `mailstream_item` WHERE `uid` = %d AND `contact-id` = %d AND `uri` = "%s"', intval($item['uid']), intval($item['contact-id']), dbesc($item['uri']));
+ if (count($r) != 1) {
+ logger('mailstream_post_remote_hook: Unexpected number of items returned from mailstream_item', LOGGER_NORMAL);
+ return;
+ }
+ $ms_item = $r[0];
+ logger('mailstream_post_remote_hook: created mailstream_item '
+ . $ms_item['id'] . ' for item ' . $item['uri'] . ' '
+ . $item['uid'] . ' ' . $item['contact-id'], LOGGER_DATA);
+ $user = mailstream_get_user($item['uid']);
+ if (!$user) {
+ logger('mailstream_post_remote_hook: no user ' . $item['uid'], LOGGER_NORMAL);
+ return;
+ }
+ mailstream_send($a, $ms_item, $item, $user);
+}
+
+function mailstream_get_user($uid) {
+ $r = q('SELECT * FROM `user` WHERE `uid` = %d', intval($uid));
+ if (count($r) != 1) {
+ logger('mailstream_post_remote_hook: Unexpected number of users returned', LOGGER_NORMAL);
+ return;
+ }
+ return $r[0];