from django.conf import settings
from views import cache
-from models import Message, List
+from models import Message, List, ApiClient, ThreadSubscription
import json
for m in mlist], resp)
resp['X-pgthread'] = m.threadid
return resp
+
+def thread_subscribe(request, msgid):
+ if not settings.PUBLIC_ARCHIVES:
+ return HttpResponseForbidden('No API access on private archives for now')
+
+ if not request.META['REMOTE_ADDR'] in settings.API_CLIENTS:
+ return HttpResponseForbidden('Invalid host')
+
+ if not request.META.has_key('HTTP_X_APIKEY'):
+ return HttpResponseForbidden('No API key')
+
+ if request.method != 'PUT':
+ return HttpResponseForbidden('Invalid HTTP verb')
+
+ apiclient = get_object_or_404(ApiClient, apikey=request.META['HTTP_X_APIKEY'])
+ msg = get_object_or_404(Message, messageid=msgid)
+
+ (obj, created) = ThreadSubscription.objects.get_or_create(apiclient=apiclient,
+ threadid=msg.threadid)
+ if created:
+ return HttpResponse(status=201)
+ else:
+ return HttpResponse(status=200)
class Meta:
unique_together = (('list', 'username'), )
db_table = 'listsubscribers'
+
+class ApiClient(models.Model):
+ apikey = models.CharField(max_length=100, null=False, blank=False)
+ postback = models.URLField(max_length=500, null=False, blank=False)
+
+ class Meta:
+ db_table = 'apiclients'
+
+class ThreadSubscription(models.Model):
+ apiclient = models.ForeignKey(ApiClient, null=False, blank=False)
+ threadid = models.IntegerField(null=False, blank=False)
+
+ class Meta:
+ db_table = 'threadsubscriptions'
+ unique_together = (('apiclient', 'threadid'),)
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.contrib.messages.context_processors.messages',
],
+ 'loaders': [
+ 'django.template.loaders.filesystem.Loader',
+ 'django.template.loaders.app_directories.Loader',
+ ],
},
}]
);
CREATE INDEX idx_attachments_msg ON attachments(message);
+CREATE TABLE apiclients(
+ id SERIAL NOT NULL PRIMARY KEY,
+ apikey varchar(100) NOT NULL,
+ postback varchar(500) NOT NULL
+);
+
+CREATE TABLE threadsubscriptions(
+ id SERIAL NOT NULL PRIMARY KEY,
+ apiclient_id integer NOT NULL REFERENCES apiclients(id),
+ threadid integer NOT NULL
+);
+
+CREATE TABLE threadnotifications(
+ apiclient_id integer NOT NULL REFERENCES apiclients(id),
+ threadid integer NOT NULL,
+ CONSTRAINT threadnotifications_pkey PRIMARY KEY (apiclient_id, threadid)
+);
+
CREATE TABLE loaderrors(
id SERIAL NOT NULL PRIMARY KEY,
listid int NOT NULL,
FOR EACH ROW EXECUTE PROCEDURE messages_fti_trigger_func();
CREATE INDEX messages_fti_idx ON messages USING gin(fti);
+CREATE OR REPLACE FUNCTION messages_notify_threads_trg_func() RETURNS trigger AS $$
+BEGIN
+ INSERT INTO threadnotifications (apiclient_id, threadid)
+ SELECT apiclient_id, threadid
+ FROM threadsubscriptions
+ WHERE threadsubscriptions.threadid=NEW.threadid
+ ON CONFLICT DO NOTHING;
+ IF FOUND THEN
+ NOTIFY thread_updated;
+ END IF;
+ RETURN NEW;
+END
+$$ LANGUAGE 'plpgsql';
+CREATE TRIGGER messages_notify_trigger
+ AFTER INSERT ON messages
+ FOR EACH ROW EXECUTE PROCEDURE messages_notify_threads_trg_func();
+
CREATE TABLE legacymap(
listid int not null,
year int not null,