forked from nolan/titlebot-ng-ng
Voting was easier than I thought
This commit is contained in:
+1
-1
@@ -32,7 +32,7 @@ async def main():
|
||||
# Then read and post
|
||||
room.read_db(since_timestamp=last_post, only_unposted=True)
|
||||
if room.posts:
|
||||
await room.post_suggestions()
|
||||
await room.post_suggestions_individually()
|
||||
room.update_last_post()
|
||||
else:
|
||||
await room.alert_post("No new unposted suggestions found.")
|
||||
|
||||
@@ -9,6 +9,7 @@ import datetime
|
||||
import yaml
|
||||
import sqlite3
|
||||
import re
|
||||
import asyncio
|
||||
from nio import AsyncClient, LoginResponse, RoomMessageText, SyncResponse, RoomSendResponse
|
||||
from post import Post
|
||||
|
||||
@@ -407,6 +408,62 @@ class Room:
|
||||
# Restore original posts list
|
||||
self.posts = original_posts
|
||||
|
||||
async def post_suggestions_individually(self):
|
||||
"""Post each unposted suggestion as its own message for reaction voting."""
|
||||
unposted_posts = [post for post in self.posts if not post.is_posted]
|
||||
if not unposted_posts:
|
||||
await self.alert_post("No new unposted suggestions found.")
|
||||
return
|
||||
|
||||
await self.alert_post("Time to vote! Since different clients don't have polls, each suggestion is posted individually so you can upvote with 👍 reactions.")
|
||||
|
||||
for idx, post in enumerate(unposted_posts, start=1):
|
||||
clean_msg = " ".join(post.content.strip().splitlines())
|
||||
user = post.display_name
|
||||
if len(user) > 20:
|
||||
user = user[:20] + "..."
|
||||
body = f"Suggestion {idx}: {user}: {clean_msg}"
|
||||
content = {
|
||||
"msgtype": "m.text",
|
||||
"body": body,
|
||||
}
|
||||
resp = await self.client.room_send(
|
||||
self.config.room_id,
|
||||
message_type="m.room.message",
|
||||
content=content,
|
||||
)
|
||||
if isinstance(resp, RoomSendResponse):
|
||||
print(f"Posted suggestion {idx}")
|
||||
post.is_posted = True
|
||||
self.mark_posts_as_posted([post])
|
||||
|
||||
# Seed an initial 👍 reaction so users can click to upvote
|
||||
try:
|
||||
event_id = getattr(resp, "event_id", None)
|
||||
if event_id:
|
||||
reaction = {
|
||||
"m.relates_to": {
|
||||
"rel_type": "m.annotation",
|
||||
"event_id": event_id,
|
||||
"key": "👍",
|
||||
}
|
||||
}
|
||||
react_resp = await self.client.room_send(
|
||||
self.config.room_id,
|
||||
message_type="m.reaction",
|
||||
content=reaction,
|
||||
)
|
||||
if isinstance(react_resp, RoomSendResponse):
|
||||
print(f"Seeded upvote for suggestion {idx}")
|
||||
else:
|
||||
print(f"Failed to seed upvote for suggestion {idx}: {react_resp}")
|
||||
except Exception as e:
|
||||
print(f"Error sending upvote reaction: {e}")
|
||||
else:
|
||||
print(f"Failed to post suggestion {idx}: {resp}")
|
||||
# small delay to avoid rate limiting
|
||||
await asyncio.sleep(0.15)
|
||||
|
||||
def mark_posts_as_posted(self, posts=None):
|
||||
"""Mark posts as posted in the database"""
|
||||
if posts is None:
|
||||
|
||||
Reference in New Issue
Block a user