File: preferences.py

package info (click to toggle)
python-mastodon 2.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,836 kB
  • sloc: python: 9,438; makefile: 206; sql: 98; sh: 27
file content (80 lines) | stat: -rw-r--r-- 3,336 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# preferences.py - user preferences, markers

import collections

from mastodon.errors import MastodonIllegalArgumentError
from mastodon.utility import api_version

from mastodon.internals import Mastodon as Internals
from mastodon.return_types import Preferences, Marker, Status, IdType
from mastodon.types_base import AttribAccessDict, try_cast_recurse
from typing import Union, List, Dict

class Mastodon(Internals):
    ###
    # Reading data: Preferences
    ###
    @api_version("2.8.0", "2.8.0")
    def preferences(self) -> Preferences:
        """
        Fetch the user's preferences, which can be used to set some default options.
        As of 2.8.0, apps can only fetch, not update preferences.
        """
        return self.__api_request('GET', '/api/v1/preferences')

    ##
    # Reading data: Read markers
    ##
    @api_version("3.0.0", "3.0.0")
    def markers_get(self, timeline: Union[str, List[str]] = ["home"]) -> Dict[str, Marker]:
        """
        Get the last-read-location markers for the specified timelines. Valid timelines
        are `home` (the home timeline) and `notifications` (the notifications timeline,
        affects which notifications are considered read).

        Note that despite the singular name, `timeline` can be a list.

        Returns a dict with the markers, keyed by timeline name.
        """
        if not isinstance(timeline, (list, tuple)):
            timeline = [timeline]
        params = self.__generate_params(locals())
        result = self.__api_request('GET', '/api/v1/markers', params)
        result_real = AttribAccessDict()
        for key, value in result.items():
            result_real[key] = try_cast_recurse(Marker, value)
        return result_real

    ##
    # Writing data: Read markers
    ##
    @api_version("3.0.0", "3.0.0")
    def markers_set(self, timelines: Union[str, List[str]], last_read_ids: Union[Status, IdType, List[Status], List[IdType]]) -> Dict[str, Marker]:
        """
        Set the "last read" marker(s) for the given timeline(s) to the given id(s)

        Valid timelines are `home` (the home timeline) and `notifications` (the notifications timeline,
        affects which notifications are considered read).
        
        Note that if you give an invalid timeline name, this will silently do nothing.

        Returns a dict with the updated markers, keyed by timeline name.
        """
        if not isinstance(timelines, (list, tuple)):
            timelines = [timelines]

        if not isinstance(last_read_ids, (list, tuple)):
            last_read_ids = [last_read_ids]

        if len(last_read_ids) != len(timelines):
            raise MastodonIllegalArgumentError("Number of specified timelines and ids must be the same")

        params = collections.OrderedDict()
        for timeline, last_read_id in zip(timelines, last_read_ids):
            params[timeline] = collections.OrderedDict()
            params[timeline]["last_read_id"] = self.__unpack_id(last_read_id)
        result = self.__api_request('POST', '/api/v1/markers', params, use_json=True)
        result_real = AttribAccessDict()
        for key, value in result.items():
            result_real[key] = try_cast_recurse(Marker, value)
        return result_real