terms.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. class TermsStore(object):
  16. def __init__(self, sydent):
  17. self.sydent = sydent
  18. def getAgreedUrls(self, user_id):
  19. """
  20. Retrieves the URLs of the terms the given user has agreed to.
  21. :param user_id: Matrix user ID to fetch the URLs for.
  22. :type user_id: str
  23. :return: A list of the URLs of the terms accepted by the user.
  24. :rtype: list[unicode]
  25. """
  26. cur = self.sydent.db.cursor()
  27. res = cur.execute(
  28. "select url from accepted_terms_urls "
  29. "where user_id = ?", (user_id,),
  30. )
  31. urls = []
  32. for url, in res:
  33. # Ensure we're dealing with unicode.
  34. if url and isinstance(url, bytes):
  35. url = url.decode("UTF-8")
  36. urls.append(url)
  37. return urls
  38. def addAgreedUrls(self, user_id, urls):
  39. """
  40. Saves that the given user has accepted the terms at the given URLs.
  41. :param user_id: The Matrix user ID that has accepted the terms.
  42. :type user_id: str
  43. :param urls: The list of URLs.
  44. :type urls: list[unicode]
  45. """
  46. cur = self.sydent.db.cursor()
  47. res = cur.executemany(
  48. "insert or ignore into accepted_terms_urls (user_id, url) values (?, ?)",
  49. ((user_id, u) for u in urls),
  50. )
  51. self.sydent.db.commit()