Commit graph

33 commits

Author SHA1 Message Date
Ankush Menat
2a56869c99 refactor: better permission checking on sockets
- no more "can subscribe X"
- add a new util function on socket `has_permission`

Basic usage:

```js
socket.has_permission(doctype, [name]).then(() => { // do something });
```
2024-04-06 21:14:48 +05:30
Ankush Menat
a313fb90cb fix(DX): no need to specify separate hook for socketio 2024-04-06 18:28:49 +05:30
Ankush Menat
b7f2073380 feat: Extensible SocketIO
You can now specify custom event handlers for SocketIO.

Usage:

1. In your app's hooks.py add `has_realtime_event_handlers=True` so
Framework can assume your app contains custom handler and import them.
2. Create a file called `/app/realtime/handlers.js` with single module
   export a function that will setup handlers using socket.

Here's sample code:

```js
// This is /app_root/realtime/handler.js`

function chat_app_handlers(socket) {
    socket.on("hello_chat") {
	console.log("hello world!");
    }
}

module.exports = chat_app_handlers;
```

3. Restart SocketIO server and see if it worked by sending event from
   client. In desk based app you can do
   `frappe.realtime.socket.emit("hello_chat")`

Middlewares are not yet possible... will be worked upon __some other day__ [tm]

closes https://github.com/frappe/frappe/issues/21528
2024-04-06 18:17:27 +05:30
Akhil Narang
3f1e19de85
refactor(treewide): enable RUF rules
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-02-21 16:20:28 +05:30
Ankush Menat
de9ac89748 style: re-format with ruff 2024-02-05 18:53:33 +05:30
barredterra
a9c4894ccf fix: task_id parameter for publish_progress 2023-12-05 19:16:27 +01:00
Raffael Meyer
b070c06f53
fix: show progress in doc, if given (#22256)
If doc is given, show progress in doc only.
Else, show progress to the current user (in all tabs).
2023-08-31 11:33:48 +05:30
Ankush Menat
d59e499418 refactor!: Use SocketIO namespaces for multitenancy 2023-07-01 10:53:34 +05:30
Ankush Menat
95e49193c8
fix: dont retry if redis not available for realtime (#21517)
* fix: reduce retries for rq connection

10 seconds of retries when connection isn't available is too much, just
   failing might be beneficial.

- BusyLoadingError only occurs when redis is restarting
- ConnectionError mostly means redis is dead, no amount of retries will
  bring it back.

* fix: dont retry if redis is down for realtime
2023-06-28 15:21:04 +05:30
Ankush Menat
4f797f0bcd fix: realtime log when commit/rollback happen in same req 2023-06-11 11:40:22 +05:30
Ankush Menat
687b660370 perf: Share queue connection for realtime 2023-06-11 11:40:22 +05:30
Ankush Menat
3414c0d063 refactor!: Merge redis_socketio and redis_queue
- realtime communication uses pub-sub and no storage. So using same
  redis server for both should be just fine.
- This is how FC works since quite a lot of time. We haven't seen any
  problem so far.
2023-06-11 11:40:22 +05:30
Ankush Menat
54ae0c4a21 refactor: move flush_realtime_log to realtime.py
This doesn't have anything to do with databases
2023-06-03 18:44:19 +05:30
Ankush Menat
98e8b6d389
fix: correctly return session usertype (#20787)
`user_type` is under data dict and not on session itself.
2023-04-19 23:56:33 +05:30
Ankush Menat
3f717f2fbd perf: Don't re-initate sessions in realtime.py
Session is already created for all requests in app.py -> init_request -> HTTPRequest()
2023-03-23 16:55:09 +05:30
Gavin D'souza
c960382667 fix(socketio)!: Event list_update > doctype_subscribe 2022-12-28 00:25:58 +05:30
Gavin D'souza
d3250f6504 refactor(whitelisted): Add typing hints to APIs
* Refactor type checks defined in APIs
* Remove dead/deprecated kwargs usages
* Added appropriate hints to APIs and consecutive utils defined in the following modules:
 - frappe.realtime
 - frappe.translate
 - frappe.utils.global_search
 - frappe.www.third_party_apps
 - frappe.www.search
 - frappe.www.printview
2022-12-15 13:47:05 +05:30
Gavin D'souza
40df601a73 fix: Auto-add all users to website room
Keep website room open for all - currently only used for discussions
2022-11-17 17:04:07 +05:30
gavin
862a5a398d fix(socketio): Revert irrelevant & unused changes
Co-authored-by: Ankush Menat <ankushmenat@gmail.com>
2022-11-17 15:20:00 +05:30
Gavin D'souza
3a8fa6cbd5 refactor(socketio): Use same room for doc & info events
other changes
- Name list room as doctype room for more generic use
- avoid re-setting up listeners for generic events
- discard docinfo_subscribe event
2022-11-15 17:55:53 +05:30
Gavin D'souza
97d2eab3e2 refactor(socketio): docinfo_update
- Rename event from `update_docinfo_for_{}_{}` to docinfo_update
- Separate rooms for separate documents generated on requirement
- Check if user has access to doc before sharing docinfo
2022-11-15 13:15:34 +05:30
Gavin D'souza
9931c3af04 refactor(socketio)!: list_update
- Subscribe to list_update only for the list/report views that are
  opened
- Check if user has read permission for doctype to subscribe to list
  updates
2022-11-15 13:15:34 +05:30
Gavin D'souza
4de9c39bb8 refactor: SocketIO
- Check request data in middleware
- Authenticate each connection before allowing room access
- Allow site room access only to System Users, restrict Website User &
  Guests to their respective user rooms

Note: This doesn't check for roles / permissions
2022-11-15 13:15:30 +05:30
Suraj Shetty
05f416e448 refactor: Removed vendored socketio library
- Removed vendored socketio library
- Add socket.io-client library
- Removed unnecessary code in socketio server
- Remove chat related code
2022-06-29 14:58:13 +05:30
Suraj Shetty
c0c5b2ebdd
style: format all python files using black (#16453)
Co-authored-by: Frappe Bot <developers@frappe.io>
2022-04-12 10:59:25 +05:30
Abhishek Saxena
5c8856d66e
refactor: db.sql calls to frappe.qb (#16107)
# Changes

- Introduces `subqry` class to use in where clause when there is a non-column condition. eg.
> .where(subqry(no_of_roles) == 0)
- Convert SQL queries to frappe.qb 

# Testing

Functions with query refactors
- frappe.boot.get_user_pages_or_reports() -> Same output of `get_bootinfo()` as develop
- frappe.boot.get_unseen_notes() -> Forms the same query as develop 
```sql
SELECT `name`,`title`,`content`,`notify_on_every_login`
FROM `tabNote` WHERE `notify_on_every_login`=1
AND `expire_notification_on`>'2022-03-30 01:10:53.393874'
AND (SELECT `nsb`.`user` FROM `tabNote Seen By` `nsb` WHERE `nsb`.`parent`=`tabNote`.`name`) NOT IN ('Administrator')
```
- frappe.installer._delete_doctypes() -> installed and uninsalled a dummy app to drop tables

### Not tested
- frappe.make_property_setter()
- frappe.realtime.get_pending_tasks_for_doc() [whitelist method]
- frappe.sessions.Session.start()
- frappe.twofactor.cache_2fa_data()
2022-04-12 05:07:25 +00:00
phot0n
bebc8058b6 feat: integer primary keys 2022-03-11 23:46:00 +05:30
barredterra
745297a49d refactor: a not in b
Search: if not ([\w\d]*?) in ([\w\d]*?)
Replace: if $1 not in $2
2022-02-21 19:54:34 +01:00
Gavin D'souza
3446026555 chore: Update header: license.txt => LICENSE
The license.txt file has been replaced with LICENSE for quite a while
now. INAL but it didn't seem accurate to say "hey, checkout license.txt
although there's no such file". Apart from this, there were
inconsistencies in the headers altogether...this change brings
consistency.
2021-09-03 12:02:59 +05:30
Gavin D'souza
393696fa4a chore: Drop dead code
* Don't create task-logs folder during site creation
* Remove old unused async code for tracking tasks
2021-05-06 19:24:12 +05:30
Sagar Vora
c1911a0858 refactor: remove unused parameter, minor cleanup 2021-02-16 14:16:30 +05:30
Walstan Baptista
06f69accca
refactor: Removed unused parameter (#12400) 2021-02-16 12:03:59 +05:30
Aditya Hase
f3b959b1fe frappe: refactor async to realtime (#6015)
* frappe: refactor async to realtime

* frappe: refactor async to is_async
2018-08-28 15:31:20 +05:30
Renamed from frappe/async.py (Browse further)