summaryrefslogtreecommitdiffstats
path: root/js/controller/ShareController.js
blob: 852650c173e54e52e479c3f776e14a65960549c1 (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
/**
 * 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>
 */
app.controller('ShareController', function (ShareResource, Loading) {
    'use strict';

    this.userList = [];

    this.searchUsers = function(search) {

        Loading.setLoading('user', true);

        // TODO: search === undefined 🤢 je pense pas que c'est ouf comme syntaxe
        if (search === '' || search === undefined) {
            this.userList = [];
            Loading.setLoading('user', false);
            return;
        }

        // TODO: bug - requetes retardataires (regarder issues git)
        var response = ShareResource.getUsers(search);
        response.then((response) => {
            this.userList = response.ocs.data.users;
            Loading.setLoading('user', false);
        });
    };

    // Dict <itemId, List<Int>(user_id)>: Local mapping b/w users & articles: 
    //[Article 1 : <Jimmy, Aurelien, ...>, Article 2: <...>]
    this.usersSharedArticles = {};

    this.shareItem = function(itemId, userId) {
        Loading.setLoading(userId, true);
        if (this.usersSharedArticles[itemId] && this.usersSharedArticles[itemId].includes(userId)) {
            Loading.setLoading(userId, false);
            return;
         }

        // quick initialization (instead of if (...) : [])
        this.usersSharedArticles[itemId] = this.usersSharedArticles[itemId] ? this.usersSharedArticles[itemId] : [];

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

});