forked from dylex/slack-libpurple
-
Notifications
You must be signed in to change notification settings - Fork 2
/
slack-conversation.c
99 lines (82 loc) · 3.11 KB
/
slack-conversation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "slack-json.h"
#include "slack-api.h"
#include "slack-channel.h"
#include "slack-im.h"
#include "slack-conversation.h"
static void conversation_update(SlackAccount *sa, json_value *json) {
if (json_get_prop_boolean(json, "is_im", FALSE))
slack_im_set(sa, json, &json_value_none);
else
slack_channel_set(sa, json, SLACK_CHANNEL_UNKNOWN);
}
#define CONVERSATIONS_LIST_CALL(sa, ARGS...) \
slack_api_call(sa, conversations_list_cb, NULL, "conversations.list", "types", "public_channel,private_channel,mpim,im", "exclude_archived", "true", SLACK_PAGINATE_LIMIT, ##ARGS, NULL)
static void conversations_list_cb(SlackAccount *sa, gpointer data, json_value *json, const char *error) {
json_value *chans = json_get_prop_type(json, "channels", array);
if (!chans) {
purple_connection_error_reason(sa->gc,
PURPLE_CONNECTION_ERROR_NETWORK_ERROR, error ?: "Missing conversation list");
return;
}
for (unsigned i = 0; i < chans->u.array.length; i++)
conversation_update(sa, chans->u.array.values[i]);
char *cursor = json_get_prop_strptr(json_get_prop(json, "response_metadata"), "next_cursor");
if (cursor && *cursor)
CONVERSATIONS_LIST_CALL(sa, "cursor", cursor);
else
slack_login_step(sa);
}
void slack_conversations_load(SlackAccount *sa) {
g_hash_table_remove_all(sa->channels);
g_hash_table_remove_all(sa->ims);
CONVERSATIONS_LIST_CALL(sa);
}
SlackObject *slack_conversation_get_conversation(SlackAccount *sa, PurpleConversation *conv) {
switch (conv->type) {
case PURPLE_CONV_TYPE_IM:
return g_hash_table_lookup(sa->user_names, purple_conversation_get_name(conv));
case PURPLE_CONV_TYPE_CHAT:
return g_hash_table_lookup(sa->channel_cids, GUINT_TO_POINTER(purple_conv_chat_get_id(PURPLE_CONV_CHAT(conv))));
default:
return NULL;
}
}
static gboolean mark_conversation_timer(gpointer data) {
SlackAccount *sa = data;
sa->mark_timer = 0; /* always return FALSE */
/* we just send them all at once -- maybe would be better to chain? */
SlackObject *next = sa->mark_list;
sa->mark_list = MARK_LIST_END;
while (next != MARK_LIST_END) {
SlackObject *obj = next;
next = obj->mark_next;
obj->mark_next = NULL;
g_free(obj->last_mark);
obj->last_mark = g_strdup(obj->last_read);
/* XXX conversations.mark call??? */
slack_api_channel_call(sa, NULL, NULL, obj, "mark", "ts", obj->last_mark, NULL);
}
return FALSE;
}
void slack_mark_conversation(SlackAccount *sa, PurpleConversation *conv) {
SlackObject *obj = slack_conversation_get_conversation(sa, conv);
if (!obj)
return;
int c = GPOINTER_TO_INT(purple_conversation_get_data(conv, "unseen-count"));
if (c != 0)
/* we could update read count to farther back, but best to only move it forward to latest */
return;
if (slack_ts_cmp(obj->last_mesg, obj->last_mark) <= 0)
return; /* already marked newer */
g_free(obj->last_read);
obj->last_read = g_strdup(obj->last_mesg);
if (obj->mark_next)
return; /* already on list */
/* add to list */
obj->mark_next = sa->mark_list;
sa->mark_list = obj;
if (sa->mark_timer)
return; /* already running */
/* start */
sa->mark_timer = purple_timeout_add_seconds(5, mark_conversation_timer, sa);
}