summaryrefslogtreecommitdiff
path: root/plugins/OStatus/classes
diff options
context:
space:
mode:
authorBrion Vibber <brion@pobox.com>2010-02-12 01:11:46 +0000
committerBrion Vibber <brion@pobox.com>2010-02-12 01:11:46 +0000
commitbc46621af2bea8d2f9f132f275c70c5964f880b4 (patch)
treeebba5ae7894699986afb81e00b300e9427753de0 /plugins/OStatus/classes
parent525358fa101784fa5bbbac8b214091de89ec0634 (diff)
OStatus sub setup code cleanup and partial group fixes (needs more work after the Atom updates are done)
Diffstat (limited to 'plugins/OStatus/classes')
-rw-r--r--plugins/OStatus/classes/Ostatus_profile.php158
1 files changed, 118 insertions, 40 deletions
diff --git a/plugins/OStatus/classes/Ostatus_profile.php b/plugins/OStatus/classes/Ostatus_profile.php
index 748ecce18..f7bbcd028 100644
--- a/plugins/OStatus/classes/Ostatus_profile.php
+++ b/plugins/OStatus/classes/Ostatus_profile.php
@@ -182,9 +182,24 @@ class Ostatus_profile extends Memcached_DataObject
* Fetch the StatusNet-side profile for this feed
* @return Profile
*/
- public function getLocalProfile()
+ public function localProfile()
{
- return Profile::staticGet('id', $this->profile_id);
+ if ($this->profile_id) {
+ return Profile::staticGet('id', $this->profile_id);
+ }
+ return null;
+ }
+
+ /**
+ * Fetch the StatusNet-side profile for this feed
+ * @return Profile
+ */
+ public function localGroup()
+ {
+ if ($this->group_id) {
+ return User_group::staticGet('id', $this->group_id);
+ }
+ return null;
}
/**
@@ -194,62 +209,48 @@ class Ostatus_profile extends Memcached_DataObject
*/
public static function ensureProfile($munger)
{
- $entity = $munger->ostatusProfile();
+ $profile = $munger->ostatusProfile();
- $current = self::staticGet('feeduri', $entity->feeduri);
+ $current = self::staticGet('feeduri', $profile->feeduri);
if ($current) {
// @fixme we should probably update info as necessary
return $current;
}
- $entity->query('BEGIN');
+ $profile->query('BEGIN');
// Awful hack! Awful hack!
- $entity->verify = common_good_rand(16);
- $entity->secret = common_good_rand(32);
+ $profile->verify = common_good_rand(16);
+ $profile->secret = common_good_rand(32);
try {
- $profile = $munger->profile();
- $result = $profile->insert();
- if (empty($result)) {
- throw new FeedDBException($profile);
- }
-
- $avatar = $munger->getAvatar();
- if ($avatar) {
- // @fixme this should be better encapsulated
- // ripped from oauthstore.php (for old OMB client)
- $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
- copy($avatar, $temp_filename);
- $imagefile = new ImageFile($profile->id, $temp_filename);
- $filename = Avatar::filename($profile->id,
- image_type_to_extension($imagefile->type),
- null,
- common_timestamp());
- rename($temp_filename, Avatar::path($filename));
- $profile->setOriginal($filename);
- }
+ $local = $munger->profile();
- $entity->profile_id = $profile->id;
if ($entity->isGroup()) {
$group = new User_group();
- $group->nickname = $profile->nickname . '@remote'; // @fixme
- $group->fullname = $profile->fullname;
- $group->homepage = $profile->homepage;
- $group->location = $profile->location;
- $group->created = $profile->created;
+ $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 ($avatar) {
- $group->setOriginal($filename);
+ if (empty($result)) {
+ throw new FeedDBException($group);
}
-
- $entity->group_id = $group->id;
+ $profile->group_id = $group->id;
+ } else {
+ $result = $local->insert();
+ if (empty($result)) {
+ throw new FeedDBException($local);
+ }
+ $profile->profile_id = $local->id;
}
- $result = $entity->insert();
+ $profile->created = sql_common_date();
+ $profile->lastupdate = sql_common_date();
+ $result = $profile->insert();
if (empty($result)) {
- throw new FeedDBException($entity);
+ throw new FeedDBException($profile);
}
$entity->query('COMMIT');
@@ -258,10 +259,47 @@ class Ostatus_profile extends Memcached_DataObject
$entity->query('ROLLBACK');
return false;
}
+
+ $avatar = $munger->getAvatar();
+ if ($avatar) {
+ try {
+ $this->updateAvatar($avatar);
+ } catch (Exception $e) {
+ common_log(LOG_ERR, "Exception setting OStatus avatar: " .
+ $e->getMessage());
+ }
+ }
+
return $entity;
}
/**
+ * 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);
+ $imagefile = new ImageFile($profile->id, $temp_filename);
+ $filename = Avatar::filename($profile->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);
+ }
+ }
+
+ /**
* Damn dirty hack!
*/
function isGroup()
@@ -319,6 +357,46 @@ class Ostatus_profile extends Memcached_DataObject
}
/**
+ * Save PuSH subscription confirmation.
+ * Sets approximate lease start and end times and finalizes state.
+ *
+ * @param int $lease_seconds provided hub.lease_seconds parameter, if given
+ */
+ public function confirmSubscribe($lease_seconds=0)
+ {
+ $original = clone($this);
+
+ $this->sub_state = 'active';
+ $this->sub_start = common_sql_date(time());
+ if ($lease_seconds > 0) {
+ $this->sub_end = common_sql_date(time() + $lease_seconds);
+ } else {
+ $this->sub_end = null;
+ }
+ $this->lastupdate = common_sql_date();
+
+ return $this->update($original);
+ }
+
+ /**
+ * Save PuSH unsubscription confirmation.
+ * Wipes active PuSH sub info and resets state.
+ */
+ public function confirmUnsubscribe()
+ {
+ $original = clone($this);
+
+ $this->verify_token = null;
+ $this->secret = null;
+ $this->sub_state = null;
+ $this->sub_start = null;
+ $this->sub_end = null;
+ $this->lastupdate = common_sql_date();
+
+ return $this->update($original);
+ }
+
+ /**
* Send an unsubscription request to the hub for this feed.
* The hub will later send us a confirmation POST to /main/push/callback.
*