aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/views/wiki/edit.py
blob: cc121cc6c2368e675acb99a41c6129545d8a2afe (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import datetime
import difflib
import html
import re

import requests
from flask import redirect, request, url_for
from werkzeug.exceptions import BadRequest

from pysite.base_route import RouteView
from pysite.constants import DEBUG_MODE, EDITOR_ROLES, GITHUB_TOKEN, WIKI_AUDIT_WEBHOOK
from pysite.decorators import csrf, require_roles
from pysite.mixins import DBMixin
from pysite.rst import render

STRIP_REGEX = re.compile(r"<[^<]+?>")


class EditView(RouteView, DBMixin):
    path = "/edit/<path:page>"  # "path" means that it accepts slashes
    name = "edit"
    table_name = "wiki"
    revision_table_name = "wiki_revisions"

    @require_roles(*EDITOR_ROLES)
    def get(self, page):
        rst = ""
        title = ""
        preview = "<p>Preview will appear here.</p>"

        obj = self.db.get(self.table_name, page)

        if obj:
            rst = obj.get("rst", "")
            title = obj.get("title", "")
            preview = obj.get("html", preview)

            if obj.get("lock_expiry") and obj.get("lock_user") != self.user_data.get("user_id"):
                lock_time = datetime.datetime.fromtimestamp(obj["lock_expiry"])
                if datetime.datetime.utcnow() < lock_time:
                    return self.render("wiki/page_in_use.html", page=page, can_edit=True)

        lock_expiry = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)

        if not DEBUG_MODE:  # If we are in debug mode we have no user logged in, therefore we can skip locking
            self.db.insert(
                self.table_name,
                {
                    "slug": page,
                    "lock_expiry": lock_expiry.timestamp(),
                    "lock_user": self.user_data.get("user_id")
                },
                conflict="update"
            )

        return self.render("wiki/page_edit.html", page=page, rst=rst, title=title, preview=preview, can_edit=True)

    @require_roles(*EDITOR_ROLES)
    @csrf
    def post(self, page):
        rst = request.form.get("rst")
        title = request.form["title"]

        if not rst or not rst.strip():
            raise BadRequest()

        if not title or not title.strip():
            raise BadRequest()

        rendered = render(rst)

        obj = {
            "slug": page,
            "title": request.form["title"],
            "rst": rst,
            "html": rendered["html"],
            "text": html.unescape(STRIP_REGEX.sub("", rendered["html"]).strip()),
            "headers": rendered["headers"]
        }

        self.audit_log(page, obj)

        self.db.insert(
            self.table_name,
            obj,
            conflict="replace"
        )

        if not DEBUG_MODE:
            # Add the post to the revisions table
            revision_payload = {
                "slug": page,
                "post": obj,
                "date": datetime.datetime.utcnow().timestamp(),
                "user": self.user_data.get("user_id")
            }

            del revision_payload["post"]["slug"]

            self.db.insert(self.revision_table_name, revision_payload)

        return redirect(url_for("wiki.page", page=page), code=303)  # Redirect, ensuring a GET

    @require_roles(*EDITOR_ROLES)
    @csrf
    def patch(self, page):
        current = self.db.get(self.table_name, page)
        if not current:
            return "", 404

        if current.get("lock_expiry"):  # If there is a lock present

            # If user patching is not the user with the lock end here
            if current["lock_user"] != self.user_data.get("user_id"):
                return "", 400
            new_lock = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)  # New lock time, 5 minutes in future
            self.db.insert(self.table_name, {
                "slug": page,
                "lock_expiry": new_lock.timestamp()
            }, conflict="update")  # Update with new lock time
        return "", 204

    def audit_log(self, page, obj):
        if WIKI_AUDIT_WEBHOOK:  # If the audit webhook is not configured there is no point processing the diff
            before = self.db.get(self.table_name, page)
            if not before:  # If this is a new page, before will be None
                before = []
            else:
                if before.get("rst") is None:
                    before = []
                else:
                    before = before["rst"].splitlines(keepends=True)
                    if len(before) == 0:
                        pass
                    else:
                        if not before[-1].endswith("\n"):
                            before[-1] += "\n"  # difflib sometimes messes up if a newline is missing on last line

            after = obj['rst'].splitlines(keepends=True) or [""]

            if not after[-1].endswith("\n"):
                after[-1] += "\n"  # Does the same thing as L57

            diff = difflib.unified_diff(before, after, fromfile="before.rst", tofile="after.rst")
            diff = "".join(diff)

            gist_payload = {
                "description": f"Changes to: {obj['title']}",
                "public": False,
                "files": {
                    "changes.md": {
                        "content": f"```diff\n{diff}\n```"
                    }
                }
            }

            headers = {
                "Authorization": f"token {GITHUB_TOKEN}",
                "User-Agent": "Discord Python Wiki (https://gitlab.com/discord-python)"
            }

            gist = requests.post("https://api.github.com/gists",
                                 json=gist_payload,
                                 headers=headers)

            audit_payload = {
                "username": "Wiki Updates",
                "embeds": [
                    {
                        "title": "Page Edit",
                        "description": f"**{obj['title']}** was edited by **{self.user_data.get('username')}**"
                                       f".\n\n[View diff]({gist.json().get('html_url')})",
                        "color": 4165079,
                        "timestamp": datetime.datetime.utcnow().isoformat(),
                        "thumbnail": {
                            "url": "https://pythondiscord.com/static/logos/logo_discord.png"
                        }
                    }
                ]
            }

            requests.post(WIKI_AUDIT_WEBHOOK, json=audit_payload)