From 4dbf6205d0ab778edaa0c43e2eca2b3f92bd2dcb Mon Sep 17 00:00:00 2001 From: Chip Black Date: Sun, 29 Jun 2014 01:16:00 -0500 Subject: [PATCH] Generalize status setting (mute, etc.) --- database/database.c | 12 ++++++------ database/database.h | 11 ++++++++--- lib/perl/Blerg-Database/Database.xs | 8 ++++---- lib/perl/Blerg-Database/Makefile.PL | 5 ++--- lib/perl/Blerg-Database/lib/Blerg/Database.pm | 16 +++++++++++++--- lib/perl/Blerg-Database/t/database.t | 18 ++++++++++++------ tools/blergtool.c | 4 ++-- 7 files changed, 47 insertions(+), 27 deletions(-) diff --git a/database/database.c b/database/database.c index e0c9eb7..fd8279c 100644 --- a/database/database.c +++ b/database/database.c @@ -291,7 +291,7 @@ uint64_t blerg_store(struct blerg *blerg, const char *data, int length) { flock(blerg->data_fd, LOCK_UN); flock(blerg->index_fd, LOCK_UN); - if (!blerg_get_mute(blerg)) { + if (!blerg_get_status(blerg, BLERGSTATUS_MUTED)) { /* Now do those dirty microblogging deeds */ tag_scan(blerg->name, data, length, record); subscription_notify(blerg->name, record); @@ -379,17 +379,17 @@ uint64_t blerg_get_subscription_mark(struct blerg *blerg) { return blerg->meta->subscription_mark; } -int blerg_set_mute(struct blerg *blerg, int v) { +int blerg_set_status(struct blerg *blerg, uint32_t status, int v) { CHECK_VALID_BLERG(0) if (v) { - blerg->meta->status |= BLERGMETA_MUTED; + blerg->meta->status |= status; } else { - blerg->meta->status &= ~BLERGMETA_MUTED; + blerg->meta->status &= ~status; } return 1; } -int blerg_get_mute(struct blerg *blerg) { +int blerg_get_status(struct blerg *blerg, uint32_t status) { CHECK_VALID_BLERG(0) - return (blerg->meta->status & BLERGMETA_MUTED) > 0; + return (blerg->meta->status & status) > 0; } diff --git a/database/database.h b/database/database.h index 7971249..15fd072 100644 --- a/database/database.h +++ b/database/database.h @@ -23,7 +23,12 @@ struct meta { uint32_t status; }; -#define BLERGMETA_MUTED 0x00000001 +/* meta.status is a bitfield of these options */ +/* Muted - if set, tags/refs and subscriptions are not calculated */ +#define BLERGSTATUS_MUTED 0x00000001 +/* Mentioned - if set, user has been mentioned. Cleared by /status/ with + * {"clear": "mentioned"} */ +#define BLERGSTATUS_MENTIONED 0x00000002 struct blerg { int meta_fd; @@ -50,7 +55,7 @@ uint64_t blerg_get_record_count(struct blerg *); time_t blerg_get_timestamp(struct blerg *blerg, uint64_t record); int blerg_set_subscription_mark(struct blerg *blerg); uint64_t blerg_get_subscription_mark(struct blerg *blerg); -int blerg_set_mute(struct blerg *blerg, int v); -int blerg_get_mute(struct blerg *blerg); +int blerg_set_status(struct blerg *blerg, uint32_t status, int v); +int blerg_get_status(struct blerg *blerg, uint32_t status); #endif //_DATABASE_H diff --git a/lib/perl/Blerg-Database/Database.xs b/lib/perl/Blerg-Database/Database.xs index 1911538..e3614db 100644 --- a/lib/perl/Blerg-Database/Database.xs +++ b/lib/perl/Blerg-Database/Database.xs @@ -117,15 +117,15 @@ const char * _get_subscription_mark(struct blerg *ptr) n = snprintf(buf, 21, "%llu", mark); XPUSHs(sv_2mortal(newSVpv(buf, n))); -int _set_mute(struct blerg *ptr, int v) +int _set_status(struct blerg *ptr, int status, int v) CODE: - RETVAL = blerg_set_mute(ptr, v); + RETVAL = blerg_set_status(ptr, status, v); OUTPUT: RETVAL -int _get_mute(struct blerg *ptr) +int _get_status(struct blerg *ptr, int status) CODE: - RETVAL = blerg_get_mute(ptr); + RETVAL = blerg_get_status(ptr, status); OUTPUT: RETVAL diff --git a/lib/perl/Blerg-Database/Makefile.PL b/lib/perl/Blerg-Database/Makefile.PL index 6003b64..5f0ab24 100644 --- a/lib/perl/Blerg-Database/Makefile.PL +++ b/lib/perl/Blerg-Database/Makefile.PL @@ -13,15 +13,13 @@ WriteMakefile( OBJECT => '$(BASEEXT)$(OBJ_EXT) ../../../blerg.a ../../../blerg_auth.a ../../../builddeps/scrypt.a', DEFINE => '', # e.g., '-DHAVE_SOMETHING' INC => '-I../../..', # e.g., '-I. -I/usr/include/other' - # Un-comment this if you add C files to link with later: - # OBJECT => '$(O_FILES)', # link all the C files too ); if (eval {require ExtUtils::Constant; 1}) { # If you edit these definitions to change the constants used by this module, # you will need to use the generated const-c.inc and const-xs.inc # files to replace their "fallback" counterparts before distributing your # changes. - my @names = (qw(RECORDS_PER_SEGMENT MAX_RECORD_SIZE MAX_TAG_LENGTH MAX_TAGS_PER_RECORD), + my @names = (qw(RECORDS_PER_SEGMENT MAX_RECORD_SIZE MAX_TAG_LENGTH MAX_TAGS_PER_RECORD BLERGSTATUS_MUTED BLERGSTATUS_MENTIONED), {name => 'BASEURL', type => 'PV'}, {name => 'DATA_PATH', type => 'PV'}, {name => 'HASH_TAGS_PATH', type => 'PV'}, @@ -33,6 +31,7 @@ if (eval {require ExtUtils::Constant; 1}) { DEFAULT_TYPE => 'IV', C_FILE => 'const-c.inc', XS_FILE => 'const-xs.inc', + PROXYSUBS => 1, ); } diff --git a/lib/perl/Blerg-Database/lib/Blerg/Database.pm b/lib/perl/Blerg-Database/lib/Blerg/Database.pm index d71340d..bf41706 100644 --- a/lib/perl/Blerg-Database/lib/Blerg/Database.pm +++ b/lib/perl/Blerg-Database/lib/Blerg/Database.pm @@ -130,9 +130,19 @@ sub mute { my ($obj, $v) = @_; $obj->_ensure_pointer; if (defined $v) { - return Blerg::Database::_set_mute($obj->{ptr}, $v); + return Blerg::Database::_set_status($obj->{ptr}, $obj->BLERGSTATUS_MUTED, $v); } else { - return Blerg::Database::_get_mute($obj->{ptr}); + return Blerg::Database::_get_status($obj->{ptr}, $obj->BLERGSTATUS_MUTED); + } +} + +sub mention { + my ($obj, $v) = @_; + $obj->_ensure_pointer; + if (defined $v) { + return Blerg::Database::_set_status($obj->{ptr}, $obj->BLERGSTATUS_MENTIONED, $v); + } else { + return Blerg::Database::_get_status($obj->{ptr}, $obj->BLERGSTATUS_MENTIONED); } } @@ -303,7 +313,7 @@ C{name})>. =item mute(v) -When v = 1, mute the user, otherwise, unmute. +When v = 1, mute the user, otherwise, unmute. If v is absent, return the mute status. =item close() diff --git a/lib/perl/Blerg-Database/t/database.t b/lib/perl/Blerg-Database/t/database.t index 203d286..2944a06 100644 --- a/lib/perl/Blerg-Database/t/database.t +++ b/lib/perl/Blerg-Database/t/database.t @@ -9,7 +9,7 @@ use File::Path qw/remove_tree/; use strict; use warnings; -use Test::More tests => 26; +use Test::More tests => 30; BEGIN { use_ok('Blerg::Database') }; ######################### @@ -68,14 +68,14 @@ ok( @list == 3 ); ok( $list[0]->{author} eq $test_user ); ok( $list[0]->{record} == $n ); -# Test mute -ok( Blerg::Database::_set_mute($ptr, 1) ); -ok( Blerg::Database::_get_mute($ptr) == 1 ); +# Test status +ok( Blerg::Database::_set_status($ptr, Blerg::Database::BLERGSTATUS_MUTED, 1) ); +ok( Blerg::Database::_get_status($ptr, Blerg::Database::BLERGSTATUS_MUTED) == 1 ); Blerg::Database::_store($ptr, "Testing more #data"); @list = Blerg::Database::tag_list('#data', 0, 1); ok( @list == 3 ); -ok( Blerg::Database::_set_mute($ptr, 0) ); -ok( Blerg::Database::_get_mute($ptr) == 0 ); +ok( Blerg::Database::_set_status($ptr, Blerg::Database::BLERGSTATUS_MUTED, 0) ); +ok( Blerg::Database::_get_status($ptr, Blerg::Database::BLERGSTATUS_MUTED) == 0 ); Blerg::Database::_store($ptr, "Testing more #data"); @list = Blerg::Database::tag_list('#data', 0, 1); ok( @list == 4 ); @@ -109,6 +109,12 @@ ok( $blerg->store($data) == $n + 1 ); $n++; ok( $blerg->fetch($n) eq $data ); +# Mute +ok( $blerg->mute(1) ); +ok( $blerg->mute == 1 ); +ok( $blerg->mute(0) ); +ok( $blerg->mute == 0 ); + # Close database $blerg->close; diff --git a/tools/blergtool.c b/tools/blergtool.c index eee8aef..c689d2f 100644 --- a/tools/blergtool.c +++ b/tools/blergtool.c @@ -93,7 +93,7 @@ int main(int argc, char *argv[]) { exit(1); } - blerg_set_mute(f, 1); + blerg_set_status(f, BLERGSTATUS_MUTED, 1); blerg_close(f); } else if (strncmp(argv[1], "unmute", 6) == 0) { char *store = argv[2]; @@ -103,7 +103,7 @@ int main(int argc, char *argv[]) { exit(1); } - blerg_set_mute(f, 0); + blerg_set_status(f, BLERGSTATUS_MUTED, 0); blerg_close(f); } else if (strncmp(argv[1], "subscribe", 9) == 0) { if (argc < 4) { -- 2.25.1