implement channel acking and new messages

This commit is contained in:
2019-07-12 18:30:44 +02:00
parent b8fa202ce3
commit ab65d49605
10 changed files with 139 additions and 11 deletions

View File

@@ -13,6 +13,7 @@ SET(SOURCES
"include/ncdc/mainwindow.h"
"include/ncdc/ncdc.h"
"include/ncdc/textview.h"
"src/ack.c"
"src/cmds.c"
"src/config.c"
"src/friends.c"

View File

@@ -30,6 +30,7 @@ bool ncdc_dispatch(ncdc_mainwindow_t n, wchar_t const *s);
*/
ncdc_commands_t *ncdc_find_cmd(ncdc_commands_t *cmds, wchar_t const *name);
bool ncdc_cmd_ack(ncdc_mainwindow_t n, size_t ac, wchar_t **av, wchar_t const *f);
bool ncdc_cmd_friends(ncdc_mainwindow_t n, size_t ac, wchar_t **av, wchar_t const *f);
bool ncdc_cmd_login(ncdc_mainwindow_t n, size_t ac, wchar_t **av, wchar_t const *f);
bool ncdc_cmd_logout(ncdc_mainwindow_t n, size_t ac, wchar_t **av, wchar_t const *f);

33
ncdc/src/ack.c Normal file
View File

@@ -0,0 +1,33 @@
#include <ncdc/cmds.h>
#include <ncdc/ncdc.h>
bool ncdc_cmd_ack(ncdc_mainwindow_t n, size_t ac, wchar_t **av, wchar_t const *f)
{
dc_channel_t c = NULL;
dc_message_t m = NULL;
bool ret = false;
if (!is_logged_in()) {
return false;
}
c = ncdc_mainwindow_current_channel(n);
return_if_true(c == NULL, false);
return_if_true(dc_channel_messages(c) == 0, false);
m = dc_channel_nth_message(c, dc_channel_messages(c)-1);
ret = dc_api_channel_ack(dc_session_api(current_session),
dc_session_me(current_session),
c, m
);
if (!ret) {
LOG(n, L"ack: failed to ack the given channel");
return false;
}
dc_channel_mark_read(c);
return true;
}

View File

@@ -1,13 +1,15 @@
#include <ncdc/cmds.h>
ncdc_commands_t cmds[] = {
{ L"/friend", ncdc_cmd_friends },
{ L"/friends", ncdc_cmd_friends },
{ L"/login", ncdc_cmd_login },
{ L"/logout", ncdc_cmd_logout },
{ L"/msg", ncdc_cmd_msg },
{ L"/post", ncdc_cmd_post },
{ L"/quit", ncdc_cmd_quit },
{ L"/ack", ncdc_cmd_ack },
{ L"/friend", ncdc_cmd_friends },
{ L"/friends", ncdc_cmd_friends },
{ L"/login", ncdc_cmd_login },
{ L"/logout", ncdc_cmd_logout },
{ L"/markread", ncdc_cmd_ack },
{ L"/msg", ncdc_cmd_msg },
{ L"/post", ncdc_cmd_post },
{ L"/quit", ncdc_cmd_quit },
{ NULL, NULL }
};

View File

@@ -119,7 +119,7 @@ ncdc_mainwindow_callback(ncdc_input_t i, wchar_t const *s,
if (s[0] == '/') {
ret = ncdc_dispatch(mainwin, s);
} else {
wchar_t *post = calloc(wcslen(s)+6, sizeof(wchar_t));
wchar_t *post = calloc(wcslen(s)+7, sizeof(wchar_t));
wcscat(post, L"/post ");
wcscat(post, s);
@@ -202,6 +202,8 @@ static void ncdc_mainwindow_render_status(ncdc_mainwindow_t n)
FILE *f = open_wmemstream(&status, &statuslen);
wchar_t const *wintitle = NULL;
ncdc_textview_t view = NULL;
size_t i = 0;
dc_channel_t channel = NULL;
werase(n->sep1);
return_if_true(f == NULL,);
@@ -221,8 +223,18 @@ static void ncdc_mainwindow_render_status(ncdc_mainwindow_t n)
fwprintf(f, L" [%d: %ls]", n->curview,
(wintitle != NULL ? wintitle : L"n/a")
);
fclose(f);
fwprintf(f, L" [Act:");
for (i = 0; i < n->views->len; i++) {
view = g_ptr_array_index(n->views, i);
channel = ncdc_textview_channel(view);
if (channel != NULL && dc_channel_has_new_messages(channel)) {
fwprintf(f, L" %d", i);
}
}
fwprintf(f, L"]");
fclose(f);
mvwaddwstr(n->sep1, 0, 0, status);
free(status);
}
@@ -287,6 +299,7 @@ void ncdc_mainwindow_switchview(ncdc_mainwindow_t n, int idx)
{
return_if_true(n == NULL || n->views == NULL,);
return_if_true(idx >= n->views->len,);
n->curview = idx;
}
@@ -329,16 +342,33 @@ void ncdc_mainwindow_log(ncdc_mainwindow_t w, wchar_t const *fmt, ...)
ncdc_textview_append(w->log, buf);
}
static void ncdc_mainwindow_ack_view(ncdc_mainwindow_t n)
{
#if 0
dc_channel_t c = ncdc_mainwindow_current_channel(n);
return_if_true(c == NULL,);
return_if_true(dc_channel_messages(c) == 0,);
dc_message_t m = dc_channel_nth_message(c, dc_channel_messages(c)-1);
dc_api_channel_ack(dc_session_api(current_session),
dc_session_me(current_session),
c, m
);
#endif
}
void ncdc_mainwindow_rightview(ncdc_mainwindow_t n)
{
return_if_true(n == NULL,);
n->curview = (n->curview + 1) % n->views->len;
ncdc_mainwindow_ack_view(n);
}
void ncdc_mainwindow_leftview(ncdc_mainwindow_t n)
{
return_if_true(n == NULL,);
n->curview = (n->curview - 1) % n->views->len;
ncdc_mainwindow_ack_view(n);
}
dc_channel_t ncdc_mainwindow_current_channel(ncdc_mainwindow_t n)