-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
187 lines (140 loc) · 5.75 KB
/
api.py
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
import re
import json
from datetime import datetime, timezone
from twilio.rest import Client
from flask import current_app, session, request, Blueprint
from flask.json import jsonify
from db import get_model
from api_auth import account_required
from api_server_sent_events import broker
# from api_server_sent_events import broker, presence_manager
# a Blueprint is a collection of routes under a certain prefix or "folder" on the http server
bp = Blueprint("api", __name__, url_prefix="/api")
@bp.route("/conversations", methods=['GET'])
@account_required
def list_conversations():
conversations = get_model().list_conversations()
return jsonify(conversations)
@bp.route("/setName", methods=['POST'])
@account_required
def setName():
request_body = request.json
if 'name' not in request_body or not request_body['name']:
return jsonify({'error': "'name' field is required"}), 400
get_model().set_account_name(session['account_id'], request_body['name'])
session['name'] = request_body['name']
# presence_manager.update(session['account_id'], {
# 'name': request_body['name'],
# 'location': "home",
# })
return jsonify({'ok': True}), 200
@bp.route("/conversations/<string:remote_number>", methods=['GET'])
@account_required
def get_conversation(remote_number):
messages = get_model().list_messages(remote_number)
status_updates = get_model().list_status_updates(remote_number)
events = joinerate(messages, status_updates, "date")
remote_number_profile = get_model().get_remote_number_profile(remote_number)
status = "new" if len(status_updates) == 0 else status_updates[0]['status']
return jsonify({'events': events, 'name': remote_number_profile['name'], 'status': status})
@bp.route("/conversations/<string:remote_number>/status", methods=['POST'])
@account_required
def set_conversation_status(remote_number):
request_body = request.json
if 'status' not in request_body:
return jsonify({'error': "'status' field is required"}), 400
comment = "" if 'comment' not in request_body else request_body['comment']
get_model().save_status(remote_number, session['account_id'], request_body["status"], comment)
broker.publish({
'type': "conversation_event",
'remoteNumber': remote_number,
'sentBy': session['name'],
'status': request_body["status"],
'comment': comment,
'date': datetime.now(timezone.utc)
})
return jsonify({'ok': True}), 200
@bp.route("/conversations/<string:remote_number>/name", methods=['POST'])
@account_required
def set_conversation_name(remote_number):
request_body = request.json
if 'name' not in request_body:
return jsonify({'error': "'name' field is required"}), 400
get_model().set_conversation_name(remote_number, request_body["name"])
return jsonify({'ok': True}), 200
@bp.route("/send/<string:remote_number>", methods=['POST'])
@account_required
def send_sms(remote_number):
request_body = request.json
if 'body' not in request_body:
return jsonify({'error': "'body' field is required"}), 400
message = current_app.config["TWILIO_CLIENT"].messages.create(
to=remote_number,
from_=current_app.config["TEXTLINE_NUMBER"],
body=request_body["body"],
)
get_model().save_message(message.sid, remote_number, session['account_id'], request_body["body"])
broker.publish({
'type': "conversation_event",
'remoteNumber': remote_number,
'sentBy': session['name'],
'body': request_body["body"],
'date': datetime.now(timezone.utc)
})
current_app.logger.debug(f"sent message to {remote_number}: sid: {message.sid}")
return jsonify({'ok': True}), 200
@bp.route("/presence", methods=['POST'])
@account_required
def post_presence():
request_body = request.json
if 'path' not in request_body:
return jsonify({'error': "'path' field is required"}), 400
if 'active' not in request_body:
return jsonify({'error': "'active' field is required"}), 400
if type(request_body['active']) != bool:
return jsonify({'error': "'active' must be a boolean"}), 400
if type(request_body['path']) != str:
return jsonify({'error': "'path' must be a string"}), 400
if not re.match(r"^[a-z0-9/?&_+-]+$", request_body['path'], re.IGNORECASE):
return jsonify({'error': "'path' may only include the characters a-z0-9/?&_+-"}), 400
#current_app.logger.info(f"presence: account_id={session['account_id']} path={request_body['path']} active={request_body['active']}")
get_model().set_presence(session['account_id'], request_body['path'], request_body['active'])
broker.publish({
'type': "presence",
#'account_id': session['account_id'], # account_id gets added by broker
'name': session['name'],
'path': request_body['path'],
'active': request_body['active'],
'date': datetime.now(timezone.utc)
})
return jsonify({'ok': True}), 200
@bp.route("/presence", methods=['GET'])
@account_required
def get_presence():
items = list(filter(lambda x: x['account_id'] != session['account_id'], get_model().get_presence()))
return jsonify(items), 200
# "joinerater" (joining iterator) pattern, zip two sorted lists of dict objects together into a single sorted list
def joinerate(list_a, list_b, sorted_by_key):
to_return = []
i = 0
j = 0
while i < len(list_a) or j < len(list_b):
list_a_has_more = i < len(list_a)
list_b_has_more = j < len(list_b)
if list_a_has_more and list_b_has_more:
if list_a[i][sorted_by_key] > list_b[j][sorted_by_key]:
to_return.append(list_a[i])
i += 1
else:
to_return.append(list_b[j])
j += 1
elif list_a_has_more:
to_return.append(list_a[i])
i += 1
elif list_b_has_more:
to_return.append(list_b[j])
j += 1
return to_return
@bp.errorhandler(500)
def json_error(error):
return jsonify({'error': 'Internal Server Error'}), 500