From 8d4e617d4e36fdcee7c2536085e5d2b13449e14a Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 28 Dec 2009 13:53:28 -0800 Subject: [PATCH 1/9] add table user_location_prefs --- db/statusnet.sql | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/db/statusnet.sql b/db/statusnet.sql index 6b3c2ca068..94b03df639 100644 --- a/db/statusnet.sql +++ b/db/statusnet.sql @@ -587,3 +587,12 @@ create table login_token ( constraint primary key (user_id) ) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; +create table user_location_prefs ( + user_id integer not null comment 'user who has the preference' references user (id), + share_location tinyint default 1 comment 'Whether to share location data', + created datetime not null comment 'date this record was created', + modified timestamp comment 'date this record was modified', + + constraint primary key (user_id) +) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; + From b141f7aea5be3ad66d51d0ec958b2c342bcf489f Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 28 Dec 2009 13:54:09 -0800 Subject: [PATCH 2/9] Add user_location_prefs to upgrade script --- db/08to09.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/db/08to09.sql b/db/08to09.sql index 28ec3ec16b..d9c25bc723 100644 --- a/db/08to09.sql +++ b/db/08to09.sql @@ -84,3 +84,13 @@ create table login_token ( constraint primary key (user_id) ) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; + +create table user_location_prefs ( + user_id integer not null comment 'user who has the preference' references user (id), + share_location tinyint default 1 comment 'Whether to share location data', + created datetime not null comment 'date this record was created', + modified timestamp comment 'date this record was modified', + + constraint primary key (user_id) +) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; + From c32682413530e5f2f4a85bd9397c533ba17f7e86 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 28 Dec 2009 13:59:03 -0800 Subject: [PATCH 3/9] add user-location-prefs data objects --- classes/User_location_prefs.php | 48 +++++++++++++++++++++++++++++++++ classes/statusnet.ini | 12 +++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100755 classes/User_location_prefs.php diff --git a/classes/User_location_prefs.php b/classes/User_location_prefs.php new file mode 100755 index 0000000000..52cb254bad --- /dev/null +++ b/classes/User_location_prefs.php @@ -0,0 +1,48 @@ +. + * + * @category Data + * @package StatusNet + * @author Evan Prodromou + * @copyright 2009 StatusNet Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; + +class User_location_prefs extends Memcached_DataObject +{ + ###START_AUTOCODE + /* the code below is auto generated do not remove the above tag */ + + public $__table = 'user_location_prefs'; // table name + public $user_id; // int(4) primary_key not_null + public $share_location; // tinyint(1) default_1 + public $created; // datetime not_null default_0000-00-00%2000%3A00%3A00 + public $modified; // timestamp not_null default_CURRENT_TIMESTAMP + + /* Static get */ + function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User_location_prefs',$k,$v); } + + /* the code above is auto generated do not remove the tag below */ + ###END_AUTOCODE +} diff --git a/classes/statusnet.ini b/classes/statusnet.ini index 2cc37dbfef..ac69403a44 100644 --- a/classes/statusnet.ini +++ b/classes/statusnet.ini @@ -1,4 +1,3 @@ - [avatar] profile_id = 129 original = 17 @@ -564,4 +563,13 @@ modified = 384 [user_openid__keys] trustroot = K -user_id = K \ No newline at end of file +user_id = K + +[user_location_prefs] +user_id = 129 +share_location = 17 +created = 142 +modified = 384 + +[user_location_prefs__keys] +user_id = K From bbf516b96594770c5dc0dea7f9b15937d7c9a237 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 28 Dec 2009 13:59:31 -0800 Subject: [PATCH 4/9] turn off exe bits --- classes/User_location_prefs.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 classes/User_location_prefs.php diff --git a/classes/User_location_prefs.php b/classes/User_location_prefs.php old mode 100755 new mode 100644 From bb93d6b1c7c697891baca7082261ee694727f161 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 28 Dec 2009 14:21:07 -0800 Subject: [PATCH 5/9] remove namespace setting from location; it's unused --- actions/newnotice.php | 43 +++++++++++++++++++++---------------- actions/profilesettings.php | 5 +++++ classes/User.php | 16 ++++++++++++++ lib/default.php | 2 +- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/actions/newnotice.php b/actions/newnotice.php index c014f1781c..8591522dc5 100644 --- a/actions/newnotice.php +++ b/actions/newnotice.php @@ -164,19 +164,6 @@ class NewnoticeAction extends Action $replyto = 'false'; } - $lat = $this->trimmed('lat'); - $lon = $this->trimmed('lon'); - $location_id = $this->trimmed('location_id'); - $location_ns = $this->trimmed('location_ns'); - - if (!empty($lat) && !empty($lon) && empty($location_id)) { - $location = Location::fromLatLon($lat, $lon); - if (!empty($location)) { - $location_id = $location->location_id; - $location_ns = $location->location_ns; - } - } - $upload = null; $upload = MediaFile::fromUpload('attach'); @@ -195,12 +182,30 @@ class NewnoticeAction extends Action } } - $notice = Notice::saveNew($user->id, $content_shortened, 'web', - array('reply_to' => ($replyto == 'false') ? null : $replyto, - 'lat' => $lat, - 'lon' => $lon, - 'location_id' => $location_id, - 'location_ns' => $location_ns)); + $options = array('reply_to' => ($replyto == 'false') ? null : $replyto); + + if ($user->shareLocation()) { + + $lat = $this->trimmed('lat'); + $lon = $this->trimmed('lon'); + $location_id = $this->trimmed('location_id'); + $location_ns = $this->trimmed('location_ns'); + + if (!empty($lat) && !empty($lon) && empty($location_id)) { + $location = Location::fromLatLon($lat, $lon); + if (!empty($location)) { + $location_id = $location->location_id; + $location_ns = $location->location_ns; + } + } + + $options['lat'] = $lat; + $options['lon'] = $lon; + $options['location_id'] = $location_id; + $options['location_ns'] = $location_ns; + } + + $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options); if (isset($upload)) { $upload->attachToNotice($notice); diff --git a/actions/profilesettings.php b/actions/profilesettings.php index 359664096e..58bf838d76 100644 --- a/actions/profilesettings.php +++ b/actions/profilesettings.php @@ -133,6 +133,11 @@ class ProfilesettingsAction extends AccountSettingsAction ($this->arg('location')) ? $this->arg('location') : $profile->location, _('Where you are, like "City, State (or Region), Country"')); $this->elementEnd('li'); + $this->elementStart('li'); + $this->checkbox('sharelocation', _('Share my current location when posting notices'), + ($this->arg('sharelocation')) ? + $this->arg('sharelocation') : $user->shareLocation()); + $this->elementEnd('li'); Event::handle('EndProfileFormData', array($this)); $this->elementStart('li'); $this->input('tags', _('Tags'), diff --git a/classes/User.php b/classes/User.php index 6708d95b6a..2bcb7c2a0a 100644 --- a/classes/User.php +++ b/classes/User.php @@ -996,4 +996,20 @@ class User extends Memcached_DataObject return $ids; } + + function shareLocation() + { + $share = true; + + $prefs = User_location_prefs::staticGet('user_id', $this->id); + + if (empty($prefs)) { + $share = common_config('location', 'share'); + } else { + $share = $prefs->share_location; + $prefs->free(); + } + + return $share; + } } diff --git a/lib/default.php b/lib/default.php index 42d4623b1e..b5eda7b2cf 100644 --- a/lib/default.php +++ b/lib/default.php @@ -226,7 +226,7 @@ $default = 'message' => array('contentlimit' => null), 'location' => - array('namespace' => 1), // 1 = geonames, 2 = Yahoo Where on Earth + array(), 'omb' => array('timeout' => 5), // HTTP request timeout in seconds when contacting remote hosts for OMB updates 'logincommand' => From 29a1669c01135faf125b2e53718a4112f14525fd Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 28 Dec 2009 14:43:14 -0800 Subject: [PATCH 6/9] user_id is primary key for user_location_prefs --- classes/statusnet.ini | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/classes/statusnet.ini b/classes/statusnet.ini index ac69403a44..35a000aacd 100644 --- a/classes/statusnet.ini +++ b/classes/statusnet.ini @@ -572,4 +572,5 @@ created = 142 modified = 384 [user_location_prefs__keys] -user_id = K +user_id = N + From 39bdda9c7e7ee7571298c84c406a9dd74805f050 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 28 Dec 2009 14:43:34 -0800 Subject: [PATCH 7/9] More configuration options for location sharing --- classes/User.php | 26 +++++++++++++++++--------- lib/default.php | 3 ++- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/classes/User.php b/classes/User.php index 2bcb7c2a0a..34151778c5 100644 --- a/classes/User.php +++ b/classes/User.php @@ -999,17 +999,25 @@ class User extends Memcached_DataObject function shareLocation() { - $share = true; + $cfg = common_config('location', 'share'); - $prefs = User_location_prefs::staticGet('user_id', $this->id); + if ($cfg == 'always') { + return true; + } else if ($cfg == 'never') { + return false; + } else { // user + $share = true; - if (empty($prefs)) { - $share = common_config('location', 'share'); - } else { - $share = $prefs->share_location; - $prefs->free(); + $prefs = User_location_prefs::staticGet('user_id', $this->id); + + if (empty($prefs)) { + $share = common_config('location', 'sharedefault'); + } else { + $share = $prefs->share_location; + $prefs->free(); + } + + return $share; } - - return $share; } } diff --git a/lib/default.php b/lib/default.php index b5eda7b2cf..8a70ed3fa1 100644 --- a/lib/default.php +++ b/lib/default.php @@ -226,7 +226,8 @@ $default = 'message' => array('contentlimit' => null), 'location' => - array(), + array('share' => 'user', // whether to share location; 'always', 'user', 'never' + 'sharedefault' => true), 'omb' => array('timeout' => 5), // HTTP request timeout in seconds when contacting remote hosts for OMB updates 'logincommand' => From e009f613d39d81e98d1438dbc182515b332a5ece Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 28 Dec 2009 14:44:05 -0800 Subject: [PATCH 8/9] let users set location prefs from profile form --- actions/profilesettings.php | 43 ++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/actions/profilesettings.php b/actions/profilesettings.php index 58bf838d76..7db86a6c81 100644 --- a/actions/profilesettings.php +++ b/actions/profilesettings.php @@ -133,11 +133,13 @@ class ProfilesettingsAction extends AccountSettingsAction ($this->arg('location')) ? $this->arg('location') : $profile->location, _('Where you are, like "City, State (or Region), Country"')); $this->elementEnd('li'); - $this->elementStart('li'); - $this->checkbox('sharelocation', _('Share my current location when posting notices'), - ($this->arg('sharelocation')) ? - $this->arg('sharelocation') : $user->shareLocation()); - $this->elementEnd('li'); + if (common_config('location', 'share') == 'user') { + $this->elementStart('li'); + $this->checkbox('sharelocation', _('Share my current location when posting notices'), + ($this->arg('sharelocation')) ? + $this->arg('sharelocation') : $user->shareLocation()); + $this->elementEnd('li'); + } Event::handle('EndProfileFormData', array($this)); $this->elementStart('li'); $this->input('tags', _('Tags'), @@ -323,6 +325,37 @@ class ProfilesettingsAction extends AccountSettingsAction $profile->profileurl = common_profile_url($nickname); + if (common_config('location', 'share') == 'user') { + + $exists = false; + + $prefs = User_location_prefs::staticGet('user_id', $user->id); + + if (empty($prefs)) { + $prefs = new User_location_prefs(); + + $prefs->user_id = $user->id; + $prefs->created = common_sql_now(); + } else { + $exists = true; + $orig = clone($prefs); + } + + $prefs->share_location = $this->boolean('sharelocation'); + + if ($exists) { + $result = $prefs->update($orig); + } else { + $result = $prefs->insert(); + } + + if ($result === false) { + common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__); + $this->serverError(_('Couldn\'t save location prefs.')); + return; + } + } + common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__); common_debug('New profile: ' . common_log_objstring($profile), __FILE__); From ca6669538a16f36f92df918d679671b95b1859ac Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 28 Dec 2009 15:13:15 -0800 Subject: [PATCH 9/9] Move location-argument-handling code into a single function Moved the important parts of the location-argument-handling stuff to a single function. Handles defaults and overrides correctly, and easy to use. Changed Web and API channels to use it. --- actions/apistatusesupdate.php | 20 ++++++------ actions/newnotice.php | 22 ++++--------- classes/Notice.php | 59 +++++++++++++++++++++++++++-------- 3 files changed, 61 insertions(+), 40 deletions(-) diff --git a/actions/apistatusesupdate.php b/actions/apistatusesupdate.php index dabbea92f5..f594bbf393 100644 --- a/actions/apistatusesupdate.php +++ b/actions/apistatusesupdate.php @@ -203,12 +203,6 @@ class ApiStatusesUpdateAction extends ApiAuthAction } } - $location = null; - - if (!empty($this->lat) && !empty($this->lon)) { - $location = Location::fromLatLon($this->lat, $this->lon); - } - $upload = null; try { @@ -235,11 +229,15 @@ class ApiStatusesUpdateAction extends ApiAuthAction $options = array('reply_to' => $reply_to); - if (!empty($location)) { - $options['lat'] = $location->lat; - $options['lon'] = $location->lon; - $options['location_id'] = $location->location_id; - $options['location_ns'] = $location->location_ns; + if ($this->user->shareLocation()) { + + $locOptions = Notice::locationOptions($this->lat, + $this->lon, + null, + null, + $this->user->getProfile()); + + $options = array_merge($options, $locOptions); } $this->notice = diff --git a/actions/newnotice.php b/actions/newnotice.php index 8591522dc5..2d9f0ff797 100644 --- a/actions/newnotice.php +++ b/actions/newnotice.php @@ -186,23 +186,13 @@ class NewnoticeAction extends Action if ($user->shareLocation()) { - $lat = $this->trimmed('lat'); - $lon = $this->trimmed('lon'); - $location_id = $this->trimmed('location_id'); - $location_ns = $this->trimmed('location_ns'); + $locOptions = Notice::locationOptions($this->trimmed('lat'), + $this->trimmed('lon'), + $this->trimmed('location_id'), + $this->trimmed('location_ns'), + $user->getProfile()); - if (!empty($lat) && !empty($lon) && empty($location_id)) { - $location = Location::fromLatLon($lat, $lon); - if (!empty($location)) { - $location_id = $location->location_id; - $location_ns = $location->location_ns; - } - } - - $options['lat'] = $lat; - $options['lon'] = $lon; - $options['location_id'] = $location_id; - $options['location_ns'] = $location_ns; + $options = array_merge($options, $locOptions); } $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options); diff --git a/classes/Notice.php b/classes/Notice.php index 7651d8bd50..9f68c52553 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -289,21 +289,11 @@ class Notice extends Memcached_DataObject 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; - } else if (!empty($location_ns) && !empty($location_id)) { - $location = Location::fromId($location_id, $location_ns); - if (!empty($location)) { - $notice->lat = $location->lat; - $notice->lon = $location->lon; - $notice->location_id = $location_id; - $notice->location_ns = $location_ns; - } - } else { - $notice->lat = $profile->lat; - $notice->lon = $profile->lon; - $notice->location_id = $profile->location_id; - $notice->location_ns = $profile->location_ns; } if (Event::handle('StartNoticeSave', array(&$notice))) { @@ -1429,4 +1419,47 @@ class Notice extends Memcached_DataObject return $ids; } + + function locationOptions($lat, $lon, $location_id, $location_ns, $profile = null) + { + $options = array(); + + if (!empty($location_id) && !empty($location_ns)) { + + $options['location_id'] = $location_id; + $options['location_ns'] = $location_ns; + + $location = Location::fromId($location_id, $location_ns); + + if (!empty($location)) { + $options['lat'] = $location->lat; + $options['lon'] = $location->lon; + } + + } else if (!empty($lat) && !empty($lon)) { + + $options['lat'] = $lat; + $options['lon'] = $lon; + + $location = Location::fromLatLon($lat, $lon); + + if (!empty($location)) { + $options['location_id'] = $location->location_id; + $options['location_ns'] = $location->location_ns; + } + } else if (!empty($profile)) { + + if (isset($profile->lat) && isset($profile->lon)) { + $options['lat'] = $profile->lat; + $options['lon'] = $profile->lon; + } + + if (isset($profile->location_id) && isset($profile->location_ns)) { + $options['location_id'] = $profile->location_id; + $options['location_ns'] = $profile->location_ns; + } + } + + return $options; + } }