Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added friendslist functionality

  • Loading branch information...
commit fdc38f193829241d43ef4aa1f42c7bf9c6cbeb37 1 parent 32890a2
Marcus Nyeholt authored
View
4 dashboards/code/pages/SiteDashboardPage.php
@@ -23,6 +23,10 @@ public function init() {
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-ui/jquery-ui.js'); // -1.8.5.custom.min.js');
Requirements::css('dashboards/thirdparty/aristo/aristo.css');
+
+ if (class_exists('WebServiceController')) {
+ Requirements::javascript('webservices/javascript/webservices.js');
+ }
}
/**
View
26 microblog/code/extensions/MicroBlogMember.php
@@ -19,6 +19,15 @@ class MicroBlogMember extends DataExtension {
'Followers' => 'Member',
);
+ public static $dependencies = array(
+ 'microBlogService' => '%$MicroBlogService',
+ );
+
+ /**
+ * @var MicroBlogService
+ */
+ public $microBlogService;
+
public function onBeforeWrite() {
parent::onBeforeWrite();
$this->memberFolder();
@@ -47,8 +56,23 @@ public function memberFolder() {
return $this->owner->UploadFolder();
}
- public function Friends() {
+ public function toFilteredMap() {
+ $allowed = array(
+ 'FirstName',
+ 'Surname',
+ 'Email',
+ );
+
+ $map = array();
+ foreach ($allowed as $prop) {
+ $map[$prop] = $this->owner->$prop;
+ }
+ return $map;
+ }
+
+ public function Friends() {
+ return $this->microBlogService->friendsList($this->owner);
}
public function Link() {
View
43 microblog/code/services/MicroBlogService.php
@@ -11,8 +11,12 @@ class MicroBlogService {
*/
public $dataService;
+ public $securityContext;
+
public static $dependencies = array(
'dataService' => '%$DataService',
+ 'permissionService' => '%$PermissionService',
+ 'securityContext' => '%$SecurityContext',
);
public function __construct() {
@@ -23,6 +27,7 @@ public function webEnabledMethods() {
return array(
'getStatusUpdates' => 'GET',
'getTimeline' => 'GET',
+ 'addFriendship' => 'POST',
);
}
@@ -135,9 +140,41 @@ public function findMember($searchTerm) {
// $list = DataList::create('Member')->dataQuery()->
}
- public function addFollower(DataObject $member, DataObject $follower) {
- $follower->follow($member);
- return $follower;
+ public function addFriendship(DataObject $member, DataObject $follower) {
+
+ if (!$member || !$follower) {
+ throw new PermissionDeniedException('Read', 'Cannot read those users');
+ }
+
+ if (!$member->ID == $this->securityContext->getMember()->ID) {
+ throw new PermissionDeniedException('Write', 'Cannot create a friendship for that user');
+ }
+
+ $existing = DataList::create('Friendship')->filter(array(
+ 'InitiatorID' => $member->ID,
+ 'OtherID' => $follower->ID,
+ ))->first();
+
+ if ($existing) {
+ return $existing;
+ }
+
+ // otherwise, we have a new one!
+ $friendship = new Friendship;
+ $friendship->InitiatorID = $member->ID;
+ $friendship->OtherID = $follower->ID;
+
+ $friendship->write();
+
+
+ return $friendship;
+ }
+
+ public function friendsList(DataObject $member) {
+ $list = DataList::create('Member')
+ ->innerJoin('Friendship', '"Friendship"."OtherID" = "Member"."ID"')
+ ->filter(array('InitiatorID' => $member->ID));
+ return $list;
}
public function removeFollower($member, $follower) {
View
13 microblog/javascript/friends.js
@@ -10,10 +10,19 @@
})
}
})
-
+
$('input.addFriendButton').entwine({
onclick: function () {
-
+ var params = {
+ 'memberType': 'Member',
+ 'memberID': $(this).parents('div.FriendsDashlet').find('input[name=MemberID]').val(),
+ 'followerType': 'Member',
+ 'followerID': $(this).attr('data-id')
+ };
+
+ SSWebServices.post('microBlog', 'addFriendship', params, function (data) {
+ console.log(data);
+ })
}
})
})
View
13 microblog/templates/Dashlets/FriendsDashlet.ss
@@ -1,5 +1,5 @@
<% if CurrentMember %>
-
+ <input type="hidden" value="$Owner.ID" name="MemberID" />
<% if $CurrentMember.ID != $OwnerID %>
<!-- add the user we're looking at as a friend -->
@@ -12,9 +12,14 @@
<% end_if %>
<!-- list of this user's friends -->
- <% with Owner.Friends %>
-
- <% end_with %>
+ <% loop Owner.Friends %>
+ <div class="userFriend">
+ <a href="$Link">
+ <img src="http://www.gravatar.com/avatar/$Owner.gravatarHash" />
+ $FirstName $Surname
+ </a>
+ </div>
+ <% end_loop %>
<% else %>
Please login
<% end_if %>
View
8 mysite/_config.php
@@ -6,14 +6,6 @@
include_once dirname(__FILE__).'/local.conf.php';
-// Sites running on the following servers will be
-// run in development mode. See
-// http://doc.silverstripe.com/doku.php?id=devmode
-// for a description of what dev mode does.
-Director::set_dev_servers(array(
- '127.0.0.1',
-));
-
if (!defined('SS_LOG_FILE')) {
define('SS_LOG_FILE', '/var/log/silverstripe/'.basename(dirname(dirname(__FILE__))).'.log');
}
View
2  restrictedobjects/_config.php
@@ -8,7 +8,7 @@
die('The restricted objects module requires the multivaluefield module from http://github.com/nyeholt/silverstripe-multivaluefield');
}
-Director::addRules(15, array(
+Director::addRules(100, array(
'Security/logout' => 'RestrictedSecurityController',
));
View
2  restrictedobjects/code/dataobjects/AccessAuthority.php
@@ -59,7 +59,7 @@ public function onAfterDelete() {
}
}
- public function canView() {
+ public function canView($member = null) {
return Member::currentUserID() > 0;
}
View
4 webservices/code/controllers/WebServiceController.php
@@ -66,7 +66,7 @@ public function init() {
}
}
- public function handleRequest(SS_HTTPRequest $request) {
+ public function handleRequest(SS_HTTPRequest $request, DataModel $model) {
try {
$this->pushCurrent();
if ((!Member::currentUserID() && !self::$allow_public_access) || $request->requestVar('token')) {
@@ -88,7 +88,7 @@ public function handleRequest(SS_HTTPRequest $request) {
} else if (!self::$allow_public_access) {
throw new WebServiceException(403, "Invalid request");
}
- $response = parent::handleRequest($request);
+ $response = parent::handleRequest($request, $model);
if (self::has_curr()) {
$this->popCurrent();
View
3  webservices/code/serialisers/DataObjectJsonConverter.php
@@ -9,6 +9,9 @@
class DataObjectJsonConverter {
public function convert(DataObject $object) {
+ if ($object->hasMethod('toFilteredMap')) {
+ return Convert::raw2json($object->toFilteredMap());
+ }
return Convert::raw2json($object->toMap());
}
}
View
4 webservices/code/serialisers/DataObjectSetJsonConverter.php
@@ -12,7 +12,9 @@ public function convert($set) {
$ret = new stdClass();
$ret->items = array();
foreach ($set as $item) {
- if (method_exists($item, 'toMap')) {
+ if ($item instanceof Object && $item->hasMethod('toFilteredMap')) {
+ $ret->items[] = $item->toFilteredMap();
+ } else if (method_exists($item, 'toMap')) {
$ret->items[] = $item->toMap();
} else {
$ret->items[] = $item;
View
1  webservices/code/services/DummyWebService.php
@@ -24,7 +24,6 @@ public function webEnabledMethods() {
}
public function myMethod($param) {
-
return array(
'SomeParam' => 'Goes here',
'Boolean' => true,
View
3  webservices/javascript/webservices.js
@@ -2,6 +2,9 @@
;(function ($) {
window.SSWebServices = (function () {
var securityId = $('#SecurityID').val();
+ if (!securityId) {
+ securityId = $('input[name=SecurityID]').val();
+ }
var getService = function (name, method, params, cb) {
params['SecurityID'] = securityId;
Please sign in to comment.
Something went wrong with that request. Please try again.