Show More
@@ -1,9 +1,7 b'' | |||||
1 | from boards import settings |
|
1 | import boards | |
2 | from boards.models import Tag, TagAlias, Attachment |
|
2 | from boards.models import Tag, TagAlias, Attachment | |
3 | from boards.models.attachment import AttachmentSticker |
|
3 | from boards.models.attachment import AttachmentSticker | |
4 | from boards.models.thread import FAV_THREAD_NO_UPDATES |
|
|||
5 | from boards.models.user import UserSettings |
|
4 | from boards.models.user import UserSettings | |
6 | from boards.settings import SECTION_VIEW |
|
|||
7 |
|
5 | |||
8 | MAX_TRIPCODE_COLLISIONS = 50 |
|
6 | MAX_TRIPCODE_COLLISIONS = 50 | |
9 |
|
7 | |||
@@ -20,6 +18,7 b" SETTING_LAST_NOTIFICATION_ID = 'last_not" | |||||
20 | SETTING_IMAGE_VIEWER = 'image_viewer' |
|
18 | SETTING_IMAGE_VIEWER = 'image_viewer' | |
21 | SETTING_IMAGES = 'images_aliases' |
|
19 | SETTING_IMAGES = 'images_aliases' | |
22 | SETTING_ONLY_FAVORITES = 'only_favorites' |
|
20 | SETTING_ONLY_FAVORITES = 'only_favorites' | |
|
21 | SETTING_LAST_POSTS = 'last_posts' | |||
23 |
|
22 | |||
24 | DEFAULT_THEME = 'md' |
|
23 | DEFAULT_THEME = 'md' | |
25 |
|
24 | |||
@@ -102,31 +101,32 b' class SettingsManager:' | |||||
102 | tags.remove(tag.get_name()) |
|
101 | tags.remove(tag.get_name()) | |
103 | self.set_setting(SETTING_HIDDEN_TAGS, tags) |
|
102 | self.set_setting(SETTING_HIDDEN_TAGS, tags) | |
104 |
|
103 | |||
105 | def get_fav_threads(self) -> dict: |
|
|||
106 | return self.get_setting(SETTING_FAVORITE_THREADS, default=dict()) |
|
|||
107 |
|
||||
108 | def add_or_read_fav_thread(self, opening_post): |
|
104 | def add_or_read_fav_thread(self, opening_post): | |
109 | threads = self.get_fav_threads() |
|
105 | last_post_ids = self.get_setting(SETTING_LAST_POSTS) | |
|
106 | if not last_post_ids: | |||
|
107 | last_post_ids = [] | |||
110 |
|
108 | |||
111 | max_fav_threads = settings.get_int(SECTION_VIEW, 'MaxFavoriteThreads') |
|
109 | self.del_fav_thread(opening_post) | |
112 | if (str(opening_post.id) in threads) or (len(threads) < max_fav_threads): |
|
110 | ||
113 |
|
|
111 | last_post_id = opening_post.get_thread().get_replies().last().id | |
114 | # Don't check for new posts if the thread is archived already |
|
112 | last_post_ids.append(last_post_id) | |
115 | if thread.is_archived(): |
|
113 | ||
116 | last_id = FAV_THREAD_NO_UPDATES |
|
114 | self.set_setting(SETTING_LAST_POSTS, last_post_ids) | |
117 | else: |
|
|||
118 | last_id = thread.get_replies().last().id |
|
|||
119 | threads[str(opening_post.id)] = last_id |
|
|||
120 | self.set_setting(SETTING_FAVORITE_THREADS, threads) |
|
|||
121 |
|
115 | |||
122 | def del_fav_thread(self, opening_post): |
|
116 | def del_fav_thread(self, opening_post): | |
123 | threads = self.get_fav_threads() |
|
117 | last_posts_ids = self.get_setting(SETTING_LAST_POSTS) | |
124 | if self.thread_is_fav(opening_post): |
|
118 | ||
125 | del threads[str(opening_post.id)] |
|
119 | for post in self.get_last_posts(): | |
126 | self.set_setting(SETTING_FAVORITE_THREADS, threads) |
|
120 | if post.get_thread() == opening_post.get_thread(): | |
|
121 | last_posts_ids.remove(post.id) | |||
|
122 | ||||
|
123 | self.set_setting(SETTING_LAST_POSTS, last_posts_ids) | |||
127 |
|
124 | |||
128 | def thread_is_fav(self, opening_post): |
|
125 | def thread_is_fav(self, opening_post): | |
129 |
r |
|
126 | for post in self.get_last_posts(): | |
|
127 | if post.get_thread() == opening_post.get_thread(): | |||
|
128 | return True | |||
|
129 | return False | |||
130 |
|
130 | |||
131 | def get_notification_usernames(self): |
|
131 | def get_notification_usernames(self): | |
132 | names = set() |
|
132 | names = set() | |
@@ -178,6 +178,10 b' class SettingsManager:' | |||||
178 | hidden_tag_names = self.get_setting(SETTING_HIDDEN_TAGS) |
|
178 | hidden_tag_names = self.get_setting(SETTING_HIDDEN_TAGS) | |
179 | return hidden_tag_names is not None and tag.get_name() in hidden_tag_names |
|
179 | return hidden_tag_names is not None and tag.get_name() in hidden_tag_names | |
180 |
|
180 | |||
|
181 | def get_last_posts(self): | |||
|
182 | post_ids = self.get_setting(SETTING_LAST_POSTS) or [] | |||
|
183 | return [boards.models.Post.objects.get(id=post_id) for post_id in post_ids] | |||
|
184 | ||||
181 |
|
185 | |||
182 | class SessionSettingsManager(SettingsManager): |
|
186 | class SessionSettingsManager(SettingsManager): | |
183 | """ |
|
187 | """ | |
@@ -202,6 +206,8 b' class SessionSettingsManager(SettingsMan' | |||||
202 | class DatabaseSettingsManager(SessionSettingsManager): |
|
206 | class DatabaseSettingsManager(SessionSettingsManager): | |
203 | def __init__(self, session): |
|
207 | def __init__(self, session): | |
204 | super().__init__(session) |
|
208 | super().__init__(session) | |
|
209 | if not session.session_key: | |||
|
210 | session.save() | |||
205 | self.settings, created = UserSettings.objects.get_or_create(session_key=session.session_key) |
|
211 | self.settings, created = UserSettings.objects.get_or_create(session_key=session.session_key) | |
206 |
|
212 | |||
207 | def add_fav_tag(self, tag): |
|
213 | def add_fav_tag(self, tag): |
@@ -39,14 +39,10 b' def get_notifications(context, settings_' | |||||
39 |
|
39 | |||
40 |
|
40 | |||
41 | def get_new_post_count(context, settings_manager): |
|
41 | def get_new_post_count(context, settings_manager): | |
42 |
|
|
42 | last_posts = settings_manager.get_last_posts() | |
43 | if fav_threads: |
|
43 | count = Thread.objects.get_new_post_count(last_posts) | |
44 | fav_thread_ops = Post.objects.filter(id__in=fav_threads.keys()) \ |
|
44 | if count > 0: | |
45 | .order_by('-pub_time').only('thread_id', 'pub_time') |
|
45 | context[CONTEXT_NEW_POST_COUNT] = '(+{})'.format(count) | |
46 | ops = [{'op': op, 'last_id': fav_threads[str(op.id)]} for op in fav_thread_ops] |
|
|||
47 | count = Thread.objects.get_new_post_count(ops) |
|
|||
48 | if count > 0: |
|
|||
49 | context[CONTEXT_NEW_POST_COUNT] = '(+{})'.format(count) |
|
|||
50 |
|
46 | |||
51 |
|
47 | |||
52 | def user_and_ui_processor(request): |
|
48 | def user_and_ui_processor(request): | |
@@ -75,7 +71,7 b' def user_and_ui_processor(request):' | |||||
75 | default=settings.get(SECTION_VIEW, 'DefaultImageViewer')) |
|
71 | default=settings.get(SECTION_VIEW, 'DefaultImageViewer')) | |
76 |
|
72 | |||
77 | context[CONTEXT_HAS_FAV_THREADS] =\ |
|
73 | context[CONTEXT_HAS_FAV_THREADS] =\ | |
78 |
len(settings_manager.get_ |
|
74 | len(settings_manager.get_last_posts()) > 0 | |
79 |
|
75 | |||
80 | context[CONTEXT_BANNERS] = Banner.objects.order_by('-id') |
|
76 | context[CONTEXT_BANNERS] = Banner.objects.order_by('-id') | |
81 | context[CONTEXT_ONLY_FAVORITES] = settings_manager.get_setting( |
|
77 | context[CONTEXT_ONLY_FAVORITES] = settings_manager.get_setting( |
@@ -60,13 +60,11 b' class ThreadManager(models.Manager):' | |||||
60 | thread.update_posts_time() |
|
60 | thread.update_posts_time() | |
61 | thread.save(update_fields=['last_edit_time', 'status']) |
|
61 | thread.save(update_fields=['last_edit_time', 'status']) | |
62 |
|
62 | |||
63 |
def get_new_posts(self, |
|
63 | def get_new_posts(self, last_posts): | |
64 | query = None |
|
64 | query = None | |
65 | # TODO Use classes instead of dicts |
|
65 | for post in last_posts: | |
66 | for data in datas: |
|
66 | if not post.get_thread().is_archived(): | |
67 | if data['last_id'] != FAV_THREAD_NO_UPDATES: |
|
67 | q = Q(id=post.thread_id) & Q(replies__id__gt=post.id) | |
68 | q = (Q(id=data['op'].get_thread_id()) |
|
|||
69 | & Q(replies__id__gt=data['last_id'])) |
|
|||
70 | if query is None: |
|
68 | if query is None: | |
71 | query = q |
|
69 | query = q | |
72 | else: |
|
70 | else: | |
@@ -75,8 +73,8 b' class ThreadManager(models.Manager):' | |||||
75 | return self.filter(query).annotate( |
|
73 | return self.filter(query).annotate( | |
76 | new_post_count=Count('replies')) |
|
74 | new_post_count=Count('replies')) | |
77 |
|
75 | |||
78 |
def get_new_post_count(self, |
|
76 | def get_new_post_count(self, last_posts): | |
79 |
new_posts = self.get_new_posts( |
|
77 | new_posts = self.get_new_posts(last_posts) | |
80 | return new_posts.aggregate(total_count=Count('replies'))\ |
|
78 | return new_posts.aggregate(total_count=Count('replies'))\ | |
81 | ['total_count'] if new_posts else 0 |
|
79 | ['total_count'] if new_posts else 0 | |
82 |
|
80 |
@@ -290,27 +290,25 b' def api_get_new_posts(request):' | |||||
290 | include_posts = 'include_posts' in request.GET |
|
290 | include_posts = 'include_posts' in request.GET | |
291 |
|
291 | |||
292 | settings_manager = get_settings_manager(request) |
|
292 | settings_manager = get_settings_manager(request) | |
293 | fav_threads = settings_manager.get_fav_threads() |
|
|||
294 | fav_thread_ops = Post.objects.filter(id__in=fav_threads.keys())\ |
|
|||
295 | .order_by('-pub_time').prefetch_related('thread') |
|
|||
296 |
|
293 | |||
297 | ops = [{'op': op, 'last_id': fav_threads[str(op.id)]} for op in fav_thread_ops] |
|
294 | last_posts = settings_manager.get_last_posts() | |
298 | if include_posts: |
|
295 | if include_posts: | |
299 |
new_post_threads = Thread.objects.get_new_posts( |
|
296 | new_post_threads = Thread.objects.get_new_posts(last_posts) | |
300 | if new_post_threads: |
|
297 | if new_post_threads: | |
301 | thread_ids = {thread.id: thread for thread in new_post_threads} |
|
298 | thread_ids = {thread.id: thread for thread in new_post_threads} | |
302 | else: |
|
299 | else: | |
303 | thread_ids = dict() |
|
300 | thread_ids = dict() | |
304 |
|
301 | |||
305 |
for |
|
302 | for post in last_posts: | |
306 | fav_thread_dict = dict() |
|
303 | fav_thread_dict = dict() | |
307 |
|
304 | |||
308 |
|
|
305 | thread = post.get_thread() | |
309 | if op_thread.id in thread_ids: |
|
306 | op = thread.get_opening_post() | |
310 |
|
|
307 | if thread.id in thread_ids: | |
|
308 | thread = thread_ids[thread.id] | |||
311 | new_post_count = thread.new_post_count |
|
309 | new_post_count = thread.new_post_count | |
312 | fav_thread_dict['newest_post_link'] = thread.get_replies()\ |
|
310 | fav_thread_dict['newest_post_link'] = thread.get_replies()\ | |
313 |
.filter(id__gt= |
|
311 | .filter(id__gt=post.id)\ | |
314 | .first().get_absolute_url(thread=thread) |
|
312 | .first().get_absolute_url(thread=thread) | |
315 | else: |
|
313 | else: | |
316 | new_post_count = 0 |
|
314 | new_post_count = 0 | |
@@ -325,7 +323,7 b' def api_get_new_posts(request):' | |||||
325 | else: |
|
323 | else: | |
326 | fav_thread_dict = dict() |
|
324 | fav_thread_dict = dict() | |
327 | fav_thread_dict['new_post_count'] = \ |
|
325 | fav_thread_dict['new_post_count'] = \ | |
328 |
Thread.objects.get_new_post_count( |
|
326 | Thread.objects.get_new_post_count(last_posts) | |
329 | posts.append(fav_thread_dict) |
|
327 | posts.append(fav_thread_dict) | |
330 |
|
328 | |||
331 | return HttpResponse(content=json.dumps(posts)) |
|
329 | return HttpResponse(content=json.dumps(posts)) |
@@ -53,9 +53,9 b' class FavoritesFilter(FeedFilter):' | |||||
53 | favorites = 'favorites' in request.GET |
|
53 | favorites = 'favorites' in request.GET | |
54 | if favorites: |
|
54 | if favorites: | |
55 | settings_manager = get_settings_manager(request) |
|
55 | settings_manager = get_settings_manager(request) | |
56 | fav_thread_ops = Post.objects.filter(id__in=settings_manager.get_fav_threads().keys()) |
|
56 | last_posts = settings_manager.get_last_posts() | |
57 |
|
|
57 | threads = [post.get_thread() for post in last_posts] | |
58 |
filtered_posts = filtered_posts.filter(thread__in= |
|
58 | filtered_posts = filtered_posts.filter(thread__in=threads) | |
59 | return filtered_posts |
|
59 | return filtered_posts | |
60 |
|
60 | |||
61 |
|
61 |
General Comments 0
You need to be logged in to leave comments.
Login now