~thrrgilag/squeak

ref: fa6dd5126fe046074b74241a277f44fba0e7b83e squeak/src/widgets.py -rw-r--r-- 18.4 KiB
fa6dd512Morgan McMillian fix display of reposted posts 3 months ago
                                                                                
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# widgets.py
#
# Copyright 2020 Morgan McMillian
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import gi
import os
import pnutpy
import logging
import requests
import timeago
import datetime

from dateutil.tz import tzlocal

gi.require_version('GdkPixbuf', '2.0')
gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
from gi.repository import GObject, GdkPixbuf, Gdk, Gtk, Gio, GLib

gi.require_version('Handy', '1')
from gi.repository import Handy

class ComposeWindow(Handy.Window):

    def __init__(self):
        super().__init__(modal=True)
        self.set_default_size(500, 300)
        self.set_type_hint(Gdk.WindowTypeHint.DIALOG)

        self.reply_to = None

        box = Gtk.Box(orientation='vertical')
        header = Handy.HeaderBar(show_close_button=False)

        cancel_button = Gtk.Button(label='Cancel')
        cancel_button.connect('clicked', self.cancel_post)
        post_button = Gtk.Button(label='Post')
        post_button.connect('clicked', self.send_post)

        header.set_title('New Post')
        header.pack_start(cancel_button)
        header.pack_end(post_button)

        scroller = Gtk.ScrolledWindow(halign='fill')
        textarea = Gtk.TextView(
            left_margin=8,
            right_margin=8,
            top_margin=8,
            bottom_margin=8
        )
        textarea.set_wrap_mode(Gtk.WrapMode.WORD)
        scroller.add(textarea)
        self.buffer = textarea.get_buffer()

        self.max_length = 256
        actionbar = Gtk.ActionBar()
        self.counter_label = Gtk.Label()
        self.counter_label.set_text(str(self.max_length))
        actionbar.pack_end(self.counter_label)

        self.buffer.connect('changed', self.validate)

        box.pack_start(header, False, False, 0)
        box.pack_start(scroller, True, True, 0)
        box.pack_end(actionbar, False, False, 0)
        self.add(box)

        self.show_all()
        textarea.grab_focus()

        self.connect('key-release-event', self.on_key_release)
        self.connect('key-press-event', self.on_key_press)

    def on_key_press(self, widget, ev, data=None):
        ctrl = (ev.state & Gdk.ModifierType.CONTROL_MASK)
        if ctrl and ev.keyval == Gdk.KEY_Return:
            self.send_post(None)

    def on_key_release(self, widget, ev, data=None):
        if ev.keyval == Gdk.KEY_Escape:
            self.close()

    def validate(self, widget):
        self.counter = self.max_length - self.buffer.get_char_count()
        self.counter_label.set_text(str(self.counter))

    def set_post(self, text):
        self.buffer.set_text(text, -1)

    def set_reply_to(self, reply_to):
        self.reply_to = reply_to

    def send_post(self, button):
        start = self.buffer.get_start_iter()
        end = self.buffer.get_end_iter()
        text = self.buffer.get_text(start, end, False)
        data = {'text': text}
        if self.reply_to is not None:
            data['reply_to'] = self.reply_to
        pnutpy.api.create_post(data=data)
        self.close()

    def cancel_post(self, button):
        self.close()

class LoginPage(Gtk.Box):

    __gsignals__ = {
        'login': (GObject.SIGNAL_RUN_FIRST, None, (str,))
    }

    def __init__(self):
        super().__init__(orientation='vertical')
        self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)

        client_id = "1PiUzxfX_CQxKvtz93lUzPX9-FMtz-va"
        redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
        scope =  "basic,stream,write_post,follow,presence,messages,files,polls"

        uri = "https://pnut.io/oauth/authenticate"
        uri += "?client_id=" + client_id
        uri += "&redirect_uri=" + redirect_uri
        uri += "&scope=" + scope
        uri += "&response_type=token"

        self.login_button = Gtk.LinkButton.new_with_label(uri, "Log In to pnut.io")
        self.login_button.connect("clicked", self.prompt_code)

        self.set_center_widget(self.login_button)

    def prompt_code(self, button):
        self.remove(self.login_button)

        label = Gtk.Label()
        label.set_markup('<span font="20">Enter authorization code</span>')
        self.code = Gtk.Entry()

        paste_button = Gtk.Button(label="Paste from clipboard")
        paste_button.connect("clicked", self.paste_code)

        cancel_button = Gtk.Button(label="Cancel")
        cancel_button.connect("clicked", self.cancel_login)
        confirm_button = Gtk.Button(label="Confirm")
        confirm_button.connect("clicked", self.confirm_login)
        lbox = Gtk.Box(orientation='horizontal')
        lbox.pack_start(cancel_button, True, True, 0)
        lbox.pack_start(confirm_button, True, True, 0)

        vbox = Gtk.Box(orientation='vertical')
        vbox.pack_start(label, False, False, 10)
        vbox.pack_start(self.code, False, False, 10)
        vbox.pack_start(paste_button, False, False, 10)
        vbox.add(lbox)

        hbox = Gtk.Box(orientation='horizontal')
        hbox.set_center_widget(vbox)

        self.set_center_widget(hbox)
        self.show_all()

    def paste_code(self, button):
        text = self.clipboard.wait_for_text()
        if text is not None:
            self.code.set_text(text)

    def cancel_login(self, button):
        # TODO: something actually useful here
        logging.debug("uh cancel i guess")

    def confirm_login(self, button):
        code = self.code.get_text()
        self.emit('login', code)

class Timeline(Gtk.Box):

    __gsignals__ = {
        'refresh': (GObject.SIGNAL_RUN_FIRST, None, ()),
        'top': (GObject.SIGNAL_RUN_FIRST, None, ()),
        'reply': (GObject.SIGNAL_RUN_FIRST, None, (int,str,))
    }

    def __init__(self, stream):
        super().__init__(orientation='vertical')

        self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
        self.max_id = 0
        self.min_id = 0

        self.scroller = Gtk.ScrolledWindow(
            halign='fill',
            kinetic_scrolling=True
        )
        self.view = Gtk.ListBox(
            selection_mode=Gtk.SelectionMode.NONE
        )
        self.scroller.add(self.view)
        self.pack_start(self.scroller, True, True, 0)

        self.stream = stream
        self.load_timeline()

        self.view.connect('button-press-event', self.on_button_pressed)
        self.scroller.connect('edge-reached', self.on_edge_reached)

        action_group = Gio.SimpleActionGroup()

        action = Gio.SimpleAction.new('reply', None)
        action.connect('activate', self.on_reply)
        action_group.add_action(action)

        action = Gio.SimpleAction.new('replyall', None)
        action.connect('activate', self.on_reply_all)
        action_group.add_action(action)

        action = Gio.SimpleAction.new('bookmark', None)
        action.connect('activate', self.on_bookmark)
        action_group.add_action(action)

        action = Gio.SimpleAction.new('repost', None)
        action.connect('activate', self.on_repost)
        action_group.add_action(action)

        action = Gio.SimpleAction.new('quote', None)
        action.connect('activate', self.on_quote)
        action_group.add_action(action)

        action = Gio.SimpleAction.new('copy', None)
        action.connect('activate', self.on_copy)
        action_group.add_action(action)

        action = Gio.SimpleAction.new('copylink', None)
        action.connect('activate', self.on_copy_link)
        action_group.add_action(action)

        action = Gio.SimpleAction.new('openlink', None)
        action.connect('activate', self.on_open_link)
        action_group.add_action(action)

        self.insert_action_group('win', action_group)

        builder = Gtk.Builder.new_from_resource("/dev/thrrgilag/squeak/menu.ui")
        self.menu = builder.get_object("post-menu")

    def load_timeline(self, older=False):
        params = {
            'include_raw': 1
        }

        if older:
            params['before_id'] = self.min_id

        if self.stream == 'unified':
            posts, meta = pnutpy.api.users_post_streams_unified(**params)
        elif self.stream == 'mentions':
            posts, meta = pnutpy.api.users_mentioned_posts('me', **params)
        elif self.stream == 'bookmarks':
            posts, meta = pnutpy.api.users_bookmarked_posts('me', **params)
        else:
            posts, meta = pnutpy.api.posts_streams_global(**params)

        self.max_id = meta.max_id
        self.min_id = meta.min_id

        for item in posts:
            if 'is_deleted' in item:
                continue
            postitem = PostItem(item)
            postitem.connect('menu-pressed', self.show_menu)
            self.view.add(postitem)

        self.show_all()

    def do_refresh(self):
        self.do_top()
        rows = self.view.get_children()
        for item in rows:
            self.view.remove(item)
        self.load_timeline()
        self.show_all()

    def do_top(self):
        vadjustment = self.scroller.get_vadjustment()
        vadjustment.set_value(0)

    def on_edge_reached(self, widget, pos):
        if pos == Gtk.PositionType.BOTTOM:
            self.load_timeline(True)

    def on_button_pressed(self, widget, event):
        if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3:
            pass

    def show_menu(self, widget, index):
        self.post_index = index
        self.post_data = widget.post
        popover = Gtk.Popover()
        popover.set_position(Gtk.PositionType.BOTTOM)
        popover.set_relative_to(widget.menu_button)
        popover.bind_model(self.menu, None)
        popover.popup()

    def on_reply(self, action, param):
        replyuser = "@" + self.post_data.user.username + " "
        self.emit('reply', self.post_data.id, replyuser)

    def on_reply_all(self, action, param):
        replyuser = "@" + self.post_data.user.username + " "
        for mention in self.post_data.content.entities.mentions:
            replyuser += "@" + mention.text + " "
        self.emit('reply', self.post_data.id, replyuser)

    def on_bookmark(self, action, param):
        if self.post_data.you_bookmarked:
            pnutpy.api.unbookmark_post(self.post_data.id)
        else:
            pnutpy.api.bookmark_post(self.post_data.id)

    def on_repost(self, action, param):
        if self.post_data.you_bookmarked:
            pnutpy.api.unrepost_post(self.post_data.id)
        else:
            pnutpy.api.repost_post(self.post_data.id)

    def on_quote(self, action, param):
        quote = " >> @" + self.post_data.user.username + ": "
        quote += self.post_data.content.text
        logging.debug(quote)
        self.emit('reply', self.post_data.id, quote)

    def on_copy(self, action, param):
        self.clipboard.set_text(self.post_data.content.text, -1)

    def on_copy_link(self, action, param):
        post_url = f"https://posts.pnut.io/{self.post_data.id}"
        self.clipboard.set_text(post_url, -1)

    def on_open_link(self, action, param):
        logging.debug("open_link")
        post_url = f"https://posts.pnut.io/{self.post_data.id}"
        opened = Gtk.show_uri_on_window(None, post_url, Gdk.CURRENT_TIME)
        logging.debug(opened)

class PostItem(Gtk.ListBoxRow):

    __gsignals__ = {
        'menu-pressed': (GObject.SIGNAL_RUN_FIRST, None, (int,))
    }

    def __init__(self, post):
        super(Gtk.ListBoxRow, self).__init__()
        if 'repost_of' in post:
            reposted_by = post.user
            post = post.repost_of
        else:
            reposted_by = None
        self.post = post

        # Avatar
        #self.avatar = Handy.Avatar(size=48, text=post.user.username, show_initials=True)
        #self.avatar.set_image_load_func(self.get_avatar, post.user.content.avatar_image)
        self.avatar = Gtk.Image.new_from_pixbuf(self.get_avatar(48, post.user.content.avatar_image))

        # Source
        self.source = Gtk.Label(label=post.source.name, xalign=1)

        self.username = Gtk.Label(label="@" + post.user.username, xalign=0)
        self.name = Gtk.Label(xalign=0)
        if 'name' in post.user:
            self.name.set_markup(f"<b>{post.user.name}</b>")

        # datetime
        now = datetime.datetime.now(tzlocal())
        post_date_local = post.created_at.astimezone(tzlocal())
        #datetime_label = Gtk.Label(label=post_date_local.strftime("%Y-%m-%d  %H:%M"), xalign=1)
        datetime_label = Gtk.Label(xalign=1)
        datetime_label.set_markup('<span size="small">' + timeago.format(post_date_local, now) + '</span>')

        # post menu
        self.menu_button = Gtk.Button.new_from_icon_name('view-more-symbolic', 1)
        self.menu_button.connect('clicked', self.show_menu)

        self.box = Gtk.Box(orientation='vertical')
        self.add(self.box)

        # name container
        self.name_box = Gtk.Box(orientation='vertical')
        self.name_box.pack_start(self.name, True, True, 0)
        self.name_box.pack_start(self.username, True, True, 0)

        # header container
        self.h_box = Gtk.Box(orientation='horizontal')
        self.h_box.pack_start(self.avatar, False, False, 10)
        self.h_box.pack_start(self.name_box, False, False, 0)
        #self.h_box.pack_end(self.source, False, False, 10)
        #self.h_box.pack_end(datetime_label, False, False, 10)

        # content container
        self.c_box = Gtk.Box(orientation='vertical')
        self.t_box = Gtk.Box(orientation='horizontal')
        self.content = Gtk.Label(wrap=True, xalign=0)
        # TODO: parse content links
        if 'content' in post:
            self.content.set_text(post.content.text)
        self.t_box.pack_start(self.content, False, False, 10)
        self.c_box.pack_start(self.t_box, False, False, 10)

        if 'raw' in post:
            for raw in post.raw:
                # TODO: hide this under toggle?
                if raw.type == "nl.chimpnut.blog.post":
                    longpost = raw.value
                    if 'title' in longpost:
                        lp_title = Gtk.Label(wrap=True)
                        lp_title.set_markup(f"<b>{longpost.title}</b>")
                        self.c_box.pack_start(lp_title, False, False, 10)
                    lp_body = Gtk.Label(wrap=True)
                    lp_body.set_text(longpost.body)
                    self.c_box.pack_start(lp_body, False, False, 10)

                # TODO: open full photo in seperate window
                if raw.type == "io.pnut.core.oembed":
                    oembed = raw.value
                    if oembed.type == "photo":
                        photo = Gtk.Image.new_from_pixbuf(self.get_oembed_thumb(oembed))
                        self.c_box.pack_end(photo, False, False, 10)

        if reposted_by is not None:
            reposted_label_text = f'<span size="small"><i>reposted by @{reposted_by.username}</i></span>'
            reposted_label = Gtk.Label(xalign=0)
            reposted_label.set_margin_left(10)
            reposted_label.set_margin_top(5)
            reposted_label.set_markup(reposted_label_text)
            self.c_box.pack_end(reposted_label, False, False, 0)

        # footer container
        self.f_box = Gtk.Box(orientation='horizontal')
        self.f_box.pack_end(self.menu_button, False, False, 5)
        self.f_box.pack_end(datetime_label, False, False, 5)
        #self.f_box.pack_end(self.source, False, False, 5)

        pad = Gtk.Label(label="")
        self.f_box.pack_start(pad, False, False, 5)

        if post.id != int(post.thread_id):
            thread_icon = Gtk.Image.new_from_icon_name("user-available-symbolic", Gtk.IconSize.SMALL_TOOLBAR)
            self.f_box.pack_start(thread_icon, False, False, 5)
        if post.counts.bookmarks > 0:
            if post.you_bookmarked:
                star_icon = Gtk.Image.new_from_icon_name("starred-symbolic", Gtk.IconSize.SMALL_TOOLBAR)
            else:
                star_icon = Gtk.Image.new_from_icon_name("non-starred-symbolic", Gtk.IconSize.SMALL_TOOLBAR)
            star_count = Gtk.Label(label=post.counts.bookmarks)
            self.f_box.pack_start(star_icon, False, False, 5)
            self.f_box.pack_start(star_count, False, False, 0)
        if post.counts.reposts > 0:
            repost_icon = Gtk.Image.new_from_icon_name("media-playlist-repeat-symbolic", Gtk.IconSize.SMALL_TOOLBAR)
            repost_count = Gtk.Label(label=post.counts.reposts)
            self.f_box.pack_start(repost_icon, False, False, 5)
            self.f_box.pack_start(repost_count, False, False, 0)

        self.box.pack_start(self.h_box, True, True, 10)
        self.box.pack_start(self.c_box, True, True, 10)
        self.box.pack_start(self.f_box, True, True, 10)

    def get_avatar(self, size, avatar):
        # TODO: age out cache
        cache_dir = os.path.join(GLib.get_user_cache_dir(), 'avatars')
        link_hash = avatar.link.rsplit('/', 1)[-1]
        file_hash = os.path.join(cache_dir, link_hash)
        if not os.path.exists(cache_dir):
            os.mkdir(cache_dir)

        if os.path.exists(file_hash):
            scaled_img = GdkPixbuf.Pixbuf.new_from_file(file_hash)
        else:
            r = requests.get(avatar.link, stream=True)
            loader = GdkPixbuf.PixbufLoader()
            loader.write(r.content)
            loader.close()
            pixbuf = loader.get_pixbuf()
            old_width = avatar.width
            old_height = avatar.height
            ratio = old_width / old_height
            new_height = size / ratio
            scaled_img = pixbuf.scale_simple(size,new_height,GdkPixbuf.InterpType.BILINEAR)
            scaled_img.savev(file_hash, 'jpeg', ["quality"], ["100"])

        return scaled_img

    def get_oembed_thumb(self, oembed):
        url = oembed.thumbnail_url
        old_width = oembed.thumbnail_width 
        old_height = oembed.thumbnail_height
        ratio = old_width / old_height
        new_height = 256 / ratio
        r = requests.get(url)
        loader = GdkPixbuf.PixbufLoader()
        loader.write(r.content)
        loader.close()
        pixbuf = loader.get_pixbuf()
        return pixbuf.scale_simple(256,new_height,GdkPixbuf.InterpType.BILINEAR)

    def show_menu(self, widget):
        self.emit('menu-pressed', self.get_index())