Mercurial > libervia-backend
diff sat/tools/common/data_objects.py @ 3310:e4121a1f2399
tools (common/data_objects): removed deprecated code:
`BlogItem`, `BlogItems` and `parsePubSubMetadata` are not needed anymore with the new blog
data serialisation.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 16 Jul 2020 09:07:26 +0200 |
parents | 9f0e28137cd0 |
children | be6d91572633 |
line wrap: on
line diff
--- a/sat/tools/common/data_objects.py Thu Jul 16 09:07:26 2020 +0200 +++ b/sat/tools/common/data_objects.py Thu Jul 16 09:07:26 2020 +0200 @@ -1,7 +1,6 @@ #!/usr/bin/env python3 - -# SAT: a jabber client +# SàT: an XMPP client # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) # This program is free software: you can redistribute it and/or modify @@ -34,251 +33,6 @@ q = lambda value: urllib.parse.quote(value.encode("utf-8"), safe="@") -def parsePubSubMetadata(metadata, items): - """Helper method to have nicer metadata while doing a PubSub request - - The "rsm_index" and "rsm_count" keys will be converted to int - The "mam_stable" and "mam_complete" keys will be converted to boolean - A "complete" key can have the following values: - - True: we are on the last page, i.e. last items have been received - - False: we are not on the last page - - None: we don't have enough data fo know our position in the pagination - @param metadata(dict): dict retrieved fro bridge with request metadata - @param items(list): items retrieved (used to determine "complete" value) - @return (dict): parsed metadata - """ - data = {} - assert "complete" not in metadata - - for key, value in metadata.items(): - if key in ("rsm_index", "rsm_count"): - value = int(value) - elif key == "mam_stable": - value = C.bool(value) - elif key == "mam_complete": - key = "complete" - value = C.bool(value) - data[key] = value - if "complete" not in data: - index = data.get("rsm_index") - count = data.get("rsm_count") - if index is None or count is None: - # we don't have enough information to know if the data is complete or not - data["complete"] = None - else: - # normally we have a strict equality here but XEP-0059 states - # that index MAY be approximative, so just in case… - data["complete"] = index + len(items) >= count - return data - - -class BlogItem(object): - def __init__(self, mb_data, parent, deserialise=True): - """ - @param deserialise(bool): if True, mb_data is a serialised string which must - be deserialised - """ - # FIXME: deserialise should not be done here, but before BlogItem is called - if deserialise: - self.mb_data = data_format.deserialise(mb_data) - else: - self.mb_data = mb_data - self.parent = parent - self._comments = None - self._comments_items_list = None - - def json(self): - ret = {} - for attr in ( - "id", "atom_id", "uri", "published", "updated", "language", "author", - "author_jid", "author_jid_verified", "author_email", "tags", "groups", - "title", "title_xhtml", "content", "content_xhtml", "comments", - "comments_service", "comments_node", "comments_items_list" - ): - value = getattr(self, attr) - if value is not None: - ret[attr] = value - return ret - - @property - def id(self): - return self.mb_data.get("id") - - @property - def atom_id(self): - return self.mb_data.get("atom_id") - - @property - def uri(self): - node = self.parent.node - service = self.parent.service - return xmpp_uri.buildXMPPUri( - "pubsub", subtype="microblog", path=service, node=node, item=self.id - ) - - @property - def published(self): - return self.mb_data.get("published") - - @property - def updated(self): - return self.mb_data.get("updated") - - @property - def language(self): - return self.mb_data.get("language") - - @property - def author(self): - return self.mb_data.get("author") - - @property - def author_jid(self): - return self.mb_data.get("author_jid") - - @property - def author_jid_verified(self): - return self.mb_data.get("author_jid_verified") - - @property - def author_email(self): - return self.mb_data.get("author_email") - - @property - def tags(self): - return self.mb_data.get('tags', []) - - @property - def groups(self): - return self.mb_data.get('groups', []) - - @property - def title(self): - return self.mb_data.get("title") - - @property - def title_xhtml(self): - try: - return safe(self.mb_data["title_xhtml"]) - except KeyError: - return None - - @property - def content(self): - return self.mb_data.get("content") - - @property - def content_xhtml(self): - try: - return safe(self.mb_data["content_xhtml"]) - except KeyError: - return None - - @property - def comments(self): - if self._comments is None: - self._comments = data_format.dict2iterdict( - "comments", self.mb_data, ("node", "service") - ) - return self._comments - - @property - def comments_service(self): - return self.mb_data.get("comments_service") - - @property - def comments_node(self): - return self.mb_data.get("comments_node") - - @property - def comments_items_list(self): - return [] if self._comments_items_list is None else self._comments_items_list - - def appendCommentsItems(self, items): - """append comments items to self.comments_items""" - if self._comments_items_list is None: - self._comments_items_list = [] - self._comments_items_list.append(items) - - -class BlogItems(object): - def __init__(self, mb_data, deserialise=True): - """ - @param deserialise(bool): if True, mb_data is a serialised string which must - be deserialised - """ - # FIXME: deserialise should not be done here, but before BlogItem is called - self.items = [BlogItem(i, self, deserialise=deserialise) for i in mb_data[0]] - self.metadata = parsePubSubMetadata(mb_data[1], self.items) - - def json(self): - ret = { - "items": [i.json() for i in self.items], - } - for attr in ("service", "node", "uri", "with_rsm"): - ret[attr] = getattr(self, attr) - if self.with_rsm: - for attr in ("rsm_first", "rsm_last", "rsm_index", "rsm_count", "complete"): - ret[attr] = getattr(self, attr) - - return ret - - @property - def service(self): - return self.metadata["service"] - - @property - def node(self): - return self.metadata["node"] - - @property - def uri(self): - return self.metadata["uri"] - - @property - def with_rsm(self): - """Return True if RSM is activated on this request""" - return "rsm_first" in self.metadata - - @property - def rsm_first(self): - return self.metadata["rsm_first"] - - @property - def rsm_last(self): - return self.metadata["rsm_last"] - - @property - def rsm_index(self): - return self.metadata["rsm_index"] - - @property - def rsm_count(self): - return self.metadata["rsm_count"] - - @property - def complete(self): - return self.metadata["complete"] - - def __len__(self): - return self.items.__len__() - - def __missing__(self, key): - return self.items.__missing__(key) - - def __getitem__(self, key): - return self.items.__getitem__(key) - - def __iter__(self): - return self.items.__iter__() - - def __reversed__(self): - return self.items.__reversed__() - - def __contains__(self, item): - return self.items.__contains__(item) - - class Message(object): def __init__(self, msg_data): self._uid = msg_data[0]