Skip to content

Commit

Permalink
[update]
Browse files Browse the repository at this point in the history
  • Loading branch information
boke0 committed Mar 8, 2021
1 parent 4841e59 commit 26a3d94
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
54 changes: 42 additions & 12 deletions kyokusui/controller.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
from mitama.app import Controller
from mitama.app.http import Response
from mitama.models import User, Node, Role, is_admin
from datetime import datetime
import json
import re
import markdown
import magic

from .model import db, Board, Thread, Res, Permission
from .forms import CreateBoardForm, CreateThreadForm, SettingForm, UpdateThreadForm, UpdateBoardForm
from .forms import (
CreateBoardForm,
CreateThreadForm,
SettingForm,
UpdateThreadForm,
UpdateBoardForm
)
from .utils import hiroyuki


class HomeController(Controller):
def handle(self, request):
template = self.view.get_template("home.html")
Expand All @@ -24,14 +30,17 @@ def handle(self, request):
"threads": threads,
"boards": Board.list_subscribed(request.user)
})

def settings(self, request):
template = self.view.get_template("settings.html")
error = ""
if request.method == "POST":
form = SettingForm(request.post())
for permission_screen_name, permission_roles in form['permissions'].items():
permission = Permission.retrieve(screen_name = permission_screen_name)
permission.roles = [Role.retrieve(screen_name = r) for r in permission_roles]
for screen_name, roles in form['permissions'].items():
permission = Permission.retrieve(screen_name=screen_name)
permission.roles = [
Role.retrieve(screen_name=r) for r in roles
]
permission.update()
error = "保存しました"
return Response.render(template, {
Expand All @@ -40,6 +49,7 @@ def settings(self, request):
"error": error
})


class BoardController(Controller):
def create(self, request):
form = CreateBoardForm(request.post())
Expand Down Expand Up @@ -91,13 +101,15 @@ def unsubscribe(self, request):
}
})


class UserController(Controller):
def icon(self, request):
user = User.retrieve(request.params['user'])
f = magic.Magic(mime=True, uncompress=True)
mime = f.from_buffer(self.icon)
return Response(user._icon, content_type=mime)


class ThreadController(Controller):
def create(self, request):
form = CreateThreadForm(request.post())
Expand All @@ -109,9 +121,12 @@ def create(self, request):
thread.create()
for user in board.subscribers:
user.push({
"title": res.thread.title + " - Kyokusui",
"title": thread.title + " - Kyokusui",
"body": "スレッドが立ちました",
"icon": self.app.convert_fullurl(request, "/user/{}/icon".format(requst.user._id))
"icon": self.app.convert_fullurl(
request,
"/user/{}/icon".format(request.user._id)
)
})
return Response.json({
"_id": thread._id,
Expand All @@ -137,8 +152,10 @@ def update(self, request):
"title": thread.title,
})


class WebSocketController(Controller):
streams = {}

def handle(self, request):
ws = request.websocket
board = Board.retrieve(request.params['board'])
Expand All @@ -162,14 +179,22 @@ def handle(self, request):
res.user = request.user
res.thread = Thread.retrieve(request.params['thread'])
res.create()
mentions = re.findall(r"\>\>([0-9a-zA-Z.-_]+)", data['data']['message'])
mentions = re.findall(
r"\>\>([0-9a-zA-Z.-_]+)",
data['data']['message']
)
for mention in mentions:
try:
user = User.retrieve(screen_name = mention)
user = User.retrieve(
screen_name=mention
)
user.push({
"title": res.thread.title + " - Kyokusui",
"body": res.user.name+"さんがメンションしました",
"icon": self.app.convert_fullurl(request, "/user/{}/icon".format(res.user._id))
"icon": self.app.convert_fullurl(
request,
"/user/{}/icon".format(res.user._id)
)
})
except Exception as err:
print(err, mention)
Expand All @@ -181,15 +206,20 @@ def handle(self, request):
"type": "message",
"_id": res._id,
"data": {
"message": markdown.markdown(hiroyuki(res.parsed_data["message"]), extensions=['fenced_code']),
"message": markdown.markdown(
hiroyuki(res.parsed_data["message"]),
extensions=['fenced_code']
),
"images": res.parsed_data["images"]
},
"user": {
"_id": res.user._id,
"name": res.user.name,
"screen_name": res.user.screen_name
},
"datetime": res.datetime.strftime("%Y-%m-%d %H:%M:%S"),
"datetime": res.datetime.strftime(
"%Y-%m-%d %H:%M:%S"
),
"thread": thread._id,
}))
except Exception as err:
Expand Down
31 changes: 25 additions & 6 deletions kyokusui/model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from mitama.db import BaseDatabase, Table, relationship
from mitama.db.model import UUID
from mitama.db.types import *
from mitama.models import User, Group, Node, permission
from mitama.db.types import Column, String, ForeignKey, DateTime, Boolean, Text
from mitama.models import User, Node, permission
from datetime import datetime
import json

Expand All @@ -12,6 +12,7 @@ class Database(BaseDatabase):

db = Database(prefix="kyokusui")


class Board(db.Model):
name = Column(String(255))
owner_id = Column(String(64), ForeignKey("mitama_node._id"))
Expand All @@ -24,10 +25,15 @@ def list_subscribed(cls, user):
boards_ = cls.list()
boards = list()
for board in boards_:
if board.owner.object == user or (isinstance(board.owner.object, Group) and board.owner.object.is_in(user)) or user in board.subscribers:
if (
board.owner.object == user or
board.owner.is_in(user) or
user in board.subscribers
):
boards.append(board)
return boards


class Thread(db.Model):
board_id = Column(String(64), ForeignKey("kyokusui_board._id"))
board = relationship(Board, backref="threads")
Expand All @@ -41,6 +47,7 @@ def close(self):
self.closed = True
self.update()


class Res(db.Model):
thread_id = Column(String(64), ForeignKey("kyokusui_thread._id"))
thread = relationship(Thread, backref="res")
Expand All @@ -53,20 +60,32 @@ class Res(db.Model):
def parsed_data(self):
return json.loads(self.data)

subscribe_table = Table("kyokusui_subscribe",

subscribe_table = Table(
"kyokusui_subscribe",
db.metadata,
Column("_id", String(64), default=UUID(), primary_key=True),
Column("user_id", String(64), ForeignKey("mitama_user._id", ondelete="CASCADE")),
Column("board_id", String(64), ForeignKey("kyokusui_board._id", ondelete="CASCADE"))
Column(
"user_id",
String(64),
ForeignKey("mitama_user._id", ondelete="CASCADE")
),
Column(
"board_id",
String(64),
ForeignKey("kyokusui_board._id", ondelete="CASCADE")
)
)


class Subscribe(db.Model):
__table__ = subscribe_table
user_id = subscribe_table.c.user_id,
board_id = subscribe_table.c.board_id,
user = relationship(User)
board = relationship(Board)


Permission = permission(db, [
{
"name": "板を立てる",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ include = [

[tool.poetry.dependencies]
python = "^3.6"
mitama = "^4.5.8"
mitama = "^4.5.12"

[tool.poetry.dev-dependencies]
unittest = "^0.0"
Expand Down

0 comments on commit 26a3d94

Please sign in to comment.