Fix Ad-hoc form of Settings

This commit is contained in:
Schimon Jehudah 2024-02-12 19:01:28 +00:00
parent e0864cc84e
commit 89ef18d5d7
10 changed files with 153 additions and 77 deletions

View file

@ -100,7 +100,7 @@ async def xmpp_send_status(self, jid):
status_text = '📜️ Slixfeed RSS News Bot' status_text = '📜️ Slixfeed RSS News Bot'
jid_file = jid.replace('/', '_') jid_file = jid.replace('/', '_')
db_file = config.get_pathname_to_database(jid_file) 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: if not enabled:
status_mode = 'xa' status_mode = 'xa'
status_text = '📪️ Send "Start" to receive updates' status_text = '📪️ Send "Start" to receive updates'
@ -150,11 +150,11 @@ async def xmpp_send_update(self, jid, num=None):
""" """
jid_file = jid.replace('/', '_') jid_file = jid.replace('/', '_')
db_file = config.get_pathname_to_database(jid_file) 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: if enabled:
show_media = await config.get_setting_value(db_file, 'media') show_media = config.get_setting_value(db_file, 'media')
if not num: if not num:
num = await config.get_setting_value(db_file, 'quantum') num = config.get_setting_value(db_file, 'quantum')
else: else:
num = int(num) num = int(num)
results = await sqlite.get_unread_entries(db_file, num) results = await sqlite.get_unread_entries(db_file, num)
@ -269,8 +269,7 @@ def manual(filename, section=None, command=None):
return cmd_list return cmd_list
async def xmpp_change_interval(self, key, val, jid, jid_file, message=None, async def xmpp_change_interval(self, key, val, jid, jid_file, message=None):
session=None):
if val: if val:
# response = ( # response = (
# 'Updates will be sent every {} minutes.' # '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.' response = 'Missing value.'
if message: if message:
XmppMessage.send_reply(self, message, response) XmppMessage.send_reply(self, message, response)
if session:
XmppMessage.send(self, jid, response, chat_type='chat')
async def reset_settings(jid_file): async def reset_settings(jid_file):
@ -547,10 +544,10 @@ async def list_statistics(db_file):
entries_all = entries + archive entries_all = entries + archive
feeds_active = await sqlite.get_number_of_feeds_active(db_file) feeds_active = await sqlite.get_number_of_feeds_active(db_file)
feeds_all = await sqlite.get_number_of_items(db_file, 'feeds') feeds_all = await sqlite.get_number_of_items(db_file, 'feeds')
key_archive = await config.get_setting_value(db_file, 'archive') key_archive = config.get_setting_value(db_file, 'archive')
key_interval = await config.get_setting_value(db_file, 'interval') key_interval = config.get_setting_value(db_file, 'interval')
key_quantum = await config.get_setting_value(db_file, 'quantum') key_quantum = config.get_setting_value(db_file, 'quantum')
key_enabled = await config.get_setting_value(db_file, 'enabled') key_enabled = config.get_setting_value(db_file, 'enabled')
# msg = """You have {} unread news items out of {} from {} news sources. # msg = """You have {} unread news items out of {} from {} news sources.
# """.format(unread_entries, entries, feeds) # """.format(unread_entries, entries, feeds)
@ -729,7 +726,7 @@ async def add_feed(db_file, url):
updated=updated updated=updated
) )
await scan(db_file, url) 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: if not old:
feed_id = await sqlite.get_feed_id(db_file, url) feed_id = await sqlite.get_feed_id(db_file, url)
feed_id = feed_id[0] feed_id = feed_id[0]
@ -776,7 +773,7 @@ async def add_feed(db_file, url):
) )
await scan_json( await scan_json(
db_file, url) db_file, url)
old = await config.get_setting_value(db_file, "old") old = config.get_setting_value(db_file, "old")
if not old: if not old:
feed_id = await sqlite.get_feed_id(db_file, url) feed_id = await sqlite.get_feed_id(db_file, url)
feed_id = feed_id[0] feed_id = feed_id[0]
@ -927,7 +924,7 @@ async def scan_json(db_file, url):
media_link = trim_url(media_link) media_link = trim_url(media_link)
break break
except: except:
logging.error('KeyError: "url"\n' logging.info('KeyError: "url"\n'
'Missing "url" attribute for {}' 'Missing "url" attribute for {}'
.format(url)) .format(url))
logging.info('Continue scanning for next ' logging.info('Continue scanning for next '
@ -1210,7 +1207,7 @@ async def scan(db_file, url):
media_link = trim_url(media_link) media_link = trim_url(media_link)
break break
except: except:
logging.error('KeyError: "href"\n' logging.info('KeyError: "href"\n'
'Missing "href" attribute for {}' 'Missing "href" attribute for {}'
.format(url)) .format(url))
logging.info('Continue scanning for next ' logging.info('Continue scanning for next '
@ -1615,7 +1612,7 @@ async def remove_nonexistent_entries(db_file, url, feed):
else: else:
# print(">>> ARCHIVING:", entry_title) # print(">>> ARCHIVING:", entry_title)
await sqlite.archive_entry(db_file, ix) 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) 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) await sqlite.delete_entry_by_id(db_file, ix)
else: else:
await sqlite.archive_entry(db_file, ix) 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) await sqlite.maintain_archive(db_file, limit)

View file

@ -123,6 +123,7 @@ Markus Muttilainen (SalixOS), \
Martin <debacle@debian.org> (Debian, Germany), \ Martin <debacle@debian.org> (Debian, Germany), \
Mathieu Pasquet (slixmpp, France), \ Mathieu Pasquet (slixmpp, France), \
Maxime Buquet (slixmpp, France), \ Maxime Buquet (slixmpp, France), \
mirux (Germany), \
Phillip Watkins (SalixOS, United Kingdom), \ Phillip Watkins (SalixOS, United Kingdom), \
Pierrick Le Brun (SalixOS, France), \ Pierrick Le Brun (SalixOS, France), \
Raphael Groner (Fedora, Germany), \ Raphael Groner (Fedora, Germany), \

View file

@ -34,12 +34,20 @@ import sys
import tomli_w import tomli_w
import tomllib import tomllib
async def get_setting_value(db_file, key): def get_setting_value(db_file, key):
value = ( value = (
sqlite.get_settings_value(db_file, key) or sqlite.get_settings_value(db_file, key) or
get_value("settings", "Settings", key) 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 return value

View file

@ -1743,6 +1743,14 @@ async def set_settings_value(db_file, key_value):
""" """
key = key_value[0] key = key_value[0]
value = key_value[1] value = key_value[1]
if not value:
match key:
case 'interval':
value = 90
case 'quantum':
value = 3
async with DBLOCK: async with DBLOCK:
with create_connection(db_file) as conn: with create_connection(db_file) as conn:
cur = conn.cursor() cur = conn.cursor()

View file

@ -173,7 +173,7 @@ async def task_status(self, jid):
async def task_send(self, jid): async def task_send(self, jid):
jid_file = jid.replace('/', '_') jid_file = jid.replace('/', '_')
db_file = config.get_pathname_to_database(jid_file) 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) update_interval = 60 * int(update_interval)
last_update_time = await sqlite.get_last_update_time(db_file) last_update_time = await sqlite.get_last_update_time(db_file)
if last_update_time: if last_update_time:
@ -231,15 +231,14 @@ async def refresh_task(self, jid, callback, key, val=None):
if not val: if not val:
jid_file = jid.replace('/', '_') jid_file = jid.replace('/', '_')
db_file = config.get_pathname_to_database(jid_file) 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 self.task_manager[jid][key]:
if jid in self.task_manager: if jid in self.task_manager:
try: try:
self.task_manager[jid][key].cancel() self.task_manager[jid][key].cancel()
except: except:
logging.info('No task of type {} to cancel for ' 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( # self.task_manager[jid][key] = loop.call_at(
# loop.time() + 60 * float(val), # loop.time() + 60 * float(val),
# loop.create_task, # loop.create_task,

View file

@ -1,2 +1,2 @@
__version__ = '0.1.4' __version__ = '0.1.5'
__version_info__ = (0, 1, 4) __version_info__ = (0, 1, 5)

View file

@ -454,11 +454,14 @@ class Slixfeed(slixmpp.ClientXMPP):
# name='Roster', # name='Roster',
# handler=self._handle_roster) # handler=self._handle_roster)
self['xep_0050'].add_command(node='settings', self['xep_0050'].add_command(node='settings',
name='Settings', name='Edit Settings',
handler=self._handle_settings) handler=self._handle_settings)
self['xep_0050'].add_command(node='subscriptions', self['xep_0050'].add_command(node='subscriptions',
name='Subscriptions', name='Manage subscriptions',
handler=self._handle_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', # self['xep_0050'].add_command(node='search',
# name='Search', # name='Search',
# handler=self._handle_search) # handler=self._handle_search)
@ -471,14 +474,14 @@ class Slixfeed(slixmpp.ClientXMPP):
jid = session['from'].bare jid = session['from'].bare
form = self['xep_0004'].make_form('form', form = self['xep_0004'].make_form('form',
'Subscriptions for {}'.format(jid)) 'Subscriptions for {}'.format(jid))
form['instructions'] = '📰️ Manage subscriptions.' form['instructions'] = '📰️ Manage subscriptions'
# form.addField(var='interval', # form.addField(var='interval',
# ftype='text-single', # ftype='text-single',
# label='Interval period') # label='Interval period')
options = form.add_field(var='subscriptions', options = form.add_field(var='subscriptions',
ftype='list-multi', ftype='list-multi',
label='Select subscriptions', label='Select subscriptions',
desc='Select subscription(s) to edit.') desc='Select subscriptions to edit.')
jid_file = jid jid_file = jid
db_file = config.get_pathname_to_database(jid_file) db_file = config.get_pathname_to_database(jid_file)
subscriptions = await sqlite.get_feeds(db_file) subscriptions = await sqlite.get_feeds(db_file)
@ -487,7 +490,7 @@ class Slixfeed(slixmpp.ClientXMPP):
url = subscription[1] url = subscription[1]
options.addOption(title, url) options.addOption(title, url)
session['payload'] = form session['payload'] = form
session['next'] = self._handle_subscription_editor session['next'] = self._edit_subscription
session['has_next'] = True session['has_next'] = True
# Other useful session values: # Other useful session values:
# session['to'] -- The JID that received the # 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 # TODO Make form for a single subscription and several subscriptions
# single: Delete, Disable, Reset and Rename # single: Delete, Disable, Reset and Rename
# several: Delete, Disable, Reset # several: Delete, Disable, Reset
async def _handle_subscription_editor(self, iq, session): async def _handle_subscription(self, iq, session):
jid = session['from'].bare jid = session['from'].bare
form = self['xep_0004'].make_form('form', form = self['xep_0004'].make_form('form',
'Subscriptions for {}'.format(jid)) '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', options = form.add_field(var='enable',
ftype='boolean', ftype='boolean',
label='Enable', label='Enable',
@ -534,7 +562,7 @@ class Slixfeed(slixmpp.ClientXMPP):
jid = session['from'].bare jid = session['from'].bare
form = self['xep_0004'].make_form('form', form = self['xep_0004'].make_form('form',
'Bookmarks for {}'.format(jid)) 'Bookmarks for {}'.format(jid))
form['instructions'] = '📑️ Organize bookmarks.' form['instructions'] = '📑️ Organize bookmarks'
options = form.add_field(var='bookmarks', options = form.add_field(var='bookmarks',
# ftype='list-multi' # ftype='list-multi'
ftype='list-single', ftype='list-single',
@ -553,7 +581,7 @@ class Slixfeed(slixmpp.ClientXMPP):
jid = session['from'].bare jid = session['from'].bare
form = self['xep_0004'].make_form('form', form = self['xep_0004'].make_form('form',
'Bookmarks for {}'.format(jid)) 'Bookmarks for {}'.format(jid))
form['instructions'] = '📝️ Edit bookmarks.' form['instructions'] = '📝️ Edit bookmarks'
form.addField(var='name', form.addField(var='name',
ftype='text-single', ftype='text-single',
label='Name') label='Name')
@ -603,9 +631,9 @@ class Slixfeed(slixmpp.ClientXMPP):
db_file = config.get_pathname_to_database(jid_file) db_file = config.get_pathname_to_database(jid_file)
form = self['xep_0004'].make_form('form', form = self['xep_0004'].make_form('form',
'Settings for {}'.format(jid)) '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) value = int(value)
if value: if value:
value = True value = True
@ -617,7 +645,7 @@ class Slixfeed(slixmpp.ClientXMPP):
desc='Enable news updates.', desc='Enable news updates.',
value=value) value=value)
value = await config.get_setting_value(db_file, 'media') value = config.get_setting_value(db_file, 'media')
value = int(value) value = int(value)
if value: if value:
value = True value = True
@ -629,12 +657,12 @@ class Slixfeed(slixmpp.ClientXMPP):
label='Display media', label='Display media',
value=value) value=value)
value = await config.get_setting_value(db_file, 'old') value = config.get_setting_value(db_file, 'old')
value = int(value) value = int(value)
if value: if value:
value = False
else:
value = True value = True
else:
value = False
form.add_field(var='old', form.add_field(var='old',
ftype='boolean', ftype='boolean',
desc='Send old items of newly added subscriptions.', desc='Send old items of newly added subscriptions.',
@ -642,8 +670,11 @@ class Slixfeed(slixmpp.ClientXMPP):
label='Include old news', label='Include old news',
value=value) value=value)
value = await config.get_setting_value(db_file, 'interval') value = config.get_setting_value(db_file, 'interval')
value = str(int(value/60)) value = int(value)
value = value
value = int(value)
value = str(value)
options = form.add_field(var='interval', options = form.add_field(var='interval',
ftype='list-single', ftype='list-single',
label='Interval', label='Interval',
@ -654,9 +685,12 @@ class Slixfeed(slixmpp.ClientXMPP):
var = str(i) var = str(i)
lab = str(int(i/60)) lab = str(int(i/60))
options.addOption(lab, var) 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) value = str(value)
options = form.add_field(var='archive', options = form.add_field(var='archive',
ftype='list-single', ftype='list-single',
@ -669,13 +703,13 @@ class Slixfeed(slixmpp.ClientXMPP):
options.addOption(x, x) options.addOption(x, x)
i += 50 i += 50
value = await config.get_setting_value(db_file, 'quantum') value = config.get_setting_value(db_file, 'quantum')
value = str(value) value = str(value)
options = form.add_field(var='quantum', options = form.add_field(var='quantum',
ftype='list-single', ftype='list-single',
label='Amount', label='Amount',
desc='Set amount of updates per update.', desc='Set amount of updates per update.',
value='3') value=value)
i = 1 i = 1
while i <= 5: while i <= 5:
x = str(i) x = str(i)
@ -702,28 +736,57 @@ class Slixfeed(slixmpp.ClientXMPP):
session. Additional, custom data may be saved session. Additional, custom data may be saved
here to persist across handler callbacks. here to persist across handler callbacks.
""" """
# This looks nice in Gajim, though there are dropdown menues
# form = payload
jid = session['from'].bare jid = session['from'].bare
form = self['xep_0004'].make_form('form',
'Settings for {}'.format(jid))
form['instructions'] = ('🛡️ Settings have beem saved')
jid_file = jid jid_file = jid
db_file = config.get_pathname_to_database(jid_file) db_file = config.get_pathname_to_database(jid_file)
# In this case (as is typical), the payload is a form # In this case (as is typical), the payload is a form
form = payload values = payload['values']
values = form['values']
for value in values: for value in values:
key = value key = value
val = values[value] val = values[value]
if key == 'interval':
val = int(val)
if val < 60:
val = 90
if sqlite.get_settings_value(db_file, key): if sqlite.get_settings_value(db_file, key):
await sqlite.update_settings_value(db_file, [key, val]) await sqlite.update_settings_value(db_file, [key, val])
else: else:
await sqlite.set_settings_value(db_file, [key, val]) await sqlite.set_settings_value(db_file, [key, val])
match value:
case 'enabled': val = sqlite.get_settings_value(db_file, key)
pass if key in ('enabled', 'media', 'old'):
case 'interval': if val == '1':
pass val = 'Yes'
# Having no return statement is the same as unsetting the 'payload' elif val == '0':
# and 'next' session values and returning the session. val = 'No'
# Unless it is the final step, always return the session dictionary.
session['payload'] = None 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 session['next'] = None
return session # return session

View file

@ -530,7 +530,7 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
jid = session['from'].bare jid = session['from'].bare
jid_file = jid jid_file = jid
db_file = config.get_pathname_to_database(jid_file) 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) value = int(value)
if value: if value:
value = True value = True
@ -541,7 +541,7 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
label='Enable', label='Enable',
desc='Enable news updates.', desc='Enable news updates.',
value=value) value=value)
value = await config.get_setting_value(db_file, 'old') value = config.get_setting_value(db_file, 'old')
value = int(value) value = int(value)
if value: if value:
value = False value = False
@ -554,7 +554,7 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
# label='Send only new items', # label='Send only new items',
label='Include old news', label='Include old news',
value=value) value=value)
value = await config.get_setting_value(db_file, 'interval') value = config.get_setting_value(db_file, 'interval')
value = str(int(value/60)) value = str(int(value/60))
options = form.add_field(var='interval', options = form.add_field(var='interval',
ftype='list-single', ftype='list-single',
@ -567,7 +567,7 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
lab = str(int(i/60)) lab = str(int(i/60))
options.addOption(lab, var) options.addOption(lab, var)
i += 60 i += 60
value = await config.get_setting_value(db_file, 'archive') value = config.get_setting_value(db_file, 'archive')
value = str(value) value = str(value)
options = form.add_field(var='archive', options = form.add_field(var='archive',
ftype='list-single', ftype='list-single',
@ -579,7 +579,7 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
x = str(i) x = str(i)
options.addOption(x, x) options.addOption(x, x)
i += 1 i += 1
value = await config.get_setting_value(db_file, 'quantum') value = config.get_setting_value(db_file, 'quantum')
value = str(value) value = str(value)
options = form.add_field(var='quantum', options = form.add_field(var='quantum',
ftype='list-single', ftype='list-single',

View file

@ -87,18 +87,18 @@ class XmppGroupchat:
async def leave(self, muc_jid): async def leave(self, muc_jid):
messages = [ jid = self.boundjid.bare
"Whenever you need an RSS service again, " message = ('This news bot will now leave this groupchat.\n'
"please dont hesitate to contact me.", 'The JID of this news bot is xmpp:{}?message'
"My JID is xmpp:{}?message".format(self.boundjid.bare), .format(jid))
"Farewell." status_message = ('This bot has left the group. '
] 'It can be reached directly via {}'
for message in messages: .format(jid))
self.send_message(mto=muc_jid, self.send_message(mto=muc_jid,
mfrom=self.boundjid.bare, mfrom=self.boundjid.bare,
mbody=message, mbody=message,
mtype='groupchat') mtype='groupchat')
self.plugin['xep_0045'].leave_muc(muc_jid, self.plugin['xep_0045'].leave_muc(muc_jid,
self.alias, self.alias,
'Goodbye!', status_message,
self.boundjid.bare) self.boundjid.bare)

View file

@ -294,7 +294,7 @@ async def message(self, message):
if not exist: if not exist:
await sqlite.insert_feed(db_file, url, title) await sqlite.insert_feed(db_file, url, title)
await action.scan(db_file, url) 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: if old:
# task.clean_tasks_xmpp(self, jid, ['status']) # task.clean_tasks_xmpp(self, jid, ['status'])
# await send_status(jid) # await send_status(jid)