push.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Sapphire backend
  3. *
  4. * Copyright (C) 2018 Alyssa Rosenzweig
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
  19. *
  20. */
  21. #include "push.h"
  22. #include "websocket.h"
  23. /* Has a push notification been sent since the last login? We default to TRUE
  24. * to avoid sporadic push notifications before the first connection*/
  25. gboolean sapphire_already_pushed = TRUE;
  26. /* We got a connection, so we're clear for another push */
  27. void
  28. sapphire_push_connected(void)
  29. {
  30. sapphire_already_pushed = FALSE;
  31. }
  32. /* Send a push notification with the given content. This wrapper function
  33. * determines whether the notification should actually be pushed, handling
  34. * state appropriately. Actual medium-specific sending is delegated. */
  35. extern gboolean sapphire_any_connected_clients;
  36. void
  37. sapphire_push_notification(Connection *conn, const gchar *content)
  38. {
  39. /* Push notifications can only be sent if:
  40. *
  41. * 0. Push notifications are enabled (TODO)
  42. * 1. There are no connected clients
  43. * 2. There has not been another push notification sent
  44. *
  45. * Check these criteria
  46. */
  47. if (sapphire_any_connected_clients) return;
  48. if (sapphire_already_pushed) return;
  49. /* We've passed the criteria, so send the notification via the proxy */
  50. JsonObject *resp = json_object_new();
  51. json_object_set_string_member(resp, "op", "push");
  52. json_object_set_string_member(resp, "content", content);
  53. gchar *str = json_object_to_string(resp);
  54. gchar *str_prefixed = g_strdup_printf(">%s", str);
  55. sapphire_send_raw_packet(str_prefixed);
  56. json_object_unref(resp);
  57. g_free(str_prefixed);
  58. g_free(str);
  59. /* Record that we pushed something */
  60. sapphire_already_pushed = TRUE;
  61. }