示例#1
0
        /// <summary>
        /// Index all messages of a channel
        /// </summary>
        /// <param name="channelName">Channel name for logging</param>
        /// <param name="channel">Json object which contains messages</param>
        /// <returns></returns>
        public bool IndexChannelMessages(string channelName, JsonObject channel)
        {
            Sys.Log("Index Messages of '" + channelName + "'");
            bool         ok  = true;
            SlackMessage doc = new SlackMessage();

            doc.cntr = this;
            if (Connector.IsModeRealTime())
            {
                Sys.Log("Index messages from date: " + Connector.RealTimeReferenceDate);
            }
            string oldest   = Connector.IsModeRealTime() ? Sys.ToStr(Dat.ToUnixTimestamp(Connector.RealTimeReferenceDate)) : "";
            string latest   = "";
            bool   has_more = false;

            do
            {
                //iterate on all messages by page (has_more = true)
                has_more = false;
                JsonObject response = slackGet("conversations.history?channel=" + channel.ValueStr("id") + "&latest=" + latest + "&oldest=" + oldest) as JsonObject;
                if (response == null || (response != null && !response.ValueBoo("ok")))
                {
                    Sys.LogError(Json.Serialize(response)); return(false);
                }
                if (response != null)
                {
                    has_more = response.ValueBoo("has_more", false);
                    JsonArray messages = response.GetAsArray("messages");
                    if (messages != null)
                    {
                        for (int i = 0; i < messages.EltCount(); i++)
                        {
                            //clear and reuse the doc instance
                            doc.Clear();

                            //current message
                            JsonObject message = messages.Elt(i) as JsonObject;

                            //set custom to connectorDoc (slackDoc)
                            doc.channel = channel;
                            doc.message = message;

                            //set mandatory fields: id, fileext & version
                            doc.Id      = message.GetValue("user") + "-" + message.GetValue("ts");
                            doc.Version = message.GetValue("ts");
                            doc.FileExt = "htm";


                            //set latest for Slack API Pagination
                            latest = doc.Version;

                            //Process Doc (send to Indexer)
                            ok &= Connector.ProcessConnectorDoc(doc);
                        }
                    }
                }
            }while (has_more);
            return(ok);
        }