From 64cd31c0b5d14f5a3279d9552e80243c391b99d6 Mon Sep 17 00:00:00 2001 From: Tony Olagbaiye Date: Sat, 26 May 2018 15:14:02 +0100 Subject: [PATCH] thread_broadcast partial impl --- .../slack-api-message-thread_broadcast.c | 99 +++++++++++++++++++ .../slack-api-message-thread_broadcast.h | 12 +++ 2 files changed, 111 insertions(+) create mode 100644 api/message/slack-api-message-thread_broadcast.c create mode 100644 api/message/slack-api-message-thread_broadcast.h diff --git a/api/message/slack-api-message-thread_broadcast.c b/api/message/slack-api-message-thread_broadcast.c new file mode 100644 index 0000000..1c3c4e5 --- /dev/null +++ b/api/message/slack-api-message-thread_broadcast.c @@ -0,0 +1,99 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, version 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include +#include + +#include "../../weechat-plugin.h" +#include "../../slack.h" +#include "../../slack-workspace.h" +#include "../../slack-message.h" +#include "../../slack-api.h" +#include "../../slack-channel.h" +#include "../../slack-user.h" +#include "../slack-api-message.h" +#include "slack-api-message-thread_broadcast.h" + +static const char *subtype = "thread_broadcast"; + +static inline int json_valid(json_object *object, struct t_slack_workspace *workspace) +{ + if (!object) + { + weechat_printf( + workspace->buffer, + _("%s%s: error handling websocket %smessage.%s%s message: " + "unexpected response from server"), + weechat_prefix("error"), SLACK_PLUGIN_NAME, + weechat_color("chat_value"), subtype, weechat_color("reset")); + return 0; + } + + return 1; +} + +int slack_api_message_thread_broadcast_handle(struct t_slack_workspace *workspace, + json_object *root, const char *user, + const char *text, const char *ts) +{ + struct t_slack_channel *ptr_channel; + struct t_slack_user *ptr_user; + struct t_slack_channel_typing *ptr_typing; + + /* + ptr_channel = slack_channel_search(workspace, channel); + if (!ptr_channel) + return 1; // silently ignore if channel hasn't been loaded yet + ptr_user = slack_user_search(workspace, user); + if (!ptr_user) + return 1; // silently ignore if user hasn't been loaded yet + + char *message = slack_message_decode(workspace, text); + weechat_printf_date_tags( + ptr_channel->buffer, + (time_t)atof(ts), + "slack_message,slack_thread_broadcast", + _("%s%s"), + slack_user_as_prefix(workspace, ptr_user, NULL), + message); + free(message); + + ptr_typing = slack_channel_typing_search(ptr_channel, + ptr_user->profile.display_name); + if (ptr_typing) + { + slack_channel_typing_free(ptr_channel, ptr_typing); + slack_channel_typing_cb(ptr_channel, NULL, 0); + } + */ + + return 1; +} + +int slack_api_message_thread_broadcast(struct t_slack_workspace *workspace, + json_object *message) +{ + json_object *root, *user, *text, *ts; + root = json_object_object_get(message, "root"); + if (!json_valid(root, workspace)) + return 0; + + user = json_object_object_get(message, "user"); + if (!json_valid(user, workspace)) + return 0; + + text = json_object_object_get(message, "text"); + if (!json_valid(text, workspace)) + return 0; + + ts = json_object_object_get(message, "ts"); + if (!json_valid(ts, workspace)) + return 0; + + return slack_api_message_thread_broadcast_handle(workspace, + root, json_object_get_string(user), + json_object_get_string(text), + json_object_get_string(ts)); +} + diff --git a/api/message/slack-api-message-thread_broadcast.h b/api/message/slack-api-message-thread_broadcast.h new file mode 100644 index 0000000..ea56ce7 --- /dev/null +++ b/api/message/slack-api-message-thread_broadcast.h @@ -0,0 +1,12 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, version 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#ifndef _SLACK_API_MESSAGE_THREAD_BROADCAST_H_ +#define _SLACK_API_MESSAGE_THREAD_BROADCAST_H_ + +int slack_api_message_thread_broadcast( + struct t_slack_workspace *workspace, + json_object *message); + +#endif /*SLACK_API_MESSAGE_THREAD_BROADCAST_H*/