Skip to content
This repository has been archived by the owner on May 3, 2019. It is now read-only.

Commit

Permalink
remove firebase PMs, fix firebase token reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Caerostris committed Feb 27, 2015
1 parent 15d3e31 commit 58cfa7a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 95 deletions.
17 changes: 17 additions & 0 deletions class.van2shout.plugin.php
Expand Up @@ -81,6 +81,23 @@ public function VanillaController_Shoutbox_Create($Sender) {
$Sender->Render($this->GetView('discussionscontroller.php'));
}

// handle firebase token requests
public function PluginController_Van2ShoutFirebase_Create($Sender) {
// check if user is allowed to view
$Session = GDN::Session();
if(!$Session->CheckPermission('Plugins.Van2Shout.View'))
return;

// check if the firebase backend is active
if(!C('Plugin.Van2Shout.Firebase.Enable', false))
return;

// display last shoutbox posts
include_once(dirname(__FILE__).DS.'controllers'.DS.'class.van2shoutfirebase.php');
$Van2ShoutFirebase = new Van2ShoutFirebase($Sender);
echo $Van2ShoutFirebase->ToString();
}

// register a new controller which lists the shoutbox messages as json when using the local backend
public function PluginController_Van2ShoutData_Create($Sender) {
// check if user is allowed to view
Expand Down
11 changes: 0 additions & 11 deletions controllers/class.van2shoutdata.php
Expand Up @@ -113,17 +113,6 @@ public function ToString() {
$SQL->Delete('Shoutbox', array(
'ID' => $_GET["del"]
));
} else if(!empty($_GET["newtoken"]) && empty($_GET["reset_tokens"])) {
// client requesting a new firebase token
include_once(PATH_ROOT.DS.plugins.DS.'Van2Shout'.DS.'firebase'.DS.'v2s.php');
echo fb_new_token();
} else if(!empty($_GET["reset_tokens"]) && empty($_GET["newtoken"])) {
// admin requesting a reset of all firebase tokens
if(!$Session->CheckPermission('Garden.Settings.Manage'))
return;

include_once(PATH_ROOT.DS.plugins.DS.'Van2Shout'.DS.'firebase'.DS.'v2s.php');
fb_reset_tokens();
}

$String = ob_get_contents();
Expand Down
46 changes: 46 additions & 0 deletions controllers/class.van2shoutfirebase.php
@@ -0,0 +1,46 @@
<?php if(!defined('APPLICATION')) exit();
//Copyright (c) 2010-2013 by Caerostris <caerostris@gmail.com>
// This file is part of Van2Shout.
//
// Van2Shout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Van2Shout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Van2Shout. If not, see <http://www.gnu.org/licenses/>.

class Van2ShoutFirebase extends Gdn_Module {

public function __connstruct($Sender = '') {
parent::__construct($Sender);
}

public function ToString() {
ob_start();
$Session = GDN::Session();
$UserModel = new UserModel();

if(!empty($_GET["newtoken"]) && empty($_GET["reset_tokens"])) {
// client requesting a new firebase token
include_once(PATH_ROOT.DS.plugins.DS.'Van2Shout'.DS.'firebase'.DS.'v2s.php');
echo fb_new_token();
} else if(!empty($_GET["reset_tokens"]) && empty($_GET["newtoken"])) {
// admin requesting a reset of all firebase tokens
if(!$Session->CheckPermission('Garden.Settings.Manage'))
return;

include_once(PATH_ROOT.DS.plugins.DS.'Van2Shout'.DS.'firebase'.DS.'v2s.php');
fb_reset_tokens();
}

$String = ob_get_contents();
@ob_end_clean();
return $String;
}
}
10 changes: 1 addition & 9 deletions firebase/rules.html
Expand Up @@ -10,15 +10,7 @@ <h1>Firebase Security Rules</h1>
".write": "auth != null && (!data.exists() || auth.delete == true) && auth.post == true",
".validate": "newData.child('colour').val() == auth.colour && auth.id == newData.child('uname').val()"
}
},
"private": {
"$user": {
"$key": {
".write": "((auth != null && !data.exists() && auth.post == true && auth.id == newData.child('uname').val()) || (auth.id == $user))"
},
".read": "auth.id == $user"
}
}
}
}
</pre>
</pre>
103 changes: 28 additions & 75 deletions js/van2shout.firebase.js
@@ -1,20 +1,21 @@
var messageCounter = 0;
var oldestID = 0;
var firebase;

jQuery(document).ready(function($) {
firebase = new Firebase(gdn.definition('Van2ShoutFirebaseUrl'));
firebase.auth(gdn.definition('Van2ShoutFirebaseToken'), function(err)
{
firebase.auth(gdn.definition('Van2ShoutFirebaseToken'), function(err) {
if(err) {
console.log("Login to firebase failed, trying to generate new auth token!");
$.get(gdn.url('plugin/Van2ShoutData?newtoken=1'), function(data){
$.get(gdn.url('plugin/Van2ShoutFirebase?newtoken=1'), function(data) {
console.log("This is the new token: " + data);
});
} else {
console.log("Login to firebase succeeded!");
}
});

// show new messages
firebase.child('broadcast').on('child_added', function(snapshot) {
messageCounter++;
if(messageCounter >= parseInt(gdn.definition('Van2ShoutMessageCount'))) {
Expand Down Expand Up @@ -46,52 +47,31 @@ jQuery(document).ready(function($) {
$("#shout" + messageCounter).emoticonize();
});

// remove messages locally which have been removed from the database
firebase.child('broadcast').on('child_removed', function(snapshot) {
$("[name='brod_" + snapshot.name() + "']").remove();
});
});

firebase.child('private').child(gdn.definition('UserName').toLowerCase()).on('child_removed', function(snapshot) {
$("[name='priv_" + snapshot.name() + "']").remove();
});

firebase.child('private').child(gdn.definition('UserName').toLowerCase()).on('child_added', function(snapshot) {
messageCounter++;
if(messageCounter >= parseInt(gdn.definition('Van2ShoutMessageCount'))) {
$("#shout" + oldestID).remove();
oldestID++;
}

var obj = document.getElementById("van2shoutscroll");
//the slider currently is at the bottom ==> make it stay there after adding new posts
if(obj.scrollTop == (obj.scrollHeight - obj.offsetHeight)) {
var scrolldown = true;
} else {
var scrolldown = false;
}

var msg = snapshot.val();
var pmtext = '';
var timetext = '';
var time = moment.unix(msg.time).calendar();

if(msg.uname == gdn.definition('UserName')) {
pmtext = " <strong>PM to <a href='" + gdn.url('profile/' + msg.to) + "' target='blank'>" + msg.to + "</a></strong>: ";
} else if(msg.to == gdn.definition('UserName')) {
pmtext = " <strong>PM from <a href='" + gdn.url('profile/' + msg.uname) + "' target='blank'>" + msg.uname + "</a></strong>: ";
} else {
pmtext = 'Some pm';
}
if(gdn.definition('Van2ShoutTimestamp') == 'true')
timetext = "<font color='" + gdn.definition('Van2ShoutTimeColor') + "'>[" + time + "]</font>";
function firebase_push(firebase, uname, content, callback) {
firebase.push({uname: uname, colour: gdn.definition('Van2ShoutUserColor'), content: content, time: Math.round((new Date()).getTime() / 1000)}, callback);
}

$("#shoutboxcontent").append("<li name='priv_" + snapshot.name() + "'>" + DeleteBttn('priv_' + snapshot.name()) + timetext + pmtext + htmlEntities(msg.content) + "</li>");
function firebase_push_pm(firebase, uname, to, content, callback) {
firebase.child(to).push({uname: uname, to: to, content: content, time: Math.round((new Date()).getTime() / 1000)}, callback);
firebase.child(uname).push({uname: uname, to: to, content: content, time: Math.round((new Date()).getTime() / 1000)}, callback);
}

if(scrolldown == true)
obj.scrollTop = obj.scrollHeight;
function firebase_delete(firebase, name) {
firebase.child(name).remove();
}

$("#shout" + messageCounter).emoticonize();
});
});
function htmlEntities(str) {
return String(str).replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
}

function SubmitMessage() {
var msg = $("#shoutboxinput").val();
Expand All @@ -106,45 +86,25 @@ function SubmitMessage() {
}

if(msg.indexOf('/w ') == 0) {
var substr = msg.substr(3, msg.length);
var uname = substr.substr(0, substr.indexOf(' ')).toLowerCase();
var msg = substr.substr(substr.indexOf(' '), substr.length);

firebase_push_pm(firebase.child('private'), gdn.definition('UserName'), uname, msg, function(err) {
if(err != null)
alert("Couldn't send message");

$("#van2shoutsubmit").show();
$("#shoutboxloading").hide();
});
alert("Private messages not supported.");
} else {
firebase_push(firebase.child('broadcast'), gdn.definition('UserName'), msg, function(err) {
if(err != null)
alert("Couldn't send message");

// reset the loading icon
$("#van2shoutsubmit").show();
$("#shoutboxloading").hide();
});

// show the loading icon
$("#van2shoutsubmit").hide();
$("#shoutboxloading").show();
}

$("#van2shoutsubmit").hide();
$("#shoutboxloading").show();
$("#shoutboxinput").val("");
}

function firebase_push(firebase, uname, content, callback) {
firebase.push({uname: uname, colour: gdn.definition('Van2ShoutUserColor'), content: content, time: Math.round((new Date()).getTime() / 1000)}, callback);
}

function firebase_push_pm(firebase, uname, to, content, callback) {
firebase.child(to).push({uname: uname, to: to, content: content, time: Math.round((new Date()).getTime() / 1000)}, callback);
firebase.child(uname).push({uname: uname, to: to, content: content, time: Math.round((new Date()).getTime() / 1000)}, callback);
}

function firebase_delete(firebase, name) {
firebase.child(name).remove();
}

function DeletePost(id) {
var name = id.substr(5, id.length - 5);
if(id.substr(0, 5) == 'brod_') {
Expand All @@ -154,10 +114,3 @@ function DeletePost(id) {
}
}

function htmlEntities(str) {
return String(str).replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
}

0 comments on commit 58cfa7a

Please sign in to comment.