Fix user online/offline status

Fix bug that prevented users from being displayed as online. Add option to set inactive users offline after a certain amount of days. Clean up user status code.
This commit is contained in:
mjentsch 2015-03-13 16:57:08 +01:00
parent 0590862151
commit 7903ce1811
6 changed files with 80 additions and 51 deletions

View file

@ -80,31 +80,6 @@ const char *pk_path = "/etc/telegram-purple/server.pub";
void on_user_get_info (struct tgl_state *TLS, void *info_data, int success, struct tgl_user *U);
static char *format_status (struct tgl_user_status *status) {
return status->online ? "Online" : "Mobile";
}
char *format_user_status (struct tgl_user_status *status) {
char *when;
switch (status->online) {
case -1:
when = g_strdup_printf("%s", format_time (status->when));
break;
case -2:
when = g_strdup_printf("recently");
break;
case -3:
when = g_strdup_printf("last week");
break;
case -4:
when = g_strdup_printf("last month");
break;
default:
when = g_strdup ("unknown");
break;
}
return when;
}
static char *format_print_name (struct tgl_state *TLS, tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4) {
const char *d[4];
@ -150,6 +125,7 @@ static void start_secret_chat (PurpleBlistNode *node, gpointer data) {
static void update_message_received (struct tgl_state *TLS, struct tgl_message *M);
static void update_user_handler (struct tgl_state *TLS, struct tgl_user *U, unsigned flags);
static void update_user_status_handler (struct tgl_state *TLS, struct tgl_user *U);
static void update_chat_handler (struct tgl_state *TLS, struct tgl_chat *C, unsigned flags);
static void update_secret_chat_handler (struct tgl_state *TLS, struct tgl_secret_chat *C, unsigned flags);
static void update_user_typing (struct tgl_state *TLS, struct tgl_user *U, enum tgl_typing_status status);
@ -158,6 +134,7 @@ struct tgl_update_callback tgp_callback = {
.new_msg = update_message_received,
.msg_receive = update_message_received,
.user_update = update_user_handler,
.user_status_update = update_user_status_handler,
.chat_update = update_chat_handler,
.secret_chat_update = update_secret_chat_handler,
.type_notification = update_user_typing,
@ -194,6 +171,10 @@ static void update_user_handler (struct tgl_state *TLS, struct tgl_user *user, u
}
}
static void update_user_status_handler (struct tgl_state *TLS, struct tgl_user *U) {
p2tgl_prpl_got_user_status (TLS, U->id, &U->status);
}
static void write_secret_chat_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_secret_chat *E) {
if (!success) { return; }
write_secret_chat_file (TLS);
@ -406,17 +387,16 @@ static void tgprpl_tooltip_text (PurpleBuddy * buddy, PurpleNotifyUserInfo * inf
tgl_peer_id_t *peer = purple_buddy_get_protocol_data(buddy);
if (!peer) {
purple_notify_user_info_add_pair (info, "Status", "Offline");
return;
}
tgl_peer_t *P = tgl_peer_get (get_conn_from_buddy (buddy)->TLS, *peer);
if (!P) {
warning ("tgprpl_tooltip_text: warning peer with id %d not found in tree.", peer->id);
warning ("Warning peer with id %d not found in tree.", peer->id);
return;
}
purple_notify_user_info_add_pair (info, "Status", format_status(&P->user.status));
gchar *status = format_user_status (&P->user.status);
gchar *status = tgp_format_user_status (&P->user.status);
purple_notify_user_info_add_pair (info, "last online: ", status);
g_free (status);
}
@ -814,23 +794,30 @@ static void tgprpl_init (PurplePlugin *plugin) {
ADD_VALUE(verification_values, "Always", "always");
ADD_VALUE(verification_values, "Never", "never");
opt = purple_account_option_list_new("Accept Secret Chats", "accept-secret-chats", verification_values);
opt = purple_account_option_list_new ("Accept Secret Chats", "accept-secret-chats", verification_values);
prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, opt);
opt = purple_account_option_int_new ("Display users not seen for (N) days as offline.",
"inactive-days-offline",
TGP_DEFAULT_INACTIVE_DAYS_OFFLINE);
prpl_info.protocol_options = g_list_append (prpl_info.protocol_options, opt);
opt = purple_account_option_int_new ("Split oversized messages in up to (N) chunks.",
"max-msg-split-count",
TGP_DEFAULT_MAX_MSG_SPLIT_COUNT);
prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, opt);
opt = purple_account_option_int_new ("Don't fetch messages older than (N) days.\n"
"Set 0 for unlimited.",
"history-retrieve-days",
TGP_DEFAULT_HISTORY_RETRIEVAL_THRESHOLD);
prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, opt);
prpl_info.protocol_options = g_list_append (prpl_info.protocol_options, opt);
opt = purple_account_option_bool_new ("Fetch past history on first login.\n"
"Can be very slow on big histories.",
"(Warning, can be slow)",
"history-sync-all", FALSE);
prpl_info.protocol_options = g_list_append (prpl_info.protocol_options, opt);
opt = purple_account_option_int_new ("Don't fetch messages older than (N) days.\n"
"0 for unlimited.",
"history-retrieve-days",
TGP_DEFAULT_HISTORY_RETRIEVAL_THRESHOLD);
prpl_info.protocol_options = g_list_append (prpl_info.protocol_options, opt);
_telegram_protocol = plugin;
}

View file

@ -37,15 +37,15 @@
#define TGP_APP_ID 16154
#define TGP_MAX_MSG_SIZE 4096
#define TGP_DEFAULT_INACTIVE_DAYS_OFFLINE 7
#define TGP_DEFAULT_MAX_MSG_SPLIT_COUNT 4
#define TGP_DEFAULT_HISTORY_RETRIEVAL_THRESHOLD 14
void on_chat_get_info (struct tgl_state *TLS, void *extra, int success, struct tgl_chat *C);
void on_ready (struct tgl_state *TLS);
extern const char *pk_path;
extern const char *config_dir;
extern PurplePlugin *_telegram_protocol;
char *format_user_status (struct tgl_user_status *status);
char *p2tgl_peer_strdup_id (tgl_peer_id_t user);
#endif

View file

@ -240,11 +240,29 @@ void p2tgl_prpl_got_set_status_offline (struct tgl_state *TLS, tgl_peer_id_t use
g_free (name);
}
void p2tgl_prpl_got_set_status_online (struct tgl_state *TLS, tgl_peer_id_t user) {
char *name = p2tgl_peer_strdup_id (user);
purple_prpl_got_user_status (tg_get_acc(TLS), name, "available", NULL);
g_free (name);
}
void p2tgl_prpl_got_user_status (struct tgl_state *TLS, tgl_peer_id_t user, struct tgl_user_status *status) {
connection_data *data = TLS->ev_base;
if (status->online == 1) {
p2tgl_prpl_got_set_status_offline(TLS, user);
p2tgl_prpl_got_set_status_online (TLS, user);
} else {
p2tgl_prpl_got_set_status_mobile(TLS, user);
debug ("%d: when=%d", user.id, status->when);
if (tgp_time_n_days_ago (purple_account_get_int (data->pa, "inactive-days-offline", TGP_DEFAULT_INACTIVE_DAYS_OFFLINE)) > status->when && status->when) {
debug ("offline");
p2tgl_prpl_got_set_status_offline (TLS, user);
}
else {
debug ("mobile");
p2tgl_prpl_got_set_status_mobile (TLS, user);
}
}
}
@ -327,7 +345,7 @@ PurpleNotifyUserInfo *p2tgl_notify_user_info_new (struct tgl_user *U) {
purple_notify_user_info_add_pair (info, "Username", U->username);
}
char *status = format_user_status (&U->status);
char *status = tgp_format_user_status (&U->status);
purple_notify_user_info_add_pair (info, "Last seen", status);
g_free (status);

View file

@ -356,14 +356,8 @@ static void tgp_msg_display (struct tgl_state *TLS, struct tgp_msg_loading *C) {
static time_t tgp_msg_oldest_relevant_ts (struct tgl_state *TLS) {
connection_data *conn = TLS->ev_base;
time_t now;
time (&now);
int daysAgo = purple_account_get_int (conn->pa, "history-retrieve-days",
TGP_DEFAULT_HISTORY_RETRIEVAL_THRESHOLD);
if (daysAgo == 0) {
return 0;
}
return now - 24 * 3600 * (time_t)daysAgo;
return tgp_time_n_days_ago (purple_account_get_int (conn->pa, "history-retrieve-days",
TGP_DEFAULT_HISTORY_RETRIEVAL_THRESHOLD));
}
static void tgp_msg_process_ready (struct tgl_state *TLS)

View file

@ -42,6 +42,28 @@ char *format_img_full (int imgstore) {
return g_strdup_printf ("%s<img id=\"%u\">", br, imgstore);
}
char *tgp_format_user_status (struct tgl_user_status *status) {
char *when;
switch (status->online) {
case -1:
when = g_strdup_printf("%s", format_time (status->when));
break;
case -2:
when = g_strdup_printf("recently");
break;
case -3:
when = g_strdup_printf("last week");
break;
case -4:
when = g_strdup_printf("last month");
break;
default:
when = g_strdup ("unknown");
break;
}
return when;
}
int str_not_empty (const char *string) {
return string && string[0] != '\0';
}
@ -68,6 +90,12 @@ tgl_peer_t *tgp_encr_chat_get_partner (struct tgl_state *TLS, struct tgl_secret_
return tgl_peer_get (TLS, TGL_MK_USER(chat->admin_id == TLS->our_id ? chat->user_id : chat->admin_id));
}
long tgp_time_n_days_ago (int days) {
time_t now;
time (&now);
return now - 24 * 3600 * (time_t)days;
};
char *tgp_g_format_size (gint64 size) {
char *sizes[] = {
"B",

View file

@ -43,7 +43,9 @@ int out_msg (struct tgl_state *TLS, struct tgl_message *M);
const char *format_time (time_t date);
char *format_img_full (int imgstore);
char *tgp_format_user_status (struct tgl_user_status *status);
int str_not_empty (const char *string);
long tgp_time_n_days_ago (int days);
char *tgp_g_format_size (gint64 size);
void tgp_g_queue_free_full (GQueue *queue, GDestroyNotify free_func);
void tgp_g_list_free_full (GList *list, GDestroyNotify free_func);