Exemplo n.º 1
0
 def _get_all_posts_and_tags(self):
     find_resp = self._db.TaggedPosts(database_pb2.PostsRequest())
     if find_resp.result_type == database_pb2.PostsResponse.ERROR:
         self._logger.error(
             'Error getting TaggedPosts for Cosine: {}'.format(find_resp.error))
         return []
     return self._clean_post_entries(find_resp.results)
Exemplo n.º 2
0
 def find_post(self, user, author_id=None):
     req = database_pb2.PostsRequest(
         request_type=database_pb2.RequestType.FIND)
     if author_id is not None:
         req.match.author_id = author_id
     req.user_global_id.value = user
     res = self.posts.Posts(req, self.ctx)
     self.assertNotEqual(res.result_type, general_pb2.ResultType.ERROR)
     return res
Exemplo n.º 3
0
 def _add_ap_id(self, global_id, ap_id):
     req = database_pb2.PostsRequest(
         request_type=database_pb2.RequestType.UPDATE,
         match=database_pb2.PostsEntry(global_id=global_id, ),
         entry=database_pb2.PostsEntry(ap_id=ap_id, ),
     )
     resp = self._db_stub.Posts(req)
     if resp.result_type != general_pb2.ResultType.OK:
         return "Error inserting ap_id into DB: " + str(resp.error)
     return None
Exemplo n.º 4
0
 def _get_article(self, article_id):
     req = database_pb2.PostsRequest(
         request_type=database_pb2.PostsRequest.FIND,
         match=database_pb2.PostsEntry(global_id=article_id, ),
     )
     self._logger.info("Sending request to DB for article %d", article_id)
     resp = self._db.Posts(req)
     if resp.result_type == database_pb2.PostsResponse.ERROR:
         return None, resp.error
     elif len(resp.results) != 1:
         return None, "Expected 1 result, got " + str(len(resp.results))
     return resp.results[0], None
Exemplo n.º 5
0
 def _get_article(self, article_id):
     posts_req = dbpb.PostsRequest(
         request_type=dbpb.RequestType.FIND,
         match=dbpb.PostsEntry(global_id=article_id, ),
     )
     find_resp = self._db.Posts(posts_req)
     if find_resp.result_type != general_pb2.ResultType.OK:
         raise SendUndoException(find_resp.error)
     elif len(find_resp.results) != 1:
         raise SendUndoException("Expecting 1 result, got {}".format(
             len(find_resp.results)))
     return find_resp.results[0]
Exemplo n.º 6
0
 def _get_shared_article(self, article_id):
     posts_req = database_pb2.PostsRequest(
         request_type=database_pb2.RequestType.FIND,
         match=database_pb2.PostsEntry(global_id=article_id, ),
     )
     resp = self._db.Posts(posts_req)
     if resp.result_type != general_pb2.ResultType.OK:
         return None, resp.error
     elif len(resp.results) > 1:
         return None, "Recieved too many results from DB"
     elif len(resp.results) == 0:
         return None, "No matching DB entry for this article"
     return resp.results[0], None
Exemplo n.º 7
0
    def add_post(self, author_id=None, title=None, body=None):
        post_entry = database_pb2.PostsEntry(
            author_id=author_id,
            title=title,
            body=body,
        )

        req = database_pb2.PostsRequest(
            request_type=database_pb2.RequestType.INSERT,
            entry=post_entry,
        )

        add_res = self.posts.Posts(req, self.ctx)
        self.assertNotEqual(add_res.result_type, general_pb2.ResultType.ERROR)
        return add_res
Exemplo n.º 8
0
 def get_article_by_ap_id(self, obj_id):
     posts_req = database_pb2.PostsRequest(
         request_type=database_pb2.PostsRequest.FIND,
         match=database_pb2.PostsEntry(ap_id=obj_id, ),
     )
     resp = self._db.Posts(posts_req)
     if resp.result_type != database_pb2.PostsResponse.OK:
         return None, resp.error
     elif len(resp.results) > 1:
         return None, "Recieved too many results from DB"
     elif len(resp.results) == 0:
         # NOTE: This can happen natually.
         # [email protected] follows [email protected].
         # b.org sends a Like for an article by ross that already existed.
         # a.com didn't get the original Create so it can't find it.
         return None, "No matching DB entry for this article"
     return resp.results[0], None
Exemplo n.º 9
0
 def _update_locally(self, article, req):
     self._logger.info("Sending update request to DB")
     html_body = md_to_html(self._md, req.body)
     resp = self._db.Posts(dbpb.PostsRequest(
         request_type=dbpb.RequestType.UPDATE,
         match=dbpb.PostsEntry(global_id=article.global_id),
         entry=dbpb.PostsEntry(
             title=req.title,
             body=html_body,
             md_body=req.body,
             tags=convert_to_tags_string(req.tags),
             summary=req.summary,
         ),
     ))
     if resp.result_type != general_pb2.ResultType.OK:
         self._logger.error("Could not update article: %s", resp.error)
         return False
     return True
Exemplo n.º 10
0
def get_article(logger, db, global_id=None, ap_id=None):
    """
    Retrieve a single PostEntry from the database.
    Returns None on error.
    """
    logger.info("Getting article global_id: %s, ap_id: %s", global_id, ap_id)
    resp = db.Posts(
        database_pb2.PostsRequest(request_type=database_pb2.RequestType.FIND,
                                  match=database_pb2.PostsEntry(
                                      global_id=global_id,
                                      ap_id=ap_id,
                                  )))
    if resp.result_type != general_pb2.ResultType.OK:
        logger.error("Error getting article: %s", resp.error)
        return None
    elif len(resp.results) == 0:
        logger.error("Could not find article")
        return None
    return resp.results[0]
Exemplo n.º 11
0
 def ReceiveUpdateActivity(self, req, ctx):
     self._logger.info("Received edit for article '%s'", req.title)
     html_body = md_to_html(self._md, req.body)
     resp = self._db.Posts(
         dbpb.PostsRequest(
             request_type=dbpb.PostsRequest.UPDATE,
             match=dbpb.PostsEntry(ap_id=req.ap_id),
             entry=dbpb.PostsEntry(
                 title=req.title,
                 body=html_body,
                 md_body=req.body,
                 summary=req.summary,
             ),
         ))
     if resp.result_type != dbpb.PostsResponse.OK:
         self._logger.error("Could not update article: %s", resp.error)
         return upb.UpdateResponse(
             result_type=upb.UpdateRespones.ERROR,
             error="Error updating article in DB",
         )
     return upb.UpdateResponse(result_type=upb.UpdateResponse.OK)
Exemplo n.º 12
0
    def send_insert_request(self, req):
        global_id = req.author_id
        author = self._users_util.get_user_from_db(global_id=global_id)
        if author is None:
            self._logger.error(
                'Could not find user id in db: ' + str(global_id))
            return database_pb2.PostsResponse.error, None
        global_id = author.global_id

        html_body = md_to_html(self._md_stub, req.body)
        tags_string = convert_to_tags_string(req.tags)
        pe = database_pb2.PostsEntry(
            author_id=global_id,
            title=req.title,
            body=html_body,
            md_body=req.body,
            creation_datetime=req.creation_datetime,
            ap_id=req.ap_id,
            tags=tags_string,
            summary=req.summary,
        )
        pr = database_pb2.PostsRequest(
            request_type=database_pb2.PostsRequest.INSERT,
            entry=pe
        )
        posts_resp = self._db_stub.Posts(pr)
        if posts_resp.result_type == database_pb2.PostsResponse.ERROR:
            self._logger.error(
                'Could not insert into db: %s', posts_resp.error)

        pe.global_id = posts_resp.global_id
        self.index(pe)

        # If post_recommender is on, send new post to post_recommender
        if self._post_recommendation_stub is not None:
            self._add_post_to_recommender(pe)

        return posts_resp.result_type, posts_resp.global_id