Navigation Menu

Skip to content

Commit

Permalink
Add UI for accepting friend requests (sending is already functional, …
Browse files Browse the repository at this point in the history
…too)
  • Loading branch information
codedust committed Aug 14, 2015
1 parent 778e07e commit cf0099f
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 41 deletions.
9 changes: 9 additions & 0 deletions html/app.css
Expand Up @@ -389,6 +389,9 @@ body {
height: 60px;
float: right;
}

/*** settings view ***/

#mainview-settings {
height: 100%;
overflow: auto;
Expand All @@ -399,6 +402,12 @@ body {
font-size: 2em;
}

/*** friend request modal ***/

.friend-request .panel-heading {
cursor: pointer;
}

/*** MEDIA QUERYS ***/

@media (max-width: 767px) {
Expand Down
18 changes: 14 additions & 4 deletions html/index.html
Expand Up @@ -102,7 +102,8 @@
<option value="">All</option>
<option value="1">Online</option>
</select>
<button class="btn btn-toxgreen inline-button" data-toggle="modal" href="#modal-friend-requests">1 Friend Request</button>
<button class="btn btn-toxgreen inline-button" ng-show="friendRequests.length == 1" data-toggle="modal" href="#modal-friend-requests">1 Friend Request</button>
<button class="btn btn-toxgreen inline-button" ng-show="friendRequests.length >= 2" data-toggle="modal" href="#modal-friend-requests">{{ friendRequests.length }} Friend Requests</button>
<button class="btn btn-toxgreen inline-button" href="#" ng-show="appInstallationStatus == 'notinstalled'" ng-click="installWebApp()">install</button>
<button class="btn btn-toxgreen inline-button disabled" href="#" ng-show="appInstallationStatus == 'success'" ng-click="installWebApp()">installed</button>
<a href="#" class="contact" ng-class="{active: contacts[activecontactindex] == contact}" ng-repeat="contact in contacts | orderBy:'online':true" ng-click="showChat(contact.number); scrollLeft();" ng-show="!onlyShowOnlineContacts || contact.online">
Expand Down Expand Up @@ -289,7 +290,7 @@ <h4 class="modal-title" id="modal-toxid-title">Your Tox ID</h4>
<h4 class="modal-title" id="modal-friend-requests-title">Friend requests</h4>
</div>
<div class="modal-body">
<h3>Send friend request</h3>
<h3>Send a friend request</h3>
<form role="form" action="#" ng-submit="sendFriendRequest(new_friend_request.friend_id, new_friend_request.message)">
<div class="form-group input-group">
<span class="input-group-addon">Tox ID</span>
Expand All @@ -304,8 +305,17 @@ <h3>Send friend request</h3>
</form>
<hr>
<h3>Incoming friend requests</h3>
<p>No outstanding friend requests.</p>
<p>// TODO </p>
<div class="panel panel-default friend-request" ng-repeat="friendRequest in friendRequests">
<div class="panel-heading" ng-click="toggleFriendRequestBody()">{{friendRequest.publicKey|uppercase}}</div>
<div class="panel-body" ng-show="!friendRequest.is_ignored">
<div style="white-space: pre;">{{friendRequest.message}}</div>
<div class="text-right">
<button class="btn btn-sm" ng-click="ignoreFriendRequest()">Ignore</button>
<button class="btn btn-sm btn-toxgreen" ng-click="acceptFriendRequest()">Accept</button>
</div>
</div>
</div>
<p ng-show="friendRequests.length == 0">No outstanding friend requests.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-sm" data-dismiss="modal">
Expand Down
41 changes: 41 additions & 0 deletions html/js/app.js
Expand Up @@ -40,6 +40,7 @@
$scope.contacts = [];
$scope.activecontactindex = -1;
$scope.messagetosend = '';
$scope.friendRequests = [];
$scope.new_friend_request = {
friend_id: '',
message: '',
Expand Down Expand Up @@ -166,6 +167,37 @@
});
};

$scope.toggleFriendRequestBody = function(){
$(this)[0].friendRequest.is_ignored = !$(this)[0].friendRequest.is_ignored;
};

$scope.ignoreFriendRequest = function() {
var friendRequest = $(this)[0].friendRequest;

$http.post('api/post/friend_request_is_ignored', {
publicKey: friendRequest.publicKey,
is_ignored: true
}).success(function() {
friendRequest.is_ignored = true;
}).error(function(err) {
// TODO
alert(err.message);
});
};

$scope.acceptFriendRequest = function() {
var friendRequest = $(this)[0].friendRequest;

$http.post('api/post/friend_request_accept', {
publicKey: friendRequest.publicKey
}).success(function() {
fetchFriendRequests();
}).error(function(err) {
// TODO
alert(err.message);
});
};

$scope.deleteFriend = function(friend) {
$http.post('api/post/delete_friend', {
friend: friend
Expand All @@ -180,6 +212,7 @@
});
};


// == Event handlers ==
$('#profile-card-back-button').click(function() {
$('#profile-card, #contact-list-wrapper, #button-panel').removeClass('translate75left');
Expand Down Expand Up @@ -268,6 +301,12 @@
});
};

var fetchFriendRequests = function() {
$http.get('api/get/friend_requests').success(function(data) {
$scope.friendRequests = data;
});
};

// == WebSocket connection ==
WS.registerHandler('friend_message', function(data) {
var i = getContactIndexByNum(data.friend);
Expand Down Expand Up @@ -318,6 +357,7 @@

WS.registerHandler('profile_update', fetchProfile);
WS.registerHandler('friendlist_update', fetchContactlist);
WS.registerHandler('friend_requests_update', fetchFriendRequests);

WS.registerHandler('avatar_update', function() {
$scope.curDate = Date.now(); // reload avatar images
Expand All @@ -328,6 +368,7 @@
$('#modal-connection-error').modal('hide');
fetchProfile();
fetchContactlist();
fetchFriendRequests();
fetchSettings();
$scope.$apply();
};
Expand Down
66 changes: 66 additions & 0 deletions server/handleAPI.go
Expand Up @@ -42,6 +42,28 @@ var handleAPI = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}
fmt.Fprintf(w, friendlist)

case "/get/friend_requests":
type friendRequest struct {
PublicKey string `json:"publicKey"`
Message string `json:"message"`
IsIgnored bool `json:"is_ignored"`
}

dbFriendRequests := storage.GetFriendRequests(-1)

var friendRequests []friendRequest

for _, dbFriendRequest := range dbFriendRequests {
friendRequests = append(friendRequests, friendRequest{PublicKey: dbFriendRequest.PublicKey, Message: dbFriendRequest.Message, IsIgnored: dbFriendRequest.IsIgnored})
}

if friendRequests == nil {
friendRequests = []friendRequest{}
}

jsonFriendRequests, _ := json.Marshal(friendRequests)
fmt.Fprintf(w, string(jsonFriendRequests))

case "/get/profile":
type profile struct {
Username string `json:"username"`
Expand Down Expand Up @@ -228,6 +250,50 @@ var handleAPI = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}
fmt.Fprintf(w, string(friendID))

case "/post/friend_request_is_ignored":
type friendRequest struct {
PublicKey string `json:"publicKey"`
IsIgnored bool `json:"is_ignored"`
}

var incomingData friendRequest
err = json.Unmarshal(data, &incomingData)
if err != nil {
rejectWithDefaultErrorJSON(w)
return
}

storage.StoreFriendRequestIgnoreStatus(incomingData.PublicKey, incomingData.IsIgnored)

case "/post/friend_request_accept":
type friendRequest struct {
PublicKey string `json:"publicKey"`
}

var incomingData friendRequest
err = json.Unmarshal(data, &incomingData)
if err != nil {
rejectWithDefaultErrorJSON(w)
return
}

publicKeyBytes, err := hex.DecodeString(incomingData.PublicKey)
if err != nil {
rejectWithDefaultErrorJSON(w)
return
}

_, err = tox.FriendAddNorequest(publicKeyBytes)
if err != nil {
rejectWithDefaultErrorJSON(w)
return
}

storage.DeleteFriendRequest(incomingData.PublicKey)

// broadcast status to all connected clients
broadcastToClients(createSimpleJSONEvent("friendlist_update"))

case "/post/delete_friend":
type friend struct {
Number uint32 `json:"friend"`
Expand Down

0 comments on commit cf0099f

Please sign in to comment.