- /**
- * @param FeedMunger $munger
- * @param boolean $isGroup is this a group record?
- * @return Ostatus_profile
- */
- public static function ensureProfile($munger)
- {
- $profile = $munger->ostatusProfile();
-
- $current = self::staticGet('feeduri', $profile->feeduri);
- if ($current) {
- // @fixme we should probably update info as necessary
- return $current;
- }
-
- $profile->query('BEGIN');
-
- try {
- $local = $munger->profile();
-
- if ($profile->isGroup()) {
- $group = new User_group();
- $group->nickname = $local->nickname . '@remote'; // @fixme
- $group->fullname = $local->fullname;
- $group->homepage = $local->homepage;
- $group->location = $local->location;
- $group->created = $local->created;
- $group->insert();
- if (empty($result)) {
- throw new FeedDBException($group);
- }
- $profile->group_id = $group->id;
- } else {
- $result = $local->insert();
- if (empty($result)) {
- throw new FeedDBException($local);
- }
- $profile->profile_id = $local->id;
- }
-
- $profile->created = common_sql_now();
- $profile->lastupdate = common_sql_now();
- $result = $profile->insert();
- if (empty($result)) {
- throw new FeedDBException($profile);
- }
-
- $profile->query('COMMIT');
- } catch (FeedDBException $e) {
- common_log_db_error($e->obj, 'INSERT', __FILE__);
- $profile->query('ROLLBACK');
- return false;
- }
-
- $avatar = $munger->getAvatar();
- if ($avatar) {
- try {
- $profile->updateAvatar($avatar);
- } catch (Exception $e) {
- common_log(LOG_ERR, "Exception setting OStatus avatar: " .
- $e->getMessage());
- }
- }
-
- return $profile;
- }
-
- /**
- * Download and update given avatar image
- * @param string $url
- * @throws Exception in various failure cases
- */
- public function updateAvatar($url)
- {
- // @fixme this should be better encapsulated
- // ripped from oauthstore.php (for old OMB client)
- $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
- copy($url, $temp_filename);
-
- // @fixme should we be using different ids?
- $imagefile = new ImageFile($this->id, $temp_filename);
- $filename = Avatar::filename($this->id,
- image_type_to_extension($imagefile->type),
- null,
- common_timestamp());
- rename($temp_filename, Avatar::path($filename));
- if ($this->isGroup()) {
- $group = $this->localGroup();
- $group->setOriginal($filename);
- } else {
- $profile = $this->localProfile();
- $profile->setOriginal($filename);
- }
- }
-