diff options
Diffstat (limited to 'src/api.c')
-rw-r--r-- | src/api.c | 23 |
1 files changed, 16 insertions, 7 deletions
@@ -104,7 +104,9 @@ struct dc_channel { _Atomic(struct dc_guild *) guild; /* nofree, nouiw */ struct dc_message ** messages; /* yesfree, nouiw */ _Atomic(size_t) messages_sizeof; /* nouiw */ - int slowmode; /* number of seconds to wait in case of slowmode, HANDLED BY THE USER INTERFACE!! */ + int slowmode; /* nouiw - number of seconds to wait in case of slowmode, HANDLED BY THE USER INTERFACE!! */ + unsigned short int joined; /* noapiw, if joined (1) api will fetch messages and ui display them. api will still send messages even if this is not set */ + unsigned short int focused; /* noapi, if focused (1) ui will send messages to this channel */ }; void dc_channel_free (struct dc_channel * ch) { /* noui, noapi, nolock - only called by dc_guild_free */ free(ch->name); ch->name = NULL; @@ -146,7 +148,6 @@ struct dc_client { struct dc_guild ** guilds; /* yesfree, nouiw */ _Atomic(size_t) guilds_sizeof; /* nouiw */ pthread_rwlock_t * guilds_lock; - _Atomic(struct dc_channel *) joinedchannel; /* nofree, noapiw */ struct dc_error ** errors; /* yesfree */ _Atomic(size_t) errors_sizeof; pthread_rwlock_t * errors_lock; @@ -154,6 +155,7 @@ struct dc_client { _Atomic(size_t) sent_messages_sizeof; pthread_rwlock_t * sent_messages_lock; _Atomic(time_t) last_sent_message; /* for slowmode implementations */ + _Atomic(short unsigned int) newmessages; /* > 0 if api got new messages. ui resets to 0 - nolock - ui should still check without this flag for new messages - racy */ }; struct dc_client * dc_client_init () { /* gives you a prepared dc_client */ struct dc_client * c = calloc(1, sizeof(struct dc_client)); @@ -176,7 +178,6 @@ void dc_client_free (struct dc_client * c) { /* noui, noapi, nolock - only calle free(c->password); c->password = NULL; free(c->username); c->username = NULL; c->discriminator = -1; - c->joinedchannel = NULL; for (int i = 0; i < c->guilds_sizeof; i++) dc_guild_free(c->guilds[i]); free(c->guilds); @@ -516,7 +517,7 @@ int dc_fetch_channels (struct dc_guild * g) { } if (skip) continue; g->channels = realloc(g->channels, sizeof(struct dc_channel *)*++g->channels_sizeof); - g->channels[g->channels_sizeof-1] = malloc(sizeof(struct dc_channel)); + g->channels[g->channels_sizeof-1] = calloc(1, sizeof(struct dc_channel)); g->channels[g->channels_sizeof-1]->name = malloc(strlen(name)+1); strcpy(g->channels[g->channels_sizeof-1]->name, name); g->channels[g->channels_sizeof-1]->topic = malloc(strlen(topic)+1); @@ -526,6 +527,8 @@ int dc_fetch_channels (struct dc_guild * g) { g->channels[g->channels_sizeof-1]->messages = NULL; g->channels[g->channels_sizeof-1]->messages_sizeof = 0; g->channels[g->channels_sizeof-1]->slowmode = slowmode; + g->channels[g->channels_sizeof-1]->joined = 0; + g->channels[g->channels_sizeof-1]->focused = 0; } if (DC_CUE(c, c->guilds_lock)) {rs = -8; goto rc;} rc: @@ -617,6 +620,7 @@ int dc_fetch_messages (struct dc_channel * ch) { } if (DC_CWLE(c, c->guilds_lock)) {rs = -7; goto rc;} /* we lock all guilds of a client when writing messages */ cJSON * message = NULL; + int msgs = 0; cJSON_ArrayForEach(message, json) { int skip = 0; char * timestamp = cJSON_GetStringValue(cJSON_GetObjectItem(message, "timestamp")); @@ -657,9 +661,12 @@ int dc_fetch_messages (struct dc_channel * ch) { DC_FMTM->id = idull; DC_FMTM->discriminator = strtol(discriminator, NULL, 10); DC_FMTM->channel = ch; + msgs++; } qsort(ch->messages, ch->messages_sizeof, sizeof(struct dc_message *), dc_message_compare); /* we sort so that present messages are in the start of the array and old messages are to the end of the array */ if (DC_CUE(c, c->guilds_lock)) {rs = -8; goto rc;} + if (msgs > 0) + rs = msgs; rc: cJSON_Delete(json); json = NULL; return rs; @@ -691,10 +698,12 @@ int dc_api_thread (struct dc_thread_control * t) { /* updates messages and sends for (int j = 0; j < t->clients[i]->guilds_sizeof; j++) { if (!t->clients[i]->guilds[j]->channels_sizeof /*|| !(rand() % 100)*/) /* roughly every 100+inf cycles we'll update channels */ dc_fetch_channels(t->clients[i]->guilds[j]); - /* for (int k = 0; k < t->clients[i]->guilds[j]->channels_sizeof; k++) */ - if (!(rand() % 10) && t->clients[i]->joinedchannel) /* roughly every 10 cycles we'll update messages in the joined ch */ - dc_fetch_messages(t->clients[i]->joinedchannel); } + for (int k = 0; k < t->clients[i]->guilds_sizeof; k++) + for (int l = 0; l < t->clients[i]->guilds[k]->channels_sizeof; l++) + if (t->clients[i]->guilds[k]->channels[l]->joined && !(rand() % 10)) /* roughly every 10 cycles we'll update messages in the joined channels */ + if (dc_fetch_messages(t->clients[i]->guilds[k]->channels[l]) > 0) + t->clients[i]->newmessages++; if (DC_CWLE(t->clients[i], t->clients[i]->sent_messages_lock)) continue; if (t->clients[i]->sent_messages_sizeof > 0) { struct dc_message * msg2send = t->clients[i]->sent_messages[0]; |