Fixes for #516:
Added output when message are marked read (if ```--json```), which fixes Issue [#516] (https://github.com/vysheng/tg/issues/516#issuecomment-101710890) Also added event field to json which will be send to terminal/main_session: "event" = "message", "read" (message got read), "service", (events only in secret chats?) "updates" (peer name change etc), "download" (file downloaded)
This commit is contained in:
parent
5935c97ed0
commit
8b8a69ec83
53
interface.c
53
interface.c
@ -1870,6 +1870,7 @@ void print_filename_gw (struct tgl_state *TLSR, void *extra, int success, const
|
|||||||
#ifdef USE_JSON
|
#ifdef USE_JSON
|
||||||
json_t *res = json_object ();
|
json_t *res = json_object ();
|
||||||
assert (json_object_set (res, "result", json_string (name)) >= 0);
|
assert (json_object_set (res, "result", json_string (name)) >= 0);
|
||||||
|
assert (json_object_set (res, "event", json_string ("download")) >= 0);
|
||||||
char *s = json_dumps (res, 0);
|
char *s = json_dumps (res, 0);
|
||||||
mprintf (ev, "%s\n", s);
|
mprintf (ev, "%s\n", s);
|
||||||
json_decref (res);
|
json_decref (res);
|
||||||
@ -2135,6 +2136,15 @@ void print_read_list (int num, struct tgl_message *list[]) {
|
|||||||
int i;
|
int i;
|
||||||
mprint_start (ev);
|
mprint_start (ev);
|
||||||
for (i = 0; i < num; i++) if (list[i]) {
|
for (i = 0; i < num; i++) if (list[i]) {
|
||||||
|
if (enable_json) {
|
||||||
|
#ifdef USE_JSON
|
||||||
|
json_t *res = json_pack_read (list[i]);
|
||||||
|
char *s = json_dumps (res, 0);
|
||||||
|
mprintf (ev, "%s\n", s);
|
||||||
|
json_decref (res);
|
||||||
|
free (s);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
tgl_peer_id_t to_id;
|
tgl_peer_id_t to_id;
|
||||||
if (tgl_get_peer_type (list[i]->to_id) == TGL_PEER_USER && tgl_get_peer_id (list[i]->to_id) == TLS->our_id) {
|
if (tgl_get_peer_type (list[i]->to_id) == TGL_PEER_USER && tgl_get_peer_id (list[i]->to_id) == TLS->our_id) {
|
||||||
to_id = list[i]->from_id;
|
to_id = list[i]->from_id;
|
||||||
@ -2162,25 +2172,27 @@ void print_read_list (int num, struct tgl_message *list[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert (c1 + c2 > 0);
|
assert (c1 + c2 > 0);
|
||||||
mpush_color (ev, COLOR_YELLOW);
|
if (!enable_json) {
|
||||||
switch (tgl_get_peer_type (to_id)) {
|
mpush_color (ev, COLOR_YELLOW);
|
||||||
case TGL_PEER_USER:
|
switch (tgl_get_peer_type (to_id)) {
|
||||||
mprintf (ev, "User ");
|
case TGL_PEER_USER:
|
||||||
print_user_name (ev, to_id, tgl_peer_get (TLS, to_id));
|
mprintf (ev, "User ");
|
||||||
break;
|
print_user_name (ev, to_id, tgl_peer_get (TLS, to_id));
|
||||||
case TGL_PEER_CHAT:
|
break;
|
||||||
mprintf (ev, "Chat ");
|
case TGL_PEER_CHAT:
|
||||||
print_chat_name (ev, to_id, tgl_peer_get (TLS, to_id));
|
mprintf (ev, "Chat ");
|
||||||
break;
|
print_chat_name (ev, to_id, tgl_peer_get (TLS, to_id));
|
||||||
case TGL_PEER_ENCR_CHAT:
|
break;
|
||||||
mprintf (ev, "Secret chat ");
|
case TGL_PEER_ENCR_CHAT:
|
||||||
print_encr_chat_name (ev, to_id, tgl_peer_get (TLS, to_id));
|
mprintf (ev, "Secret chat ");
|
||||||
break;
|
print_encr_chat_name (ev, to_id, tgl_peer_get (TLS, to_id));
|
||||||
default:
|
break;
|
||||||
assert (0);
|
default:
|
||||||
}
|
assert (0);
|
||||||
mprintf (ev, " marked read %d outbox and %d inbox messages\n", c1, c2);
|
}
|
||||||
mpop_color (ev);
|
mprintf (ev, " marked read %d outbox and %d inbox messages\n", c1, c2);
|
||||||
|
mpop_color (ev);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
mprint_end (ev);
|
mprint_end (ev);
|
||||||
}
|
}
|
||||||
@ -2371,9 +2383,10 @@ void print_peer_updates (struct in_ev *ev, int flags) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_peer_update (struct in_ev *ev, tgl_peer_t *P, unsigned flags) {
|
void json_peer_update (struct in_ev *ev, tgl_peer_t *P, unsigned flags) {
|
||||||
#ifdef USE_JSON
|
#ifdef USE_JSON
|
||||||
json_t *res = json_object ();
|
json_t *res = json_object ();
|
||||||
|
assert (json_object_set (res, "event", json_string ("updates")) >= 0);
|
||||||
assert (json_object_set (res, "peer", json_pack_peer (P->id, P)) >= 0);
|
assert (json_object_set (res, "peer", json_pack_peer (P->id, P)) >= 0);
|
||||||
assert (json_object_set (res, "updates", json_pack_updates (flags)) >= 0);
|
assert (json_object_set (res, "updates", json_pack_updates (flags)) >= 0);
|
||||||
char *s = json_dumps (res, 0);
|
char *s = json_dumps (res, 0);
|
||||||
|
15
json-tg.c
15
json-tg.c
@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
#include <jansson.h>
|
#include <jansson.h>
|
||||||
#include "json-tg.h"
|
#include "json-tg.h"
|
||||||
#include <tgl.h>
|
#include <tgl/tgl.h>
|
||||||
#include <tgl-layout.h>
|
#include <tgl/tgl-layout.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#ifndef json_boolean
|
#ifndef json_boolean
|
||||||
@ -331,6 +331,8 @@ json_t *json_pack_service (struct tgl_message *M) {
|
|||||||
|
|
||||||
json_t *json_pack_message (struct tgl_message *M) {
|
json_t *json_pack_message (struct tgl_message *M) {
|
||||||
json_t *res = json_object ();
|
json_t *res = json_object ();
|
||||||
|
assert (json_object_set (res, "event", json_string ("message")) >= 0);
|
||||||
|
//will overwriten to service, if service.
|
||||||
|
|
||||||
assert (json_object_set (res, "id", json_integer (M->id)) >= 0);
|
assert (json_object_set (res, "id", json_integer (M->id)) >= 0);
|
||||||
if (!(M->flags & TGLMF_CREATED)) { return res; }
|
if (!(M->flags & TGLMF_CREATED)) { return res; }
|
||||||
@ -366,8 +368,17 @@ json_t *json_pack_message (struct tgl_message *M) {
|
|||||||
assert (json_object_set (res, "media", json_pack_media (&M->media)) >= 0);
|
assert (json_object_set (res, "media", json_pack_media (&M->media)) >= 0);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
assert (json_object_set (res, "event", json_string ("service")) >= 0);
|
||||||
assert (json_object_set (res, "action", json_pack_service (M)) >= 0);
|
assert (json_object_set (res, "action", json_pack_service (M)) >= 0);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
json_t *json_pack_read (struct tgl_message *M) {
|
||||||
|
json_t *res = json_pack_message (M);
|
||||||
|
assert (json_object_set (res, "event", json_string ("read")) >= 0);
|
||||||
|
//this will overwrite "event":"message" to "event":"read".
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -8,5 +8,6 @@
|
|||||||
json_t *json_pack_message (struct tgl_message *M);
|
json_t *json_pack_message (struct tgl_message *M);
|
||||||
json_t *json_pack_updates (unsigned flags);
|
json_t *json_pack_updates (unsigned flags);
|
||||||
json_t *json_pack_peer (tgl_peer_id_t id, tgl_peer_t *P);
|
json_t *json_pack_peer (tgl_peer_id_t id, tgl_peer_t *P);
|
||||||
|
json_t *json_pack_read (struct tgl_message *M);
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user