forked from sch/Slixfeed
Fix Ad-hoc form of Settings
This commit is contained in:
parent
e0864cc84e
commit
89ef18d5d7
10 changed files with 153 additions and 77 deletions
|
@ -100,7 +100,7 @@ async def xmpp_send_status(self, jid):
|
|||
status_text = '📜️ Slixfeed RSS News Bot'
|
||||
jid_file = jid.replace('/', '_')
|
||||
db_file = config.get_pathname_to_database(jid_file)
|
||||
enabled = await config.get_setting_value(db_file, 'enabled')
|
||||
enabled = config.get_setting_value(db_file, 'enabled')
|
||||
if not enabled:
|
||||
status_mode = 'xa'
|
||||
status_text = '📪️ Send "Start" to receive updates'
|
||||
|
@ -150,11 +150,11 @@ async def xmpp_send_update(self, jid, num=None):
|
|||
"""
|
||||
jid_file = jid.replace('/', '_')
|
||||
db_file = config.get_pathname_to_database(jid_file)
|
||||
enabled = await config.get_setting_value(db_file, 'enabled')
|
||||
enabled = config.get_setting_value(db_file, 'enabled')
|
||||
if enabled:
|
||||
show_media = await config.get_setting_value(db_file, 'media')
|
||||
show_media = config.get_setting_value(db_file, 'media')
|
||||
if not num:
|
||||
num = await config.get_setting_value(db_file, 'quantum')
|
||||
num = config.get_setting_value(db_file, 'quantum')
|
||||
else:
|
||||
num = int(num)
|
||||
results = await sqlite.get_unread_entries(db_file, num)
|
||||
|
@ -269,8 +269,7 @@ def manual(filename, section=None, command=None):
|
|||
return cmd_list
|
||||
|
||||
|
||||
async def xmpp_change_interval(self, key, val, jid, jid_file, message=None,
|
||||
session=None):
|
||||
async def xmpp_change_interval(self, key, val, jid, jid_file, message=None):
|
||||
if val:
|
||||
# response = (
|
||||
# 'Updates will be sent every {} minutes.'
|
||||
|
@ -289,8 +288,6 @@ async def xmpp_change_interval(self, key, val, jid, jid_file, message=None,
|
|||
response = 'Missing value.'
|
||||
if message:
|
||||
XmppMessage.send_reply(self, message, response)
|
||||
if session:
|
||||
XmppMessage.send(self, jid, response, chat_type='chat')
|
||||
|
||||
|
||||
async def reset_settings(jid_file):
|
||||
|
@ -547,10 +544,10 @@ async def list_statistics(db_file):
|
|||
entries_all = entries + archive
|
||||
feeds_active = await sqlite.get_number_of_feeds_active(db_file)
|
||||
feeds_all = await sqlite.get_number_of_items(db_file, 'feeds')
|
||||
key_archive = await config.get_setting_value(db_file, 'archive')
|
||||
key_interval = await config.get_setting_value(db_file, 'interval')
|
||||
key_quantum = await config.get_setting_value(db_file, 'quantum')
|
||||
key_enabled = await config.get_setting_value(db_file, 'enabled')
|
||||
key_archive = config.get_setting_value(db_file, 'archive')
|
||||
key_interval = config.get_setting_value(db_file, 'interval')
|
||||
key_quantum = config.get_setting_value(db_file, 'quantum')
|
||||
key_enabled = config.get_setting_value(db_file, 'enabled')
|
||||
|
||||
# msg = """You have {} unread news items out of {} from {} news sources.
|
||||
# """.format(unread_entries, entries, feeds)
|
||||
|
@ -729,7 +726,7 @@ async def add_feed(db_file, url):
|
|||
updated=updated
|
||||
)
|
||||
await scan(db_file, url)
|
||||
old = await config.get_setting_value(db_file, "old")
|
||||
old = config.get_setting_value(db_file, "old")
|
||||
if not old:
|
||||
feed_id = await sqlite.get_feed_id(db_file, url)
|
||||
feed_id = feed_id[0]
|
||||
|
@ -776,7 +773,7 @@ async def add_feed(db_file, url):
|
|||
)
|
||||
await scan_json(
|
||||
db_file, url)
|
||||
old = await config.get_setting_value(db_file, "old")
|
||||
old = config.get_setting_value(db_file, "old")
|
||||
if not old:
|
||||
feed_id = await sqlite.get_feed_id(db_file, url)
|
||||
feed_id = feed_id[0]
|
||||
|
@ -927,7 +924,7 @@ async def scan_json(db_file, url):
|
|||
media_link = trim_url(media_link)
|
||||
break
|
||||
except:
|
||||
logging.error('KeyError: "url"\n'
|
||||
logging.info('KeyError: "url"\n'
|
||||
'Missing "url" attribute for {}'
|
||||
.format(url))
|
||||
logging.info('Continue scanning for next '
|
||||
|
@ -1210,7 +1207,7 @@ async def scan(db_file, url):
|
|||
media_link = trim_url(media_link)
|
||||
break
|
||||
except:
|
||||
logging.error('KeyError: "href"\n'
|
||||
logging.info('KeyError: "href"\n'
|
||||
'Missing "href" attribute for {}'
|
||||
.format(url))
|
||||
logging.info('Continue scanning for next '
|
||||
|
@ -1615,7 +1612,7 @@ async def remove_nonexistent_entries(db_file, url, feed):
|
|||
else:
|
||||
# print(">>> ARCHIVING:", entry_title)
|
||||
await sqlite.archive_entry(db_file, ix)
|
||||
limit = await config.get_setting_value(db_file, "archive")
|
||||
limit = config.get_setting_value(db_file, "archive")
|
||||
await sqlite.maintain_archive(db_file, limit)
|
||||
|
||||
|
||||
|
@ -1688,5 +1685,5 @@ async def remove_nonexistent_entries_json(db_file, url, feed):
|
|||
await sqlite.delete_entry_by_id(db_file, ix)
|
||||
else:
|
||||
await sqlite.archive_entry(db_file, ix)
|
||||
limit = await config.get_setting_value(db_file, "archive")
|
||||
limit = config.get_setting_value(db_file, "archive")
|
||||
await sqlite.maintain_archive(db_file, limit)
|
|
@ -123,6 +123,7 @@ Markus Muttilainen (SalixOS), \
|
|||
Martin <debacle@debian.org> (Debian, Germany), \
|
||||
Mathieu Pasquet (slixmpp, France), \
|
||||
Maxime Buquet (slixmpp, France), \
|
||||
mirux (Germany), \
|
||||
Phillip Watkins (SalixOS, United Kingdom), \
|
||||
Pierrick Le Brun (SalixOS, France), \
|
||||
Raphael Groner (Fedora, Germany), \
|
||||
|
|
|
@ -34,12 +34,20 @@ import sys
|
|||
import tomli_w
|
||||
import tomllib
|
||||
|
||||
async def get_setting_value(db_file, key):
|
||||
def get_setting_value(db_file, key):
|
||||
value = (
|
||||
sqlite.get_settings_value(db_file, key) or
|
||||
get_value("settings", "Settings", key)
|
||||
)
|
||||
value = int(value)
|
||||
try:
|
||||
value = int(value)
|
||||
except ValueError as e:
|
||||
print('ValueError for value {} (key {}):\n{}'.format(value, key, e))
|
||||
if isinstance(value, bool):
|
||||
if value:
|
||||
value = 1
|
||||
else:
|
||||
value = 0
|
||||
return value
|
||||
|
||||
|
||||
|
|
|
@ -1743,6 +1743,14 @@ async def set_settings_value(db_file, key_value):
|
|||
"""
|
||||
key = key_value[0]
|
||||
value = key_value[1]
|
||||
|
||||
if not value:
|
||||
match key:
|
||||
case 'interval':
|
||||
value = 90
|
||||
case 'quantum':
|
||||
value = 3
|
||||
|
||||
async with DBLOCK:
|
||||
with create_connection(db_file) as conn:
|
||||
cur = conn.cursor()
|
||||
|
|
|
@ -173,7 +173,7 @@ async def task_status(self, jid):
|
|||
async def task_send(self, jid):
|
||||
jid_file = jid.replace('/', '_')
|
||||
db_file = config.get_pathname_to_database(jid_file)
|
||||
update_interval = await config.get_setting_value(db_file, 'interval')
|
||||
update_interval = config.get_setting_value(db_file, 'interval')
|
||||
update_interval = 60 * int(update_interval)
|
||||
last_update_time = await sqlite.get_last_update_time(db_file)
|
||||
if last_update_time:
|
||||
|
@ -231,15 +231,14 @@ async def refresh_task(self, jid, callback, key, val=None):
|
|||
if not val:
|
||||
jid_file = jid.replace('/', '_')
|
||||
db_file = config.get_pathname_to_database(jid_file)
|
||||
val = await config.get_setting_value(db_file, key)
|
||||
val = config.get_setting_value(db_file, key)
|
||||
# if self.task_manager[jid][key]:
|
||||
if jid in self.task_manager:
|
||||
try:
|
||||
self.task_manager[jid][key].cancel()
|
||||
except:
|
||||
logging.info('No task of type {} to cancel for '
|
||||
'JID {} (refresh_task)'.format(key, jid)
|
||||
)
|
||||
'JID {} (refresh_task)'.format(key, jid))
|
||||
# self.task_manager[jid][key] = loop.call_at(
|
||||
# loop.time() + 60 * float(val),
|
||||
# loop.create_task,
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
__version__ = '0.1.4'
|
||||
__version_info__ = (0, 1, 4)
|
||||
__version__ = '0.1.5'
|
||||
__version_info__ = (0, 1, 5)
|
||||
|
|
|
@ -454,11 +454,14 @@ class Slixfeed(slixmpp.ClientXMPP):
|
|||
# name='Roster',
|
||||
# handler=self._handle_roster)
|
||||
self['xep_0050'].add_command(node='settings',
|
||||
name='Settings',
|
||||
name='Edit Settings',
|
||||
handler=self._handle_settings)
|
||||
self['xep_0050'].add_command(node='subscriptions',
|
||||
name='Subscriptions',
|
||||
name='Manage subscriptions',
|
||||
handler=self._handle_subscriptions)
|
||||
self['xep_0050'].add_command(node='subscription',
|
||||
name='View subscription',
|
||||
handler=self._handle_subscription)
|
||||
# self['xep_0050'].add_command(node='search',
|
||||
# name='Search',
|
||||
# handler=self._handle_search)
|
||||
|
@ -471,14 +474,14 @@ class Slixfeed(slixmpp.ClientXMPP):
|
|||
jid = session['from'].bare
|
||||
form = self['xep_0004'].make_form('form',
|
||||
'Subscriptions for {}'.format(jid))
|
||||
form['instructions'] = '📰️ Manage subscriptions.'
|
||||
form['instructions'] = '📰️ Manage subscriptions'
|
||||
# form.addField(var='interval',
|
||||
# ftype='text-single',
|
||||
# label='Interval period')
|
||||
options = form.add_field(var='subscriptions',
|
||||
ftype='list-multi',
|
||||
label='Select subscriptions',
|
||||
desc='Select subscription(s) to edit.')
|
||||
desc='Select subscriptions to edit.')
|
||||
jid_file = jid
|
||||
db_file = config.get_pathname_to_database(jid_file)
|
||||
subscriptions = await sqlite.get_feeds(db_file)
|
||||
|
@ -487,7 +490,7 @@ class Slixfeed(slixmpp.ClientXMPP):
|
|||
url = subscription[1]
|
||||
options.addOption(title, url)
|
||||
session['payload'] = form
|
||||
session['next'] = self._handle_subscription_editor
|
||||
session['next'] = self._edit_subscription
|
||||
session['has_next'] = True
|
||||
# Other useful session values:
|
||||
# session['to'] -- The JID that received the
|
||||
|
@ -509,11 +512,36 @@ class Slixfeed(slixmpp.ClientXMPP):
|
|||
# TODO Make form for a single subscription and several subscriptions
|
||||
# single: Delete, Disable, Reset and Rename
|
||||
# several: Delete, Disable, Reset
|
||||
async def _handle_subscription_editor(self, iq, session):
|
||||
async def _handle_subscription(self, iq, session):
|
||||
jid = session['from'].bare
|
||||
form = self['xep_0004'].make_form('form',
|
||||
'Subscriptions for {}'.format(jid))
|
||||
form['instructions'] = '🗞️ Edit subscriptions.'
|
||||
form['instructions'] = '📰️ View subscription properties'
|
||||
# form.addField(var='interval',
|
||||
# ftype='text-single',
|
||||
# label='Interval period')
|
||||
options = form.add_field(var='subscriptions',
|
||||
ftype='list-single',
|
||||
label='Select subscriptions',
|
||||
desc='Select a subscription to view.')
|
||||
jid_file = jid
|
||||
db_file = config.get_pathname_to_database(jid_file)
|
||||
subscriptions = await sqlite.get_feeds(db_file)
|
||||
for subscription in subscriptions:
|
||||
title = subscription[0]
|
||||
url = subscription[1]
|
||||
options.addOption(title, url)
|
||||
session['payload'] = form
|
||||
session['next'] = self._edit_subscription
|
||||
session['has_next'] = True
|
||||
return session
|
||||
|
||||
|
||||
async def _edit_subscription(self, iq, session):
|
||||
jid = session['from'].bare
|
||||
form = self['xep_0004'].make_form('form',
|
||||
'Subscription editor'.format(jid))
|
||||
form['instructions'] = '🗞️ Edit subscription: {}'.format(title)
|
||||
options = form.add_field(var='enable',
|
||||
ftype='boolean',
|
||||
label='Enable',
|
||||
|
@ -534,7 +562,7 @@ class Slixfeed(slixmpp.ClientXMPP):
|
|||
jid = session['from'].bare
|
||||
form = self['xep_0004'].make_form('form',
|
||||
'Bookmarks for {}'.format(jid))
|
||||
form['instructions'] = '📑️ Organize bookmarks.'
|
||||
form['instructions'] = '📑️ Organize bookmarks'
|
||||
options = form.add_field(var='bookmarks',
|
||||
# ftype='list-multi'
|
||||
ftype='list-single',
|
||||
|
@ -553,7 +581,7 @@ class Slixfeed(slixmpp.ClientXMPP):
|
|||
jid = session['from'].bare
|
||||
form = self['xep_0004'].make_form('form',
|
||||
'Bookmarks for {}'.format(jid))
|
||||
form['instructions'] = '📝️ Edit bookmarks.'
|
||||
form['instructions'] = '📝️ Edit bookmarks'
|
||||
form.addField(var='name',
|
||||
ftype='text-single',
|
||||
label='Name')
|
||||
|
@ -603,9 +631,9 @@ class Slixfeed(slixmpp.ClientXMPP):
|
|||
db_file = config.get_pathname_to_database(jid_file)
|
||||
form = self['xep_0004'].make_form('form',
|
||||
'Settings for {}'.format(jid))
|
||||
form['instructions'] = ('📮️ Customize news updates.')
|
||||
form['instructions'] = ('📮️ Customize news updates')
|
||||
|
||||
value = await config.get_setting_value(db_file, 'enabled')
|
||||
value = config.get_setting_value(db_file, 'enabled')
|
||||
value = int(value)
|
||||
if value:
|
||||
value = True
|
||||
|
@ -617,7 +645,7 @@ class Slixfeed(slixmpp.ClientXMPP):
|
|||
desc='Enable news updates.',
|
||||
value=value)
|
||||
|
||||
value = await config.get_setting_value(db_file, 'media')
|
||||
value = config.get_setting_value(db_file, 'media')
|
||||
value = int(value)
|
||||
if value:
|
||||
value = True
|
||||
|
@ -629,12 +657,12 @@ class Slixfeed(slixmpp.ClientXMPP):
|
|||
label='Display media',
|
||||
value=value)
|
||||
|
||||
value = await config.get_setting_value(db_file, 'old')
|
||||
value = config.get_setting_value(db_file, 'old')
|
||||
value = int(value)
|
||||
if value:
|
||||
value = False
|
||||
else:
|
||||
value = True
|
||||
else:
|
||||
value = False
|
||||
form.add_field(var='old',
|
||||
ftype='boolean',
|
||||
desc='Send old items of newly added subscriptions.',
|
||||
|
@ -642,8 +670,11 @@ class Slixfeed(slixmpp.ClientXMPP):
|
|||
label='Include old news',
|
||||
value=value)
|
||||
|
||||
value = await config.get_setting_value(db_file, 'interval')
|
||||
value = str(int(value/60))
|
||||
value = config.get_setting_value(db_file, 'interval')
|
||||
value = int(value)
|
||||
value = value
|
||||
value = int(value)
|
||||
value = str(value)
|
||||
options = form.add_field(var='interval',
|
||||
ftype='list-single',
|
||||
label='Interval',
|
||||
|
@ -654,9 +685,12 @@ class Slixfeed(slixmpp.ClientXMPP):
|
|||
var = str(i)
|
||||
lab = str(int(i/60))
|
||||
options.addOption(lab, var)
|
||||
i += 60
|
||||
if i >= 720:
|
||||
i += 360
|
||||
else:
|
||||
i += 60
|
||||
|
||||
value = await config.get_setting_value(db_file, 'archive')
|
||||
value = config.get_setting_value(db_file, 'archive')
|
||||
value = str(value)
|
||||
options = form.add_field(var='archive',
|
||||
ftype='list-single',
|
||||
|
@ -669,13 +703,13 @@ class Slixfeed(slixmpp.ClientXMPP):
|
|||
options.addOption(x, x)
|
||||
i += 50
|
||||
|
||||
value = await config.get_setting_value(db_file, 'quantum')
|
||||
value = config.get_setting_value(db_file, 'quantum')
|
||||
value = str(value)
|
||||
options = form.add_field(var='quantum',
|
||||
ftype='list-single',
|
||||
label='Amount',
|
||||
desc='Set amount of updates per update.',
|
||||
value='3')
|
||||
value=value)
|
||||
i = 1
|
||||
while i <= 5:
|
||||
x = str(i)
|
||||
|
@ -702,28 +736,57 @@ class Slixfeed(slixmpp.ClientXMPP):
|
|||
session. Additional, custom data may be saved
|
||||
here to persist across handler callbacks.
|
||||
"""
|
||||
# This looks nice in Gajim, though there are dropdown menues
|
||||
# form = payload
|
||||
|
||||
jid = session['from'].bare
|
||||
form = self['xep_0004'].make_form('form',
|
||||
'Settings for {}'.format(jid))
|
||||
form['instructions'] = ('🛡️ Settings have beem saved')
|
||||
|
||||
jid_file = jid
|
||||
db_file = config.get_pathname_to_database(jid_file)
|
||||
# In this case (as is typical), the payload is a form
|
||||
form = payload
|
||||
values = form['values']
|
||||
values = payload['values']
|
||||
for value in values:
|
||||
key = value
|
||||
val = values[value]
|
||||
|
||||
if key == 'interval':
|
||||
val = int(val)
|
||||
if val < 60:
|
||||
val = 90
|
||||
|
||||
if sqlite.get_settings_value(db_file, key):
|
||||
await sqlite.update_settings_value(db_file, [key, val])
|
||||
else:
|
||||
await sqlite.set_settings_value(db_file, [key, val])
|
||||
match value:
|
||||
case 'enabled':
|
||||
pass
|
||||
case 'interval':
|
||||
pass
|
||||
# Having no return statement is the same as unsetting the 'payload'
|
||||
# and 'next' session values and returning the session.
|
||||
# Unless it is the final step, always return the session dictionary.
|
||||
session['payload'] = None
|
||||
|
||||
val = sqlite.get_settings_value(db_file, key)
|
||||
if key in ('enabled', 'media', 'old'):
|
||||
if val == '1':
|
||||
val = 'Yes'
|
||||
elif val == '0':
|
||||
val = 'No'
|
||||
|
||||
if key == 'interval':
|
||||
val = int(val)
|
||||
val = val/60
|
||||
val = int(val)
|
||||
val = str(val)
|
||||
|
||||
# match value:
|
||||
# case 'enabled':
|
||||
# pass
|
||||
# case 'interval':
|
||||
# pass
|
||||
|
||||
result = '{}: {}'.format(key.capitalize(), val)
|
||||
|
||||
form.add_field(var=key,
|
||||
ftype='fixed',
|
||||
value=result)
|
||||
session['payload'] = form # Comment when this is fixed in Gajim
|
||||
session["has_next"] = False
|
||||
session['next'] = None
|
||||
return session
|
||||
# return session
|
||||
|
|
|
@ -530,7 +530,7 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
|
|||
jid = session['from'].bare
|
||||
jid_file = jid
|
||||
db_file = config.get_pathname_to_database(jid_file)
|
||||
value = await config.get_setting_value(db_file, 'enabled')
|
||||
value = config.get_setting_value(db_file, 'enabled')
|
||||
value = int(value)
|
||||
if value:
|
||||
value = True
|
||||
|
@ -541,7 +541,7 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
|
|||
label='Enable',
|
||||
desc='Enable news updates.',
|
||||
value=value)
|
||||
value = await config.get_setting_value(db_file, 'old')
|
||||
value = config.get_setting_value(db_file, 'old')
|
||||
value = int(value)
|
||||
if value:
|
||||
value = False
|
||||
|
@ -554,7 +554,7 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
|
|||
# label='Send only new items',
|
||||
label='Include old news',
|
||||
value=value)
|
||||
value = await config.get_setting_value(db_file, 'interval')
|
||||
value = config.get_setting_value(db_file, 'interval')
|
||||
value = str(int(value/60))
|
||||
options = form.add_field(var='interval',
|
||||
ftype='list-single',
|
||||
|
@ -567,7 +567,7 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
|
|||
lab = str(int(i/60))
|
||||
options.addOption(lab, var)
|
||||
i += 60
|
||||
value = await config.get_setting_value(db_file, 'archive')
|
||||
value = config.get_setting_value(db_file, 'archive')
|
||||
value = str(value)
|
||||
options = form.add_field(var='archive',
|
||||
ftype='list-single',
|
||||
|
@ -579,7 +579,7 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
|
|||
x = str(i)
|
||||
options.addOption(x, x)
|
||||
i += 1
|
||||
value = await config.get_setting_value(db_file, 'quantum')
|
||||
value = config.get_setting_value(db_file, 'quantum')
|
||||
value = str(value)
|
||||
options = form.add_field(var='quantum',
|
||||
ftype='list-single',
|
||||
|
|
|
@ -87,18 +87,18 @@ class XmppGroupchat:
|
|||
|
||||
|
||||
async def leave(self, muc_jid):
|
||||
messages = [
|
||||
"Whenever you need an RSS service again, "
|
||||
"please don’t hesitate to contact me.",
|
||||
"My JID is xmpp:{}?message".format(self.boundjid.bare),
|
||||
"Farewell."
|
||||
]
|
||||
for message in messages:
|
||||
self.send_message(mto=muc_jid,
|
||||
mfrom=self.boundjid.bare,
|
||||
mbody=message,
|
||||
mtype='groupchat')
|
||||
jid = self.boundjid.bare
|
||||
message = ('This news bot will now leave this groupchat.\n'
|
||||
'The JID of this news bot is xmpp:{}?message'
|
||||
.format(jid))
|
||||
status_message = ('This bot has left the group. '
|
||||
'It can be reached directly via {}'
|
||||
.format(jid))
|
||||
self.send_message(mto=muc_jid,
|
||||
mfrom=self.boundjid.bare,
|
||||
mbody=message,
|
||||
mtype='groupchat')
|
||||
self.plugin['xep_0045'].leave_muc(muc_jid,
|
||||
self.alias,
|
||||
'Goodbye!',
|
||||
status_message,
|
||||
self.boundjid.bare)
|
||||
|
|
|
@ -294,7 +294,7 @@ async def message(self, message):
|
|||
if not exist:
|
||||
await sqlite.insert_feed(db_file, url, title)
|
||||
await action.scan(db_file, url)
|
||||
old = await config.get_setting_value(db_file, "old")
|
||||
old = config.get_setting_value(db_file, "old")
|
||||
if old:
|
||||
# task.clean_tasks_xmpp(self, jid, ['status'])
|
||||
# await send_status(jid)
|
||||
|
|
Loading…
Reference in a new issue