Commit 8b8a69ec authored by luckydonald's avatar luckydonald

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)
parent 5935c97e
...@@ -1870,6 +1870,7 @@ void print_filename_gw (struct tgl_state *TLSR, void *extra, int success, const ...@@ -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[]) { ...@@ -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,6 +2172,7 @@ void print_read_list (int num, struct tgl_message *list[]) { ...@@ -2162,6 +2172,7 @@ void print_read_list (int num, struct tgl_message *list[]) {
} }
assert (c1 + c2 > 0); assert (c1 + c2 > 0);
if (!enable_json) {
mpush_color (ev, COLOR_YELLOW); mpush_color (ev, COLOR_YELLOW);
switch (tgl_get_peer_type (to_id)) { switch (tgl_get_peer_type (to_id)) {
case TGL_PEER_USER: case TGL_PEER_USER:
...@@ -2182,6 +2193,7 @@ void print_read_list (int num, struct tgl_message *list[]) { ...@@ -2182,6 +2193,7 @@ void print_read_list (int num, struct tgl_message *list[]) {
mprintf (ev, " marked read %d outbox and %d inbox messages\n", c1, c2); mprintf (ev, " marked read %d outbox and %d inbox messages\n", c1, c2);
mpop_color (ev); mpop_color (ev);
} }
}
mprint_end (ev); mprint_end (ev);
} }
...@@ -2374,6 +2386,7 @@ void print_peer_updates (struct in_ev *ev, int flags) { ...@@ -2374,6 +2386,7 @@ 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);
......
...@@ -3,8 +3,8 @@ ...@@ -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) { ...@@ -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) { ...@@ -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 @@ ...@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment