Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Save client options on server in user config file #319

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 54 additions & 47 deletions client/js/shout.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ $(function() {
.show();
});

var options;

socket.on("init", function(data) {
if (data.networks.length === 0) {
$("#footer").find(".connect").trigger("click");
Expand All @@ -132,6 +134,8 @@ $(function() {
confirmExit();
}

options = initOptions(data.options);

if (data.token) {
$.cookie(
"token",
Expand Down Expand Up @@ -329,53 +333,6 @@ $(function() {

$.cookie.json = true;

var settings = $("#settings");
var options = $.extend({
badge: false,
colors: false,
join: true,
links: true,
mode: true,
motd: false,
nick: true,
notification: true,
part: true,
thumbnails: true,
quit: true,
}, $.cookie("settings"));

for (var i in options) {
if (options[i]) {
settings.find("input[name=" + i + "]").prop("checked", true);
}
}

settings.on("change", "input", function() {
var self = $(this);
var name = self.attr("name");
options[name] = self.prop("checked");
$.cookie(
"settings",
options, {
expires: expire(365)
}
);
if ([
"join",
"mode",
"motd",
"nick",
"part",
"quit",
].indexOf(name) !== -1) {
chat.toggleClass("hide-" + name, !self.prop("checked"));
}
if (name == "colors") {
chat.toggleClass("no-colors", !self.prop("checked"));
}
}).find("input")
.trigger("change");

$("#badge").on("change", function() {
var self = $(this);
if (self.prop("checked")) {
Expand Down Expand Up @@ -881,4 +838,54 @@ $(function() {
}
}
);

function initOptions(serverOptions) {

var settings = $("#settings");
var options = $.extend({
badge: false,
colors: false,
join: true,
links: true,
mode: true,
motd: false,
nick: true,
notification: true,
part: true,
thumbnails: true,
quit: true,
}, serverOptions);

for (var i in options) {
if (options[i]) {
settings.find("input[name=" + i + "]").prop("checked", true);
}
}

settings.off('change').on("change", "input", function() {
var self = $(this);
var name = self.attr("name");
options[name] = self.prop("checked");
socket.emit("options", options);

if ([
"join",
"mode",
"motd",
"nick",
"part",
"quit",
].indexOf(name) !== -1) {
chat.toggleClass("hide-" + name, !self.prop("checked"));
}
if (name == "colors") {
chat.toggleClass("no-colors", !self.prop("checked"));
}
}).find("input")
.trigger("change");

return options;
}
});


85 changes: 60 additions & 25 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function Client(sockets, name, config) {
id: id++,
name: name,
networks: [],
options: config ? config.options : {},
sockets: sockets
});
var client = this;
Expand Down Expand Up @@ -323,6 +324,59 @@ Client.prototype.quit = function() {
});
};

Client.prototype.saveUserConfig = function(data, cb) {

var path = Helper.HOME + "/users/" + this.name + ".json";

fs.writeFile(
path,
JSON.stringify(data, null, " "),
{mode: "0777"},
cb
);

};

Client.prototype.loadUserConfig = function(cb) {

var path = Helper.HOME + "/users/" + this.name + ".json";

fs.readFile(path, "utf-8", function(err, data) {
if (err) {
console.log(err);
return;
}
try {
data = JSON.parse(data);
} catch(e) {
console.log(e);
return;
}
cb(data);
});
};


Client.prototype.saveOptions = function(options) {

var client = this;
var config = Helper.getConfig();

if(config.public) {
return;
}

this.options = options;

this.loadUserConfig(function(data) {

data.options = options;
client.saveUserConfig(data);
});


};

var timer;
Client.prototype.save = function(force) {
var client = this;
Expand All @@ -340,40 +394,21 @@ Client.prototype.save = function(force) {
return;
}

var name = this.name;
var path = Helper.HOME + "/users/" + name + ".json";

var networks = _.map(
this.networks,
function(n) {
return n.export();
}
);

var json = {};
fs.readFile(path, "utf-8", function(err, data) {
if (err) {
console.log(err);
return;
}
this.loadUserConfig(function(data) {

try {
json = JSON.parse(data);
json.networks = networks;
} catch(e) {
console.log(e);
return;
}
data.networks = networks;

fs.writeFile(
path,
JSON.stringify(json, null, " "),
{mode: "0777"},
function(err) {
if (err) {
console.log(err);
}
client.saveUserConfig(data, function(err) {
if (err) {
console.log(err);
}
);
});
});
};
7 changes: 7 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,17 @@ function init(socket, client, token) {
client.sort(data);
}
);
socket.on(
"options",
_.debounce(function(data) {
client.saveOptions(data);
}, 1000)
);
socket.join(client.id);
socket.emit("init", {
active: client.activeChannel,
networks: client.networks,
options: client.options,
token: token || ""
});
}
Expand Down