/**
  * Store a newly created conversation in storage.
  *
  * @return Response
  */
 public function store()
 {
     $rules = array('users' => 'required|array', 'body' => 'required');
     $validator = Validator::make(Input::only('users', 'body'), $rules);
     if ($validator->fails()) {
         return Response::json(['success' => false, 'result' => $validator->messages()]);
     }
     // Create Conversation
     $params = array('created_at' => new DateTime(), 'name' => str_random(30), 'author_id' => Auth::user()->id);
     $conversation = Conversation::create($params);
     $conversation->users()->attach(Input::get('users'));
     $conversation->users()->attach(array(Auth::user()->id));
     // Create Message
     $params = array('conversation_id' => $conversation->id, 'body' => Input::get('body'), 'user_id' => Auth::user()->id, 'created_at' => new DateTime());
     $message = Message::create($params);
     // Create Message Notifications
     $messages_notifications = array();
     foreach (Input::get('users') as $user_id) {
         array_push($messages_notifications, new MessageNotification(array('user_id' => $user_id, 'read' => false, 'conversation_id' => $conversation->id)));
         // Publish Data To Redis
         $data = array('room' => $user_id, 'message' => array('conversation_id' => $conversation->id));
         Event::fire(ChatConversationsEventHandler::EVENT, array(json_encode($data)));
     }
     $message->messages_notifications()->saveMany($messages_notifications);
     return Redirect::route('chat.index', array('conversation', $conversation->name));
 }
 function saveStatus($status)
 {
     $profile = $this->ensureProfile($status->user);
     if (empty($profile)) {
         common_log(LOG_ERR, $this->name() . ' - Problem saving notice. No associated Profile.');
         return null;
     }
     $statusId = twitter_id($status);
     $statusUri = $this->makeStatusURI($status->user->screen_name, $statusId);
     // check to see if we've already imported the status
     $n2s = Notice_to_status::staticGet('status_id', $statusId);
     if (!empty($n2s)) {
         common_log(LOG_INFO, $this->name() . " - Ignoring duplicate import: {$statusId}");
         return Notice::staticGet('id', $n2s->notice_id);
     }
     // If it's a retweet, save it as a repeat!
     if (!empty($status->retweeted_status)) {
         common_log(LOG_INFO, "Status {$statusId} is a retweet of " . twitter_id($status->retweeted_status) . ".");
         $original = $this->saveStatus($status->retweeted_status);
         if (empty($original)) {
             return null;
         } else {
             $author = $original->getProfile();
             // TRANS: Message used to repeat a notice. RT is the abbreviation of 'retweet'.
             // TRANS: %1$s is the repeated user's name, %2$s is the repeated notice.
             $content = sprintf(_m('RT @%1$s %2$s'), $author->nickname, $original->content);
             if (Notice::contentTooLong($content)) {
                 $contentlimit = Notice::maxContent();
                 $content = mb_substr($content, 0, $contentlimit - 4) . ' ...';
             }
             $repeat = Notice::saveNew($profile->id, $content, 'twitter', array('repeat_of' => $original->id, 'uri' => $statusUri, 'is_local' => Notice::GATEWAY));
             common_log(LOG_INFO, "Saved {$repeat->id} as a repeat of {$original->id}");
             Notice_to_status::saveNew($repeat->id, $statusId);
             return $repeat;
         }
     }
     $notice = new Notice();
     $notice->profile_id = $profile->id;
     $notice->uri = $statusUri;
     $notice->url = $statusUri;
     $notice->created = strftime('%Y-%m-%d %H:%M:%S', strtotime($status->created_at));
     $notice->source = 'twitter';
     $notice->reply_to = null;
     $replyTo = twitter_id($status, 'in_reply_to_status_id');
     if (!empty($replyTo)) {
         common_log(LOG_INFO, "Status {$statusId} is a reply to status {$replyTo}");
         $n2s = Notice_to_status::staticGet('status_id', $replyTo);
         if (empty($n2s)) {
             common_log(LOG_INFO, "Couldn't find local notice for status {$replyTo}");
         } else {
             $reply = Notice::staticGet('id', $n2s->notice_id);
             if (empty($reply)) {
                 common_log(LOG_INFO, "Couldn't find local notice for status {$replyTo}");
             } else {
                 common_log(LOG_INFO, "Found local notice {$reply->id} for status {$replyTo}");
                 $notice->reply_to = $reply->id;
                 $notice->conversation = $reply->conversation;
             }
         }
     }
     if (empty($notice->conversation)) {
         $conv = Conversation::create();
         $notice->conversation = $conv->id;
         common_log(LOG_INFO, "No known conversation for status {$statusId} so making a new one {$conv->id}.");
     }
     $notice->is_local = Notice::GATEWAY;
     $notice->content = html_entity_decode($status->text, ENT_QUOTES, 'UTF-8');
     $notice->rendered = $this->linkify($status);
     if (Event::handle('StartNoticeSave', array(&$notice))) {
         $id = $notice->insert();
         if (!$id) {
             common_log_db_error($notice, 'INSERT', __FILE__);
             common_log(LOG_ERR, $this->name() . ' - Problem saving notice.');
         }
         Event::handle('EndNoticeSave', array($notice));
     }
     Notice_to_status::saveNew($notice->id, $statusId);
     $this->saveStatusMentions($notice, $status);
     $this->saveStatusAttachments($notice, $status);
     $notice->blowOnInsert();
     return $notice;
 }
Exemple #3
0
 static function saveActivity(Activity $act, Profile $actor, array $options = array())
 {
     // First check if we're going to let this Activity through from the specific actor
     if (!$actor->hasRight(Right::NEWNOTICE)) {
         common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $actor->getNickname());
         // TRANS: Client exception thrown when a user tries to post while being banned.
         throw new ClientException(_m('You are banned from posting notices on this site.'), 403);
     }
     if (common_config('throttle', 'enabled') && !self::checkEditThrottle($actor->id)) {
         common_log(LOG_WARNING, 'Excessive posting by profile #' . $actor->id . '; throttled.');
         // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame.
         throw new ClientException(_m('Too many notices too fast; take a breather ' . 'and post again in a few minutes.'));
     }
     // Get ActivityObject properties
     $actobj = null;
     if (!empty($act->id)) {
         // implied object
         $options['uri'] = $act->id;
         $options['url'] = $act->link;
     } else {
         $actobj = count($act->objects) == 1 ? $act->objects[0] : null;
         if (!is_null($actobj) && !empty($actobj->id)) {
             $options['uri'] = $actobj->id;
             if ($actobj->link) {
                 $options['url'] = $actobj->link;
             } elseif (preg_match('!^https?://!', $actobj->id)) {
                 $options['url'] = $actobj->id;
             }
         }
     }
     $defaults = array('groups' => array(), 'is_local' => $actor->isLocal() ? self::LOCAL_PUBLIC : self::REMOTE, 'mentions' => array(), 'reply_to' => null, 'repeat_of' => null, 'scope' => null, 'source' => 'unknown', 'tags' => array(), 'uri' => null, 'url' => null, 'urls' => array(), 'distribute' => true);
     // options will have default values when nothing has been supplied
     $options = array_merge($defaults, $options);
     foreach (array_keys($defaults) as $key) {
         // Only convert the keynames we specify ourselves from 'defaults' array into variables
         ${$key} = $options[$key];
     }
     extract($options, EXTR_SKIP);
     // dupe check
     $stored = new Notice();
     if (!empty($uri) && !ActivityUtils::compareVerbs($act->verb, array(ActivityVerb::DELETE))) {
         $stored->uri = $uri;
         if ($stored->find()) {
             common_debug('cannot create duplicate Notice URI: ' . $stored->uri);
             // I _assume_ saving a Notice with a colliding URI means we're really trying to
             // save the same notice again...
             throw new AlreadyFulfilledException('Notice URI already exists');
         }
     }
     $autosource = common_config('public', 'autosource');
     // Sandboxed are non-false, but not 1, either
     if (!$actor->hasRight(Right::PUBLICNOTICE) || $source && $autosource && in_array($source, $autosource)) {
         // FIXME: ...what about remote nonpublic? Hmmm. That is, if we sandbox remote profiles...
         $stored->is_local = Notice::LOCAL_NONPUBLIC;
     } else {
         $stored->is_local = intval($is_local);
     }
     if (!$stored->isLocal()) {
         // Only do these checks for non-local notices. Local notices will generate these values later.
         if (!common_valid_http_url($url)) {
             common_debug('Bad notice URL: [' . $url . '], URI: [' . $uri . ']. Cannot link back to original! This is normal for shared notices etc.');
         }
         if (empty($uri)) {
             throw new ServerException('No URI for remote notice. Cannot accept that.');
         }
     }
     $stored->profile_id = $actor->id;
     $stored->source = $source;
     $stored->uri = $uri;
     $stored->url = $url;
     $stored->verb = $act->verb;
     // Notice content. We trust local users to provide HTML we like, but of course not remote users.
     // FIXME: What about local users importing feeds? Mirror functions must filter out bad HTML first...
     $content = $act->content ?: $act->summary;
     if (is_null($content) && !is_null($actobj)) {
         $content = $actobj->content ?: $actobj->summary;
     }
     $stored->rendered = $actor->isLocal() ? $content : common_purify($content);
     // yeah, just don't use getRendered() here since it's not inserted yet ;)
     $stored->content = common_strip_html($stored->rendered);
     // Maybe a missing act-time should be fatal if the actor is not local?
     if (!empty($act->time)) {
         $stored->created = common_sql_date($act->time);
     } else {
         $stored->created = common_sql_now();
     }
     $reply = null;
     if ($act->context instanceof ActivityContext && !empty($act->context->replyToID)) {
         $reply = self::getKV('uri', $act->context->replyToID);
     }
     if (!$reply instanceof Notice && $act->target instanceof ActivityObject) {
         $reply = self::getKV('uri', $act->target->id);
     }
     if ($reply instanceof Notice) {
         if (!$reply->inScope($actor)) {
             // TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
             // TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
             throw new ClientException(sprintf(_m('%1$s has no right to reply to notice %2$d.'), $actor->getNickname(), $reply->id), 403);
         }
         $stored->reply_to = $reply->id;
         $stored->conversation = $reply->conversation;
         // If the original is private to a group, and notice has no group specified,
         // make it to the same group(s)
         if (empty($groups) && $reply->scope & Notice::GROUP_SCOPE) {
             $replyGroups = $reply->getGroups();
             foreach ($replyGroups as $group) {
                 if ($actor->isMember($group)) {
                     $groups[] = $group->id;
                 }
             }
         }
         if (is_null($scope)) {
             $scope = $reply->scope;
         }
     } else {
         // If we don't know the reply, we might know the conversation!
         // This will happen if a known remote user replies to an
         // unknown remote user - within a known conversation.
         if (empty($stored->conversation) and !empty($act->context->conversation)) {
             $conv = Conversation::getKV('uri', $act->context->conversation);
             if ($conv instanceof Conversation) {
                 common_debug('Conversation stitched together from (probably) a reply activity to unknown remote user. Activity creation time (' . $stored->created . ') should maybe be compared to conversation creation time (' . $conv->created . ').');
             } else {
                 // Conversation entry with specified URI was not found, so we must create it.
                 common_debug('Conversation URI not found, so we will create it with the URI given in the context of the activity: ' . $act->context->conversation);
                 // The insert in Conversation::create throws exception on failure
                 $conv = Conversation::create($act->context->conversation, $stored->created);
             }
             $stored->conversation = $conv->getID();
             unset($conv);
         }
     }
     // If it's not part of a conversation, it's the beginning of a new conversation.
     if (empty($stored->conversation)) {
         $conv = Conversation::create();
         $stored->conversation = $conv->getID();
         unset($conv);
     }
     $notloc = null;
     if ($act->context instanceof ActivityContext) {
         if ($act->context->location instanceof Location) {
             $notloc = Notice_location::fromLocation($act->context->location);
         }
     } else {
         $act->context = new ActivityContext();
     }
     $stored->scope = self::figureOutScope($actor, $groups, $scope);
     foreach ($act->categories as $cat) {
         if ($cat->term) {
             $term = common_canonical_tag($cat->term);
             if (!empty($term)) {
                 $tags[] = $term;
             }
         }
     }
     foreach ($act->enclosures as $href) {
         // @todo FIXME: Save these locally or....?
         $urls[] = $href;
     }
     if (ActivityUtils::compareVerbs($stored->verb, array(ActivityVerb::POST))) {
         if (empty($act->objects[0]->type)) {
             // Default type for the post verb is 'note', but we know it's
             // a 'comment' if it is in reply to something.
             $stored->object_type = empty($stored->reply_to) ? ActivityObject::NOTE : ActivityObject::COMMENT;
         } else {
             //TODO: Is it safe to always return a relative URI? The
             // JSON version of ActivityStreams always use it, so we
             // should definitely be able to handle it...
             $stored->object_type = ActivityUtils::resolveUri($act->objects[0]->type, true);
         }
     }
     if (Event::handle('StartNoticeSave', array(&$stored))) {
         // XXX: some of these functions write to the DB
         try {
             $result = $stored->insert();
             // throws exception on error
             if ($notloc instanceof Notice_location) {
                 $notloc->notice_id = $stored->getID();
                 $notloc->insert();
             }
             $orig = clone $stored;
             // for updating later in this try clause
             $object = null;
             Event::handle('StoreActivityObject', array($act, $stored, $options, &$object));
             if (empty($object)) {
                 throw new ServerException('Unsuccessful call to StoreActivityObject ' . $stored->getUri() . ': ' . $act->asString());
             }
             // If something changed in the Notice during StoreActivityObject
             $stored->update($orig);
         } catch (Exception $e) {
             if (empty($stored->id)) {
                 common_debug('Failed to save stored object entry in database (' . $e->getMessage() . ')');
             } else {
                 common_debug('Failed to store activity object in database (' . $e->getMessage() . '), deleting notice id ' . $stored->id);
                 $stored->delete();
             }
             throw $e;
         }
     }
     if (!$stored instanceof Notice) {
         throw new ServerException('StartNoticeSave did not give back a Notice');
     }
     // Only save 'attention' and metadata stuff (URLs, tags...) stuff if
     // the activityverb is a POST (since stuff like repeat, favorite etc.
     // reasonably handle notifications themselves.
     if (ActivityUtils::compareVerbs($stored->verb, array(ActivityVerb::POST))) {
         if (!empty($tags)) {
             $stored->saveKnownTags($tags);
         } else {
             $stored->saveTags();
         }
         // Note: groups may save tags, so must be run after tags are saved
         // to avoid errors on duplicates.
         $stored->saveAttentions($act->context->attention);
         if (!empty($urls)) {
             $stored->saveKnownUrls($urls);
         } else {
             $stored->saveUrls();
         }
     }
     if ($distribute) {
         // Prepare inbox delivery, may be queued to background.
         $stored->distribute();
     }
     return $stored;
 }
Exemple #4
0
 /**
  * Save a new notice and push it out to subscribers' inboxes.
  * Poster's permissions are checked before sending.
  *
  * @param int $profile_id Profile ID of the poster
  * @param string $content source message text; links may be shortened
  *                        per current user's preference
  * @param string $source source key ('web', 'api', etc)
  * @param array $options Associative array of optional properties:
  *              string 'created' timestamp of notice; defaults to now
  *              int 'is_local' source/gateway ID, one of:
  *                  Notice::LOCAL_PUBLIC    - Local, ok to appear in public timeline
  *                  Notice::REMOTE          - Sent from a remote service;
  *                                            hide from public timeline but show in
  *                                            local "and friends" timelines
  *                  Notice::LOCAL_NONPUBLIC - Local, but hide from public timeline
  *                  Notice::GATEWAY         - From another non-OStatus service;
  *                                            will not appear in public views
  *              float 'lat' decimal latitude for geolocation
  *              float 'lon' decimal longitude for geolocation
  *              int 'location_id' geoname identifier
  *              int 'location_ns' geoname namespace to interpret location_id
  *              int 'reply_to'; notice ID this is a reply to
  *              int 'repeat_of'; notice ID this is a repeat of
  *              string 'uri' unique ID for notice; defaults to local notice URL
  *              string 'url' permalink to notice; defaults to local notice URL
  *              string 'rendered' rendered HTML version of content
  *              array 'replies' list of profile URIs for reply delivery in
  *                              place of extracting @-replies from content.
  *              array 'groups' list of group IDs to deliver to, in place of
  *                              extracting ! tags from content
  *              array 'tags' list of hashtag strings to save with the notice
  *                           in place of extracting # tags from content
  *              array 'urls' list of attached/referred URLs to save with the
  *                           notice in place of extracting links from content
  *              boolean 'distribute' whether to distribute the notice, default true
  *              string 'object_type' URL of the associated object type (default ActivityObject::NOTE)
  *              string 'verb' URL of the associated verb (default ActivityVerb::POST)
  *              int 'scope' Scope bitmask; default to SITE_SCOPE on private sites, 0 otherwise
  *
  * @fixme tag override
  *
  * @return Notice
  * @throws ClientException
  */
 static function saveNew($profile_id, $content, $source, $options = null)
 {
     $defaults = array('uri' => null, 'url' => null, 'reply_to' => null, 'repeat_of' => null, 'scope' => null, 'distribute' => true, 'object_type' => null, 'verb' => null);
     if (!empty($options) && is_array($options)) {
         $options = array_merge($defaults, $options);
         extract($options);
     } else {
         extract($defaults);
     }
     if (!isset($is_local)) {
         $is_local = Notice::LOCAL_PUBLIC;
     }
     $profile = Profile::staticGet('id', $profile_id);
     $user = User::staticGet('id', $profile_id);
     if ($user) {
         // Use the local user's shortening preferences, if applicable.
         $final = $user->shortenLinks($content);
     } else {
         $final = common_shorten_links($content);
     }
     if ($source != 'activity' && $source != 'event') {
         if (Notice::contentTooLong($final)) {
             // TRANS: Client exception thrown if a notice contains too many characters.
             throw new ClientException(_('Problem saving notice. Too long.'));
         }
     }
     if (empty($profile)) {
         // TRANS: Client exception thrown when trying to save a notice for an unknown user.
         throw new ClientException(_('Problem saving notice. Unknown user.'));
     }
     if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
         common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
         // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame.
         throw new ClientException(_('Too many notices too fast; take a breather ' . 'and post again in a few minutes.'));
     }
     if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
         common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
         // TRANS: Client exception thrown when a user tries to post too many duplicate notices in a given time frame.
         throw new ClientException(_('Too many duplicate messages too quickly;' . ' take a breather and post again in a few minutes.'));
     }
     if (!$profile->hasRight(Right::NEWNOTICE)) {
         common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $profile->nickname);
         // TRANS: Client exception thrown when a user tries to post while being banned.
         throw new ClientException(_('You are banned from posting notices on this site.'), 403);
     }
     $notice = new Notice();
     $notice->profile_id = $profile_id;
     $autosource = common_config('public', 'autosource');
     // Sandboxed are non-false, but not 1, either
     if (!$profile->hasRight(Right::PUBLICNOTICE) || $source && $autosource && in_array($source, $autosource)) {
         $notice->is_local = Notice::LOCAL_NONPUBLIC;
     } else {
         $notice->is_local = $is_local;
     }
     if (!empty($created)) {
         $notice->created = $created;
     } else {
         $notice->created = common_sql_now();
     }
     $notice->content = $final;
     $notice->source = $source;
     $notice->uri = $uri;
     $notice->url = $url;
     // Get the groups here so we can figure out replies and such
     if (!isset($groups)) {
         $groups = self::groupsFromText($notice->content, $profile);
     }
     $reply = null;
     // Handle repeat case
     if (isset($repeat_of)) {
         // Check for a private one
         $repeat = Notice::staticGet('id', $repeat_of);
         if (empty($repeat)) {
             // TRANS: Client exception thrown in notice when trying to repeat a missing or deleted notice.
             throw new ClientException(_('Cannot repeat; original notice is missing or deleted.'));
         }
         if ($profile->id == $repeat->profile_id) {
             // TRANS: Client error displayed when trying to repeat an own notice.
             throw new ClientException(_('You cannot repeat your own notice.'));
         }
         if ($repeat->scope != Notice::SITE_SCOPE && $repeat->scope != Notice::PUBLIC_SCOPE) {
             // TRANS: Client error displayed when trying to repeat a non-public notice.
             throw new ClientException(_('Cannot repeat a private notice.'), 403);
         }
         if (!$repeat->inScope($profile)) {
             // The generic checks above should cover this, but let's be sure!
             // TRANS: Client error displayed when trying to repeat a notice you cannot access.
             throw new ClientException(_('Cannot repeat a notice you cannot read.'), 403);
         }
         if ($profile->hasRepeated($repeat->id)) {
             // TRANS: Client error displayed when trying to repeat an already repeated notice.
             throw new ClientException(_('You already repeated that notice.'));
         }
         $notice->repeat_of = $repeat_of;
     } else {
         $reply = self::getReplyTo($reply_to, $profile_id, $source, $final);
         if (!empty($reply)) {
             if (!$reply->inScope($profile)) {
                 // TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
                 // TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
                 throw new ClientException(sprintf(_('%1$s has no access to notice %2$d.'), $profile->nickname, $reply->id), 403);
             }
             $notice->reply_to = $reply->id;
             $notice->conversation = $reply->conversation;
             // If the original is private to a group, and notice has no group specified,
             // make it to the same group(s)
             if (empty($groups) && $reply->scope | Notice::GROUP_SCOPE) {
                 $groups = array();
                 $replyGroups = $reply->getGroups();
                 foreach ($replyGroups as $group) {
                     if ($profile->isMember($group)) {
                         $groups[] = $group->id;
                     }
                 }
             }
             // Scope set below
         }
     }
     if (!empty($lat) && !empty($lon)) {
         $notice->lat = $lat;
         $notice->lon = $lon;
     }
     if (!empty($location_ns) && !empty($location_id)) {
         $notice->location_id = $location_id;
         $notice->location_ns = $location_ns;
     }
     if (!empty($rendered)) {
         $notice->rendered = $rendered;
     } else {
         $notice->rendered = common_render_content($final, $notice);
     }
     if (empty($verb)) {
         if (!empty($notice->repeat_of)) {
             $notice->verb = ActivityVerb::SHARE;
             $notice->object_type = ActivityObject::ACTIVITY;
         } else {
             $notice->verb = ActivityVerb::POST;
         }
     } else {
         $notice->verb = $verb;
     }
     if (empty($object_type)) {
         $notice->object_type = empty($notice->reply_to) ? ActivityObject::NOTE : ActivityObject::COMMENT;
     } else {
         $notice->object_type = $object_type;
     }
     if (is_null($scope)) {
         // 0 is a valid value
         if (!empty($reply)) {
             $notice->scope = $reply->scope;
         } else {
             $notice->scope = self::defaultScope();
         }
     } else {
         $notice->scope = $scope;
     }
     // For private streams
     $user = $profile->getUser();
     if (!empty($user)) {
         if ($user->private_stream && ($notice->scope == Notice::PUBLIC_SCOPE || $notice->scope == Notice::SITE_SCOPE)) {
             $notice->scope |= Notice::FOLLOWER_SCOPE;
         }
     }
     // Force the scope for private groups
     foreach ($groups as $groupId) {
         $group = User_group::staticGet('id', $groupId);
         if (!empty($group)) {
             if ($group->force_scope) {
                 $notice->scope |= Notice::GROUP_SCOPE;
                 break;
             }
         }
     }
     if (Event::handle('StartNoticeSave', array(&$notice))) {
         // XXX: some of these functions write to the DB
         $id = $notice->insert();
         if (!$id) {
             common_log_db_error($notice, 'INSERT', __FILE__);
             // TRANS: Server exception thrown when a notice cannot be saved.
             throw new ServerException(_('Problem saving notice.'));
         }
         // Update ID-dependent columns: URI, conversation
         $orig = clone $notice;
         $changed = false;
         if (empty($uri)) {
             $notice->uri = common_notice_uri($notice);
             $changed = true;
         }
         // If it's not part of a conversation, it's
         // the beginning of a new conversation.
         if (empty($notice->conversation)) {
             $conv = Conversation::create();
             $notice->conversation = $conv->id;
             $changed = true;
         }
         if ($changed) {
             if (!$notice->update($orig)) {
                 common_log_db_error($notice, 'UPDATE', __FILE__);
                 // TRANS: Server exception thrown when a notice cannot be updated.
                 throw new ServerException(_('Problem saving notice.'));
             }
         }
     }
     // Clear the cache for subscribed users, so they'll update at next request
     // XXX: someone clever could prepend instead of clearing the cache
     $notice->blowOnInsert();
     // Save per-notice metadata...
     if (isset($replies)) {
         $notice->saveKnownReplies($replies);
     } else {
         $notice->saveReplies();
     }
     if (isset($tags)) {
         $notice->saveKnownTags($tags);
     } else {
         $notice->saveTags();
     }
     // Note: groups may save tags, so must be run after tags are saved
     // to avoid errors on duplicates.
     // Note: groups should always be set.
     $notice->saveKnownGroups($groups);
     if (isset($urls)) {
         $notice->saveKnownUrls($urls);
     } else {
         $notice->saveUrls();
     }
     if ($distribute) {
         // Prepare inbox delivery, may be queued to background.
         $notice->distribute();
     }
     return $notice;
 }
Exemple #5
0
 /**
  * Save a new notice and push it out to subscribers' inboxes.
  * Poster's permissions are checked before sending.
  *
  * @param int $profile_id Profile ID of the poster
  * @param string $content source message text; links may be shortened
  *                        per current user's preference
  * @param string $source source key ('web', 'api', etc)
  * @param array $options Associative array of optional properties:
  *              string 'created' timestamp of notice; defaults to now
  *              int 'is_local' source/gateway ID, one of:
  *                  Notice::LOCAL_PUBLIC    - Local, ok to appear in public timeline
  *                  Notice::REMOTE_OMB      - Sent from a remote OMB service;
  *                                            hide from public timeline but show in
  *                                            local "and friends" timelines
  *                  Notice::LOCAL_NONPUBLIC - Local, but hide from public timeline
  *                  Notice::GATEWAY         - From another non-OMB service;
  *                                            will not appear in public views
  *              float 'lat' decimal latitude for geolocation
  *              float 'lon' decimal longitude for geolocation
  *              int 'location_id' geoname identifier
  *              int 'location_ns' geoname namespace to interpret location_id
  *              int 'reply_to'; notice ID this is a reply to
  *              int 'repeat_of'; notice ID this is a repeat of
  *              string 'uri' unique ID for notice; defaults to local notice URL
  *              string 'url' permalink to notice; defaults to local notice URL
  *              string 'rendered' rendered HTML version of content
  *              array 'replies' list of profile URIs for reply delivery in
  *                              place of extracting @-replies from content.
  *              array 'groups' list of group IDs to deliver to, in place of
  *                              extracting ! tags from content
  *              array 'tags' list of hashtag strings to save with the notice
  *                           in place of extracting # tags from content
  *              array 'urls' list of attached/referred URLs to save with the
  *                           notice in place of extracting links from content
  * @fixme tag override
  *
  * @return Notice
  * @throws ClientException
  */
 static function saveNew($profile_id, $content, $source, $options = null)
 {
     $defaults = array('uri' => null, 'url' => null, 'reply_to' => null, 'repeat_of' => null);
     if (!empty($options)) {
         $options = $options + $defaults;
         extract($options);
     } else {
         extract($defaults);
     }
     if (!isset($is_local)) {
         $is_local = Notice::LOCAL_PUBLIC;
     }
     $profile = Profile::staticGet($profile_id);
     $final = common_shorten_links($content);
     if (Notice::contentTooLong($final)) {
         // TRANS: Client exception thrown if a notice contains too many characters.
         throw new ClientException(_('Problem saving notice. Too long.'));
     }
     if (empty($profile)) {
         // TRANS: Client exception thrown when trying to save a notice for an unknown user.
         throw new ClientException(_('Problem saving notice. Unknown user.'));
     }
     if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
         common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
         // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame.
         throw new ClientException(_('Too many notices too fast; take a breather ' . 'and post again in a few minutes.'));
     }
     if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
         common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
         // TRANS: Client exception thrown when a user tries to post too many duplicate notices in a given time frame.
         throw new ClientException(_('Too many duplicate messages too quickly;' . ' take a breather and post again in a few minutes.'));
     }
     if (!$profile->hasRight(Right::NEWNOTICE)) {
         common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $profile->nickname);
         // TRANS: Client exception thrown when a user tries to post while being banned.
         throw new ClientException(_('You are banned from posting notices on this site.'), 403);
     }
     $notice = new Notice();
     $notice->profile_id = $profile_id;
     $autosource = common_config('public', 'autosource');
     # Sandboxed are non-false, but not 1, either
     if (!$profile->hasRight(Right::PUBLICNOTICE) || $source && $autosource && in_array($source, $autosource)) {
         $notice->is_local = Notice::LOCAL_NONPUBLIC;
     } else {
         $notice->is_local = $is_local;
     }
     if (!empty($created)) {
         $notice->created = $created;
     } else {
         $notice->created = common_sql_now();
     }
     $notice->content = $final;
     $notice->source = $source;
     $notice->uri = $uri;
     $notice->url = $url;
     // Handle repeat case
     if (isset($repeat_of)) {
         $notice->repeat_of = $repeat_of;
     } else {
         $notice->reply_to = self::getReplyTo($reply_to, $profile_id, $source, $final);
     }
     if (!empty($notice->reply_to)) {
         $reply = Notice::staticGet('id', $notice->reply_to);
         $notice->conversation = $reply->conversation;
     }
     if (!empty($lat) && !empty($lon)) {
         $notice->lat = $lat;
         $notice->lon = $lon;
     }
     if (!empty($location_ns) && !empty($location_id)) {
         $notice->location_id = $location_id;
         $notice->location_ns = $location_ns;
     }
     if (!empty($rendered)) {
         $notice->rendered = $rendered;
     } else {
         $notice->rendered = common_render_content($final, $notice);
     }
     if (Event::handle('StartNoticeSave', array(&$notice))) {
         // XXX: some of these functions write to the DB
         $id = $notice->insert();
         if (!$id) {
             common_log_db_error($notice, 'INSERT', __FILE__);
             // TRANS: Server exception thrown when a notice cannot be saved.
             throw new ServerException(_('Problem saving notice.'));
         }
         // Update ID-dependent columns: URI, conversation
         $orig = clone $notice;
         $changed = false;
         if (empty($uri)) {
             $notice->uri = common_notice_uri($notice);
             $changed = true;
         }
         // If it's not part of a conversation, it's
         // the beginning of a new conversation.
         if (empty($notice->conversation)) {
             $conv = Conversation::create();
             $notice->conversation = $conv->id;
             $changed = true;
         }
         if ($changed) {
             if (!$notice->update($orig)) {
                 common_log_db_error($notice, 'UPDATE', __FILE__);
                 // TRANS: Server exception thrown when a notice cannot be updated.
                 throw new ServerException(_('Problem saving notice.'));
             }
         }
     }
     # Clear the cache for subscribed users, so they'll update at next request
     # XXX: someone clever could prepend instead of clearing the cache
     $notice->blowOnInsert();
     // Save per-notice metadata...
     if (isset($replies)) {
         $notice->saveKnownReplies($replies);
     } else {
         $notice->saveReplies();
     }
     if (isset($tags)) {
         $notice->saveKnownTags($tags);
     } else {
         $notice->saveTags();
     }
     // Note: groups may save tags, so must be run after tags are saved
     // to avoid errors on duplicates.
     if (isset($groups)) {
         $notice->saveKnownGroups($groups);
     } else {
         $notice->saveGroups();
     }
     if (isset($urls)) {
         $notice->saveKnownUrls($urls);
     } else {
         $notice->saveUrls();
     }
     // Prepare inbox delivery, may be queued to background.
     $notice->distribute();
     return $notice;
 }
<?php

require_once "../includes/init.php";
if (!$session->is_logged_in()) {
    redirect_to("login.php");
}
if (isset($_GET['to'])) {
    $to = $database->escape_value($_GET['to']);
    $timestamp = strftime("%Y-%m-%d %H:%M:%S", time());
    $sql = "SELECT * FROM active_chats WHERE (from_id = {$session->user_id}  AND to_id = {$to})";
    $sql .= " OR (from_id = {$to} AND to_id = {$session->user_id} ) LIMIT 1";
    $result_set = $database->query($sql);
    $row = $database->fetch_assoc($result_set);
    if (!empty($row)) {
        $c_id = $row['id'];
        redirect_to("index.php?c_id={$c_id}");
    } else {
        $conversation = new Conversation();
        $conversation->user_id = $conversation->from_id = $session->user_id;
        $conversation->to_id = $to;
        $conversation->timestamp = $timestamp;
        if ($conversation->create()) {
            redirect_to("index.php?msg=message&c_id={$conversation->c_id}");
        } else {
            redirect_to("index.php?msg=message");
        }
    }
}
Exemple #7
0
 public static function createNew($object, $creator, $members, $is_ticket = false, $tech_id = null)
 {
     if (!$is_ticket) {
         $thumbnail = $creator->getAvatar();
     } else {
         $thumbnail = StaffContact::getImageName(User::find_by_id($tech_id));
     }
     $conv = Conversation::create(array('id' => Conversation::generateId(6), 'object' => $object, 'members_ids' => $members, 'thumbnail' => $thumbnail, 'is_ticket' => $is_ticket, 'tech_id' => $tech_id));
     return $conv->id;
 }
Exemple #8
0
 static function saveActivity(Activity $act, Profile $actor, array $options = array())
 {
     // First check if we're going to let this Activity through from the specific actor
     if (!$actor->hasRight(Right::NEWNOTICE)) {
         common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $actor->getNickname());
         // TRANS: Client exception thrown when a user tries to post while being banned.
         throw new ClientException(_m('You are banned from posting notices on this site.'), 403);
     }
     if (common_config('throttle', 'enabled') && !self::checkEditThrottle($actor->id)) {
         common_log(LOG_WARNING, 'Excessive posting by profile #' . $actor->id . '; throttled.');
         // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame.
         throw new ClientException(_m('Too many notices too fast; take a breather ' . 'and post again in a few minutes.'));
     }
     // Get ActivityObject properties
     if (!empty($act->id)) {
         // implied object
         $options['uri'] = $act->id;
         $options['url'] = $act->link;
     } else {
         $actobj = count($act->objects) == 1 ? $act->objects[0] : null;
         if (!is_null($actobj) && !empty($actobj->id)) {
             $options['uri'] = $actobj->id;
             if ($actobj->link) {
                 $options['url'] = $actobj->link;
             } elseif (preg_match('!^https?://!', $actobj->id)) {
                 $options['url'] = $actobj->id;
             }
         }
     }
     $defaults = array('groups' => array(), 'is_local' => self::LOCAL_PUBLIC, 'mentions' => array(), 'reply_to' => null, 'repeat_of' => null, 'scope' => null, 'source' => 'unknown', 'tags' => array(), 'uri' => null, 'url' => null, 'urls' => array(), 'distribute' => true);
     // options will have default values when nothing has been supplied
     $options = array_merge($defaults, $options);
     foreach (array_keys($defaults) as $key) {
         // Only convert the keynames we specify ourselves from 'defaults' array into variables
         ${$key} = $options[$key];
     }
     extract($options, EXTR_SKIP);
     $stored = new Notice();
     if (!empty($uri)) {
         $stored->uri = $uri;
         if ($stored->find()) {
             common_debug('cannot create duplicate Notice URI: ' . $stored->uri);
             throw new Exception('Notice URI already exists');
         }
     }
     $stored->profile_id = $actor->id;
     $stored->source = $source;
     $stored->uri = $uri;
     $stored->url = $url;
     $stored->verb = $act->verb;
     // Use the local user's shortening preferences, if applicable.
     $stored->rendered = $actor->isLocal() ? $actor->shortenLinks($act->content) : $act->content;
     $stored->content = common_strip_html($stored->rendered);
     $autosource = common_config('public', 'autosource');
     // Sandboxed are non-false, but not 1, either
     if (!$actor->hasRight(Right::PUBLICNOTICE) || $source && $autosource && in_array($source, $autosource)) {
         $stored->is_local = Notice::LOCAL_NONPUBLIC;
     }
     // Maybe a missing act-time should be fatal if the actor is not local?
     if (!empty($act->time)) {
         $stored->created = common_sql_date($act->time);
     } else {
         $stored->created = common_sql_now();
     }
     $reply = null;
     if ($act->context instanceof ActivityContext && !empty($act->context->replyToID)) {
         $reply = self::getKV('uri', $act->context->replyToID);
     }
     if (!$reply instanceof Notice && $act->target instanceof ActivityObject) {
         $reply = self::getKV('uri', $act->target->id);
     }
     if ($reply instanceof Notice) {
         if (!$reply->inScope($actor)) {
             // TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
             // TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
             throw new ClientException(sprintf(_m('%1$s has no right to reply to notice %2$d.'), $actor->getNickname(), $reply->id), 403);
         }
         $stored->reply_to = $reply->id;
         $stored->conversation = $reply->conversation;
         // If the original is private to a group, and notice has no group specified,
         // make it to the same group(s)
         if (empty($groups) && $reply->scope & Notice::GROUP_SCOPE) {
             $groups = array();
             $replyGroups = $reply->getGroups();
             foreach ($replyGroups as $group) {
                 if ($actor->isMember($group)) {
                     $groups[] = $group->id;
                 }
             }
         }
         if (is_null($scope)) {
             $scope = $reply->scope;
         }
     }
     if ($act->context instanceof ActivityContext) {
         $location = $act->context->location;
         if ($location) {
             $stored->lat = $location->lat;
             $stored->lon = $location->lon;
             if ($location->location_id) {
                 $stored->location_ns = $location->location_ns;
                 $stored->location_id = $location->location_id;
             }
         }
     } else {
         $act->context = new ActivityContext();
     }
     $stored->scope = self::figureOutScope($actor, $groups, $scope);
     foreach ($act->categories as $cat) {
         if ($cat->term) {
             $term = common_canonical_tag($cat->term);
             if (!empty($term)) {
                 $tags[] = $term;
             }
         }
     }
     foreach ($act->enclosures as $href) {
         // @todo FIXME: Save these locally or....?
         $urls[] = $href;
     }
     if (Event::handle('StartNoticeSave', array(&$stored))) {
         // XXX: some of these functions write to the DB
         try {
             $stored->insert();
             // throws exception on error
             $orig = clone $stored;
             // for updating later in this try clause
             $object = null;
             Event::handle('StoreActivityObject', array($act, $stored, $options, &$object));
             if (empty($object)) {
                 throw new ServerException('Unsuccessful call to StoreActivityObject ' . $stored->uri . ': ' . $act->asString());
             }
             // If it's not part of a conversation, it's
             // the beginning of a new conversation.
             if (empty($stored->conversation)) {
                 // $act->context->conversation will be null if it was not provided
                 $conv = Conversation::create($stored, $act->context->conversation);
                 $stored->conversation = $conv->id;
             }
             $stored->update($orig);
         } catch (Exception $e) {
             if (empty($stored->id)) {
                 common_debug('Failed to save stored object entry in database (' . $e->getMessage() . ')');
             } else {
                 common_debug('Failed to store activity object in database (' . $e->getMessage() . '), deleting notice id ' . $stored->id);
                 $stored->delete();
             }
             throw $e;
         }
     }
     if (!$stored instanceof Notice) {
         throw new ServerException('StartNoticeSave did not give back a Notice');
     }
     // Save per-notice metadata...
     $mentions = array();
     $groups = array();
     // This event lets plugins filter out non-local recipients (attentions we don't care about)
     // Used primarily for OStatus (and if we don't federate, all attentions would be local anyway)
     Event::handle('GetLocalAttentions', array($actor, $act->context->attention, &$mentions, &$groups));
     if (!empty($mentions)) {
         $stored->saveKnownReplies($mentions);
     } else {
         $stored->saveReplies();
     }
     if (!empty($tags)) {
         $stored->saveKnownTags($tags);
     } else {
         $stored->saveTags();
     }
     // Note: groups may save tags, so must be run after tags are saved
     // to avoid errors on duplicates.
     // Note: groups should always be set.
     $stored->saveKnownGroups($groups);
     if (!empty($urls)) {
         $stored->saveKnownUrls($urls);
     } else {
         $stored->saveUrls();
     }
     if ($distribute) {
         // Prepare inbox delivery, may be queued to background.
         $stored->distribute();
     }
     return $stored;
 }
 function saveStatus($status, $flink)
 {
     $profile = $this->ensureProfile($status->user);
     if (empty($profile)) {
         common_log(LOG_ERR, $this->name() . ' - Problem saving notice. No associated Profile.');
         return;
     }
     $statusUri = 'http://twitter.com/' . $status->user->screen_name . '/status/' . $status->id;
     // check to see if we've already imported the status
     $dupe = $this->checkDupe($profile, $statusUri);
     if (!empty($dupe)) {
         common_log(LOG_INFO, $this->name() . " - Ignoring duplicate import: {$statusUri}");
         return;
     }
     $notice = new Notice();
     $notice->profile_id = $profile->id;
     $notice->uri = $statusUri;
     $notice->url = $statusUri;
     $notice->created = strftime('%Y-%m-%d %H:%M:%S', strtotime($status->created_at));
     $notice->source = 'twitter';
     $notice->reply_to = null;
     $notice->is_local = Notice::GATEWAY;
     $notice->content = common_shorten_links($status->text);
     $notice->rendered = common_render_content($notice->content, $notice);
     if (Event::handle('StartNoticeSave', array(&$notice))) {
         $id = $notice->insert();
         if (!$id) {
             common_log_db_error($notice, 'INSERT', __FILE__);
             common_log(LOG_ERR, $this->name() . ' - Problem saving notice.');
         }
         Event::handle('EndNoticeSave', array($notice));
     }
     $orig = clone $notice;
     $conv = Conversation::create();
     $notice->conversation = $conv->id;
     if (!$notice->update($orig)) {
         common_log_db_error($notice, 'UPDATE', __FILE__);
         common_log(LOG_ERR, $this->name() . ' - Problem saving notice.');
     }
     Inbox::insertNotice($flink->user_id, $notice->id);
     $notice->blowOnInsert();
     return $notice;
 }
Exemple #10
0
<?php

include "../../inc/init.inc";
if (isset($_GET['conversation']) && $_GET['conversation'] != 0 && Follower::isFriend($res->user->id, $_GET['conversation'])) {
    $filename = $res->user->id > $_GET['conversation'] ? 'conversations/' . $_GET['conversation'] . "-" . $res->user->id . ".xml" : 'conversations/' . $res->user->id . "-" . $_GET['conversation'] . ".xml";
    if (!Conversation::exists(array('conditions' => array('filename' => $filename)))) {
        $attributes = array("user_id" => $res->user->id, "filename" => $filename);
        $conversation = Conversation::create($attributes);
    } else {
        $conversation = Conversation::find_by_filename($filename);
    }
    if ($conversation == null) {
        throw new Exception('can not init conversation', 504);
    }
    $xmlfile = VAR_PATH . $conversation->filename;
    // <editor-fold defaultstate="collapsed" desc="creation d'un message">
    if (isset($_GET['msg'])) {
        $msg = $_GET['msg'];
        $userChat = isset($_GET['userChat']) ? $_GET['userChat'] : "Invité";
        //Load it using simpleXML
        $doc = new DOMDocument();
        $doc->load($xmlfile);
        //Add a conversation item
        $item = $doc->createElement("item", $msg);
        //Add the sender's name as an attribute
        $sender = $doc->createAttribute("sender");
        $sender->appendChild($doc->createTextNode($userChat));
        //Add another attribute for time on which the message was added
        $time = $doc->createAttribute("time");
        $time->appendChild($doc->createTextNode(date("H:i", time())));
        //Put it together