Skip to content
This repository has been archived by the owner on May 31, 2023. It is now read-only.

自分の投稿したレビューを一覧で確認する機能を追加 #432

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lambda/app/bee_slack_app/view/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def home( # pylint: disable=too-many-locals
post_review_action_id: str,
list_user_posted_review_action_id: str,
user_info_action_id: str,
list_posted_review_action_id: str,
total_review_count: int,
user_name: str,
recommend_timestamp: str,
Expand All @@ -30,6 +31,7 @@ def home( # pylint: disable=too-many-locals
post_review_action_id: 「レビューを投稿する」ボタンのaction_id
list_user_posted_review_action_id: 「レビューを投稿したユーザ」ボタンのaction_id
user_info_action_id: 「プロフィール」ボタンのaction_id
list_posted_review_action_id: 「投稿したレビュー」ボタンのaction_id
total_review_count: 表示する「レビュー投稿数」
user_name:表示する「ユーザ名」
recommend_timestamp:MLのjsonファイルを作成した日時
Expand Down Expand Up @@ -172,6 +174,16 @@ def home( # pylint: disable=too-many-locals
"value": "dummy_value",
"action_id": user_info_action_id,
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "投稿したレビュー",
"emoji": True,
},
"value": "dummy_value",
"action_id": list_posted_review_action_id,
},
],
},
{"type": "divider"},
Expand Down
14 changes: 10 additions & 4 deletions lambda/app/bee_slack_app/view/read_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ def review_modal(*, callback_id: str, book: BookOfReview, reviews: list[Review])


def review_of_user_modal(
*, callback_id: str, reviews_param: ReviewPagination, private_metadata: str
*,
callback_id: str,
reviews_param: ReviewPagination,
private_metadata: str,
review_move_to_back_action: str,
review_move_to_next_action: str,
):
"""
ユーザのレビューモーダル
Expand All @@ -79,7 +84,8 @@ def review_of_user_modal(
callback_id: モーダルのcallback_id
reviews_param: 「投稿されているレビュー」のデータ
private_metadata: private_metadata

review_move_to_back_action: 「前へ」ボタンのaction_id
review_move_to_next_action: 「次へ」ボタンのaction_id
"""

view = {
Expand Down Expand Up @@ -134,7 +140,7 @@ def review_of_user_modal(
{ # type: ignore
"type": "button",
"text": {"type": "plain_text", "text": "前へ"},
"action_id": "review_move_to_back_action",
"action_id": review_move_to_back_action,
}
]

Expand All @@ -143,7 +149,7 @@ def review_of_user_modal(
{
"type": "button",
"text": {"type": "plain_text", "text": "次へ"},
"action_id": "review_move_to_next_action",
"action_id": review_move_to_next_action,
}
]

Expand Down
4 changes: 4 additions & 0 deletions lambda/app/bee_slack_app/view_controller/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def update_home_view(ack, event, client):
recommended_books=recommended_result["recommended_books"],
list_user_posted_review_action_id="list_user_posted_review_action",
user_info_action_id="user_info_action",
list_posted_review_action_id="list_posted_review_action",
total_review_count=total_review_count,
user_name=user_name,
recommend_timestamp=parse(recommended_result["created_at"]),
Expand Down Expand Up @@ -126,6 +127,7 @@ def home_move_to_next_action(ack, client, body): # pylint: disable=too-many-loc
recommended_books=recommended_result["recommended_books"],
list_user_posted_review_action_id="list_user_posted_review_action",
user_info_action_id="user_info_action",
list_posted_review_action_id="list_posted_review_action",
total_review_count=total_review_count,
user_name=user_name,
recommend_timestamp=parse(recommended_result["created_at"]),
Expand Down Expand Up @@ -187,6 +189,7 @@ def home_move_to_back_action(ack, client, body): # pylint: disable=too-many-loc
recommended_books=recommended_result["recommended_books"],
list_user_posted_review_action_id="list_user_posted_review_action",
user_info_action_id="user_info_action",
list_posted_review_action_id="list_posted_review_action",
total_review_count=total_review_count,
user_name=user_name,
recommend_timestamp=parse(recommended_result["created_at"]),
Expand Down Expand Up @@ -269,6 +272,7 @@ def handle_update_button_action(
recommended_books=recommended_books,
list_user_posted_review_action_id="list_user_posted_review_action",
user_info_action_id="user_info_action",
list_posted_review_action_id="list_posted_review_action",
total_review_count=total_review_count,
user_name=user_name,
recommend_timestamp=parse(recommended_result["created_at"]),
Expand Down
183 changes: 181 additions & 2 deletions lambda/app/bee_slack_app/view_controller/review.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import os
from typing import Any, TypedDict
from typing import Any, Optional, TypedDict

from slack_bolt import App

Expand Down Expand Up @@ -119,6 +119,8 @@ def read_review_of_user_action(ack, body, client, action):
callback_id="review_of_user_modal",
reviews_param=reviews_param,
private_metadata=metadata_str,
review_move_to_back_action="review_move_to_back_action",
review_move_to_next_action="review_move_to_next_action",
),
)

Expand Down Expand Up @@ -178,6 +180,8 @@ def review_move_to_next_action(
callback_id="review_of_user_modal",
reviews_param=reviews_param,
private_metadata=metadata_str,
review_move_to_back_action="review_move_to_back_action",
review_move_to_next_action="review_move_to_next_action",
),
)

Expand Down Expand Up @@ -237,6 +241,8 @@ def review_move_to_back_action(
callback_id="review_of_user_modal",
reviews_param=reviews_param,
private_metadata=metadata_str,
review_move_to_back_action="review_move_to_back_action",
review_move_to_next_action="review_move_to_next_action",
),
)

Expand Down Expand Up @@ -405,6 +411,177 @@ def open_review_detail_modal(ack, body, client, action):
trigger_id=body["trigger_id"], view=review_detail_modal(review)
)

@app.action("list_posted_review_action")
def list_posted_review_action(ack, body, client):
"""
投稿したレビューを一覧で表示する
"""

ack()
user_id = body["user"]["id"]

review_items = review_service.get_next_reviews_by_user_id(
user_id=user_id, limit=BOOK_NUMBER_PER_PAGE, keys=[]
)

reviews = []

if review_items:
reviews = review_items["items"]
reviews = _make_review_contents_list_comment_short(reviews)

reviews_param: ReviewPagination = {
"reviews": reviews,
"show_move_to_back": False,
"show_move_to_next": review_items["has_next"],
}
# メタデータに変換する
metadata_str = _PrivateMetadataConvertor.to_private_metadata(
keys=review_items["keys"],
)

user_action_service.record_user_action(
user_id=user_id,
action_name="list_posted_review_action",
payload={"reviews": reviews},
)

if not reviews:
client.views_open(
trigger_id=body["trigger_id"],
view=simple_modal(title="エラー", text="レビュー取得でエラーが発生しました"),
)
return

client.views_open(
trigger_id=body["trigger_id"],
view=review_of_user_modal(
callback_id="review_of_user_modal",
reviews_param=reviews_param,
private_metadata=metadata_str,
review_move_to_back_action="list_posted_review_view_move_to_back_action",
review_move_to_next_action="list_posted_review_view_move_to_next_action",
),
)

@app.action("list_posted_review_view_move_to_next_action")
def list_posted_review_view_move_to_next_action(
ack, client, body
): # pylint: disable=too-many-locals
"""
レビューリストで「次へ」を押下されたときの処理
"""
ack()

user_id = body["user"]["id"]

# メタデータから取り出す
private_metadata = body["view"]["private_metadata"]
metadata_dict = _PrivateMetadataConvertor.to_dict(
private_metadata=private_metadata
)

reviews_param = None
metadata_str = ""

review_items = review_service.get_next_reviews_by_user_id(
user_id=user_id,
limit=BOOK_NUMBER_PER_PAGE,
keys=metadata_dict["keys"],
)

reviews = []

if review_items:
reviews = review_items["items"]
reviews = _make_review_contents_list_comment_short(reviews)

reviews_param: ReviewPagination = {
"reviews": review_items["items"],
"show_move_to_back": True,
"show_move_to_next": review_items["has_next"],
}
# メタデータに変換する
metadata_str = _PrivateMetadataConvertor.to_private_metadata(
keys=review_items["keys"],
)

user_action_service.record_user_action(
user_id=user_id,
action_name="list_posted_review_view_move_to_next_action",
payload={"reviews": reviews},
)

client.views_update(
view_id=body["view"]["id"],
view=review_of_user_modal(
callback_id="review_of_user_modal",
reviews_param=reviews_param,
private_metadata=metadata_str,
review_move_to_back_action="list_posted_review_view_move_to_back_action",
review_move_to_next_action="list_posted_review_view_move_to_next_action",
),
)

@app.action("list_posted_review_view_move_to_back_action")
def list_posted_review_view_move_to_back_action(
ack, client, body
): # pylint: disable=too-many-locals
"""
レビューリストで「前へ」を押下されたときの処理
"""
ack()

user_id = body["user"]["id"]

# メタデータから取り出す
private_metadata = body["view"]["private_metadata"]
metadata_dict = _PrivateMetadataConvertor.to_dict(
private_metadata=private_metadata
)

reviews_param = None
metadata_str = ""

review_items = review_service.get_before_reviews_by_user_id(
user_id=user_id,
limit=BOOK_NUMBER_PER_PAGE,
keys=metadata_dict["keys"],
)

reviews = []

if review_items:
reviews = review_items["items"]
reviews = _make_review_contents_list_comment_short(reviews)

reviews_param: ReviewPagination = {
"reviews": review_items["items"],
"show_move_to_back": not review_items["is_move_to_first"],
"show_move_to_next": True,
}
# メタデータに変換する
metadata_str = _PrivateMetadataConvertor.to_private_metadata(
keys=review_items["keys"]
)

user_action_service.record_user_action(
user_id=user_id,
action_name="read_review_of_user_action",
payload={"reviews": reviews},
)

client.views_update(
view_id=body["view"]["id"],
view=review_of_user_modal(
callback_id="review_of_user_modal",
reviews_param=reviews_param,
private_metadata=metadata_str,
review_move_to_back_action="list_posted_review_view_move_to_back_action",
review_move_to_next_action="list_posted_review_view_move_to_next_action",
),
)


def _make_review_contents_list_comment_short(
review_contents_list: list[Review],
Expand Down Expand Up @@ -435,7 +612,9 @@ class _MetadataDict(TypedDict):

@staticmethod
# メタデータへの変換
def to_private_metadata(*, keys: Any, user_id_of_review: str) -> str:
def to_private_metadata(
*, keys: Any, user_id_of_review: Optional[str] = None
) -> str:
return json.dumps({"keys": keys, "user_id_of_review": user_id_of_review})

@staticmethod
Expand Down
30 changes: 27 additions & 3 deletions lambda/app/bee_slack_app/view_controller/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

from slack_bolt import App

from bee_slack_app.model import User
from bee_slack_app.service import user_action_service, user_service
from bee_slack_app.view import user_profile_modal
from bee_slack_app.model import Review, ReviewPagination, User
from bee_slack_app.service import review_service, user_action_service, user_service
from bee_slack_app.view import review_of_user_modal, simple_modal, user_profile_modal

BOOK_NUMBER_PER_PAGE = 20


def user_controller(app: App) -> None:
Expand Down Expand Up @@ -95,6 +97,28 @@ def handle_submission(ack, body, _, view):
)


def _make_review_contents_list_comment_short(
review_contents_list: list[Review],
) -> list[Review]:
"""
レビューのコメントを、一覧表示用に短くする
"""
comment_len = 20

for review_contents in review_contents_list:
review_comment = review_contents["review_comment"]
if review_comment:
review_contents["review_comment"] = (
review_comment[0:comment_len] + "..."
if len(review_comment) > comment_len
else review_comment
)
else:
review_contents["review_comment"] = "-"

return review_contents_list


class _PrivateMetadataConvertor:
class _MetadataDict(TypedDict):
post_review_count: str
Expand Down