Slixfeed/slixfeed/xmpp/bookmark.py

97 lines
3.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
TODO
1) Save groupchat name instead of jid in field name.
"""
from slixmpp.plugins.xep_0048.stanza import Bookmarks
2024-02-06 04:04:43 +01:00
class XmppBookmark:
2024-02-06 04:04:43 +01:00
async def get(self):
result = await self.plugin['xep_0048'].get_bookmarks()
2024-02-14 04:04:49 +01:00
conferences = result['private']['bookmarks']['conferences']
2024-02-06 04:04:43 +01:00
return conferences
async def properties(self, jid):
result = await self.plugin['xep_0048'].get_bookmarks()
groupchats = result['private']['bookmarks']['conferences']
for groupchat in groupchats:
if jid == groupchat['jid']:
properties = {'password': groupchat['password'],
'jid': groupchat['jid'],
'name': groupchat['name'],
'nick': groupchat['nick'],
'autojoin': groupchat['autojoin'],
'lang': groupchat['lang']}
break
return properties
2024-02-14 04:04:49 +01:00
async def add(self, jid=None, properties=None):
2024-02-06 04:04:43 +01:00
result = await self.plugin['xep_0048'].get_bookmarks()
2024-02-14 04:04:49 +01:00
conferences = result['private']['bookmarks']['conferences']
groupchats = []
2024-02-06 04:04:43 +01:00
for conference in conferences:
2024-02-14 04:04:49 +01:00
groupchats.extend([conference])
if properties:
properties['jid'] = properties['room'] + '@' + properties['host']
if not properties['alias']: properties['alias'] = self.alias
2024-02-14 04:04:49 +01:00
else:
properties = {
'jid' : jid,
'alias' : self.alias,
'name' : jid.split('@')[0],
'autojoin' : True,
'password' : None,
}
if jid not in groupchats:
2024-02-06 04:04:43 +01:00
bookmarks = Bookmarks()
2024-02-14 04:04:49 +01:00
for groupchat in groupchats:
# if groupchat['jid'] == groupchat['name']:
# groupchat['name'] = groupchat['name'].split('@')[0]
bookmarks.add_conference(groupchat['jid'],
groupchat['alias'],
name=groupchat['name'],
autojoin=groupchat['autojoin'],
password=groupchat['password'])
bookmarks.add_conference(properties['jid'],
properties['alias'],
name=properties['name'],
autojoin=properties['autojoin'],
password=properties['password'])
2024-02-06 04:04:43 +01:00
await self.plugin['xep_0048'].set_bookmarks(bookmarks)
# bookmarks = Bookmarks()
# await self.plugin['xep_0048'].set_bookmarks(bookmarks)
# print(await self.plugin['xep_0048'].get_bookmarks())
2024-02-06 04:04:43 +01:00
# bm = BookmarkStorage()
# bm.conferences.append(Conference(muc_jid, autojoin=True, nick=self.alias))
# await self['xep_0402'].publish(bm)
2024-02-14 04:04:49 +01:00
async def remove(self, jid):
2024-02-06 04:04:43 +01:00
result = await self.plugin['xep_0048'].get_bookmarks()
2024-02-14 04:04:49 +01:00
conferences = result['private']['bookmarks']['conferences']
groupchats = []
2024-02-06 04:04:43 +01:00
for conference in conferences:
2024-02-14 04:04:49 +01:00
if not conference['jid'] == jid:
groupchats.extend([conference])
bookmarks = Bookmarks()
for groupchat in groupchats:
bookmarks.add_conference(groupchat['jid'],
groupchat['nick'],
name=groupchat['name'],
autojoin=groupchat['autojoin'],
password=groupchat['password'])
await self.plugin['xep_0048'].set_bookmarks(bookmarks)