Add subscription marking to keep track of things you've read
authorChip Black <bytex64@bytex64.net>
Mon, 18 Jul 2011 08:45:12 +0000 (03:45 -0500)
committerChip Black <bytex64@bytex64.net>
Mon, 18 Jul 2011 08:46:29 +0000 (03:46 -0500)
cgi/cgi_blerg.c
database/database.c
database/database.h
database/subscription.c
database/subscription.h

index b479535..85af748 100644 (file)
@@ -381,21 +381,21 @@ int main(int argc, char *argv[]) {
                if (!check_auth(username))
                        exit(0);
 
-               if (path[9] != '/') {
-                       respond_404();
-                       exit(0);
-               }
-
+               yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
+               yajl_gen_map_open(g);
                ret = parse_url_info(path + 10, &info);
                if ((ret & URL_INFO_NAME) == 0) {
-                       respond_404();
-                       exit(0);
-               }
+                       struct blerg *b = blerg_open(username);
+                       uint64_t subscription_mark = blerg_get_subscription_mark(b);
+                       blerg_close(b);
 
-               yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
-               yajl_gen_map_open(g);
-               yajl_gen_string(g, "subscribed", 10);
-               yajl_gen_bool(g, is_subscribed(username, info.name));
+                       yajl_gen_string(g, "new", 3);
+                       yajl_gen_integer(g, subscription_count_items(username) - subscription_mark);
+               } else {
+                       yajl_gen_string(g, "subscribed", 10);
+                       yajl_gen_bool(g, is_subscribed(username, info.name));
+
+               }
                yajl_gen_map_close(g);
 
                const unsigned char *ybuf;
index a45c92b..e6e8707 100644 (file)
@@ -164,10 +164,17 @@ struct blerg *blerg_open(const char *name) {
                goto open_failed_meta_open;
        }
        fstat(blerg->meta_fd, &st);
-       if (st.st_size == 0) {
-               char *buf = (char *) malloc(sizeof(struct meta));
-               memset(buf, 0, sizeof(struct meta));
-               write(blerg->meta_fd, buf, sizeof(struct meta));
+       if (st.st_size < sizeof(struct meta)) {
+               // Fill the difference in size between sizeof(struct meta) and
+               // the file size with nulls.  This allows seamless upgrades as
+               // long as struct meta only adds members.
+               int len = sizeof(struct meta) - st.st_size;
+               char *buf = (char *) malloc(len);
+               memset(buf, 0, len);
+               int tmpfd = dup(blerg->meta_fd);
+               FILE* tmp = fdopen(tmpfd, "a");
+               fwrite(buf, len, 1, tmp);
+               fclose(tmp);
                free(buf);
        }
        blerg->meta = (struct meta *) mmap(NULL, sizeof(struct meta), PROT_READ | PROT_WRITE, MAP_SHARED, blerg->meta_fd, 0);
@@ -324,3 +331,11 @@ time_t blerg_get_timestamp(struct blerg *blerg, int record) {
 
        return blerg->index[seg_rec].timestamp;
 }
+
+int blerg_set_subscription_mark(struct blerg *blerg) {
+       blerg->meta->subscription_mark = subscription_count_items(blerg->name);
+}
+
+uint64_t blerg_get_subscription_mark(struct blerg *blerg) {
+       return blerg->meta->subscription_mark;
+}
index 2f71bee..412d6c7 100644 (file)
@@ -19,6 +19,7 @@ struct record {
 
 struct meta {
        uint64_t sequence;
+       uint64_t subscription_mark;
 };
 
 struct blerg {
@@ -41,5 +42,7 @@ int blerg_store(struct blerg *, const char *, int);
 int blerg_fetch(struct blerg *, int, char **, int *);
 uint64_t blerg_get_record_count(struct blerg *);
 time_t blerg_get_timestamp(struct blerg *blerg, int record);
+int blerg_set_subscription_mark(struct blerg *blerg);
+uint64_t blerg_get_subscription_mark(struct blerg *blerg);
 
 #endif //_DATABASE_H
index d3686ad..5a948d7 100644 (file)
@@ -142,3 +142,16 @@ int is_subscribed(const char *from, const char *to) {
 
        return ret;
 }
+
+int subscription_count_items(const char *user) {
+       char filename[512];
+       struct stat st;
+
+       if (!valid_name(user))
+               return -1;
+
+       snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, user);
+
+       stat(filename, &st);
+       return st.st_size / sizeof(struct blergref);
+}
index c07fc6d..a35c568 100644 (file)
@@ -12,5 +12,6 @@ int subscription_remove(const char *from, const char *to);
 int subscription_notify(const char *author, uint64_t record);
 struct blergref * subscription_list(const char *author, uint64_t offset, int *count, int direction);
 int is_subscribed(const char *from, const char *to);
+int subscription_count_items(const char *user);
 
 #endif /* _SUBSCRIPTION_H */