Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for backend favorites #1025

Merged
merged 6 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m

]]></description>

<version>3.99.3</version>
<version>3.99.4</version>
<licence>agpl</licence>

<author>Daniel Calviño Sánchez</author>
Expand Down
19 changes: 19 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,25 @@
'token' => '^[a-z0-9]{4,30}$',
],
],
[
'name' => 'Room#addToFavorites',
'url' => '/api/{apiVersion}/room/{token}/favorite',
'verb' => 'POST',
'requirements' => [
'apiVersion' => 'v1',
'token' => '^[a-z0-9]{4,30}$',
],
],
[
'name' => 'Room#removeFromFavorites',
'url' => '/api/{apiVersion}/room/{token}/favorite',
'verb' => 'DELETE',
'requirements' => [
'apiVersion' => 'v1',
'token' => '^[a-z0-9]{4,30}$',
],
],


/**
* Guest
Expand Down
22 changes: 22 additions & 0 deletions docs/api-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* [Delete a room](#delete-a-room)
* [Allow guests in a room (public room)](#allow-guests-in-a-room-public-room)
* [Disallow guests in a room (group room)](#disallow-guests-in-a-room-group-room)
* [Add room to favorites](#add-room-to-favorites)
* [Remove room from favorites](#remove-room-from-favorites)
- [Participant management](#participant-management)
* [Get list of participants in a room](#get-list-of-participants-in-a-room)
* [Add a participant to a room](#add-a-participant-to-a-room)
Expand Down Expand Up @@ -122,6 +124,7 @@ Base endpoint is: `/ocs/v2.php/apps/spreed/api/v1`
`sessionId` | string | `'0'` if not connected, otherwise a 512 character long string
`hasPassword` | bool | Flag if the room has a password
`hasCall` | bool | Flag if the room has an active call
`isFavorite` | bool | Flag if the room is favorited by the user
`unreadMessages` | int | Number of unread chat messages in the room (only available with `chat-v2` capability)

### Get single room (also for guests)
Expand Down Expand Up @@ -220,6 +223,25 @@ Base endpoint is: `/ocs/v2.php/apps/spreed/api/v1`
+ `403 Forbidden` When the room is not a public room
+ `404 Not Found` When the room could not be found for the participant

### Add room to favorites

* Method: `POST`
* Endpoint: `/room/{token}/favorite`

* Response:
- Header:
+ `200 OK`
+ `404 Not Found` When the room could not be found for the participant or the participant is a guest

### Remove room from favorites

* Method: `DELETE`
* Endpoint: `/room/{token}/favorite`

* Response:
- Header:
+ `200 OK`
+ `404 Not Found` When the room could not be found for the participant or the participant is a guest

## Participant management

Expand Down
7 changes: 7 additions & 0 deletions js/models/roomcollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
var RoomCollection = Backbone.Collection.extend({
model: OCA.SpreedMe.Models.Room,
comparator: function(modelA, modelB) {
var favoriteA = modelA.get('isFavorite'),
favoriteB = modelB.get('isFavorite');

if (favoriteA !== favoriteB) {
return favoriteB - favoriteA;
}

var activeA = modelA.get('active'),
activeB = modelB.get('active');

Expand Down
47 changes: 47 additions & 0 deletions js/views/roomlistview.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@
'</div>'+
'<div class="app-navigation-entry-menu">'+
'<ul class="app-navigation-entry-menu-list">'+
'{{#if canFavorite}}'+
'{{#if isFavorite}}'+
'<li>'+
'<button class="unfavorite-room-button">'+
'<span class="icon-star-dark"></span>'+
'<span>'+t('spreed', 'Unpin conversation')+'</span>'+
'</button>'+
'</li>'+
'{{else}}'+
'<li>'+
'<button class="favorite-room-button">'+
'<span class="icon-starred"></span>'+
'<span>'+t('spreed', 'Pin conversation')+'</span>'+
'</button>'+
'</li>'+
'{{/if}}'+
'{{/if}}'+
'{{#if isRemovable}}'+
'<li>'+
'<button class="remove-room-button">'+
Expand Down Expand Up @@ -83,6 +100,9 @@
'change:participantType': function() {
this.render();
},
'change:isFavorite': function() {
this.render();
},
'change:unreadMessages': function() {
this.render();
},
Expand All @@ -107,6 +127,7 @@
var isRemovable = this.model.get('type') !== 1;
return {
isRemovable: isRemovable,
canFavorite: this.model.get('participantType') !== 5,
isDeletable: !isRemovable || ((this.model.get('participantType') === 1 || this.model.get('participantType') === 2) &&
(Object.keys(this.model.get('participants')).length > 1 || this.model.get('numGuests') > 0)),
numUnreadMessages: this.model.get('unreadMessages') > 99 ? '99+' : this.model.get('unreadMessages')
Expand Down Expand Up @@ -138,6 +159,8 @@
},
events: {
'click .app-navigation-entry-utils-menu-button button': 'toggleMenu',
'click @ui.menu .favorite-room-button': 'addRoomToFavorites',
'click @ui.menu .unfavorite-room-button': 'removeRoomFromFavorites',
'click @ui.menu .remove-room-button': 'removeRoom',
'click @ui.menu .delete-room-button': 'deleteRoom',
'click @ui.room': 'joinRoom'
Expand Down Expand Up @@ -205,6 +228,30 @@
type: 'DELETE'
});
},
addRoomToFavorites: function() {
if (this.model.get('participantType') === 5) {
return;
}

this.model.set('isFavorite', 1);

$.ajax({
url: OC.linkToOCS('apps/spreed/api/v1/room', 2) + this.model.get('token') + '/favorite',
type: 'POST'
});
},
removeRoomFromFavorites: function() {
if (this.model.get('participantType') === 5) {
return;
}

this.model.set('isFavorite', 0);

$.ajax({
url: OC.linkToOCS('apps/spreed/api/v1/room', 2) + this.model.get('token') + '/favorite',
type: 'DELETE'
});
},
cleanupIfActiveRoom: function() {
if (!this.model.get('active')) {
return;
Expand Down
1 change: 1 addition & 0 deletions lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function getCapabilities() {
'empty-group-room',
'guest-display-names',
'multi-room-users',
'favorites',
],
],
];
Expand Down
47 changes: 46 additions & 1 deletion lib/Controller/RoomController.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,10 @@ protected function formatRoom(Room $room, Participant $participant = null) {
if ($participant instanceof Participant) {
$participantType = $participant->getParticipantType();
$participantInCall = $participant->isInCall();
$favorite = $participant->isFavorite();
} else {
$participantType = Participant::GUEST;
$participantInCall = false;
$participantInCall = $favorite = false;
}

$roomData = [
Expand All @@ -177,6 +178,7 @@ protected function formatRoom(Room $room, Participant $participant = null) {
'hasPassword' => $room->hasPassword(),
'hasCall' => $room->getActiveSince() instanceof \DateTimeInterface,
'unreadMessages' => 0,
'isFavorite' => $favorite,
];

if (!$participant instanceof Participant) {
Expand Down Expand Up @@ -462,6 +464,49 @@ protected function createEmptyRoom($roomName, $public = true) {
], Http::STATUS_CREATED);
}

/**
* @NoAdminRequired
*
* @param string $token
* @return DataResponse
*/
public function addToFavorites($token) {
try {
$room = $this->manager->getRoomForParticipantByToken($token, $this->userId);
$participant = $room->getParticipant($this->userId);
} catch (RoomNotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (ParticipantNotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}

$participant->setFavorite(true);

return new DataResponse([]);
}

/**
* @NoAdminRequired
*
* @param string $token
* @return DataResponse
*/
public function removeFromFavorites($token) {
try {
$room = $this->manager->getRoomForParticipantByToken($token, $this->userId);
$participant = $room->getParticipant($this->userId);
} catch (RoomNotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (ParticipantNotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}

$participant->setFavorite(false);

return new DataResponse([]);
}


/**
* @NoAdminRequired
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function createRoomObject(array $row) {
* @return Participant
*/
public function createParticipantObject(Room $room, array $row) {
return new Participant($this->db, $room, $row['user_id'], (int) $row['participant_type'], (int) $row['last_ping'], $row['session_id'], (bool) $row['in_call']);
return new Participant($this->db, $room, $row['user_id'], (int) $row['participant_type'], (int) $row['last_ping'], $row['session_id'], (bool) $row['in_call'], (bool) $row['favorite']);
}

/**
Expand Down
51 changes: 51 additions & 0 deletions lib/Migration/Version3003Date20180707222130.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* @copyright Copyright (c) 2018 Mario Danic <mario@lovelyhq.com>
*
* @author Mario Danic <mario@lovelyhq.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Spreed\Migration;

use Doctrine\DBAL\Types\Type;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;

class Version3003Date20180707222130 extends SimpleMigrationStep {


/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
* @since 13.0.0
*/
public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$table = $schema->getTable('talk_participants');
$table->addColumn('favorite', Type::BOOLEAN, [
'default' => 0,
]);

return $schema;
}
}
30 changes: 29 additions & 1 deletion lib/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\Spreed;

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

class Participant {
Expand All @@ -46,6 +47,8 @@ class Participant {
protected $sessionId;
/** @var bool */
protected $inCall;
/** @var bool */
private $isFavorite;

/**
* @param IDBConnection $db
Expand All @@ -55,15 +58,17 @@ class Participant {
* @param int $lastPing
* @param string $sessionId
* @param bool $inCall
* @param bool $isFavorite
*/
public function __construct(IDBConnection $db, Room $room, $user, $participantType, $lastPing, $sessionId, $inCall) {
public function __construct(IDBConnection $db, Room $room, $user, $participantType, $lastPing, $sessionId, $inCall, $isFavorite) {
$this->db = $db;
$this->room = $room;
$this->user = $user;
$this->participantType = $participantType;
$this->lastPing = $lastPing;
$this->sessionId = $sessionId;
$this->inCall = $inCall;
$this->isFavorite = $isFavorite;
}

public function getUser() {
Expand All @@ -85,4 +90,27 @@ public function getSessionId() {
public function isInCall() {
return $this->inCall;
}

/**
* @return bool
*/
public function isFavorite() {
return $this->isFavorite;
}

public function setFavorite($favor) {
if (!$this->user) {
return false;
}

$query = $this->db->getQueryBuilder();
$query->update('talk_participants')
->set('favorite', $query->createNamedParameter((int) $favor, IQueryBuilder::PARAM_INT))
->where($query->expr()->eq('user_id', $query->createNamedParameter($this->user)))
->andWhere($query->expr()->eq('room_id', $query->createNamedParameter($this->room->getId())));
$query->execute();

$this->isFavorite = $favor;
return true;
}
}
1 change: 1 addition & 0 deletions tests/php/CapabilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function testGetCapabilities() {
'empty-group-room',
'guest-display-names',
'multi-room-users',
'favorites',
],
],
], $capabilities->getCapabilities());
Expand Down