Esempio n. 1
0
 async def helper(self, sid, request):
   try:
     result = None
     logger.info("REQUEST\nfunc_name: {}\nrequest: {}\n".format(
       name, request
     ))
     product = await func(self, sid, request)
     logger.info("REQUEST\nfunc_name: {}\nproduct: {}\nrequest: {}\n".format(
       name, product, request
     ))
     if not is_error(product):
       try:
         validate(instance=product, schema=bres.schema[name])
         result = dumps(product, cls=DictEncoder)
       except ValidationError:
         logger.info("VALIDATION ERROR\nfunc_name: {}\nReturn response: {}\nReturn schema: {}\n".format(
           name, product, name
         ))
         result = make_error(Errors.SERVER_ERR)
     else:
       logger.info("INPUT ERROR\nfunc_name: {}\nReturn response: {}".format(
         name, product
       ))
       result = product
   except Exception as e: # catch generic exceptions
     logger.info("GENERIC ERROR\nfunc_name: {}\nrequest: {}\nexception: {}\n".format(
       name, request, e
     ))
     result = make_error(Errors.SERVER_ERR)
   
   logger.info("RESULT\n{}\n".format(result))
   return result
Esempio n. 2
0
 def helper(self, **kwargs):
     board_id = kwargs["board_id"]
     if self.gm.boards.find_one({"short_id": board_id}):
         return func(self, **kwargs)
     else:
         return make_error(Errors.DNE_BOARD,
                           error_meta={"board_id": board_id})
Esempio n. 3
0
async def on_create(sid, request):
    """
    :return: :ref:`bres_created-label`
    :errors: BAD_RESPONSE 
    """
    product = gm.board_manager.create()

    if not is_error(product):
      try:
        validate(instance=product, schema=bres.created)
        serialized = dumps(product, cls=DictEncoder)
      except ValidationError:
        serialized = make_error(Errors.BAD_RESPONSE)
    else:
      serialized = make_error(product)
    return serialized
Esempio n. 4
0
 async def helper(self, sid, request):
   try:
     session = await self.get_session(sid)
     discussion_id = session["discussion_id"]
     user_id = session["user_id"]
   except Exception:
       return make_error(Errors.INVALID_USER_SESSION)
   return await func(self, sid, request)
Esempio n. 5
0
        async def helper(self, sid, request):
          try:
            result = None
            logger.info("REQUEST (sid: {})\nfunc_name: {}\nrequest: {}\n".format(
              sid, name, request
            ))
            product = await func(self, sid, request)
            logger.info("func_name: {}\nproduct: {}\nrequest: {}\n".format(
              name, product, request
            ))
            if not is_error(product):
              try:
                validate(instance=product, schema=dres.schema[name])
                result = dumps(product, cls=DictEncoder)
                if emit: # send result to others in room
                  session = await self.get_session(sid)
                  logger.info("Emitting (sid: {}) with {}".format(sid, session))
                  if "board_id" in session and "discussion_id" in session:
                    board_id = session["board_id"]
                    discussion_id = session["discussion_id"]
                    logger.info("Emitting to {}/{}.".format(board_id, discussion_id))
                    await self.emit(
                      name, 
                      result, 
                      room=get_room(board_id, discussion_id), 
                      skip_sid=sid
                    )
              except ValidationError:
                logger.info("Return response: {}\nReturn schema: {}".format(
                  product, name
                ))
                result = make_error(Errors.SERVER_ERR)
            else:
              logger.info("INPUT ERROR\nfunc_name: {}\nReturn response: {}".format(
                name, product
              ))
              result = product
          except Exception as e: # catch generic exceptions
            logger.info("func_name: {}\nrequest: {}\nexception: {}\n".format(
              name, request, e
            ))
            result = make_error(Errors.SERVER_ERR)

          return result
Esempio n. 6
0
 def helper(self, **kwargs):
     board_id = kwargs["board_id"]
     link_id = kwargs["link_id"]
     if self.gm.links.find_one({
             "short_id": link_id,
             "board_id": board_id
     }):
         return func(self, **kwargs)
     else:
         return make_error(Errors.DNE_LINK,
                           error_meta={"link_id": link_id})
Esempio n. 7
0
 def helper(self, **kwargs):
     board_id = kwargs["board_id"]
     user_id = kwargs["user_id"]
     if self.gm.users.find_one({
             "short_id": user_id,
             "board_id": board_id
     }):
         return func(self, **kwargs)
     else:
         return make_error(Errors.DNE_USER,
                           error_meta={"user_id": user_id})
Esempio n. 8
0
 def helper(self, **kwargs):
     board_id = kwargs["board_id"]
     discussion_id = kwargs["discussion_id"]
     if self.gm.discussions.find_one({
             "short_id": discussion_id,
             "board_id": board_id
     }):
         return func(self, **kwargs)
     else:
         return make_error(Errors.DNE_DISCUSSION,
                           error_meta={"discussion_id": discussion_id})
Esempio n. 9
0
 def helper(self, **kwargs):
     board_id = kwargs["board_id"]
     for p in Checker.unit_input:
         if p in kwargs:
             unit_id = kwargs[p]
             if self.gm.units.find_one({
                     "short_id": unit_id,
                     "board_id": board_id
             }):
                 return func(self, **kwargs)
             else:
                 return make_error(Errors.DNE_UNIT,
                                   error_meta={"unit_id": unit_id})
Esempio n. 10
0
    def add_focused(self, board_id, discussion_id, unit_id):
      unit = self.gm.units.find_one({"short_id": unit_id, "board_id": board_id})
      if unit["chat"] is True:
        return make_error(Errors.NOT_BOARD, 
          error_meta={"unit_id": unit_id}
        )

      discussion = self.gm.discussions.find_one({"short_id": discussion_id, "board_id": board_id})
      if unit["short_id"] not in discussion["focused"]:
        self.gm.discussions.update_one(
            {"short_id" : discussion_id, "board_id": board_id},
            {"$addToSet": {"focused": unit_id}}
        )
        return {"unit": self.gm._get_basic_unit(board_id, unit_id)}
      else:
        return {}
Esempio n. 11
0
    def add_pinned(self, board_id, discussion_id, unit_id, user_id):
      unit = self.gm.units.find_one({"short_id": unit_id, "board_id": board_id})
      if unit["chat"] is False:
        return make_error(Errors.NOT_CHAT, 
          error_meta={"unit_id": unit_id}
        )

      discussion = self.gm.discussions.find_one({"short_id": discussion_id, "board_id": board_id})
      if unit["short_id"] not in discussion["pinned"]:
        self.gm.discussions.update_one(
            {"short_id" : discussion_id, "board_id": board_id},
            {"$addToSet": {"pinned": unit_id}}
        )

        message = "pinned \"{}\"".format(unit["pith"])
        notice_unit = self.gm.create_notice_unit(board_id, discussion_id, message, user_id)

        return {
          "unit": self.gm._get_chat_unit(board_id, unit_id),
          "notice_unit": self.gm._get_chat_unit(board_id, notice_unit.short_id)
        }
      else:
        return {}
Esempio n. 12
0
 async def helper(self, sid, request):
   try:
     validate(instance=request, schema=breq.schema[name])
     return await func(self, sid, request)
   except ValidationError:
     return make_error(Errors.BAD_REQUEST)
Esempio n. 13
0
        async def helper(self, sid, request):
          try:
            result = None
            product = await func(self, sid, request)
            logger.info("func_name: {}\nproduct: {}\nrequest: {}\n".format(
              name, product, request
            ))

            if is_error(product):
              result = make_error(product, info={
                "func_name": name, "result": result, "request": request
              })
            else:
              ret_res, emits_res = product

              bad_response = False

              if ret is not None:
                try:
                  validate(instance=ret_res, schema=dres.schema[ret])
                  result = ret_res
                except ValidationError:
                  logger.info("Return response: {}\nReturn schema: {}".format(ret_res, ret))
                  bad_response = True

              if emits is not None:
                assert(emits_res is not None)
                for r, e in zip(emits_res, emits):
                  try:
                    validate(instance=r, schema=dres.schema[e])
                  except ValidationError:
                    logger.info("Emit response: {}\nEmit schema: {}".format(r, e))
                    bad_response = True

              if bad_response: # we cannot send off emits
                result = make_error(Errors.BAD_RESPONSE)
              else: # we can send off emits

                shared = {}
                if emits is not None:
                  assert(emits_res is not None)
                  for r, e in zip(emits_res, emits):
                    shared[e] = r

                if result is None:
                  result = {}
                # set emitted data to return
                result["shared"] = shared

                result = dumps(result, cls=DictEncoder) # default returns

                # every function except maybe leave should have a discussion id
                session = await self.get_session(sid)
                # send to everyone else
                if "discussion_id" in session:
                  discussion_id = session["discussion_id"]
                  emit_shared = dumps(shared, cls=DictEncoder)
                  await self.emit(name, emit_shared, room=discussion_id, skip_sid=sid)

            return result

          except Exception as e:
            logger.info("func_name: {}\nrequest: {}\nexception: {}\n".format(
              name, request, e
            ))