Add subscription marking to keep track of things you've read
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;
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);
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;
+}
struct meta {
uint64_t sequence;
+ uint64_t subscription_mark;
};
struct blerg {
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
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);
+}
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 */