summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--migrations/3_fix_lazychat_message_visibility.sql7
-rw-r--r--schema.sql9
-rw-r--r--src/mulk/benki/lazychat.clj40
3 files changed, 41 insertions, 15 deletions
diff --git a/migrations/3_fix_lazychat_message_visibility.sql b/migrations/3_fix_lazychat_message_visibility.sql
new file mode 100644
index 0000000..4af6b30
--- /dev/null
+++ b/migrations/3_fix_lazychat_message_visibility.sql
@@ -0,0 +1,7 @@
+CREATE OR REPLACE VIEW user_visible_lazychat_messages AS
+ SELECT eur.user, t.message
+ FROM effective_user_roles eur, lazychat_targets t
+ WHERE t.target = eur.role
+ UNION
+ SELECT m.author, m.message
+ FROM lazychat_messages m;
diff --git a/schema.sql b/schema.sql
index 8934883..984fdea 100644
--- a/schema.sql
+++ b/schema.sql
@@ -231,9 +231,12 @@ CREATE TRIGGER new_user_create_user_role
EXECUTE PROCEDURE new_user_create_user_role();
CREATE VIEW user_visible_lazychat_messages AS
- SELECT eur.user, t.message
- FROM effective_user_roles eur, lazychat_targets t
- WHERE t.target = eur.role;
+ SELECT eur.user, t.message
+ FROM effective_user_roles eur, lazychat_targets t
+ WHERE t.target = eur.role
+ UNION
+ SELECT m.author, m.message
+ FROM lazychat_messages m;
ROLLBACK;
--COMMIT;
diff --git a/src/mulk/benki/lazychat.clj b/src/mulk/benki/lazychat.clj
index bfee679..5986264 100644
--- a/src/mulk/benki/lazychat.clj
+++ b/src/mulk/benki/lazychat.clj
@@ -36,7 +36,7 @@
(with-dbt
(map :user (query "SELECT \"user\" FROM user_visible_lazychat_messages
WHERE message = ?"
- message-id))))
+ (:id (:id message-id))))))
(defn fill-in-author-details [x]
x)
@@ -52,10 +52,11 @@
(let [id (or id
(:id (query1 "SELECT nextval('lazychat_messages_id_seq')::INTEGER AS id")))]
(sql/with-query-results ids
- ["INSERT INTO lazychat_messages(id, author, content, format, visibility)
- VALUES (?, ?, ?, ?, ?)
+ ["INSERT INTO lazychat_messages(id, author, content, format)
+ VALUES (?, ?, ?, ?)
RETURNING id"
- id user content format visibility]
+ id user content format]
+ (log (fmt nil "~S ~S ~S ~S" id user content format))
(doseq [referee referees]
(sql/insert-values :lazychat_references
[:referrer :referee]
@@ -64,6 +65,19 @@
(sql/insert-values :lazychat_targets
[:message :target]
[id (int target)]))
+ (case visibility
+ ("public")
+ (sql/do-prepared
+ "INSERT INTO lazychat_targets
+ SELECT ?, role FROM role_tags WHERE tag = 'world'"
+ [id])
+ ("protected")
+ (sql/do-prepared
+ "INSERT INTO lazychat_targets
+ SELECT ?, role FROM role_tags WHERE tag = 'inner_circle'"
+ [id])
+ ("private")
+ (do))
(enqueue lafargue-events
(with-meta
(fill-in-author-details
@@ -78,7 +92,7 @@
(create-lazychat-message-by-user! *user* msg))
(defn push-message-to-xmpp [msg]
- (let [targets (filter integer? (determine-targets {:id msg}))]
+ (let [targets (filter integer? (determine-targets (:id msg)))]
(enqueue xmpp/messages {:message msg,
:targets targets})))
@@ -106,11 +120,13 @@
:targets targets))))
(defn may-read? [user message]
- (or (= (:visibility message) "public")
- (and user (= (:visibility message) "protected"))
- (and user
- (= (:visibility message) "personal")
- (contains? (:targets message) user))))
+ (with-dbt
+ (seq
+ (query "SELECT 't' FROM user_visible_lazychat_messages
+ WHERE \"user\" IS NOT DISTINCT FROM ?
+ AND \"message\" = ?"
+ user
+ message))))
(defn may-post? [user]
user)
@@ -204,7 +220,7 @@
(defpage-async "/lafargue/events" {} conn
(if (websocket? conn)
- (let [messages (filter* #(may-read? *user* %) lafargue-events)]
+ (let [messages (filter* #(may-read? *user* (:id %)) lafargue-events)]
(receive-all messages
(fn [msg]
(async-push conn (render-message-as-json msg)))))
@@ -239,7 +255,7 @@
(defpage [:get "/lafargue/messages/:id"] {id :id}
(with-dbt
(let [message (select-message id)]
- (if (may-read? *user* message)
+ (if (may-read? *user* (:id message))
(response/json message)
{:status 403}))))