1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
==========
Membership
==========
The REST API can be used to subscribe and unsubscribe users to mailing lists.
A subscribed user is called a 'member'. There is a top level collection that
returns all the members of all known mailing lists.
There are no mailing lists and no members yet.
>>> dump_json('http://localhost:8001/3.0/members')
resource_type_link: http://localhost:8001/3.0/#members
start: None
total_size: 0
We create a mailing list, which starts out with no members.
>>> mlist_one = create_list('test-one@example.com')
>>> transaction.commit()
>>> dump_json('http://localhost:8001/3.0/members')
resource_type_link: http://localhost:8001/3.0/#members
start: None
total_size: 0
Subscribers
===========
After Bart subscribes to the mailing list, his subscription is available via
the REST interface.
>>> from mailman.interfaces.member import MemberRole
>>> from mailman.interfaces.usermanager import IUserManager
>>> from zope.component import getUtility
>>> user_manager = getUtility(IUserManager)
>>> def subscribe(mlist, first_name, role=MemberRole.member):
... address = '{0}person@example.com'.format(first_name[0].lower())
... full_name = '{0} Person'.format(first_name)
... person = user_manager.get_user(address)
... if person is None:
... person = user_manager.create_user(address, full_name)
... preferred_address = list(person.addresses)[0]
... preferred_address.subscribe(mlist, role)
... transaction.commit()
>>> subscribe(mlist_one, 'Bart')
>>> dump_json('http://localhost:8001/3.0/members')
entry 0:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/test-one@example.com/member/bperson@example.com
resource_type_link: http://localhost:8001/3.0/#members
start: 0
total_size: 1
When Cris also joins the mailing list, her subscription is also available via
the REST interface.
>>> subscribe(mlist_one, 'Cris')
>>> dump_json('http://localhost:8001/3.0/members')
entry 0:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/test-one@example.com/member/bperson@example.com
entry 1:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/test-one@example.com/member/cperson@example.com
resource_type_link: http://localhost:8001/3.0/#members
start: 0
total_size: 2
The subscribed members are returned in alphabetical order, so when Anna
subscribes, she is returned first.
>>> subscribe(mlist_one, 'Anna')
>>> dump_json('http://localhost:8001/3.0/members')
entry 0:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/test-one@example.com/member/aperson@example.com
entry 1:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/test-one@example.com/member/bperson@example.com
entry 2:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/test-one@example.com/member/cperson@example.com
resource_type_link: http://localhost:8001/3.0/#members
start: 0
total_size: 3
Subscriptions are also returned alphabetically by mailing list posting
address. Anna and Cris subscribe to this new mailing list.
>>> mlist_two = create_list('alpha@example.com')
>>> subscribe(mlist_two, 'Anna')
>>> subscribe(mlist_two, 'Cris')
>>> dump_json('http://localhost:8001/3.0/members')
entry 0:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/alpha@example.com/member/aperson@example.com
entry 1:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/alpha@example.com/member/cperson@example.com
entry 2:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/test-one@example.com/member/aperson@example.com
entry 3:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/test-one@example.com/member/bperson@example.com
entry 4:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/test-one@example.com/member/cperson@example.com
resource_type_link: http://localhost:8001/3.0/#members
start: 0
total_size: 5
Owners and moderators
=====================
Mailing list owners and moderators also show up in the REST API. Cris becomes
an owner of the alpha mailing list and Dave becomes a moderator of the
test-one mailing list.
>>> subscribe(mlist_one, 'Cris', MemberRole.owner)
>>> subscribe(mlist_two, 'Dave', MemberRole.moderator)
>>> dump_json('http://localhost:8001/3.0/members')
entry 0:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/alpha@example.com/moderator/dperson@example.com
entry 1:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/alpha@example.com/member/aperson@example.com
entry 2:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/alpha@example.com/member/cperson@example.com
entry 3:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/test-one@example.com/owner/cperson@example.com
entry 4:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/test-one@example.com/member/aperson@example.com
entry 5:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/test-one@example.com/member/bperson@example.com
entry 6:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/test-one@example.com/member/cperson@example.com
resource_type_link: http://localhost:8001/3.0/#members
start: 0
total_size: 7
Joining a mailing list
======================
A user can be subscribed to a mailing list via the REST API. Actually,
addresses not users are subscribed to mailing lists, but addresses are always
tied to users. A subscribed user is called a member.
Elly subscribes to the alpha mailing list. By default, get gets a regular
delivery. Since Elly's email address is not yet known to Mailman, a user is
created for her.
>>> dump_json('http://localhost:8001/3.0/members', {
... 'ws.op': 'join',
... 'fqdn_listname': 'alpha@example.com',
... 'address': 'eperson@example.com',
... 'real_name': 'Elly Person',
... })
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/alpha@example.com/member/eperson@example.com
Elly is now a member of the mailing list.
>>> elly = user_manager.get_user('eperson@example.com')
>>> elly
<User "Elly Person" at ...>
>>> set(member.mailing_list for member in elly.memberships.members)
set([u'alpha@example.com'])
>>> dump_json('http://localhost:8001/3.0/members')
entry 0:
...
entry 3:
http_etag: ...
resource_type_link: http://localhost:8001/3.0/#member
self_link: http://localhost:8001/3.0/lists/alpha@example.com/member/eperson@example.com
...
Leaving a mailing list
======================
Elly decides she does not want to be a member of the mailing list after all,
so she unsubscribes from the mailing list.
# Ensure our previous reads don't keep the database lock.
>>> transaction.abort()
>>> dump_json('http://localhost:8001/3.0/members', {
... 'ws.op': 'leave',
... 'fqdn_listname': 'alpha@example.com',
... 'address': 'eperson@example.com',
... })
Elly is no longer a member of the mailing list.
>>> set(member.mailing_list for member in elly.memberships.members)
set([])
Corner cases
============
For some reason Elly tries to join a mailing list that does not exist.
>>> dump_json('http://localhost:8001/3.0/members', {
... 'ws.op': 'join',
... 'fqdn_listname': 'beta@example.com',
... 'address': 'eperson@example.com',
... 'real_name': 'Elly Person',
... })
Traceback (most recent call last):
...
HTTPError: HTTP Error 400: Bad Request
Then, she tries to leave a mailing list that does not exist.
>>> dump_json('http://localhost:8001/3.0/members', {
... 'ws.op': 'leave',
... 'fqdn_listname': 'beta@example.com',
... 'address': 'eperson@example.com',
... 'real_name': 'Elly Person',
... })
Traceback (most recent call last):
...
HTTPError: HTTP Error 400: Bad Request
For some reason, Elly tries to leave the mailing list again, but she's already
been unsubscribed.
>>> dump_json('http://localhost:8001/3.0/members', {
... 'ws.op': 'leave',
... 'fqdn_listname': 'alpha@example.com',
... 'address': 'eperson@example.com',
... })
Traceback (most recent call last):
...
HTTPError: HTTP Error 400: Bad Request
|