summaryrefslogtreecommitdiffstats
path: root/js/controller/ShareController.js
blob: 694843d7969f2e00d3a4d28fea1474df588944cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
 * Nextcloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Marco Nassabain <marco.nassabain@hotmail.com>
 * @author Nicolas Wendling <nicolas.wendling1011@gmail.com>
 * @author Jimmy Huynh <natorisaki@gmail.com>
 * @author Aurélien David <dav.aurelien@gmail.com>
 */
app.controller('ShareController', function (ShareResource, Loading) {
    'use strict';

    this.showDropDown = false;

    this.toggleDropdown = function() {
        this.showDropDown = !this.showDropDown;
    };

    /** Array containing users to share an item with */
    this.userList = [];

    /**
     * @param search Username search query
     * 
     * Retrieve users matching search query using OC
     */
    this.searchUsers = function(search) {
        Loading.setLoading('user', true);
        if (!search || search === '') {
            this.userList = [];
            Loading.setLoading('user', false);
            return;
        }

        var response = ShareResource.getUsers(search);
        response.then((response) => {
            this.userList = response.ocs.data.users;
            Loading.setLoading('user', false);
        });
    };

    /** Dictionary mapping articles to users they're shared with */
    this.usersSharedArticles = {};

    /**
     * @param itemId ID of the item to be shared
     * @param userId ID of the recipient
     * 
     * Call the /share route with the appropriate params to share an item.
     * Fills this.usersSharedArticles to avoid re-sharing the same article
     * with the same user multiple times.
     */
    this.shareItem = function(itemId, userId) {
        Loading.setLoading(userId, true);
        if (this.usersSharedArticles[itemId] && this.usersSharedArticles[itemId].includes(userId)) {
            Loading.setLoading(userId, false);
            return;
        }

        this.usersSharedArticles[itemId] = this.usersSharedArticles[itemId] ? this.usersSharedArticles[itemId] : [];
        this.usersSharedArticles[itemId].push(userId);
        
        ShareResource.shareItem(itemId, userId)
        .then((result) => {
            Loading.setLoading(userId, false);
            return result;
        });
    };

});