Ver código fonte

Presence state manipulation basic implementation

Hal De 4 anos atrás
pai
commit
251e96af1e
1 arquivos alterados com 60 adições e 2 exclusões
  1. 60 2
      app/app.py

+ 60 - 2
app/app.py

@@ -108,7 +108,7 @@ async def ui():
 async def action():
   _payload = await request.get_data()
   reply = await manager.send_action(json.loads(_payload))
-  return reply.getdict()
+  return reply
 
 @app.route('/ami/getvar/<string:variable>')
 async def amiGetVar(variable):
@@ -231,6 +231,22 @@ async def amiSetHint(context, user, hint):
     return True
   return False
 
+async def amiPresenceState(user):
+  '''AMI PresenceState request for CustomPresence provider
+
+  Parameters:
+    user (string): user
+
+  Returns:
+    string: One of: not_set, unavailable, available, away, xa, chat or dnd
+  '''
+  reply = await manager.send_action({'Action': 'PresenceState',
+                                     'Provider': 'CustomPresence:{}'.format(user)})
+  app.logger.warning('PresenceState({})'.format(user))
+  if (isinstance(reply, Message) and reply.success):
+    return reply.state
+  return None
+
 async def amiCommand(command):
   '''AMI Command
   Runs specified command using AMI action Command in background.
@@ -351,11 +367,53 @@ async def setUserDeviceStates(user, device, queues, ast):
     _cf = await amiDBGet('CF', user)
     await amiSetVar('DEVICE_STATE(Custom:DEVCF{})'.format(device), 'INUSE' if _cf != '' else 'NOT_INUSE')
 
+@app.route('/user/<user>/presence')
+class UserPresenceState(Resource):
+  @app.param('user', 'User to query for presence state', 'path')
+  @app.response(HTTPStatus.OK, 'Presence state string')
+  @app.response(HTTPStatus.UNAUTHORIZED, 'Authorization required')
+  @app.response(HTTPStatus.NOT_FOUND, 'User does not exist')
+  @app.response(HTTPStatus.BAD_REQUEST, 'AMI error')
+  async def get(self, user):
+    '''Returns user's presence state.
+    One of: not_set | unavailable | available | away | xa | chat | dnd
+    '''
+    cidnum = await getUserCID(user) # Check if user exists in astdb
+    if cidnum is None:
+      return '', HTTPStatus.NOT_FOUND
+    state = await amiPresenceState(user)
+    if state is None:
+      return '', HTTPStatus.BAD_REQUEST
+    return state
+
+@app.route('/user/<user>/presence/<state>')
+class SetUserPresenceState(Resource):
+  @app.param('user', 'Target user to set the presence state', 'path')
+  @app.param('state',
+             'The presence state to set for user, one of: not_set, unavailable, available, away, xa, chat or dnd',
+             'path')
+  @app.response(HTTPStatus.OK, 'Successfuly set the presence state')
+  @app.response(HTTPStatus.UNAUTHORIZED, 'Authorization required')
+  @app.response(HTTPStatus.NOT_FOUND, 'User does not exist')
+  @app.response(HTTPStatus.BAD_REQUEST, 'Wrong state string ot other AMI error')
+  async def get(self, user, state):
+    '''Sets user's presence state.
+    Allowed states: not_set | unavailable | available | away | xa | chat | dnd
+    '''
+    cidnum = await getUserCID(user) # Check if user exists in astdb
+    if cidnum is None:
+      return '', HTTPStatus.NOT_FOUND
+    if await amiSetVar('PRESENCE_STATE(CustomPresence:{})'.format(user),
+                       state):
+      return ''
+    else:
+      return '', HTTPStatus.BAD_REQUEST
+
 @app.route('/device/<device>/<user>/on')
 @app.route('/user/<user>/<device>/on')
 class UserDeviceBind(Resource):
   @app.param('device', 'Device number to bind to', 'path')
-  @app.param('user', 'User user to bind', 'path')
+  @app.param('user', 'User to bind to device', 'path')
   @app.response(HTTPStatus.OK, 'JSON reply with fields "success" and "result"')
   @app.response(HTTPStatus.UNAUTHORIZED, 'Authorization required')
   async def get(self, device, user):