add per-user fediverse setting
This commit is contained in:
parent
8bebcb14c7
commit
9e43ea1599
2 changed files with 38 additions and 6 deletions
|
@ -8,17 +8,39 @@ ACTIVITY_TYPE = "application/activity+json"
|
||||||
ACTIVITY_HEADERS = {"Accept": ("application/ld+json; "
|
ACTIVITY_HEADERS = {"Accept": ("application/ld+json; "
|
||||||
'profile="https://www.w3.org/ns/activitystreams"')}
|
'profile="https://www.w3.org/ns/activitystreams"')}
|
||||||
|
|
||||||
|
def _parse_username(s):
|
||||||
|
username, _, instance = s.lstrip("@").partition("@")
|
||||||
|
if username and instance:
|
||||||
|
return "@%s@%s" % (username, instance)
|
||||||
|
return None
|
||||||
|
|
||||||
|
@utils.export("set", utils.FunctionSetting(_parse_username, "fediverse",
|
||||||
|
help="Set your fediverse account", example="@gargron@mastodon.social"))
|
||||||
class Module(ModuleManager.BaseModule):
|
class Module(ModuleManager.BaseModule):
|
||||||
_name = "Fedi"
|
_name = "Fedi"
|
||||||
|
|
||||||
@utils.hook("received.command.fediverse")
|
@utils.hook("received.command.fediverse")
|
||||||
@utils.hook("received.command.fedi", alias_of="fediverse")
|
@utils.hook("received.command.fedi", alias_of="fediverse")
|
||||||
@utils.kwarg("min_args", 1)
|
|
||||||
@utils.kwarg("help", "Get someone's latest toot")
|
@utils.kwarg("help", "Get someone's latest toot")
|
||||||
@utils.kwarg("usage", "@<user>@<instance>")
|
@utils.kwarg("usage", "@<user>@<instance>")
|
||||||
def fedi(self, event):
|
def fedi(self, event):
|
||||||
full_username = event["args_split"][0].lstrip("@")
|
account = None
|
||||||
username, _, instance = full_username.partition("@")
|
if not event["args"]:
|
||||||
|
account = event["user"].get_setting("fediverse", None)
|
||||||
|
elif not "@" in event["args"]:
|
||||||
|
target = event["args_split"][0]
|
||||||
|
if event["server"].has_user_id(target):
|
||||||
|
target_user = event["server"].get_user(target)
|
||||||
|
account = target_user.get_setting("fediverse", None)
|
||||||
|
else:
|
||||||
|
account = event["args_split"][0]
|
||||||
|
|
||||||
|
username = None
|
||||||
|
instance = None
|
||||||
|
if account:
|
||||||
|
account = account.lstrip("@")
|
||||||
|
username, _, instance = account.partition("@")
|
||||||
|
|
||||||
if not username or not instance:
|
if not username or not instance:
|
||||||
raise utils.EventError("Please provide @<user>@<instance>")
|
raise utils.EventError("Please provide @<user>@<instance>")
|
||||||
|
|
||||||
|
@ -33,12 +55,11 @@ class Module(ModuleManager.BaseModule):
|
||||||
if webfinger_url == None:
|
if webfinger_url == None:
|
||||||
raise utils.EventError("host-meta lookup failed for %s" %
|
raise utils.EventError("host-meta lookup failed for %s" %
|
||||||
instance)
|
instance)
|
||||||
webfinger_url = webfinger_url.replace("{uri}",
|
webfinger_url = webfinger_url.replace("{uri}", "acct:%s" % account)
|
||||||
"acct:%s" % full_username)
|
|
||||||
|
|
||||||
webfinger = utils.http.request(webfinger_url,
|
webfinger = utils.http.request(webfinger_url,
|
||||||
headers=WEBFINGER_HEADERS,
|
headers=WEBFINGER_HEADERS,
|
||||||
get_params={"resource": "acct:%s" % full_username},
|
get_params={"resource": "acct:%s" % account},
|
||||||
json=True)
|
json=True)
|
||||||
|
|
||||||
activity_url = None
|
activity_url = None
|
||||||
|
@ -61,8 +82,10 @@ class Module(ModuleManager.BaseModule):
|
||||||
|
|
||||||
if "first" in outbox.data:
|
if "first" in outbox.data:
|
||||||
if type(outbox.data["first"]) == dict:
|
if type(outbox.data["first"]) == dict:
|
||||||
|
# pleroma
|
||||||
items = outbox.data["first"]["orderedItems"]
|
items = outbox.data["first"]["orderedItems"]
|
||||||
else:
|
else:
|
||||||
|
# mastodon
|
||||||
first = utils.http.request(outbox.data["first"],
|
first = utils.http.request(outbox.data["first"],
|
||||||
headers=ACTIVITY_HEADERS, json=True)
|
headers=ACTIVITY_HEADERS, json=True)
|
||||||
items = first.data["orderedItems"]
|
items = first.data["orderedItems"]
|
||||||
|
|
|
@ -344,6 +344,15 @@ class OptionsSetting(Setting):
|
||||||
options_str = ["'%s'" % option for option in options]
|
options_str = ["'%s'" % option for option in options]
|
||||||
return "Options: %s" % ", ".join(options_str)
|
return "Options: %s" % ", ".join(options_str)
|
||||||
|
|
||||||
|
class FunctionSetting(Setting):
|
||||||
|
def __init__(self, func: typing.Callable[[str], bool], name: str,
|
||||||
|
help: str=None, example: str=None):
|
||||||
|
self._func = func
|
||||||
|
Setting.__init__(self, name, help, example)
|
||||||
|
|
||||||
|
def parse(self, value: str) -> typing.Any:
|
||||||
|
return self._func(value)
|
||||||
|
|
||||||
class DeadlineExceededException(Exception):
|
class DeadlineExceededException(Exception):
|
||||||
pass
|
pass
|
||||||
def _raise_deadline():
|
def _raise_deadline():
|
||||||
|
|
Loading…
Reference in a new issue