Skip to content

Commit

Permalink
Merge branch 'master' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
derpicknicker1 committed Dec 13, 2016
2 parents 991113c + 0b7a0d3 commit d8ddf5f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
1 change: 1 addition & 0 deletions extras/telegram.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ compatibility:
# list of compatible versions, for example 1.2.0. If left empty no specific version requirement will be assumed
octoprint:
- 1.2.0
- 1.3.0

# list of compatible operating systems, valid values are linux, windows, macos, leaving empty defaults to all
os:
Expand Down
46 changes: 24 additions & 22 deletions octoprint_telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,55 +717,57 @@ def on_settings_migrate(self, target, current=None):


def on_settings_save(self, data):
delList = []
# Remove 'new'-flag and apply bindings for all chats
if data['chats']:
if 'chats' in data and data['chats']:
delList = []
for key in data['chats']:
if 'new' in data['chats'][key] or 'new' in data['chats'][key]:
data['chats'][key]['new'] = False
# Look for deleted chats
if not key in self.chats and not key == "zBOTTOMOFCHATS":
delList.append(key)
# Delete chats finally
for key in delList:
del data['chats'][key]
# Also remove 'new'-flag from self.chats so settingsUI is consistent
# Delete chats finally
for key in delList:
del data['chats'][key]
# Also remove 'new'-flag from self.chats so settingsUI is consistent
# self.chats will only update to settings data on first received message after saving done
for key in self.chats:
if 'new' in self.chats[key]:
self.chats[key]['new'] = False

self._logger.debug("Saving data: " + str(data))
# Check token for right format
data['token'] = data['token'].strip()
if not re.match("^[0-9]+:[a-zA-Z0-9_\-]+$", data['token']):
self._logger.error("Not saving token because it doesn't seem to have the right format.")
self.connection_state_str = gettext("The previously entered token doesn't seem to have the correct format. It should look like this: 12345678:AbCdEfGhIjKlMnOpZhGtDsrgkjkZTCHJKkzvjhb")
data['token'] = ""
if 'token' in data:
data['token'] = data['token'].strip()
if not re.match("^[0-9]+:[a-zA-Z0-9_\-]+$", data['token']):
self._logger.error("Not saving token because it doesn't seem to have the right format.")
self.connection_state_str = gettext("The previously entered token doesn't seem to have the correct format. It should look like this: 12345678:AbCdEfGhIjKlMnOpZhGtDsrgkjkZTCHJKkzvjhb")
data['token'] = ""
old_token = self._settings.get(["token"])
# Update Tracking
if not data['tracking_activated']:
if 'tracking_activated' in data and not data['tracking_activated']:
data['tracking_token'] = None
# Now save settings
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
self.set_log_level()
# Reconnect on new token
# Will stop listener on invalid token
if data['token']!=old_token:
self.stop_listening()
if data['token']!="":
self.start_listening()
else:
self.connection_state_str = gettext("No token given.")
if 'token' in data:
if data['token']!=old_token:
self.stop_listening()
if data['token']!="":
self.start_listening()
else:
self.connection_state_str = gettext("No token given.")

def on_settings_load(self):
data = octoprint.plugin.SettingsPlugin.on_settings_load(self)

# only return our restricted settings to admin users - this is only needed for OctoPrint <= 1.2.16
restricted = ("token", "tracking_token", "chats")
for r in restricted:
restricted = (("token", None), ("tracking_token", None), ("chats", dict()))
for r, v in restricted:
if r in data and (current_user is None or current_user.is_anonymous() or not current_user.is_admin()):
data[r] = None
data[r] = v

return data

Expand Down Expand Up @@ -1172,4 +1174,4 @@ def route_hook(self, server_routes, *args, **kwargs):
__plugin_hooks__ = {
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information,
"octoprint.server.http.routes": __plugin_implementation__.route_hook
}
}
23 changes: 20 additions & 3 deletions octoprint_telegram/static/js/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,32 @@ $(function() {
var entries = response.chats;
if (entries === undefined) return;
var array = [];
var formerChats = _.pluck(self.chatListHelper.allItems, 'id');
var currentChats = [];
var newChats = false;
for(var id in entries) {
var data = entries[id];
data['id'] = id;
data['image'] = data['image'] + "?" + $.now();
if(data['new'])
data['newUsr']=true;
else
if(data['new']) {
data['newUsr'] = true;
} else {
data['newUsr'] = false;
}
array.push(data);
currentChats.push(id);
newChats = newChats || !_.includes(formerChats, id);
}

var deletedChatIds = _.difference(formerChats, currentChats);
if (newChats || (deletedChatIds && deletedChatIds.length)) {
// Transfer the chats back to the server settings (because just hitting "save" on the Settings dialog
// won't transfer anything we haven't explicitely set).

// TODO: This whole workflow should be optimized!
// Currently it takes two full server/client round trips to get the chats in sync, and just reusing
// the plugin's API for that purpose would probably be way way more efficient and less error prone.
self.settings.saveData({plugins: {telegram: {chats: entries}}});
}
self.chatListHelper.updateItems(array);
self.isloading(false);
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "OctoPrint-Telegram"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "1.4.0"
plugin_version = "1.4.1"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit d8ddf5f

Please sign in to comment.