Example #1
0
func monitorChannel(channelID string, api *slack.Client, messageChan <-chan *slack.MessageEvent, msgQueue *utility.Queue, monitorSize, doLogging bool, histCond *sync.Cond, threadWait *sync.WaitGroup) {
	defer threadWait.Done()
	defer logging.Log.Noticef("(%s) Finished monitoring channel", channelID)

	logging.Log.Infof("(%s) Waiting for history", channelID)
	histCond.L.Lock()
	histCond.Wait()
	histCond.L.Unlock()

	logging.Log.Debugf("(%s) Message queue has %v items", channelID, msgQueue.Len())

	logging.Log.Infof("(%s) Monitor size: %v", channelID, monitorSize)
	logging.Log.Infof("(%s) Do logging: %v", channelID, doLogging)

	logging.Log.Infof("(%s) Waiting for events", channelID)
monitorLoop:
	for {
		select {
		case message, chanOpen := <-messageChan:
			if !chanOpen {
				logging.Log.Errorf("(%s) Incoming message channel is closed", channelID)
				break monitorLoop
			}
			/*
				logging.Log.Debugf("(%s) Received message", channelID)
				logging.Log.Debugf("\tUser: %s", message.User)
				logging.Log.Debugf("\tChannel: %s", message.Channel)
				logging.Log.Debugf("\tTimestamp: %s", message.Timestamp)
				logging.Log.Debugf("\tText: %s", message.Text)
				logging.Log.Debugf("\tSubtype: %s", message.SubType)
			*/

			msgQueue.Push(message.Timestamp)
			toDelete := msgQueue.Poll().(string)
			//logging.Log.Debugf("(%s) Adding to queue: %s; Removing from queue: %s", channelID, message.Timestamp, toDelete)
			api.DeleteMessage(channelID, toDelete)

		}
	}

	return
}
Example #2
0
func cleanHistory(channelID string, api *slack.Client, msgQueue *utility.Queue, histSize int, histCond *sync.Cond, threadWait *sync.WaitGroup) {
	defer threadWait.Done()
	defer histCond.Signal()

	histCond.L.Lock()
	defer histCond.L.Unlock()

	logging.Log.Infof("(%s) Starting cleanHistory", channelID)
	histParams := slack.NewHistoryParameters()
	histParams.Inclusive = true

	histCountMax := 1000

	// build history with histSize messages
	logging.Log.Infof("(%s) Building history with %v messages", channelID, histSize)
	var history *slack.History
	var histErr error
	nRemaining := histSize
	for nRemaining > 0 {
		if nRemaining > histCountMax {
			histParams.Count = histCountMax
		} else {
			histParams.Count = nRemaining
		}

		history, histErr = api.GetChannelHistory(channelID, histParams)
		if histErr != nil {
			logging.Log.Errorf("(%s) Unable to get the channel history: %v", channelID, histErr)
			return
		}

		iLastMsg := len(history.Messages) - 1
		//logging.Log.Debug("0: %v, %v: %v", history.Messages[0].Timestamp, iLastMsg, history.Messages[iLastMsg].Timestamp)

		logging.Log.Debugf("(%s) In skip loop; obtained history with %v messages", channelID, len(history.Messages))

		for iMsg := iLastMsg; iMsg >= 0; iMsg-- {
			msgQueue.Push(history.Messages[iMsg].Timestamp)
			//logging.Log.Debugf("(%s) Pushing to queue: %s", channelID, history.Messages[iMsg].Timestamp)
		}

		if !history.HasMore {
			return
		}
		histParams.Latest = history.Messages[iLastMsg].Timestamp
		histParams.Inclusive = false
		nRemaining -= histCountMax
	}

	histParams.Count = histCountMax
	nDeleted := 0
	for history.HasMore == true {
		history, histErr = api.GetChannelHistory(channelID, histParams)
		if histErr != nil {
			logging.Log.Errorf("(%s) Unable to get the channel history: %v", channelID, histErr)
			return
		}

		logging.Log.Debugf("(%s) Deleting %v items (latest: %v)", channelID, len(history.Messages), history.Latest)

		for _ /*iMsg*/, message := range history.Messages {
			//logging.Log.Debugf("(%s) Deleting: %s", channelID, message.Timestamp)
			_, _ /*respChan, respTS,*/, respErr := api.DeleteMessage(channelID, message.Timestamp)
			if respErr != nil {
				logging.Log.Warningf("(%s) Unable to delete message: %v", channelID, respErr)
			}
			//logging.Log.Debugf("(%s) Deletion response: %s, %s, %v", channelID, respChan, respTS, respErr)
			nDeleted++
		}
		histParams.Latest = history.Messages[len(history.Messages)-1].Timestamp
	}
	logging.Log.Noticef("(%s) Deleted %v messages", channelID, nDeleted)

	return
}