Skip to content
This repository has been archived by the owner on Mar 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #109 from Arquisoft/notifications
Browse files Browse the repository at this point in the history
Notifications added
  • Loading branch information
RisottoNacho committed Apr 30, 2019
2 parents dcfb6eb + 1790fb7 commit dc333ff
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 49 deletions.
Binary file added src/images/notis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 84 additions & 8 deletions src/scripts/chatManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,49 @@ var MESSAGES = {
toShow: []
}

async function setUpFolder(dontHave) {
var ret = false;

//Define folders name
var solidChat = INFO.userURI + "public/SolidChat/";
var folder = solidChat + INFO.receiverName.replace(/ /g, "-") + "/";
var filename = folder + "chatld.jsonld";

//WritingMessage
try {
var err3 = await podUtils.readFile(filename);
if (!err3 || dontHave) {
throw ("error");
} else {
ret = true;
}
} catch (error) {
//IF folder doesnt exist: create new user folder
try {
var err2 = await podUtils.readFolder(folder, ToLog);
if (!err2 || dontHave) {
throw ("error");
}
} catch (error) {
//Check Folder SolidChat
try {
var err = await podUtils.readFolder(solidChat, ToLog);
if (!err || dontHave) {
throw ("error");
}
} catch (error) {
//New Solid-Chat folder
await podUtils.createFolder(solidChat, ToLog);
}

//New Folder:
await podUtils.createFolder(folder, ToLog);
}
return await podUtils.writeMessage(folder + "cache.txt", "");
}
return ret;
}

//SEND Message function login
async function sendMessage(text, test) {
var ret = false;
Expand Down Expand Up @@ -92,16 +135,35 @@ async function sendMessage(text, test) {
"text": text
}]
};
await podUtils.writeMessage(folder + "cache.txt", "");

jsonString = JSON.stringify(chat);

ret = await podUtils.writeMsgJsonld(folder + "chatld", jsonString, ToLog);
if (notify)
await notiMan.writeNotification(INFO.receiverURI, INFO.user);
}
return ret;
}

async function checkNewMessages(receiverFolder, receiver) {
let uFolder = INFO.userURI + "public/SolidChat/" + receiver.trim().replace(/ /g, "-") + "/";
var rFolder = receiverFolder + "public/SolidChat/" + INFO.userName.trim().replace(/ /g, "-") + "/";
try {
let cache = await podUtils.readFile(uFolder + "cache.txt", ToLog);
if (cache == null)
throw ('error');
let receiveMessages = await podUtils.readFile(rFolder + "chatld.jsonld", ToLog);
let parsedReciever = JSON.parse(receiveMessages).messages.pop().text;
if (cache == "" && parsedReciever == "")
return false;
if (cache != parsedReciever) {
return true;
} else
return false;
} catch (error) {
return false;
}
}

async function receiveMessages() {
//Define folders name
var uFolder = INFO.userURI + "public/SolidChat/" + INFO.receiverName.trim().replace(/ /g, "-") + "/";
Expand All @@ -127,6 +189,23 @@ async function receiveMessages() {

var uParsed = JSON.parse(userMessages).messages;
var rParsed = JSON.parse(receiveMessages).messages;

let tosend;
if (rParsed) {
tosend = rParsed.pop();
rParsed.push(tosend);
tosend = tosend.text;
}

if (uFolder && tosend) {
if (await podUtils.readFile(uFolder + "cache.txt") == null)
await podUtils.writeMessage(uFolder + "cache.txt", tosend);
else {
await podUtils.deleteFile(uFolder + "cache.txt")
await podUtils.writeMessage(uFolder + "cache.txt", tosend);
}
}

if (uParsed) {
uParsed.forEach(element => {
var date = new Date(Number(element.dateSent));
Expand All @@ -153,10 +232,6 @@ async function receiveMessages() {
});
MESSAGES.toShow = MESSAGES.toShow.slice(-10);

//Delete existing notifiations
if (notify)
notiMan.deleteNotification(INFO.userURI, INFO.receiver);

return MESSAGES.toShow;
}

Expand All @@ -169,8 +244,9 @@ async function newNotifications() {
module.exports = {
sendMessage: sendMessage,
receiveMessages: receiveMessages,
newNotifications: newNotifications,
checkNewMessages: checkNewMessages,
INFO: INFO,
MESSAGES: MESSAGES,
ToLog: ToLog
ToLog: ToLog,
setUpFolder: setUpFolder
}
127 changes: 89 additions & 38 deletions src/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const store = $rdf.graph();
const fetcher = new $rdf.Fetcher(store);

class friend {
constructor(uri, name){
constructor(uri, name) {
this.uri = uri;
this.name= name;
this.name = name;
}
}

Expand All @@ -21,13 +21,13 @@ $('#login button').click(() => $('#modalIDP').modal('show'));
$('#logout button').click(() => loginM.logout());

//"Login with SOLID Community" should redirect to solid.community login page
$("#solidLogin").click(function() {
$("#solidLogin").click(function () {
$('#desiredIDP').val('https://solid.community');
loginM.login();
});

//Login with desired IDP button
$("#idpLogin").click(function() {
$("#idpLogin").click(function () {
loginM.login();
});

Expand All @@ -46,6 +46,7 @@ solid.auth.trackSession(session => {
//SendMessage Function, Send Button on click action
$('#sendButton').click(
async function sendFunc() {
//await checkNotifications();
if (document.getElementById("friends").value == "")
alert("Debe seleccionar un usuario.");
else {
Expand All @@ -65,12 +66,12 @@ $('#sendButton').click(
);

$('#filtro-nombre').on(
'input', async function(e){
'input', async function (e) {
var sortedFriends = await getFriends(), filteredFriends = [];
var nombreFiltro = $("#filtro-nombre").val();

for (i = 0; i < sortedFriends.length; i++) {
if (sortedFriends[i].name.toLowerCase().indexOf(nombreFiltro.toLowerCase()) != -1 ){
if (sortedFriends[i].name.toLowerCase().indexOf(nombreFiltro.toLowerCase()) != -1) {
filteredFriends.push(sortedFriends[i]);
}
}
Expand All @@ -81,47 +82,99 @@ $('#filtro-nombre').on(
async function getFriends() {
const friends = store.each($rdf.sym(chatM.INFO.user), FOAF('knows'));
$('#friends').empty();


var sortedFriends = [];

await Promise.all(friends.map(async f => {
await fetcher.load(f);
sortedFriends.push(new friend(f.value, await store.any(f, FOAF('name')).toString()));
}));

sortedFriends.sort(function (a, b) {
return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
});

return sortedFriends;
}

async function getFriendsAlternative() {
const friends = store.each($rdf.sym(chatM.INFO.user), FOAF('knows'));

var sortedFriends = [];

await Promise.all(friends.map(async f => {
await fetcher.load(f);
sortedFriends.push(new friend(f.value, await store.any(f,FOAF('name')).toString()));
sortedFriends.push(new friend(f.value, await store.any(f, FOAF('name')).toString()));
}));
sortedFriends.sort(function (a,b) {

sortedFriends.sort(function (a, b) {
return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
});

return sortedFriends;
}

async function checkNotifications() {
let friends = await getFriendsAlternative();
let ls = $(".list-group-item-action");
//console.log(ls);
for (let i = 0; i < ls.length; i++) {
let uri = friends[i].uri.substr(0, (friends[i].uri.length - 15));
let name = friends[i].name.trim();
if (await chatM.checkNewMessages(uri, name) && ls[i].classList[2] == 'noactive') {
ls[i].classList.add("noti");
ls[i].innerHTML += '<i class="ico"></i>';
}else{
ls[i].classList.remove("noti");
ls[i].innerHTML = friends[i].name;
}
}
console.log($(".list-group-item-action"));
}

window.setInterval(async function () {
await checkNotifications();
}, 5000);


async function showFriends(sortedFriends) {
sortedFriends.forEach(
async (friend) => {
await fetcher.load(friend);
$('#friends').append(
$('<button>').attr('type', 'button').addClass("list-group-item list-group-item-action noactive").text(friend.name).click(
async function () {
if (chatM.ToLog)
console.log("load new receiver");
//Store all reciever info need for future
chatM.INFO.receiver = friend.uri;
chatM.INFO.receiverName = friend.name.trim();
chatM.INFO.receiverURI = chatM.INFO.receiver.substr(0, (chatM.INFO.receiver.length - 15));

//Add the selected marker (That blue thing..)
$("#friends button").removeClass("active");
$("#friends button").addClass("noactive");
$(this).removeClass("noactive");
$(this).addClass("active");
//Show messages
updateMessages(await chatM.receiveMessages());
}
));
});
async (friend) => {
let notification = false;
await fetcher.load(friend);
let = clase = "";
if (notification) {
clase = "noti";
}
$('#friends').append(
$('<button>').attr('type', 'button').addClass("list-group-item list-group-item-action noactive " + clase).text(friend.name).click(
async function () {
if (chatM.ToLog)
console.log("load new receiver");
//Store all reciever info need for future
chatM.INFO.receiver = friend.uri;
chatM.INFO.receiverName = friend.name.trim();
chatM.INFO.receiverURI = chatM.INFO.receiver.substr(0, (chatM.INFO.receiver.length - 15));
chatM.setUpFolder();

//Add the selected marker (That blue thing..)
$("#friends button").removeClass("active");
$("#friends button").addClass("noactive");
$(this).removeClass("noactive");
$(this).addClass("active");
//Show messages
updateMessages(await chatM.receiveMessages());
await checkNotifications();
}
));

});
}

window.setInterval(async function () {
updateMessages(await chatM.receiveMessages());
}, 2000);

async function loadProfile() {
if (chatM.ToLog)
console.log("loading Profile");
Expand Down Expand Up @@ -151,9 +204,7 @@ async function loadProfile() {
}


window.setInterval(async function () {
updateMessages(await chatM.receiveMessages());
}, 2000);


function updateMessages(toShow) {
if (chatM.ToLog)
Expand Down
3 changes: 2 additions & 1 deletion src/scripts/podUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,6 @@ module.exports = {
writeTurtle: writeTurtle,
updateTurtle: updateTurtle,
writeMsgJson: writeMsgJson,
writeMsgJsonld: writeMsgJsonld
writeMsgJsonld: writeMsgJsonld,
writeMessage: writeMessage
}
25 changes: 24 additions & 1 deletion src/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ dt {
color: white !important;
background-color: #9c84ff !important;
}

*/
.nav-link:hover {
background-color: pink;
}
Expand Down Expand Up @@ -103,6 +103,29 @@ dt {

}

.noti{
background-color: rgb(22, 197, 75) !important;
}

.ico{
width:inherit;
height:inherit;
background:black;
color:white;
text-decoration:none;
text-align: end;
}
.ico:before{
content: '';
background:url('../images/notis.png');
background-size:cover;
position:absolute;
width:25px;
height:25px;
float: right;

}

/* Darker chat container */
.containerChatDarker {
width: inherit;
Expand Down
2 changes: 1 addition & 1 deletion src/two-people-chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<div class="well">
<h5 class="centered whitecolor" style="padding: 0.75em;" id="h5friends"></h5>
<p style="text-align:center;" id="h5friendsDescription"></p>
<div class="container" style="margin-bottom: 1em;">
<div class="container" style="margin-bottom: 1em;">
<input type="text" class="form-control" placeholder="Filter by name" id="filtro-nombre"/>
</div>
<div class="list-group list-group-flush forcefullwidth" id="friends">
Expand Down
Loading

0 comments on commit dc333ff

Please sign in to comment.