Add /feedinfo endpoint
[blerg.git] / database / subscription.c
1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2  * BSD-style license.  Please see the COPYING file for details.
3  */
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/mman.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <string.h>
11 #include "subscription.h"
12 #include "stringbucket.h"
13 #include "config.h"
14
15 int subscription_add(const char *from, const char *to) {
16         char filename[512];
17         struct stringbucket * sb;
18
19         snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
20         sb = stringbucket_open(filename);
21         stringbucket_add(sb, to);
22         stringbucket_close(sb);
23
24         snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, to);
25         sb = stringbucket_open(filename);
26         stringbucket_add(sb, from);
27         stringbucket_close(sb);
28 }
29
30 int subscription_remove(const char *from, const char *to) {
31         char filename[512];
32         struct stringbucket * sb;
33
34         snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
35         sb = stringbucket_open(filename);
36         stringbucket_delete(sb, to);
37         stringbucket_close(sb);
38
39         snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, to);
40         sb = stringbucket_open(filename);
41         stringbucket_delete(sb, from);
42         stringbucket_close(sb);
43 }
44
45 void subscription_notify_add_item(char *to, void *stuff) {
46         char filename[512];
47
48         snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, to);
49         int fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
50         write(fd, stuff, sizeof(struct blergref));
51         close(fd);
52 }
53
54 int subscription_notify(const char *author, uint64_t record) {
55         char filename[512];
56         struct blergref r;
57
58         strncpy(r.author, author, 32);
59         r.record = record;
60
61         snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, author);
62         struct stringbucket * sb = stringbucket_open(filename);
63         stringbucket_iterate(sb, subscription_notify_add_item, &r);
64         stringbucket_close(sb);
65 }
66
67 struct blergref * subscription_list(const char *author, uint64_t offset, int *count, int direction) {
68         char filename[512];
69         struct stat st;
70         struct blergref * slist;
71         struct blergref * retlist;
72         uint64_t n_subscription_records;
73
74         if (!valid_name(author))
75                 return NULL;
76
77         snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, author);
78
79         int feed_fd = open(filename, O_RDONLY);
80         if (feed_fd == -1) {
81                 perror("Could not open subscription feed");
82                 goto subscription_list_map_failed;
83         }
84
85         fstat(feed_fd, &st);
86         if (st.st_size == 0) {
87                 close(feed_fd);
88                 goto subscription_list_map_failed;
89         }
90         n_subscription_records = st.st_size / sizeof(struct blergref);
91         if (*count > n_subscription_records - offset)
92                 *count = n_subscription_records - offset;
93         if (offset > n_subscription_records) {
94                 fprintf(stderr, "Cannot access subscription record beyond end\n");
95                 goto subscription_list_map_failed;
96                 close(feed_fd);
97                 return NULL;
98         }
99
100         slist = (struct blergref *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, feed_fd, 0);
101         if (slist == MAP_FAILED) {
102                 perror("Could not mmap tag_file");
103                 goto subscription_list_map_failed;
104         }
105         retlist = (struct blergref *) malloc(sizeof(struct blergref) * *count);
106         if (retlist == NULL) {
107                 perror("Could not allocate memory for subscription feed list");
108                 goto subscription_list_malloc_failed;
109         }
110         
111         switch(direction) {
112         case 1:
113                 memcpy(retlist, slist + offset, sizeof(struct blergref) * *count);
114                 break;
115         case -1:
116                 memcpy(retlist, slist + (n_subscription_records - *count - offset), sizeof(struct blergref) * *count);
117         }
118
119         munmap(slist, st.st_size);
120         close(feed_fd);
121         return retlist;
122
123 subscription_list_malloc_failed:
124         munmap(slist, st.st_size);
125 subscription_list_map_failed:
126         close(feed_fd);
127 subscription_list_open_failed:
128         *count = 0;
129         return NULL;
130 }
131
132 int is_subscribed(const char *from, const char *to) {
133         char filename[512];
134         struct stringbucket * sb;
135         int ret = 0;
136
137         snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
138         sb = stringbucket_open(filename);
139         if (stringbucket_find(sb, to) != -1)
140                 ret = 1;
141         stringbucket_close(sb);
142
143         return ret;
144 }