]> git.ipfire.org Git - ipfire.org.git/blame - src/backend/lists.py
lists: Implement some basic code to talk to Mailman
[ipfire.org.git] / src / backend / lists.py
CommitLineData
97e15cf6
MT
1#!/usr/bin/python3
2
3import json
4import urllib.parse
5
6from . import accounts
7from . import misc
8
9class Lists(misc.Object):
10 @property
11 def url(self):
12 """
13 Returns the base URL of a Mailman instance
14 """
15 return self.settings.get("mailman-url")
16
17 @property
18 def username(self):
19 return self.settings.get("mailman-username")
20
21 @property
22 def password(self):
23 return self.settings.get("mailman-password")
24
25 async def _request(self, method, url, data=None):
26 headers, body = {}, None
27
28 # URL
29 url = urllib.parse.urljoin(self.url, url)
30
31 # For GET requests, append query arguments
32 if method == "GET":
33 if data:
34 url = "%s?%s" % (url, urllib.parse.urlencode(data))
35
36 # For POST/PUT encode all arguments as JSON
37 elif method in ("POST", "PUT", "PATCH"):
38 headers |= {
39 "Content-Type" : "application/json",
40 }
41
42 body = json.dumps(data)
43
44 # Send the request and wait for a response
45 res = await self.backend.http_client.fetch(url, method=method,
46 headers=headers, body=body,
47
48 # Authentication
49 auth_username=self.username, auth_password=self.password,
50 )
51
52 # Decode JSON response
53 body = json.loads(res.body)
54
55 # XXX handle errors
56
57 return body
58
59 async def _get_lists(self, *args, **kwargs):
60 lists = []
61
62 # Fetch the response
63 response = await self._request(*args, **kwargs)
64
65 # Fetch entries
66 for entry in response.get("entries", []):
67 list = List(self.backend, **entry)
68 lists.append(list)
69
70 return lists
71
72 async def get_lists(self):
73 """
74 Fetches all available lists
75 """
76 data = {
77 "advertised" : True,
78 }
79
80 return await self._get_lists("GET", "/3.1/lists", data=data)
81
82 async def get_subscribed_lists(self, account):
83 data = {
84 "subscriber" : account.email,
85 "role" : "member",
86 }
87
88 return await self._get_lists("GET", "/3.1/members/find", data=data)
89
90
91class List(misc.Object):
92 def init(self, list_id, **kwargs):
93 self.list_id = list_id
94
95 # Store all other data
96 self.data = kwargs
97
98 def __repr__(self):
99 return "<List %s>" % self.list_id
100
101 def __str__(self):
102 return self.display_name
103
104 def __eq__(self, other):
105 if isinstance(other, self.__class__):
106 return self.list_id == other.list_id
107
108 return NotImplemented
109
110 def __lt__(self, other):
111 if isinstance(other, self.__class__):
112 return self.list_id < other.list_id
113
114 return NotImplemented
115
116 def __len__(self):
117 return self.data.get("member_count")
118
119 @property
120 def display_name(self):
121 return self.data.get("display_name")
122
123 @property
124 def description(self):
125 return self.data.get("description")
126
127 async def subscribe(self, account):
128 pass # XXX TODO
129
130 async def unsubscribe(self, account):
131 pass # XXX TODO