<commit_before>// Server goes crazy when we try to dismiss notifications on every navigation.
// As of now, server only supports dismissing all notifications.
// But this is annoying for users as notifications don't really go away.
// So we need to handle this in better way which doesn't put load on server.
// We save the dismissed notifications in `AsyncStorage`, and avoid showing them.
// When there are no notifications left, we dismiss all notifications.

import { AsyncStorage } from "react-native";
import core from "../../store/core";
import store from "../../store/store";

const NOTIFICATION_STORE_KEY = "notifications_dismissed_store";

let dismissed;

function getNoteIdentifier(note) {
	return note.ref + ":" + note.group + ":" + note.noteType;
}

async function getDismissedNotes() {
	const notes = await AsyncStorage.getItem(NOTIFICATION_STORE_KEY);

	if (notes) {
		return JSON.parse(notes);
	} else {
		return null;
	}
}

function dismissAllNotes() {
	dismissed = [];
	core.emit("note-up", { dismissTime: Date.now() });

	return AsyncStorage.setItem(NOTIFICATION_STORE_KEY, "[]");
}

function dismissNote(note) {
	const notes = store.get("notes");

	if (notes.length === 0) {
		if (dismissed && dismissed.length) {
			dismissAllNotes();
		}

		if (Object.keys(note).length === 1) {
			return null;
		}
	}

	const dismiss = notes.filter(n => {
		if (note.noteType) {
			if (note.group) {
				return n.noteType === note.noteType && n.ref === note.ref && n.group === note.group;
			} else if (note.ref) {
				return n.noteType === note.noteType && n.ref === note.ref;
			} else {
				return n.noteType === note.noteType;
			}
		} else if (note.group) {
			if (note.ref) {
				return n.group === note.group && n.ref === note.ref;
			} else {
				return n.group === note.group;
			}
		} else if (note.ref) {
			return n.ref === note.ref;
		} else {
			return false;
		}
	});

	for (let i = 0, l = dismiss.length; i < l; i++) {
		const id = getNoteIdentifier(dismiss[i]);

		if (dismissed.indexOf(id) === -1) {
			dismissed.push(id);
		}
	}

	return AsyncStorage.setItem(NOTIFICATION_STORE_KEY, JSON.stringify(dismissed));
}

function loadNotes() {
	core.emit("getNotes", {}, async (err, res) => {
		let notes;

		if (err) {
			return;
		}

		if (res.results && res.results.length) {
			notes = res.results;

			try {
				dismissed = await getDismissedNotes();

				if (Array.isArray(dismissed)) {
					notes = notes.filter(n => dismissed.indexOf(getNoteIdentifier(n)) < 0);
				}
			} catch (e) {<commit_msg>Fix notificaions not dismissing<commit_after>// Server goes crazy when we try to dismiss notifications on every navigation.
// As of now, server only supports dismissing all notifications.
// But this is annoying for users as notifications don't really go away.
// So we need to handle this in better way which doesn't put load on server.
// We save the dismissed notifications in `AsyncStorage`, and avoid showing them.
// When there are no notifications left, we dismiss all notifications.

import { AsyncStorage } from "react-native";
import core from "../../store/core";
import store from "../../store/store";

const NOTIFICATION_STORE_KEY = "notifications_dismissed_store";

let dismissed = [];

function getNoteIdentifier(note) {
	return note.ref + ":" + note.group + ":" + note.noteType;
}

async function getDismissedNotes() {
	const notes = await AsyncStorage.getItem(NOTIFICATION_STORE_KEY);

	if (notes) {
		return JSON.parse(notes);
	} else {
		return null;
	}
}

function dismissAllNotes() {
	dismissed = [];
	core.emit("note-up", { dismissTime: Date.now() });

	return AsyncStorage.setItem(NOTIFICATION_STORE_KEY, "[]");
}

function dismissNote(note) {
	const notes = store.get("notes");

	if (notes.length === 0) {
		if (dismissed && dismissed.length) {
			dismissAllNotes();
		}

		if (Object.keys(note).length === 1) {
			return null;
		}
	}

	const dismiss = notes.filter(n => {
		if (note.noteType) {
			if (note.group) {
				return n.noteType === note.noteType && n.ref === note.ref && n.group === note.group;
			} else if (note.ref) {
				return n.noteType === note.noteType && n.ref === note.ref;
			} else {
				return n.noteType === note.noteType;
			}
		} else if (note.group) {
			if (note.ref) {
				return n.group === note.group && n.ref === note.ref;
			} else {
				return n.group === note.group;
			}
		} else if (note.ref) {
			return n.ref === note.ref;
		} else {
			return false;
		}
	});

	for (let i = 0, l = dismiss.length; i < l; i++) {
		const id = getNoteIdentifier(dismiss[i]);

		if (dismissed.indexOf(id) === -1) {
			dismissed.push(id);
		}
	}

	return AsyncStorage.setItem(NOTIFICATION_STORE_KEY, JSON.stringify(dismissed));
}

function loadNotes() {
	core.emit("getNotes", {}, async (err, res) => {
		let notes;

		if (err) {
			return;
		}

		if (res.results && res.results.length) {
			notes = res.results;

			try {
				dismissed = await getDismissedNotes();

				if (Array.isArray(dismissed)) {
					notes = notes.filter(n => dismissed.indexOf(getNoteIdentifier(n)) < 0);
				} else {
					dismissed = [];
				}
			} catch (e) {
{'fraction_non_alphanumeric': 0.15232307390707706, 'fraction_numerical': 0.001960399921584003, 'mean_word_length': 3.334749362786746, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 9, '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': '4607387', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1664, 'n_words': 461}