Rig up chat to be suitable for a Doge election. (Hopefully.) (#692)

* Integrate chat from upstream

Substantially borrowed from upstream ref: 13a208ee88e55 (before they
started editing generated artefacts instead of source).

Integrated, including:
  - Remove previously removed features: emoji, hats, and name colors
  - Compensate for lack of unified root template
  - Add React build process to Dockerfile and `bootstrap/init.sh`
  - Preliminary integration of chat websocket workers

For testing, modify `supervisord.conf.dev` to put chat on port 80 and
the site service on some other port. Then visit: http://localhost/chat

Still to do:
  - Access control for specific small-groups (and admins probably):
    Set the values somewhere (site_settings.json? Redis?) and use for
    authorization in `chat_is_allowed`.
  - Proxying only /chat to the websocket workers
  - Chat persistance across restarts: either Redis devops or to DB

* Add nginx server to do appropriate redirection.

* Add necessary columns to User.

* Wire up chat permissions.

* Reload chat on source change.

* Add a better structure for slash commands and add/remove functionality.

* Stop putting up previews of slash commands.

* We require more whitespace.

* Strip DMs out entirely, I currently do not want to deal with them.

* Change "Users Online" to just "Users".

* Clean up a little more DM detritus.

* Save chat history in database.

* Remove unnecessary hefty query to the DB.

* Clean up optimistic messages.

* Initial implementation of notification icon.

* Update readme a little bit.

* Fix notification highlight (mostly).

* Remove chat version number that will never be updated.

* Fix: Errors on logged-out users.

* Add function to nuke the chat state.

* Update DB.

* Add a dedicated deployable docker image.

* Fix: init_build.sh execute bit not set.

* Whoops, screwed up the abort() call.

* Relax chat rate limiter.

* Remove a somewhat silly comment.

* Remove an unnecessary g.db.add().

---------

Co-authored-by: TLSM <duolsm@outlook.com>
This commit is contained in:
Ben Rog-Wilhelm 2023-09-04 12:42:20 -05:00 committed by GitHub
parent 7032d0680d
commit 310c6c4424
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
62 changed files with 3018 additions and 435 deletions

View file

@ -12,6 +12,7 @@ from files.classes.alts import Alt
from files.classes.award import AwardRelationship
from files.classes.badges import Badge
from files.classes.base import CreatedBase
from files.classes.chat_message import ChatMessage
from files.classes.clients import * # note: imports Comment and Submission
from files.classes.follows import Follow
from files.classes.mod_logs import ModAction
@ -112,6 +113,9 @@ class User(CreatedBase):
volunteer_last_started_utc = Column(DateTime, nullable=True)
volunteer_janitor_correctness = Column(Float, default=0, nullable=False)
chat_authorized = Column(Boolean, default=False, nullable=False)
chat_lastseen = Column(DateTime(timezone=True), default=datetime(1970, 1, 1), nullable=False)
Index(
'users_original_username_trgm_idx',
original_username,
@ -136,6 +140,8 @@ class User(CreatedBase):
Index('users_subs_idx', stored_subscriber_count)
Index('users_unbanutc_idx', unban_utc.desc())
Index('chat_auth_index', chat_authorized)
badges = relationship("Badge", viewonly=True)
subscriptions = relationship("Subscription", viewonly=True)
following = relationship("Follow", primaryjoin="Follow.user_id==User.id", viewonly=True)
@ -157,6 +163,30 @@ class User(CreatedBase):
def can_manage_reports(self):
return self.admin_level > 1
@property
@lazy
def can_access_chat(self):
if self.is_suspended_permanently:
return False
if self.admin_level >= PERMS['CHAT_FULL_CONTROL']:
return True
if self.chat_authorized:
return True
return False
@property
@lazy
def unread_chat_messages_count(self):
if not self.can_access_chat:
return 0
# Query for all chat messages that are newer than the user's last seen timestamp
unread_messages_count = g.db.query(ChatMessage)\
.filter(ChatMessage.created_datetimez > self.chat_lastseen)\
.count()
return unread_messages_count
@property
def age_days(self):
return (datetime.now() - datetime.fromtimestamp(self.created_utc)).days