summaryrefslogtreecommitdiff
path: root/pgcommitfest/commitfest/ajax.py
blob: b8317f5087b0d92e2d806cfae8d68c0450d06889 (plain)
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
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, Http404
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.db import transaction

import requests
import json

from pgcommitfest.auth import user_search

class HttpResponseServiceUnavailable(HttpResponse): 
    status_code = 503

class Http503(Exception):
    pass

from models import CommitFest, Patch, MailThread, MailThreadAttachment
from models import MailThreadAnnotation, PatchHistory

def _archivesAPI(suburl, params=None):
    try:
        resp = requests.get("http{0}://{1}:{2}{3}".format(settings.ARCHIVES_PORT == 443 and 's' or '',
                                                          settings.ARCHIVES_SERVER,
                                                          settings.ARCHIVES_PORT,
                                                          suburl),
                            params=params,
                            headers={
                                'Host': settings.ARCHIVES_HOST,
                            },
                            timeout=settings.ARCHIVES_TIMEOUT,
        )
        if resp.status_code != 200:
            if resp.status_code == 404:
                raise Http404()
            raise Exception("JSON call failed: %s" % resp.status_code)

        return resp.json()
    except Http404:
        raise
    except Exception as e:
        raise Http503("Failed to communicate with archives backend: %s" % e)

def getThreads(request):
    search = request.GET.has_key('s') and request.GET['s'] or None
    if request.GET.has_key('a') and request.GET['a'] == "1":
        attachonly = 1
    else:
        attachonly = 0

    # Make a JSON api call to the archives server
    params = {'n': 100, 'a': attachonly}
    if search:
        params['s'] = search

    r = _archivesAPI('/list/pgsql-hackers/latest.json', params)
    return sorted(r, key=lambda x: x['date'], reverse=True)

def getMessages(request):
    threadid = request.GET['t']

    thread = MailThread.objects.get(pk=threadid)

    # Always make a call over to the archives api
    r = _archivesAPI('/message-id.json/%s' % thread.messageid)
    return sorted(r, key=lambda x: x['date'], reverse=True)

def refresh_single_thread(thread):
    r = sorted(_archivesAPI('/message-id.json/%s' % thread.messageid), key=lambda x: x['date'])
    if thread.latestmsgid != r[-1]['msgid']:
        # There is now a newer mail in the thread!
        thread.latestmsgid = r[-1]['msgid']
        thread.latestmessage = r[-1]['date']
        thread.latestauthor = r[-1]['from']
        thread.latestsubject = r[-1]['subj']
        thread.save()
        parse_and_add_attachments(r, thread)
        # Potentially update the last mail date - if there wasn't already a mail on each patch
        # from a *different* thread that had an earlier date.
        for p in thread.patches.filter(lastmail__lt=thread.latestmessage):
            p.lastmail = thread.latestmessage
            p.save()

@transaction.atomic
def annotateMessage(request):
    thread = get_object_or_404(MailThread, pk=int(request.POST['t']))
    msgid = request.POST['msgid']
    msg = request.POST['msg']

    # Get the subject, author and date from the archives
    # We only have an API call to get the whole thread right now, so
    # do that, and then find our entry in it.
    r = _archivesAPI('/message-id.json/%s' % thread.messageid)
    for m in r:
        if m['msgid'] == msgid:
            annotation = MailThreadAnnotation(mailthread=thread,
                                              user=request.user,
                                              msgid=msgid,
                                              annotationtext=msg,
                                              mailsubject=m['subj'],
                                              maildate=m['date'],
                                              mailauthor=m['from'])
            annotation.save()

            for p in thread.patches.all():
                PatchHistory(patch=p, by=request.user, what='Added annotation "%s" to %s' % (msg, msgid)).save_and_notify()
                p.set_modified()
                p.save()

            return 'OK'
    return 'Message not found in thread!'

@transaction.atomic
def deleteAnnotation(request):
    annotation = get_object_or_404(MailThreadAnnotation, pk=request.POST['id'])

    for p in annotation.mailthread.patches.all():
        PatchHistory(patch=p, by=request.user, what='Deleted annotation "%s" from %s' % (annotation.annotationtext, annotation.msgid)).save_and_notify()
        p.set_modified()
        p.save()

    annotation.delete()

    return 'OK'

def parse_and_add_attachments(threadinfo, mailthread):
    for t in threadinfo:
        if len(t['atts']):
            # One or more attachments. For now, we're only actually going
            # to store and process the first one, even though the API gets
            # us all of them.
            MailThreadAttachment.objects.get_or_create(mailthread=mailthread,
                                                       messageid=t['msgid'],
                                                       defaults={
                                                           'date': t['date'],
                                                           'author': t['from'],
                                                           'attachmentid': t['atts'][0]['id'],
                                                           'filename': t['atts'][0]['name'],
                                                       })
        # In theory we should remove objects if they don't have an
        # attachment, but how could that ever happen? Ignore for now.

@transaction.atomic
def attachThread(request):
    cf = get_object_or_404(CommitFest, pk=int(request.POST['cf']))
    patch = get_object_or_404(Patch, pk=int(request.POST['p']), commitfests=cf)
    msgid = request.POST['msg']

    return doAttachThread(cf, patch, msgid, request.user)

def doAttachThread(cf, patch, msgid, user):
    # Note! Must be called in an open transaction!
    r = sorted(_archivesAPI('/message-id.json/%s' % msgid), key=lambda x: x['date'])
    # We have the full thread metadata - using the first and last entry,
    # construct a new mailthread in our own model.
    # First, though, check if it's already there.
    threads = MailThread.objects.filter(messageid=r[0]['msgid'])
    if len(threads):
        thread = threads[0]
        if thread.patches.filter(id=patch.id).exists():
            return 'This thread is already added to this email'

        # We did not exist, so we'd better add ourselves.
        # While at it, we update the thread entry with the latest data from the
        # archives.
        thread.patches.add(patch)
        thread.latestmessage=r[-1]['date']
        thread.latestauthor=r[-1]['from']
        thread.latestsubject=r[-1]['subj']
        thread.latestmsgid=r[-1]['msgid']
        thread.save()
    else:
        # No existing thread existed, so create it
        # Now create a new mailthread entry
        m = MailThread(messageid=r[0]['msgid'],
                       subject=r[0]['subj'],
                       firstmessage=r[0]['date'],
                       firstauthor=r[0]['from'],
                       latestmessage=r[-1]['date'],
                       latestauthor=r[-1]['from'],
                       latestsubject=r[-1]['subj'],
                       latestmsgid=r[-1]['msgid'],
                       )
        m.save()
        m.patches.add(patch)
        m.save()
        parse_and_add_attachments(r, m)

    PatchHistory(patch=patch, by=user, what='Attached mail thread %s' % r[0]['msgid']).save_and_notify()
    patch.update_lastmail()
    patch.set_modified()
    patch.save()

    return 'OK'

@transaction.atomic
def detachThread(request):
    cf = get_object_or_404(CommitFest, pk=int(request.POST['cf']))
    patch = get_object_or_404(Patch, pk=int(request.POST['p']), commitfests=cf)
    thread = get_object_or_404(MailThread, messageid=request.POST['msg'])

    patch.mailthread_set.remove(thread)
    PatchHistory(patch=patch, by=request.user, what='Detached mail thread %s' % request.POST['msg']).save_and_notify()
    patch.update_lastmail()
    patch.set_modified()
    patch.save()

    return 'OK'

def searchUsers(request):
    if request.GET.has_key('s') and request.GET['s']:
        return user_search(request.GET['s'])
    else:
        return []

def importUser(request):
    if request.GET.has_key('u') and request.GET['u']:
        u = user_search(userid=request.GET['u'])
        if len (u) != 1:
            return "Internal error, duplicate user found"

        u = u[0]

        if User.objects.filter(username=u['u']).exists():
            return "User already exists"
        User(username=u['u'],
             first_name=u['f'],
             last_name=u['l'],
             email=u['e'],
             password='setbypluginnotsha1',
             ).save()
        return 'OK'
    else:
        raise Http404()

_ajax_map={
    'getThreads': getThreads,
    'getMessages': getMessages,
    'attachThread': attachThread,
    'detachThread': detachThread,
    'annotateMessage': annotateMessage,
    'deleteAnnotation': deleteAnnotation,
    'searchUsers': searchUsers,
    'importUser': importUser,
}

# Main entrypoint for /ajax/<command>/
@csrf_exempt
@login_required
def main(request, command):
    if not _ajax_map.has_key(command):
        raise Http404
    try:
        resp = HttpResponse(content_type='application/json')
        json.dump(_ajax_map[command](request), resp)
        return resp
    except Http503, e:
        return HttpResponseServiceUnavailable(e, content_type='text/plain')