<commit_before>package topics

import (
	"github.com/bulletind/khabar/db"
	"github.com/bulletind/khabar/utils"
)

func Update(user, org, ident string, doc *utils.M) error {

	return db.Conn.Update(db.TopicCollection,
		utils.M{
			"org":   org,
			"user":  user,
			"ident": ident,
		},
		utils.M{
			"$set": *doc,
		})
}

func Insert(topic *db.Topic) string {
	return db.Conn.Insert(db.TopicCollection, topic)
}

func Delete(doc *utils.M) error {
	return db.Conn.Delete(db.TopicCollection, *doc)
}

/**
 * Insert a topic in `topics` collection if it doesn't exist
 * Or Update the topic
 *
 * - 	This is not a very effecient way of doing it but we are doing it at
 * 		the cost of the data structure we want
 * - 	One of the limitations is that mongo is not yet capable of
 * 		upserting to array of documents. For this to happen in a simple way the data
 * 		structure has to change OR we use multiple collections
 */

func InsertOrUpdateTopic(org, ident, channelName, attr string, val bool, user string) error {

	var channels []db.Channel
	var channel db.Channel
	var doc utils.M
	var spec utils.M

	found := new(db.Topic)
	query := utils.M{
		"org":   org,
		"user":  user, // empty for org
		"ident": ident,
	}

	// See if this setting already exists
	err := db.Conn.GetOne(
		db.TopicCollection,
		query,
		found,
	)

	// Fetch the global setting
	// Because if the above setting already doesn't exist, then set the other
	// attr to what is set in global
	if attr == "Default" || attr == "Locked" {
		global := new(db.Topic)
		q := utils.M{"org": "", "user": "", "ident": ident}
		e := db.Conn.GetOne(db.TopicCollection, q, &global)

		if e == nil {
			for _, ch := range global.Channels {
				if ch.Name == channelName {
					if attr == "Default" {
						channel.Locked = ch.Locked
					} else if attr == "Locked" {
						channel.Default = ch.Default
					}
				} else if err != nil {
					channels = append(channels, ch)
				}
			}
		}
	}

	channel.Name = channelName

	if attr == "Default" {
		channel.Default = val
	} else if attr == "Locked" {
		channel.Locked = val
	} else {
		channel.Enabled = val
	}

	channels = append(channels, channel)

	// If it doesn't exist, insert and return<commit_msg>use global setting for the user as well<commit_after>package topics

import (
	"github.com/bulletind/khabar/db"
	"github.com/bulletind/khabar/utils"
)

func Update(user, org, ident string, doc *utils.M) error {

	return db.Conn.Update(db.TopicCollection,
		utils.M{
			"org":   org,
			"user":  user,
			"ident": ident,
		},
		utils.M{
			"$set": *doc,
		})
}

func Insert(topic *db.Topic) string {
	return db.Conn.Insert(db.TopicCollection, topic)
}

func Delete(doc *utils.M) error {
	return db.Conn.Delete(db.TopicCollection, *doc)
}

/**
 * Insert a topic in `topics` collection if it doesn't exist
 * Or Update the topic
 *
 * - 	This is not a very effecient way of doing it but we are doing it at
 * 		the cost of the data structure we want
 * - 	One of the limitations is that mongo is not yet capable of
 * 		upserting to array of documents. For this to happen in a simple way the data
 * 		structure has to change OR we use multiple collections
 */

func InsertOrUpdateTopic(org, ident, channelName, attr string, val bool, user string) error {

	var channels []db.Channel
	var channel db.Channel
	var doc utils.M
	var spec utils.M

	found := new(db.Topic)
	query := utils.M{
		"org":   org,
		"user":  user, // empty for org
		"ident": ident,
	}

	// See if this setting already exists
	err := db.Conn.GetOne(
		db.TopicCollection,
		query,
		found,
	)

	// Fetch the global setting
	// Because if the above setting already doesn't exist, then set the other
	// attr to what is set in global

	global := new(db.Topic)
	q := utils.M{"org": "", "user": "", "ident": ident}
	e := db.Conn.GetOne(db.TopicCollection, q, &global)

	if e == nil {
		for _, ch := range global.Channels {
			if ch.Name == channelName {
				if attr == "Default" {
					channel.Locked = ch.Locked
				} else if attr == "Locked" {
					channel.Default = ch.Default
				}
			} else if err != nil {
				channels = append(channels, ch)
			}
		}
	}

	channel.Name = channelName

	if attr == "Default" {
		channel.Default = val
	} else if attr == "Locked" {
		channel.Locked = val
	} else {
		channel.Enabled = val
	}

	channels = append(channels, channel)

	// If it doesn't exist, insert and return
{'fraction_non_alphanumeric': 0.1235750113999088, 'fraction_numerical': 0.0, 'mean_word_length': 2.999088422971741, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6161409', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1480, 'n_words': 507}