Added some error control to pinning.

This commit is contained in:
default 2023-06-28 20:52:09 +02:00
parent 394240b9ba
commit af180685bd
3 changed files with 35 additions and 11 deletions

16
data.c
View File

@ -1352,7 +1352,7 @@ int is_muted(snac *snac, const char *actor)
xs_str *_pinned_fn(snac *user, const char *id) xs_str *_pinned_fn(snac *user, const char *id)
{ {
xs *md5 = xs_md5_hex(id, strlen(id)); xs *md5 = xs_md5_hex(id, strlen(id));
return xs_fmt("%s/pinned/%s", user->basedir, md5); return xs_fmt("%s/pinned/%s.json", user->basedir, md5);
} }
@ -1367,26 +1367,28 @@ int is_pinned(snac *user, const char *id)
int pin(snac *user, const char *id) int pin(snac *user, const char *id)
/* pins a message */ /* pins a message */
{ {
int ret = 0; int ret = -2;
if (xs_startswith(id, user->actor)) { if (xs_startswith(id, user->actor)) {
if (is_pinned(user, id))
ret = -3;
else {
/* create the subfolder, if it does not exist */ /* create the subfolder, if it does not exist */
xs *fn = xs_fmt("%s/pinned/", user->basedir); xs *fn = xs_fmt("%s/pinned/", user->basedir);
mkdirx(fn); mkdirx(fn);
object_user_cache_add(user, id, "pinned"); ret = object_user_cache_add(user, id, "pinned");
}
ret = 1;
} }
return ret; return ret;
} }
void unpin(snac *user, const char *id) int unpin(snac *user, const char *id)
/* unpin a message */ /* unpin a message */
{ {
object_user_cache_del(user, id, "pinned"); return object_user_cache_del(user, id, "pinned");
} }

22
main.c
View File

@ -31,6 +31,8 @@ int usage(void)
printf("resetpwd {basedir} {uid} Resets the password of a user\n"); printf("resetpwd {basedir} {uid} Resets the password of a user\n");
printf("ping {basedir} {uid} {actor} Pings an actor\n"); printf("ping {basedir} {uid} {actor} Pings an actor\n");
printf("webfinger_s {basedir} {uid} {actor} Queries about an actor (@user@host or actor url)\n"); printf("webfinger_s {basedir} {uid} {actor} Queries about an actor (@user@host or actor url)\n");
printf("pin {basedir} {uid} {msg_url} Pins a message\n");
printf("unpin {basedir} {uid} {msg_url} Unpins a message\n");
/* printf("question {basedir} {uid} 'opts' Generates a poll (;-separated opts)\n");*/ /* printf("question {basedir} {uid} 'opts' Generates a poll (;-separated opts)\n");*/
return 1; return 1;
@ -269,6 +271,26 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
if (strcmp(cmd, "pin") == 0) { /** **/
int ret = pin(&snac, url);
if (ret < 0) {
fprintf(stderr, "error pinning %s %d\n", url, ret);
return 1;
}
return 0;
}
if (strcmp(cmd, "unpin") == 0) { /** **/
int ret = unpin(&snac, url);
if (ret < 0) {
fprintf(stderr, "error unpinning %s %d\n", url, ret);
return 1;
}
return 0;
}
if (strcmp(cmd, "question") == 0) { /** **/ if (strcmp(cmd, "question") == 0) { /** **/
int end_secs = 5 * 60; int end_secs = 5 * 60;
xs *opts = xs_split(url, ";"); xs *opts = xs_split(url, ";");

2
snac.h
View File

@ -127,7 +127,7 @@ void unmute(snac *snac, const char *actor);
int is_muted(snac *snac, const char *actor); int is_muted(snac *snac, const char *actor);
int pin(snac *user, const char *id); int pin(snac *user, const char *id);
void unpin(snac *user, const char *id); int unpin(snac *user, const char *id);
int is_pinned(snac *user, const char *id); int is_pinned(snac *user, const char *id);
xs_list *pinned_list(snac *user); xs_list *pinned_list(snac *user);