3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
23 * Table Definition for profile
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
27 class Profile extends Memcached_DataObject
30 /* the code below is auto generated do not remove the above tag */
32 public $__table = 'profile'; // table name
33 public $id; // int(4) primary_key not_null
34 public $nickname; // varchar(64) multiple_key not_null
35 public $fullname; // varchar(255) multiple_key
36 public $profileurl; // varchar(255)
37 public $homepage; // varchar(255) multiple_key
38 public $bio; // text() multiple_key
39 public $location; // varchar(255) multiple_key
40 public $lat; // decimal(10,7)
41 public $lon; // decimal(10,7)
42 public $location_id; // int(4)
43 public $location_ns; // int(4)
44 public $created; // datetime() not_null
45 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
48 function staticGet($k,$v=NULL) {
49 return Memcached_DataObject::staticGet('Profile',$k,$v);
52 /* the code above is auto generated do not remove the tag below */
57 return User::staticGet('id', $this->id);
60 function getAvatar($width, $height=null)
62 if (is_null($height)) {
65 return Avatar::pkeyGet(array('profile_id' => $this->id,
67 'height' => $height));
70 function getOriginalAvatar()
72 $avatar = DB_DataObject::factory('avatar');
73 $avatar->profile_id = $this->id;
74 $avatar->original = true;
75 if ($avatar->find(true)) {
82 function setOriginal($filename)
84 $imagefile = new ImageFile($this->id, Avatar::path($filename));
86 $avatar = new Avatar();
87 $avatar->profile_id = $this->id;
88 $avatar->width = $imagefile->width;
89 $avatar->height = $imagefile->height;
90 $avatar->mediatype = image_type_to_mime_type($imagefile->type);
91 $avatar->filename = $filename;
92 $avatar->original = true;
93 $avatar->url = Avatar::url($filename);
94 $avatar->created = DB_DataObject_Cast::dateTime(); # current time
96 // XXX: start a transaction here
98 if (!$this->delete_avatars() || !$avatar->insert()) {
99 @unlink(Avatar::path($filename));
103 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
104 // We don't do a scaled one if original is our scaled size
105 if (!($avatar->width == $size && $avatar->height == $size)) {
106 $scaled_filename = $imagefile->resize($size);
108 //$scaled = DB_DataObject::factory('avatar');
109 $scaled = new Avatar();
110 $scaled->profile_id = $this->id;
111 $scaled->width = $size;
112 $scaled->height = $size;
113 $scaled->original = false;
114 $scaled->mediatype = image_type_to_mime_type($imagefile->type);
115 $scaled->filename = $scaled_filename;
116 $scaled->url = Avatar::url($scaled_filename);
117 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
119 if (!$scaled->insert()) {
129 * Delete attached avatars for this user from the database and filesystem.
130 * This should be used instead of a batch delete() to ensure that files
131 * get removed correctly.
133 * @param boolean $original true to delete only the original-size file
136 function delete_avatars($original=true)
138 $avatar = new Avatar();
139 $avatar->profile_id = $this->id;
141 while ($avatar->fetch()) {
142 if ($avatar->original) {
143 if ($original == false) {
153 * Gets either the full name (if filled) or the nickname.
157 function getBestName()
159 return ($this->fullname) ? $this->fullname : $this->nickname;
163 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
164 * if no fullname is provided.
168 function getFancyName()
170 if ($this->fullname) {
171 // TRANS: Full name of a profile or group (%1$s) followed by nickname (%2$s) in parentheses.
172 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
174 return $this->nickname;
179 * Get the most recent notice posted by this user, if any.
181 * @return mixed Notice or null
183 function getCurrentNotice()
185 $notice = $this->getNotices(0, 1);
187 if ($notice->fetch()) {
188 if ($notice instanceof ArrayWrapper) {
189 // hack for things trying to work with single notices
190 return $notice->_items[0];
198 function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
200 $stream = new TaggedProfileNoticeStream($this, $tag);
202 return $stream->getNotices($offset, $limit, $since_id, $max_id);
205 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
207 $stream = new ProfileNoticeStream($this);
209 return $stream->getNotices($offset, $limit, $since_id, $max_id);
212 function isMember($group)
214 $mem = new Group_member();
216 $mem->group_id = $group->id;
217 $mem->profile_id = $this->id;
226 function isAdmin($group)
228 $mem = new Group_member();
230 $mem->group_id = $group->id;
231 $mem->profile_id = $this->id;
241 function isPendingMember($group)
243 $request = Group_join_queue::pkeyGet(array('profile_id' => $this->id,
244 'group_id' => $group->id));
245 return !empty($request);
248 function getGroups($offset=0, $limit=null)
251 'SELECT user_group.* ' .
252 'FROM user_group JOIN group_member '.
253 'ON user_group.id = group_member.group_id ' .
254 'WHERE group_member.profile_id = %d ' .
255 'ORDER BY group_member.created DESC ';
257 if ($offset>0 && !is_null($limit)) {
259 if (common_config('db','type') == 'pgsql') {
260 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
262 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
267 $groups = new User_group();
269 $cnt = $groups->query(sprintf($qry, $this->id));
274 function isTagged($peopletag)
276 $tag = Profile_tag::pkeyGet(array('tagger' => $peopletag->tagger,
277 'tagged' => $this->id,
278 'tag' => $peopletag->tag));
282 function canTag($tagged)
284 if (empty($tagged)) {
288 if ($tagged->id == $this->id) {
292 $all = common_config('peopletag', 'allow_tagging', 'all');
293 $local = common_config('peopletag', 'allow_tagging', 'local');
294 $remote = common_config('peopletag', 'allow_tagging', 'remote');
295 $subs = common_config('peopletag', 'allow_tagging', 'subs');
301 $tagged_user = $tagged->getUser();
302 if (!empty($tagged_user)) {
307 return (Subscription::exists($this, $tagged) ||
308 Subscription::exists($tagged, $this));
309 } else if ($remote) {
315 function getOwnedTags($auth_user, $offset=0, $limit=null, $since_id=0, $max_id=0)
317 $tags = new Profile_list();
318 $tags->tagger = $this->id;
320 if (($auth_user instanceof User || $auth_user instanceof Profile) &&
321 $auth_user->id === $this->id) {
322 // no condition, get both private and public tags
324 $tags->private = false;
327 $tags->selectAdd('id as "cursor"');
330 $tags->whereAdd('id > '.$since_id);
334 $tags->whereAdd('id <= '.$max_id);
337 if($offset>=0 && !is_null($limit)) {
338 $tags->limit($offset, $limit);
341 $tags->orderBy('id DESC');
347 function getOtherTags($auth_user=null, $offset=0, $limit=null, $since_id=0, $max_id=0)
349 $lists = new Profile_list();
351 $tags = new Profile_tag();
352 $tags->tagged = $this->id;
354 $lists->joinAdd($tags);
355 #@fixme: postgres (round(date_part('epoch', my_date)))
356 $lists->selectAdd('unix_timestamp(profile_tag.modified) as "cursor"');
358 if ($auth_user instanceof User || $auth_user instanceof Profile) {
359 $lists->whereAdd('( ( profile_list.private = false ) ' .
360 'OR ( profile_list.tagger = ' . $auth_user->id . ' AND ' .
361 'profile_list.private = true ) )');
363 $lists->private = false;
367 $lists->whereAdd('cursor > '.$since_id);
371 $lists->whereAdd('cursor <= '.$max_id);
374 if($offset>=0 && !is_null($limit)) {
375 $lists->limit($offset, $limit);
378 $lists->orderBy('profile_tag.modified DESC');
384 function getPrivateTags($offset=0, $limit=null, $since_id=0, $max_id=0)
386 $tags = new Profile_list();
387 $tags->private = true;
388 $tags->tagger = $this->id;
391 $tags->whereAdd('id > '.$since_id);
395 $tags->whereAdd('id <= '.$max_id);
398 if($offset>=0 && !is_null($limit)) {
399 $tags->limit($offset, $limit);
402 $tags->orderBy('id DESC');
408 function hasLocalTags()
410 $tags = new Profile_tag();
412 $tags->joinAdd(array('tagger', 'user:id'));
413 $tags->whereAdd('tagged = '.$this->id);
414 $tags->whereAdd('tagger != '.$this->id);
419 return ($tags->N == 0) ? false : true;
422 function getTagSubscriptions($offset=0, $limit=null, $since_id=0, $max_id=0)
424 $lists = new Profile_list();
425 $subs = new Profile_tag_subscription();
427 $lists->joinAdd($subs);
428 #@fixme: postgres (round(date_part('epoch', my_date)))
429 $lists->selectAdd('unix_timestamp(profile_tag_subscription.created) as "cursor"');
431 $lists->whereAdd('profile_tag_subscription.profile_id = '.$this->id);
434 $lists->whereAdd('cursor > '.$since_id);
438 $lists->whereAdd('cursor <= '.$max_id);
441 if($offset>=0 && !is_null($limit)) {
442 $lists->limit($offset, $limit);
445 $lists->orderBy('"cursor" DESC');
452 * Request to join the given group.
453 * May throw exceptions on failure.
455 * @param User_group $group
456 * @return mixed: Group_member on success, Group_join_queue if pending approval, null on some cancels?
458 function joinGroup(User_group $group)
461 if ($group->join_policy == User_group::JOIN_POLICY_MODERATE) {
462 $join = Group_join_queue::saveNew($this, $group);
464 if (Event::handle('StartJoinGroup', array($group, $this))) {
465 $join = Group_member::join($group->id, $this->id);
466 Event::handle('EndJoinGroup', array($group, $this));
470 // Send any applicable notifications...
477 * Leave a group that this profile is a member of.
479 * @param User_group $group
481 function leaveGroup(User_group $group)
483 if (Event::handle('StartLeaveGroup', array($group, $this))) {
484 Group_member::leave($group->id, $this->id);
485 Event::handle('EndLeaveGroup', array($group, $this));
489 function avatarUrl($size=AVATAR_PROFILE_SIZE)
491 $avatar = $this->getAvatar($size);
493 return $avatar->displayUrl();
495 return Avatar::defaultImage($size);
499 function getSubscriptions($offset=0, $limit=null)
501 $subs = Subscription::bySubscriber($this->id,
507 while ($subs->fetch()) {
508 $profile = Profile::staticGet($subs->subscribed);
510 $profiles[] = $profile;
514 return new ArrayWrapper($profiles);
517 function getSubscribers($offset=0, $limit=null)
519 $subs = Subscription::bySubscribed($this->id,
525 while ($subs->fetch()) {
526 $profile = Profile::staticGet($subs->subscriber);
528 $profiles[] = $profile;
532 return new ArrayWrapper($profiles);
535 function getTaggedSubscribers($tag)
538 'SELECT profile.* ' .
539 'FROM profile JOIN (subscription, profile_tag, profile_list) ' .
540 'ON profile.id = subscription.subscriber ' .
541 'AND profile.id = profile_tag.tagged ' .
542 'AND profile_tag.tagger = profile_list.tagger AND profile_tag.tag = profile_list.tag ' .
543 'WHERE subscription.subscribed = %d ' .
544 'AND subscription.subscribed != subscription.subscriber ' .
545 'AND profile_tag.tagger = %d AND profile_tag.tag = "%s" ' .
546 'AND profile_list.private = false ' .
547 'ORDER BY subscription.created DESC';
549 $profile = new Profile();
552 $cnt = $profile->query(sprintf($qry, $this->id, $this->id, $tag));
554 while ($profile->fetch()) {
555 $tagged[] = clone($profile);
561 * Get pending subscribers, who have not yet been approved.
567 function getRequests($offset=0, $limit=null)
570 'SELECT profile.* ' .
571 'FROM profile JOIN subscription_queue '.
572 'ON profile.id = subscription_queue.subscriber ' .
573 'WHERE subscription_queue.subscribed = %d ' .
574 'ORDER BY subscription_queue.created DESC ';
576 if ($limit != null) {
577 if (common_config('db','type') == 'pgsql') {
578 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
580 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
584 $members = new Profile();
586 $members->query(sprintf($qry, $this->id));
590 function subscriptionCount()
592 $c = Cache::instance();
595 $cnt = $c->get(Cache::key('profile:subscription_count:'.$this->id));
596 if (is_integer($cnt)) {
601 $sub = new Subscription();
602 $sub->subscriber = $this->id;
604 $cnt = (int) $sub->count('distinct subscribed');
606 $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
609 $c->set(Cache::key('profile:subscription_count:'.$this->id), $cnt);
615 function subscriberCount()
617 $c = Cache::instance();
619 $cnt = $c->get(Cache::key('profile:subscriber_count:'.$this->id));
620 if (is_integer($cnt)) {
625 $sub = new Subscription();
626 $sub->subscribed = $this->id;
627 $sub->whereAdd('subscriber != subscribed');
628 $cnt = (int) $sub->count('distinct subscriber');
631 $c->set(Cache::key('profile:subscriber_count:'.$this->id), $cnt);
638 * Is this profile subscribed to another profile?
640 * @param Profile $other
643 function isSubscribed($other)
645 return Subscription::exists($this, $other);
649 * Check if a pending subscription request is outstanding for this...
651 * @param Profile $other
654 function hasPendingSubscription($other)
656 return Subscription_queue::exists($this, $other);
660 * Are these two profiles subscribed to each other?
662 * @param Profile $other
665 function mutuallySubscribed($other)
667 return $this->isSubscribed($other) &&
668 $other->isSubscribed($this);
671 function hasFave($notice)
673 $cache = Cache::instance();
675 // XXX: Kind of a hack.
677 if (!empty($cache)) {
678 // This is the stream of favorite notices, in rev chron
679 // order. This forces it into cache.
681 $ids = Fave::idStream($this->id, 0, CachingNoticeStream::CACHE_WINDOW);
683 // If it's in the list, then it's a fave
685 if (in_array($notice->id, $ids)) {
689 // If we're not past the end of the cache window,
690 // then the cache has all available faves, so this one
693 if (count($ids) < CachingNoticeStream::CACHE_WINDOW) {
697 // Otherwise, cache doesn't have all faves;
698 // fall through to the default
701 $fave = Fave::pkeyGet(array('user_id' => $this->id,
702 'notice_id' => $notice->id));
703 return ((is_null($fave)) ? false : true);
708 $c = Cache::instance();
710 $cnt = $c->get(Cache::key('profile:fave_count:'.$this->id));
711 if (is_integer($cnt)) {
717 $faves->user_id = $this->id;
718 $cnt = (int) $faves->count('distinct notice_id');
721 $c->set(Cache::key('profile:fave_count:'.$this->id), $cnt);
727 function noticeCount()
729 $c = Cache::instance();
732 $cnt = $c->get(Cache::key('profile:notice_count:'.$this->id));
733 if (is_integer($cnt)) {
738 $notices = new Notice();
739 $notices->profile_id = $this->id;
740 $cnt = (int) $notices->count('distinct id');
743 $c->set(Cache::key('profile:notice_count:'.$this->id), $cnt);
749 function blowFavesCache()
751 $cache = Cache::instance();
753 // Faves don't happen chronologically, so we need to blow
755 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id));
756 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id.';last'));
757 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id));
758 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id.';last'));
760 $this->blowFaveCount();
763 function blowSubscriberCount()
765 $c = Cache::instance();
767 $c->delete(Cache::key('profile:subscriber_count:'.$this->id));
771 function blowSubscriptionCount()
773 $c = Cache::instance();
775 $c->delete(Cache::key('profile:subscription_count:'.$this->id));
779 function blowFaveCount()
781 $c = Cache::instance();
783 $c->delete(Cache::key('profile:fave_count:'.$this->id));
787 function blowNoticeCount()
789 $c = Cache::instance();
791 $c->delete(Cache::key('profile:notice_count:'.$this->id));
795 static function maxBio()
797 $biolimit = common_config('profile', 'biolimit');
798 // null => use global limit (distinct from 0!)
799 if (is_null($biolimit)) {
800 $biolimit = common_config('site', 'textlimit');
805 static function bioTooLong($bio)
807 $biolimit = self::maxBio();
808 return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
813 $this->_deleteNotices();
814 $this->_deleteSubscriptions();
815 $this->_deleteMessages();
816 $this->_deleteTags();
817 $this->_deleteBlocks();
818 $this->delete_avatars();
820 // Warning: delete() will run on the batch objects,
821 // not on individual objects.
822 $related = array('Reply',
825 Event::handle('ProfileDeleteRelated', array($this, &$related));
827 foreach ($related as $cls) {
829 $inst->profile_id = $this->id;
836 function _deleteNotices()
838 $notice = new Notice();
839 $notice->profile_id = $this->id;
841 if ($notice->find()) {
842 while ($notice->fetch()) {
843 $other = clone($notice);
849 function _deleteSubscriptions()
851 $sub = new Subscription();
852 $sub->subscriber = $this->id;
856 while ($sub->fetch()) {
857 $other = Profile::staticGet('id', $sub->subscribed);
861 if ($other->id == $this->id) {
864 Subscription::cancel($this, $other);
867 $subd = new Subscription();
868 $subd->subscribed = $this->id;
871 while ($subd->fetch()) {
872 $other = Profile::staticGet('id', $subd->subscriber);
876 if ($other->id == $this->id) {
879 Subscription::cancel($other, $this);
882 $self = new Subscription();
884 $self->subscriber = $this->id;
885 $self->subscribed = $this->id;
890 function _deleteMessages()
892 $msg = new Message();
893 $msg->from_profile = $this->id;
896 $msg = new Message();
897 $msg->to_profile = $this->id;
901 function _deleteTags()
903 $tag = new Profile_tag();
904 $tag->tagged = $this->id;
908 function _deleteBlocks()
910 $block = new Profile_block();
911 $block->blocked = $this->id;
914 $block = new Group_block();
915 $block->blocked = $this->id;
919 // XXX: identical to Notice::getLocation.
921 function getLocation()
925 if (!empty($this->location_id) && !empty($this->location_ns)) {
926 $location = Location::fromId($this->location_id, $this->location_ns);
929 if (is_null($location)) { // no ID, or Location::fromId() failed
930 if (!empty($this->lat) && !empty($this->lon)) {
931 $location = Location::fromLatLon($this->lat, $this->lon);
935 if (is_null($location)) { // still haven't found it!
936 if (!empty($this->location)) {
937 $location = Location::fromName($this->location);
944 function hasRole($name)
947 if (Event::handle('StartHasRole', array($this, $name, &$has_role))) {
948 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
950 $has_role = !empty($role);
951 Event::handle('EndHasRole', array($this, $name, $has_role));
956 function grantRole($name)
958 if (Event::handle('StartGrantRole', array($this, $name))) {
960 $role = new Profile_role();
962 $role->profile_id = $this->id;
964 $role->created = common_sql_now();
966 $result = $role->insert();
969 throw new Exception("Can't save role '$name' for profile '{$this->id}'");
972 if ($name == 'owner') {
973 User::blow('user:site_owner');
976 Event::handle('EndGrantRole', array($this, $name));
982 function revokeRole($name)
984 if (Event::handle('StartRevokeRole', array($this, $name))) {
986 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
990 // TRANS: Exception thrown when trying to revoke an existing role for a user that does not exist.
991 // TRANS: %1$s is the role name, %2$s is the user ID (number).
992 throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; does not exist.'),$name, $this->id));
995 $result = $role->delete();
998 common_log_db_error($role, 'DELETE', __FILE__);
999 // TRANS: Exception thrown when trying to revoke a role for a user with a failing database query.
1000 // TRANS: %1$s is the role name, %2$s is the user ID (number).
1001 throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; database error.'),$name, $this->id));
1004 if ($name == 'owner') {
1005 User::blow('user:site_owner');
1008 Event::handle('EndRevokeRole', array($this, $name));
1014 function isSandboxed()
1016 return $this->hasRole(Profile_role::SANDBOXED);
1019 function isSilenced()
1021 return $this->hasRole(Profile_role::SILENCED);
1026 $this->grantRole(Profile_role::SANDBOXED);
1029 function unsandbox()
1031 $this->revokeRole(Profile_role::SANDBOXED);
1036 $this->grantRole(Profile_role::SILENCED);
1039 function unsilence()
1041 $this->revokeRole(Profile_role::SILENCED);
1045 * Does this user have the right to do X?
1047 * With our role-based authorization, this is merely a lookup for whether the user
1048 * has a particular role. The implementation currently uses a switch statement
1049 * to determine if the user has the pre-defined role to exercise the right. Future
1050 * implementations may allow per-site roles, and different mappings of roles to rights.
1052 * @param $right string Name of the right, usually a constant in class Right
1053 * @return boolean whether the user has the right in question
1055 function hasRight($right)
1059 if ($this->hasRole(Profile_role::DELETED)) {
1063 if (Event::handle('UserRightsCheck', array($this, $right, &$result))) {
1066 case Right::DELETEOTHERSNOTICE:
1067 case Right::MAKEGROUPADMIN:
1068 case Right::SANDBOXUSER:
1069 case Right::SILENCEUSER:
1070 case Right::DELETEUSER:
1071 case Right::DELETEGROUP:
1072 $result = $this->hasRole(Profile_role::MODERATOR);
1074 case Right::CONFIGURESITE:
1075 $result = $this->hasRole(Profile_role::ADMINISTRATOR);
1077 case Right::GRANTROLE:
1078 case Right::REVOKEROLE:
1079 $result = $this->hasRole(Profile_role::OWNER);
1081 case Right::NEWNOTICE:
1082 case Right::NEWMESSAGE:
1083 case Right::SUBSCRIBE:
1084 case Right::CREATEGROUP:
1085 $result = !$this->isSilenced();
1087 case Right::PUBLICNOTICE:
1088 case Right::EMAILONREPLY:
1089 case Right::EMAILONSUBSCRIBE:
1090 case Right::EMAILONFAVE:
1091 $result = !$this->isSandboxed();
1093 case Right::WEBLOGIN:
1094 $result = !$this->isSilenced();
1097 $result = !$this->isSilenced();
1099 case Right::BACKUPACCOUNT:
1100 $result = common_config('profile', 'backup');
1102 case Right::RESTOREACCOUNT:
1103 $result = common_config('profile', 'restore');
1105 case Right::DELETEACCOUNT:
1106 $result = common_config('profile', 'delete');
1108 case Right::MOVEACCOUNT:
1109 $result = common_config('profile', 'move');
1119 function hasRepeated($notice_id)
1121 // XXX: not really a pkey, but should work
1123 $notice = Memcached_DataObject::pkeyGet('Notice',
1124 array('profile_id' => $this->id,
1125 'repeat_of' => $notice_id));
1127 return !empty($notice);
1131 * Returns an XML string fragment with limited profile information
1132 * as an Atom <author> element.
1134 * Assumes that Atom has been previously set up as the base namespace.
1136 * @param Profile $cur the current authenticated user
1140 function asAtomAuthor($cur = null)
1142 $xs = new XMLStringer(true);
1144 $xs->elementStart('author');
1145 $xs->element('name', null, $this->nickname);
1146 $xs->element('uri', null, $this->getUri());
1149 $attrs['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1150 $attrs['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1151 $xs->element('statusnet:profile_info', $attrs, null);
1153 $xs->elementEnd('author');
1155 return $xs->getString();
1159 * Extra profile info for atom entries
1161 * Clients use some extra profile info in the atom stream.
1162 * This gives it to them.
1164 * @param User $cur Current user
1166 * @return array representation of <statusnet:profile_info> element or null
1169 function profileInfo($cur)
1171 $profileInfoAttr = array('local_id' => $this->id);
1174 // Whether the current user is a subscribed to this profile
1175 $profileInfoAttr['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1176 // Whether the current user is has blocked this profile
1177 $profileInfoAttr['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1180 return array('statusnet:profile_info', $profileInfoAttr, null);
1184 * Returns an XML string fragment with profile information as an
1185 * Activity Streams <activity:actor> element.
1187 * Assumes that 'activity' namespace has been previously defined.
1191 function asActivityActor()
1193 return $this->asActivityNoun('actor');
1197 * Returns an XML string fragment with profile information as an
1198 * Activity Streams noun object with the given element type.
1200 * Assumes that 'activity', 'georss', and 'poco' namespace has been
1201 * previously defined.
1203 * @param string $element one of 'actor', 'subject', 'object', 'target'
1207 function asActivityNoun($element)
1209 $noun = ActivityObject::fromProfile($this);
1210 return $noun->asString('activity:' . $element);
1214 * Returns the best URI for a profile. Plugins may override.
1216 * @return string $uri
1222 // give plugins a chance to set the URI
1223 if (Event::handle('StartGetProfileUri', array($this, &$uri))) {
1225 // check for a local user first
1226 $user = User::staticGet('id', $this->id);
1228 if (!empty($user)) {
1231 // return OMB profile if any
1232 $remote = Remote_profile::staticGet('id', $this->id);
1233 if (!empty($remote)) {
1234 $uri = $remote->uri;
1237 Event::handle('EndGetProfileUri', array($this, &$uri));
1243 function hasBlocked($other)
1245 $block = Profile_block::get($this->id, $other->id);
1247 if (empty($block)) {
1256 function getAtomFeed()
1260 if (Event::handle('StartProfileGetAtomFeed', array($this, &$feed))) {
1261 $user = User::staticGet('id', $this->id);
1262 if (!empty($user)) {
1263 $feed = common_local_url('ApiTimelineUser', array('id' => $user->id,
1264 'format' => 'atom'));
1266 Event::handle('EndProfileGetAtomFeed', array($this, $feed));
1272 static function fromURI($uri)
1276 if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) {
1277 // Get a local user or remote (OMB 0.1) profile
1278 $user = User::staticGet('uri', $uri);
1279 if (!empty($user)) {
1280 $profile = $user->getProfile();
1282 $remote_profile = Remote_profile::staticGet('uri', $uri);
1283 if (!empty($remote_profile)) {
1284 $profile = Profile::staticGet('id', $remote_profile->profile_id);
1287 Event::handle('EndGetProfileFromURI', array($uri, $profile));
1293 function canRead(Notice $notice)
1295 if ($notice->scope & Notice::SITE_SCOPE) {
1296 $user = $this->getUser();
1302 if ($notice->scope & Notice::ADDRESSEE_SCOPE) {
1303 $replies = $notice->getReplies();
1305 if (!in_array($this->id, $replies)) {
1306 $groups = $notice->getGroups();
1310 foreach ($groups as $group) {
1311 if ($this->isMember($group)) {
1323 if ($notice->scope & Notice::FOLLOWER_SCOPE) {
1324 $author = $notice->getProfile();
1325 if (!Subscription::exists($this, $author)) {