~reedwade/wee-matter

ref: adfdd6516b39eb05eb19a92a2d9bbbf244444683 wee-matter/wee_matter/http.py -rw-r--r-- 11.2 KiB
adfdd651Reed Wade fix a bug when download file fail 9 months ago
                                                                                
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import weechat
import wee_matter
import json
import time
import re

from wee_matter.room import (hydrate_room_read_posts_cb, hydrate_room_posts_cb,
                             hydrate_room_users_cb, hydrate_room_user_cb)

from wee_matter.post import post_post_cb

from wee_matter.file import file_get_cb

from wee_matter.server import (connect_server_cb, connect_server_teams_cb,
                               connect_server_team_channels_cb, disconnect_server_cb,
                               connect_server_users_cb, server_completion_cb,
                               connect_server_team_channel_cb, connect_server_team_cb,
                               new_user_cb)


def build_file_url(file_id, server):
    return wee_matter.server.server_root_url(server) + "/api/v4/files/" + file_id

def singularity_cb(data, command, rc, out, err):
    if rc != 0:
        weechat.prnt("", "An error occurred while performing a request")
        return weechat.WEECHAT_RC_ERROR

    return weechat.WEECHAT_RC_OK

response_buffers = {}
def buffered_response_cb(data, command, rc, out, err):
    arg_search = re.search('([^\|]*)\|([^\|]*)\|(.*)', data)
    if not arg_search:
        weechat.prnt("", "Bad usage of buffered response cb \"{}\"".format(data))
        return weechat.WEECHAT_RC_ERROR
    response_buffer_name = arg_search.group(1)
    real_cb = arg_search.group(2)
    real_data = arg_search.group(3)

    if not response_buffer_name in response_buffers:
        response_buffers[response_buffer_name] = ""

    if rc == weechat.WEECHAT_HOOK_PROCESS_RUNNING:
        response_buffers[response_buffer_name] += out
        return weechat.WEECHAT_RC_OK

    response = response_buffers[response_buffer_name] + out
    del response_buffers[response_buffer_name]

    return eval(real_cb)(real_data, command, rc, response, err)

def build_buffer_cb_data(url, cb, cb_data):
    return "{}|{}|{}".format(url, cb, cb_data)

enqueued_requests = []
def enqueue_request(method, *params):
    enqueued_requests.append([method, params])

def handle_queued_request_cb(data, remaining_calls):
    if not enqueued_requests:
        return weechat.WEECHAT_RC_OK

    request = enqueued_requests.pop(0)
    eval(request[0])(*request[1])
    return weechat.WEECHAT_RC_OK

def run_get_user_teams(user_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/users/" + user_id + "/teams"
    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_get_team(team_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/teams/" + team_id
    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_get_users(server, page, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/users?page=" + str(page)
    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_get_user(server, user_id, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/users/" + user_id
    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_user_logout(server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/users/logout"
    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "post": "1",
            "httpheader": "\n".join([
                "Authorization: Bearer " + server.user_token,
                "Content-Type:",
            ]),
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_user_login(server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/users/login"
    params = {
        "login_id": server.username,
        "password": server.password,
    }

    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "postfields": json.dumps(params),
            "header": "1",
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_get_channel(channel_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/channels/" + channel_id
    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_get_user_team_channels(user_id, team_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/users/" + user_id + "/teams/" + team_id + "/channels"
    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_post_post(post, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/posts"
    params = {
        "channel_id": post.channel_id,
        "message": post.message,
    }

    if post.parent_id:
        params["root_id"] = post.parent_id

    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
            "postfields": json.dumps(params),
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_get_channel_posts(channel_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/channels/" + channel_id + "/posts"
    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_get_read_channel_posts(user_id, channel_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/users/" + user_id + "/channels/" + channel_id + "/posts/unread?limit_after=1"
    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_get_channel_posts_after(post_id, channel_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/channels/" + channel_id + "/posts?after=" + post_id
    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_get_channel_members(channel_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/channels/" + channel_id + "/members"
    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_get_channel_member(channel_id, member_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/channels/" + channel_id + "/members/" + member_id
    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_post_channel_view(user_id, channel_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/channels/members/" + user_id + "/view"
    params = {
        "channel_id": channel_id,
    }

    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "postfields": json.dumps(params),
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_post_reaction(emoji_name, post_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/reactions"
    params = {
        "user_id": server.user.id,
        "post_id": post_id,
        "emoji_name": emoji_name,
        "create_at": int(time.time()),
    }

    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "postfields": json.dumps(params),
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_delete_reaction(emoji_name, post_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/users/" + server.user.id + "/posts/" + post_id + "/reactions/" + emoji_name

    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "customrequest": "DELETE",
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_delete_post(post_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/posts/" + post_id

    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "customrequest": "DELETE",
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )

def run_get_file(file_id, file_out_path, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/files/" + file_id

    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "failonerror": "1",
            "file_out": file_out_path,
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
        30 * 1000,
        "buffered_response_cb",
        build_buffer_cb_data(url, cb, cb_data)
    )