summaryrefslogtreecommitdiffstats
path: root/js/test.js
blob: 212485a3e350dad8ea5f8e027af4f1ae282d1d9b (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
(function () {

  var Social = function () {

    var elem = {
      socialInstanceNewAccount: null,
      socialSubmitNewAccount: null,
      socialListAccounts: null,
      socialSubmitAccountTest: null
    }

    var test = {

      newAccount: function () {
        var data = {
          instance: elem.socialInstanceNewAccount.val()
        }

        test.sendRequest('POST', data, '/user/account', test.newAccountResult)
      },

      newAccountResult: function (data) {
        if (data.status !== 1) {
          return
        }

        window.open(data.result.authorizationUrl, 'gettoken', 'width=500,height=550')

        // test.getAccounts()
      },

      getAccounts: function () {
        test.sendRequest('GET', {}, '/user/accounts', test.getAccountsResult)
      },

      getAccountsResult: function (data) {
        if (data.status !== 1) {
          return
        }

        elem.socialListAccounts.empty()
        for (var i = 0; i < data.result.accounts.length; i++) {
          var item = data.result.accounts[i]
          elem.socialListAccounts.append(
            $('<option>', {value: item.id}).text(item.account + '@' + item.service.address))
        }

        test.refreshData()
      },

      testAccount: function (accountId) {
        test.sendRequest('GET', {}, '/user/account/' + accountId + '/test', test.testAccountResult)
      },

      testAccountResult: function (data) {
        console.log(JSON.stringify(data))
      },

      getAccountPosts: function (accountId) {
        test.sendRequest('GET', {}, '/user/account/' + accountId + '/posts', test.getAccountPostsResult)
      },

      getAccountPostsResult: function (data) {
        console.log('Your posts: ' + JSON.stringify(data))
      },

      getAccountFollows: function (accountId) {
        test.sendRequest('GET', {}, '/user/account/' + accountId + '/follows',
          test.getAccountFollowsResult)
      },

      getAccountFollowsResult: function (data) {
        console.log('Your Follows: ' + JSON.stringify(data))
      },

      refreshData: function () {
        var accountId = elem.socialListAccounts.val()
        test.getAccountFollows(accountId)
      },

      sendRequest: function (method, data, url, callback) {
        $.ajax({
          method: method,
          url: OC.generateUrl('/apps/social' + url),
          data: {data: data}
        }).done(function (res) {
          test.requestCallback(callback, res)
        }).fail(function () {
          console.log('fail to request')
        })
      },

      requestCallback: function (callback, result) {
        if (callback && (typeof callback === 'function')) {
          if (typeof result === 'object') {
            callback(result)
          } else {
            callback({status: -1})
          }

          return true
        }

        return false
      }

    }

    elem.socialInstanceNewAccount = $('#social-instance-new-account')
    elem.socialSubmitNewAccount = $('#social-submit-new-account')
    elem.socialListAccounts = $('#social-list-accounts')
    elem.socialSubmitAccountTest = $('#social-submit-account-test')

    elem.socialSubmitNewAccount.on('click', function () {
      test.newAccount()
    })

    elem.socialSubmitAccountTest.on('click', function () {
      test.testAccount(elem.socialListAccounts.val())
    })

    elem.socialListAccounts.on('change', function () {
      test.refreshData()
    })

    test.getAccounts()

//    test.getAccountPosts()
  }

  if (OCA.Social === undefined) {
    OCA.Social = {}
  }
  OCA.Social.test = new Social()

})()