-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from zodman/chat_demo
Chat demo ...
- Loading branch information
Showing
16 changed files
with
210 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,3 +154,4 @@ dmypy.json | |
node_modules | ||
/static | ||
/core/static/dist | ||
.env/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* zodman | ||
* jonathan-s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Rails from '@rails/ujs' | ||
import { debounce } from 'lodash-es' | ||
import ApplicationController from './application_controller' | ||
|
||
let lastMessageId | ||
const reload = controller => { | ||
controller.stimulate('ChatReflex#reload') | ||
} | ||
const debouncedReload = debounce(reload, 100) | ||
|
||
export default class extends ApplicationController { | ||
static get targets () { | ||
return ['list', 'input'] | ||
} | ||
|
||
connect () { | ||
super.connect() | ||
this.scroll(100) | ||
} | ||
|
||
post (event) { | ||
console.log("post"); | ||
Rails.stopEverything(event) | ||
lastMessageId = Math.random() | ||
this.stimulate( | ||
'ChatReflex#post', | ||
this.element.dataset.color, | ||
this.inputTarget.value, | ||
lastMessageId | ||
) | ||
} | ||
|
||
afterPost () { | ||
this.inputTarget.value = '' | ||
this.inputTarget.focus() | ||
this.scroll(1) | ||
} | ||
|
||
scroll (delay = 10) { | ||
const lists = document.querySelectorAll('[data-target="chat.list"]') | ||
setTimeout(() => { | ||
lists.forEach(e => (e.scrollTop = e.scrollHeight)) | ||
}, delay) | ||
} | ||
|
||
reload (event) { | ||
const { messageId } = event.detail | ||
if (messageId === lastMessageId) return | ||
debouncedReload(this) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
import { Application } from 'stimulus' | ||
import StimulusReflex from 'stimulus_reflex' | ||
import WebsocketConsumer from 'sockpuppet-js' | ||
|
||
import CableReady from 'cable_ready' | ||
import debounced from 'debounced' | ||
import BookSearchController from './controllers/book_search_controller' | ||
import ExampleController from './controllers/example_controller' | ||
import ChatController from './controllers/chat_controller' | ||
|
||
debounced.initialize() | ||
//import TurboLinks from 'turbolinks' | ||
import TurboLinks from 'turbolinks' | ||
|
||
//TurboLinks.start() | ||
TurboLinks.start() | ||
|
||
const application = Application.start() | ||
const ssl = location.protocol !== 'https:' ? '' : 's'; | ||
const consumer = new WebsocketConsumer(`ws${ssl}://${location.hostname}:${location.port}/ws/sockpuppet-sync`) | ||
|
||
consumer.subscriptions.create('ChatChannel', { | ||
received (data) { | ||
if (data.cableReady) CableReady.perform(data.operations) | ||
} | ||
}) | ||
|
||
application.register("example", ExampleController) | ||
application.register("book-search", BookSearchController) | ||
application.register("chat", ChatController) | ||
StimulusReflex.initialize(application, { consumer, debug: true }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from sockpuppet.reflex import Reflex | ||
from sockpuppet.channel import Channel | ||
from django.core.cache import cache | ||
from django.utils import timezone | ||
|
||
|
||
class ChatReflex(Reflex): | ||
def post(self, color, message, message_id): | ||
chats = cache.get("chats", []) | ||
chats.append({ | ||
'message': message, | ||
'message_id': message_id, | ||
'created_at': timezone.now() | ||
}) | ||
cache.set("chats", chats) | ||
channel = Channel("chat") | ||
channel.dispatch_event({ | ||
'name': 'chats:added', | ||
'detail': {'messagre_id': message_id} | ||
}) | ||
channel.broadcast() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
<article id="red" | ||
data-controller="chat" | ||
data-action="chats:added@document->chat#reload cable-ready:after-morph@document->chat#scroll"> | ||
<span data-target="chat.list"> | ||
{% for chat in chats %} | ||
<aside class="message" style="min-height:auto; margin-top: 15px;"> | ||
<p> | ||
{{chat.message}} <sup>{{chat.created_at|timesince}} ago </sup> | ||
</p> | ||
</aside> | ||
{% endfor %} | ||
</span> | ||
<form> | ||
<textarea data-target="chat.input" placeholder="Type your message.."></textarea> | ||
<button data-action="click->chat#post" type="submit"> Send </button> | ||
<div> | ||
<small>Message storage on <a href="https://docs.djangoproject.com/en/3.1/topics/cache/#local-memory-caching">LocMemCache</a></small> | ||
</div> | ||
</form> | ||
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block main %} | ||
<p> | ||
It is an exciting new way to build reactive, modern real-time apps. | ||
It eliminates the hassle of maintaining state on the client. | ||
It's a new way of thinking... and it works with technologies that Django developers already use, like server rendered HTML, Russian Doll caching, Stimulus and Turbolinks. | ||
<br> | ||
The demos on this site are an attempt to teach others how to build reactive applications with django-sockpuppet. | ||
</p> | ||
{% endblock main %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,9 @@ | ||
from django.views.generic.base import TemplateView | ||
from .mixins import MixinBase | ||
from .mixins import BookSearchMixin | ||
|
||
|
||
class BookSearch(MixinBase, TemplateView): | ||
class BookSearch(BookSearchMixin, TemplateView): | ||
demo_template = "_book_search_demo.html" | ||
subtitle = 'Search Book' | ||
files = ( | ||
('core/reflexes/book_search_reflex.py', 'python', 'python3'), | ||
('core/views/book_search.py', 'python', 'python3'), | ||
('core/javascript/controllers/book_search_controller.js', 'javascript', 'javascript'), | ||
('core/templates/_book_search_demo.html', 'html', 'htmldjango'), | ||
) | ||
|
||
book_search = BookSearch.as_view() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.views.generic.base import TemplateView | ||
from django.core.cache import cache | ||
from .mixins import ChatMixin | ||
|
||
class ChatView(ChatMixin, TemplateView): | ||
demo_template = '_chat_demo.html' | ||
subtitle = 'Chat' | ||
|
||
def get_context_data(self, *args, **kwargs): | ||
context = super().get_context_data(*args, **kwargs) | ||
context['chats'] = cache.get("chats", []) | ||
return context | ||
|
||
chat = ChatView.as_view() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters