util/pubsub.lua
author Kim Alvefur <zash@zash.se>
Mon, 12 Dec 2022 07:03:31 +0100
branch0.11
changeset 12802 c4b1b5cbc20b
parent 11569 087b275a9aee
child 11571 c471e19a238e
permissions -rw-r--r--
Tag 0.11.14
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4365
6704b3cd032e util.pubsub: Support for events (currently subscription-added and subscription-removed)
Matthew Wild <mwild1@gmail.com>
parents: 4364
diff changeset
     1
local events = require "util.events";
7698
56ce32cfd6d9 util.pubsub: Switch to use util.cache for item data
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
     2
local cache = require "util.cache";
4365
6704b3cd032e util.pubsub: Support for events (currently subscription-added and subscription-removed)
Matthew Wild <mwild1@gmail.com>
parents: 4364
diff changeset
     3
8504
8d9e2c2095dd util.pubsub: Move service methods object creation (just code reorganisation)
Matthew Wild <mwild1@gmail.com>
parents: 8503
diff changeset
     4
local service_mt = {};
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
8503
9bf00d0734c8 util.pubsub: For clarity, split config tables from their metatables
Matthew Wild <mwild1@gmail.com>
parents: 8404
diff changeset
     6
local default_config = {
8336
2abbb01cd756 pubsub: Distinguish internal representation of node config from XEP-0060 form (util.pubsub should be protocol-agnostic)
Kim Alvefur <zash@zash.se>
parents: 8329
diff changeset
     7
	itemstore = function (config, _) return cache.new(config["max_items"]) end;
3909
c2dc7f7eed94 util.pubsub: Modify new() to take a config, and add a default config via a metatable
Matthew Wild <mwild1@gmail.com>
parents: 3759
diff changeset
     8
	broadcaster = function () end;
10523
641e3b7a6a39 util.pubsub: Pass subscribers trough a filter callback
Kim Alvefur <zash@zash.se>
parents: 10522
diff changeset
     9
	subscriber_filter = function (subs) return subs end;
8697
059183e5571e util.pubsub: Allow setting a callback for validating items to be published
Kim Alvefur <zash@zash.se>
parents: 8505
diff changeset
    10
	itemcheck = function () return true; end;
3909
c2dc7f7eed94 util.pubsub: Modify new() to take a config, and add a default config via a metatable
Matthew Wild <mwild1@gmail.com>
parents: 3759
diff changeset
    11
	get_affiliation = function () end;
8813
9f8a746f99c1 util.pubsub: Add a default/fallback JID normalization function
Kim Alvefur <zash@zash.se>
parents: 8812
diff changeset
    12
	normalize_jid = function (jid) return jid; end;
9161
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    13
	capabilities = {
9163
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    14
		outcast = {
9236
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    15
			create = false;
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    16
			publish = false;
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    17
			retract = false;
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    18
			get_nodes = false;
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    19
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    20
			subscribe = false;
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    21
			unsubscribe = false;
9175
822e9c5ff4a4 util.pubsub: Allow outcasts to get their subscription status
Matthew Wild <mwild1@gmail.com>
parents: 9164
diff changeset
    22
			get_subscription = true;
822e9c5ff4a4 util.pubsub: Allow outcasts to get their subscription status
Matthew Wild <mwild1@gmail.com>
parents: 9164
diff changeset
    23
			get_subscriptions = true;
9236
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    24
			get_items = false;
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    25
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    26
			subscribe_other = false;
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    27
			unsubscribe_other = false;
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    28
			get_subscription_other = false;
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    29
			get_subscriptions_other = false;
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    30
9161
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    31
			be_subscribed = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    32
			be_unsubscribed = true;
9236
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    33
885dd6845e62 util.pubsub: Explicitly add all capabilities to the 'outcast' affiliation for completeness
Kim Alvefur <zash@zash.se>
parents: 9235
diff changeset
    34
			set_affiliation = false;
9161
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    35
		};
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    36
		none = {
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    37
			create = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    38
			publish = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    39
			retract = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    40
			get_nodes = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    41
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    42
			subscribe = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    43
			unsubscribe = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    44
			get_subscription = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    45
			get_subscriptions = true;
9163
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    46
			get_items = false;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    47
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    48
			subscribe_other = false;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    49
			unsubscribe_other = false;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    50
			get_subscription_other = false;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    51
			get_subscriptions_other = false;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    52
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    53
			be_subscribed = true;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    54
			be_unsubscribed = true;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    55
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    56
			set_affiliation = false;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    57
		};
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    58
		member = {
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    59
			create = false;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    60
			publish = false;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    61
			retract = false;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    62
			get_nodes = true;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    63
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    64
			subscribe = true;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    65
			unsubscribe = true;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    66
			get_subscription = true;
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
    67
			get_subscriptions = true;
9161
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    68
			get_items = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    69
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    70
			subscribe_other = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    71
			unsubscribe_other = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    72
			get_subscription_other = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    73
			get_subscriptions_other = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    74
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    75
			be_subscribed = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    76
			be_unsubscribed = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    77
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    78
			set_affiliation = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    79
		};
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    80
		publisher = {
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    81
			create = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    82
			publish = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    83
			retract = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    84
			get_nodes = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    85
			get_configuration = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    86
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    87
			subscribe = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    88
			unsubscribe = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    89
			get_subscription = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    90
			get_subscriptions = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    91
			get_items = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    92
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    93
			subscribe_other = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    94
			unsubscribe_other = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    95
			get_subscription_other = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    96
			get_subscriptions_other = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    97
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    98
			be_subscribed = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
    99
			be_unsubscribed = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   100
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   101
			set_affiliation = false;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   102
		};
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   103
		owner = {
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   104
			create = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   105
			publish = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   106
			retract = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   107
			delete = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   108
			get_nodes = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   109
			configure = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   110
			get_configuration = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   111
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   112
			subscribe = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   113
			unsubscribe = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   114
			get_subscription = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   115
			get_subscriptions = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   116
			get_items = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   117
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   118
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   119
			subscribe_other = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   120
			unsubscribe_other = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   121
			get_subscription_other = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   122
			get_subscriptions_other = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   123
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   124
			be_subscribed = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   125
			be_unsubscribed = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   126
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   127
			set_affiliation = true;
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   128
		};
37e814a680ab mod_pubsub, mod_pep, util.pubsub: Move capability definitions into util.pubsub to avoid duplication
Matthew Wild <mwild1@gmail.com>
parents: 9147
diff changeset
   129
	};
8503
9bf00d0734c8 util.pubsub: For clarity, split config tables from their metatables
Matthew Wild <mwild1@gmail.com>
parents: 8404
diff changeset
   130
};
9bf00d0734c8 util.pubsub: For clarity, split config tables from their metatables
Matthew Wild <mwild1@gmail.com>
parents: 8404
diff changeset
   131
local default_config_mt = { __index = default_config };
9bf00d0734c8 util.pubsub: For clarity, split config tables from their metatables
Matthew Wild <mwild1@gmail.com>
parents: 8404
diff changeset
   132
9bf00d0734c8 util.pubsub: For clarity, split config tables from their metatables
Matthew Wild <mwild1@gmail.com>
parents: 8404
diff changeset
   133
local default_node_config = {
8336
2abbb01cd756 pubsub: Distinguish internal representation of node config from XEP-0060 form (util.pubsub should be protocol-agnostic)
Kim Alvefur <zash@zash.se>
parents: 8329
diff changeset
   134
	["persist_items"] = false;
2abbb01cd756 pubsub: Distinguish internal representation of node config from XEP-0060 form (util.pubsub should be protocol-agnostic)
Kim Alvefur <zash@zash.se>
parents: 8329
diff changeset
   135
	["max_items"] = 20;
9098
5639dc1a3f85 util.pubsub: Add initial support for configurable access models
Kim Alvefur <zash@zash.se>
parents: 9078
diff changeset
   136
	["access_model"] = "open";
9132
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   137
	["publish_model"] = "publishers";
8503
9bf00d0734c8 util.pubsub: For clarity, split config tables from their metatables
Matthew Wild <mwild1@gmail.com>
parents: 8404
diff changeset
   138
};
9bf00d0734c8 util.pubsub: For clarity, split config tables from their metatables
Matthew Wild <mwild1@gmail.com>
parents: 8404
diff changeset
   139
local default_node_config_mt = { __index = default_node_config };
3909
c2dc7f7eed94 util.pubsub: Modify new() to take a config, and add a default config via a metatable
Matthew Wild <mwild1@gmail.com>
parents: 3759
diff changeset
   140
8505
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   141
-- Storage helper functions
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   142
8953
03ba5b4f131a util.pubsub: Fix applying per service node defaults when loading from nodestore
Kim Alvefur <zash@zash.se>
parents: 8944
diff changeset
   143
local function load_node_from_store(service, node_name)
03ba5b4f131a util.pubsub: Fix applying per service node defaults when loading from nodestore
Kim Alvefur <zash@zash.se>
parents: 8944
diff changeset
   144
	local node = service.config.nodestore:get(node_name);
03ba5b4f131a util.pubsub: Fix applying per service node defaults when loading from nodestore
Kim Alvefur <zash@zash.se>
parents: 8944
diff changeset
   145
	node.config = setmetatable(node.config or {}, {__index=service.node_defaults});
8505
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   146
	return node;
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   147
end
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   148
8953
03ba5b4f131a util.pubsub: Fix applying per service node defaults when loading from nodestore
Kim Alvefur <zash@zash.se>
parents: 8944
diff changeset
   149
local function save_node_to_store(service, node)
03ba5b4f131a util.pubsub: Fix applying per service node defaults when loading from nodestore
Kim Alvefur <zash@zash.se>
parents: 8944
diff changeset
   150
	return service.config.nodestore:set(node.name, {
8505
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   151
		name = node.name;
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   152
		config = node.config;
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   153
		subscribers = node.subscribers;
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   154
		affiliations = node.affiliations;
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   155
	});
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   156
end
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   157
8955
15bb54f96dd1 util.pubsub: Remove node from persistent storage on deletion
Kim Alvefur <zash@zash.se>
parents: 8954
diff changeset
   158
local function delete_node_in_store(service, node_name)
15bb54f96dd1 util.pubsub: Remove node from persistent storage on deletion
Kim Alvefur <zash@zash.se>
parents: 8954
diff changeset
   159
	return service.config.nodestore:set(node_name, nil);
15bb54f96dd1 util.pubsub: Remove node from persistent storage on deletion
Kim Alvefur <zash@zash.se>
parents: 8954
diff changeset
   160
end
15bb54f96dd1 util.pubsub: Remove node from persistent storage on deletion
Kim Alvefur <zash@zash.se>
parents: 8954
diff changeset
   161
8505
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   162
-- Create and return a new service object
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6518
diff changeset
   163
local function new(config)
3909
c2dc7f7eed94 util.pubsub: Modify new() to take a config, and add a default config via a metatable
Matthew Wild <mwild1@gmail.com>
parents: 3759
diff changeset
   164
	config = config or {};
8505
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   165
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   166
	local service = setmetatable({
8503
9bf00d0734c8 util.pubsub: For clarity, split config tables from their metatables
Matthew Wild <mwild1@gmail.com>
parents: 8404
diff changeset
   167
		config = setmetatable(config, default_config_mt);
9bf00d0734c8 util.pubsub: For clarity, split config tables from their metatables
Matthew Wild <mwild1@gmail.com>
parents: 8404
diff changeset
   168
		node_defaults = setmetatable(config.node_defaults or {}, default_node_config_mt);
3909
c2dc7f7eed94 util.pubsub: Modify new() to take a config, and add a default config via a metatable
Matthew Wild <mwild1@gmail.com>
parents: 3759
diff changeset
   169
		affiliations = {};
3938
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   170
		subscriptions = {};
3909
c2dc7f7eed94 util.pubsub: Modify new() to take a config, and add a default config via a metatable
Matthew Wild <mwild1@gmail.com>
parents: 3759
diff changeset
   171
		nodes = {};
5972
f365d3c8fd2c util.pubsub: Separate data from node configuration
Kim Alvefur <zash@zash.se>
parents: 5971
diff changeset
   172
		data = {};
4365
6704b3cd032e util.pubsub: Support for events (currently subscription-added and subscription-removed)
Matthew Wild <mwild1@gmail.com>
parents: 4364
diff changeset
   173
		events = events.new();
3909
c2dc7f7eed94 util.pubsub: Modify new() to take a config, and add a default config via a metatable
Matthew Wild <mwild1@gmail.com>
parents: 3759
diff changeset
   174
	}, service_mt);
8505
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   175
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   176
	-- Load nodes from storage, if we have a store and it supports iterating over stored items
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   177
	if config.nodestore and config.nodestore.users then
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   178
		for node_name in config.nodestore:users() do
8953
03ba5b4f131a util.pubsub: Fix applying per service node defaults when loading from nodestore
Kim Alvefur <zash@zash.se>
parents: 8944
diff changeset
   179
			service.nodes[node_name] = load_node_from_store(service, node_name);
8505
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   180
			service.data[node_name] = config.itemstore(service.nodes[node_name].config, node_name);
9746
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   181
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   182
			for jid in pairs(service.nodes[node_name].subscribers) do
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   183
				local normal_jid = service.config.normalize_jid(jid);
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   184
				local subs = service.subscriptions[normal_jid];
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   185
				if subs then
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   186
					if not subs[jid] then
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   187
						subs[jid] = { [node_name] = true };
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   188
					else
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   189
						subs[jid][node_name] = true;
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   190
					end
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   191
				else
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   192
					service.subscriptions[normal_jid] = { [jid] = { [node_name] = true } };
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   193
				end
18eca6afb367 util.pubsub: Restore subscription index from stored data (fixes #1281)
Kim Alvefur <zash@zash.se>
parents: 9542
diff changeset
   194
			end
8505
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   195
		end
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   196
	end
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   197
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   198
	return service;
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   199
end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   200
8504
8d9e2c2095dd util.pubsub: Move service methods object creation (just code reorganisation)
Matthew Wild <mwild1@gmail.com>
parents: 8503
diff changeset
   201
--- Service methods
8d9e2c2095dd util.pubsub: Move service methods object creation (just code reorganisation)
Matthew Wild <mwild1@gmail.com>
parents: 8503
diff changeset
   202
8d9e2c2095dd util.pubsub: Move service methods object creation (just code reorganisation)
Matthew Wild <mwild1@gmail.com>
parents: 8503
diff changeset
   203
local service = {};
8d9e2c2095dd util.pubsub: Move service methods object creation (just code reorganisation)
Matthew Wild <mwild1@gmail.com>
parents: 8503
diff changeset
   204
service_mt.__index = service;
8d9e2c2095dd util.pubsub: Move service methods object creation (just code reorganisation)
Matthew Wild <mwild1@gmail.com>
parents: 8503
diff changeset
   205
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   206
function service:jids_equal(jid1, jid2) --> boolean
3934
4bd994df7296 util.pubsub: Add service:jids_equal() and new config option normalize_jid
Matthew Wild <mwild1@gmail.com>
parents: 3931
diff changeset
   207
	local normalize = self.config.normalize_jid;
4bd994df7296 util.pubsub: Add service:jids_equal() and new config option normalize_jid
Matthew Wild <mwild1@gmail.com>
parents: 3931
diff changeset
   208
	return normalize(jid1) == normalize(jid2);
4bd994df7296 util.pubsub: Add service:jids_equal() and new config option normalize_jid
Matthew Wild <mwild1@gmail.com>
parents: 3931
diff changeset
   209
end
4bd994df7296 util.pubsub: Add service:jids_equal() and new config option normalize_jid
Matthew Wild <mwild1@gmail.com>
parents: 3931
diff changeset
   210
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   211
function service:may(node, actor, action) --> boolean
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   212
	if actor == true then return true; end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5675
diff changeset
   213
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   214
	local node_obj = self.nodes[node];
8819
0f9d5cfa84f9 util.pubsub: Also check for affiliation set on bare JID
Kim Alvefur <zash@zash.se>
parents: 8816
diff changeset
   215
	local node_aff = node_obj and (node_obj.affiliations[actor]
0f9d5cfa84f9 util.pubsub: Also check for affiliation set on bare JID
Kim Alvefur <zash@zash.se>
parents: 8816
diff changeset
   216
	              or node_obj.affiliations[self.config.normalize_jid(actor)]);
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   217
	local service_aff = self.affiliations[actor]
9098
5639dc1a3f85 util.pubsub: Add initial support for configurable access models
Kim Alvefur <zash@zash.se>
parents: 9078
diff changeset
   218
	                 or self.config.get_affiliation(actor, node, action);
5639dc1a3f85 util.pubsub: Add initial support for configurable access models
Kim Alvefur <zash@zash.se>
parents: 9078
diff changeset
   219
	local default_aff = self:get_default_affiliation(node, actor) or "none";
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5675
diff changeset
   220
4099
5c0b7947f0ef util.pubsub: Some tidying/optimisation to service:may()
Matthew Wild <mwild1@gmail.com>
parents: 3945
diff changeset
   221
	-- Check if node allows/forbids it
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   222
	local node_capabilities = node_obj and node_obj.capabilities;
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   223
	if node_capabilities then
9098
5639dc1a3f85 util.pubsub: Add initial support for configurable access models
Kim Alvefur <zash@zash.se>
parents: 9078
diff changeset
   224
		local caps = node_capabilities[node_aff or service_aff or default_aff];
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   225
		if caps then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   226
			local can = caps[action];
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   227
			if can ~= nil then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   228
				return can;
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   229
			end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   230
		end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   231
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5675
diff changeset
   232
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   233
	-- Check service-wide capabilities instead
4099
5c0b7947f0ef util.pubsub: Some tidying/optimisation to service:may()
Matthew Wild <mwild1@gmail.com>
parents: 3945
diff changeset
   234
	local service_capabilities = self.config.capabilities;
9098
5639dc1a3f85 util.pubsub: Add initial support for configurable access models
Kim Alvefur <zash@zash.se>
parents: 9078
diff changeset
   235
	local caps = service_capabilities[node_aff or service_aff or default_aff];
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   236
	if caps then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   237
		local can = caps[action];
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   238
		if can ~= nil then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   239
			return can;
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   240
		end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   241
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5675
diff changeset
   242
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   243
	return false;
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   244
end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   245
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   246
function service:get_default_affiliation(node, actor) --> affiliation
9098
5639dc1a3f85 util.pubsub: Add initial support for configurable access models
Kim Alvefur <zash@zash.se>
parents: 9078
diff changeset
   247
	local node_obj = self.nodes[node];
5639dc1a3f85 util.pubsub: Add initial support for configurable access models
Kim Alvefur <zash@zash.se>
parents: 9078
diff changeset
   248
	local access_model = node_obj and node_obj.config.access_model
9109
e70b9e8bc443 util.pubsub: Use service.node_defaults in case config.node_defaults was not provided (thanks jonasw)
Matthew Wild <mwild1@gmail.com>
parents: 9107
diff changeset
   249
		or self.node_defaults.access_model;
9098
5639dc1a3f85 util.pubsub: Add initial support for configurable access models
Kim Alvefur <zash@zash.se>
parents: 9078
diff changeset
   250
5639dc1a3f85 util.pubsub: Add initial support for configurable access models
Kim Alvefur <zash@zash.se>
parents: 9078
diff changeset
   251
	if access_model == "open" then
9164
da154ced7de4 util.pubsub: For open nodes, default affiliation is "member"
Matthew Wild <mwild1@gmail.com>
parents: 9163
diff changeset
   252
		return "member";
9098
5639dc1a3f85 util.pubsub: Add initial support for configurable access models
Kim Alvefur <zash@zash.se>
parents: 9078
diff changeset
   253
	elseif access_model == "whitelist" then
9163
e13a1a0b0107 mod_pep, util.pubsub: Rename restricted->outcast, none->member and add new 'none' affiliation to better match XEP-0060
Matthew Wild <mwild1@gmail.com>
parents: 9161
diff changeset
   254
		return "outcast";
9098
5639dc1a3f85 util.pubsub: Add initial support for configurable access models
Kim Alvefur <zash@zash.se>
parents: 9078
diff changeset
   255
	end
9101
d5bc306e93aa util.pubsub: Look for a configured callback for more complicated access models
Kim Alvefur <zash@zash.se>
parents: 9098
diff changeset
   256
d5bc306e93aa util.pubsub: Look for a configured callback for more complicated access models
Kim Alvefur <zash@zash.se>
parents: 9098
diff changeset
   257
	if self.config.access_models then
d5bc306e93aa util.pubsub: Look for a configured callback for more complicated access models
Kim Alvefur <zash@zash.se>
parents: 9098
diff changeset
   258
		local check = self.config.access_models[access_model];
d5bc306e93aa util.pubsub: Look for a configured callback for more complicated access models
Kim Alvefur <zash@zash.se>
parents: 9098
diff changeset
   259
		if check then
d5bc306e93aa util.pubsub: Look for a configured callback for more complicated access models
Kim Alvefur <zash@zash.se>
parents: 9098
diff changeset
   260
			local aff = check(actor);
d5bc306e93aa util.pubsub: Look for a configured callback for more complicated access models
Kim Alvefur <zash@zash.se>
parents: 9098
diff changeset
   261
			if aff then
d5bc306e93aa util.pubsub: Look for a configured callback for more complicated access models
Kim Alvefur <zash@zash.se>
parents: 9098
diff changeset
   262
				return aff;
d5bc306e93aa util.pubsub: Look for a configured callback for more complicated access models
Kim Alvefur <zash@zash.se>
parents: 9098
diff changeset
   263
			end
d5bc306e93aa util.pubsub: Look for a configured callback for more complicated access models
Kim Alvefur <zash@zash.se>
parents: 9098
diff changeset
   264
		end
d5bc306e93aa util.pubsub: Look for a configured callback for more complicated access models
Kim Alvefur <zash@zash.se>
parents: 9098
diff changeset
   265
	end
9098
5639dc1a3f85 util.pubsub: Add initial support for configurable access models
Kim Alvefur <zash@zash.se>
parents: 9078
diff changeset
   266
end
5639dc1a3f85 util.pubsub: Add initial support for configurable access models
Kim Alvefur <zash@zash.se>
parents: 9078
diff changeset
   267
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   268
function service:set_affiliation(node, actor, jid, affiliation) --> ok, err
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   269
	-- Access checking
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   270
	if not self:may(node, actor, "set_affiliation") then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   271
		return false, "forbidden";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   272
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   273
	--
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   274
	local node_obj = self.nodes[node];
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   275
	if not node_obj then
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   276
		return false, "item-not-found";
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   277
	end
8944
f0beba9c2822 util.pubsub: Fix typo
Kim Alvefur <zash@zash.se>
parents: 8939
diff changeset
   278
	jid = self.config.normalize_jid(jid);
8957
3b6095686498 util.pubsub: Persistence on affiliation change
Kim Alvefur <zash@zash.se>
parents: 8955
diff changeset
   279
	local old_affiliation = node_obj.affiliations[jid];
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   280
	node_obj.affiliations[jid] = affiliation;
8957
3b6095686498 util.pubsub: Persistence on affiliation change
Kim Alvefur <zash@zash.se>
parents: 8955
diff changeset
   281
3b6095686498 util.pubsub: Persistence on affiliation change
Kim Alvefur <zash@zash.se>
parents: 8955
diff changeset
   282
	if self.config.nodestore then
3b6095686498 util.pubsub: Persistence on affiliation change
Kim Alvefur <zash@zash.se>
parents: 8955
diff changeset
   283
		local ok, err = save_node_to_store(self, node_obj);
3b6095686498 util.pubsub: Persistence on affiliation change
Kim Alvefur <zash@zash.se>
parents: 8955
diff changeset
   284
		if not ok then
3b6095686498 util.pubsub: Persistence on affiliation change
Kim Alvefur <zash@zash.se>
parents: 8955
diff changeset
   285
			node_obj.affiliations[jid] = old_affiliation;
3b6095686498 util.pubsub: Persistence on affiliation change
Kim Alvefur <zash@zash.se>
parents: 8955
diff changeset
   286
			return ok, "internal-server-error";
3b6095686498 util.pubsub: Persistence on affiliation change
Kim Alvefur <zash@zash.se>
parents: 8955
diff changeset
   287
		end
3b6095686498 util.pubsub: Persistence on affiliation change
Kim Alvefur <zash@zash.se>
parents: 8955
diff changeset
   288
	end
3b6095686498 util.pubsub: Persistence on affiliation change
Kim Alvefur <zash@zash.se>
parents: 8955
diff changeset
   289
4100
69e3f1e7111e util.pubsub: Pass true instead of nil as the actor in a bunch of places, and fix a bunch of methods to not traceback on this (those with *_other capability checking).
Matthew Wild <mwild1@gmail.com>
parents: 4099
diff changeset
   290
	local _, jid_sub = self:get_subscription(node, true, jid);
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   291
	if not jid_sub and not self:may(node, jid, "be_unsubscribed") then
4100
69e3f1e7111e util.pubsub: Pass true instead of nil as the actor in a bunch of places, and fix a bunch of methods to not traceback on this (those with *_other capability checking).
Matthew Wild <mwild1@gmail.com>
parents: 4099
diff changeset
   292
		local ok, err = self:add_subscription(node, true, jid);
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   293
		if not ok then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   294
			return ok, err;
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   295
		end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   296
	elseif jid_sub and not self:may(node, jid, "be_subscribed") then
4100
69e3f1e7111e util.pubsub: Pass true instead of nil as the actor in a bunch of places, and fix a bunch of methods to not traceback on this (those with *_other capability checking).
Matthew Wild <mwild1@gmail.com>
parents: 4099
diff changeset
   297
		local ok, err = self:add_subscription(node, true, jid);
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   298
		if not ok then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   299
			return ok, err;
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   300
		end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   301
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   302
	return true;
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   303
end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   304
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   305
function service:add_subscription(node, actor, jid, options) --> ok, err
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   306
	-- Access checking
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   307
	local cap;
4100
69e3f1e7111e util.pubsub: Pass true instead of nil as the actor in a bunch of places, and fix a bunch of methods to not traceback on this (those with *_other capability checking).
Matthew Wild <mwild1@gmail.com>
parents: 4099
diff changeset
   308
	if actor == true or jid == actor or self:jids_equal(actor, jid) then
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   309
		cap = "subscribe";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   310
	else
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   311
		cap = "subscribe_other";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   312
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   313
	if not self:may(node, actor, cap) then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   314
		return false, "forbidden";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   315
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   316
	if not self:may(node, jid, "be_subscribed") then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   317
		return false, "forbidden";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   318
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   319
	--
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   320
	local node_obj = self.nodes[node];
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   321
	if not node_obj then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   322
		if not self.config.autocreate_on_subscribe then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   323
			return false, "item-not-found";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   324
		else
4364
af40cf682eba util.pubsub: Use built-in actor for auto-creating nodes on publish and subscribe (so they never fail due to permissions)
Matthew Wild <mwild1@gmail.com>
parents: 4100
diff changeset
   325
			local ok, err = self:create(node, true);
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   326
			if not ok then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   327
				return ok, err;
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   328
			end
3936
61f12f8a8539 util.pubsub: Fix traceback when using autocreate-on-subscribe
Matthew Wild <mwild1@gmail.com>
parents: 3934
diff changeset
   329
			node_obj = self.nodes[node];
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   330
		end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   331
	end
8958
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   332
	local old_subscription = node_obj.subscribers[jid];
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   333
	node_obj.subscribers[jid] = options or true;
3938
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   334
	local normal_jid = self.config.normalize_jid(jid);
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   335
	local subs = self.subscriptions[normal_jid];
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   336
	if subs then
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   337
		if not subs[jid] then
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   338
			subs[jid] = { [node] = true };
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   339
		else
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   340
			subs[jid][node] = true;
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   341
		end
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   342
	else
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   343
		self.subscriptions[normal_jid] = { [jid] = { [node] = true } };
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   344
	end
8958
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   345
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   346
	if self.config.nodestore then
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   347
		local ok, err = save_node_to_store(self, node_obj);
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   348
		if not ok then
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   349
			node_obj.subscribers[jid] = old_subscription;
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   350
			self.subscriptions[normal_jid][jid][node] = old_subscription and true or nil;
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   351
			return ok, "internal-server-error";
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   352
		end
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   353
	end
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   354
9233
711fea229e48 util.pubsub: Add 'service' field to all events
Matthew Wild <mwild1@gmail.com>
parents: 9223
diff changeset
   355
	self.events.fire_event("subscription-added", { service = self, node = node, jid = jid, normalized_jid = normal_jid, options = options });
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   356
	return true;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   357
end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   358
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   359
function service:remove_subscription(node, actor, jid) --> ok, err
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   360
	-- Access checking
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   361
	local cap;
4100
69e3f1e7111e util.pubsub: Pass true instead of nil as the actor in a bunch of places, and fix a bunch of methods to not traceback on this (those with *_other capability checking).
Matthew Wild <mwild1@gmail.com>
parents: 4099
diff changeset
   362
	if actor == true or jid == actor or self:jids_equal(actor, jid) then
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   363
		cap = "unsubscribe";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   364
	else
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   365
		cap = "unsubscribe_other";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   366
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   367
	if not self:may(node, actor, cap) then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   368
		return false, "forbidden";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   369
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   370
	if not self:may(node, jid, "be_unsubscribed") then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   371
		return false, "forbidden";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   372
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   373
	--
3698
77171fd1dc3c mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents: 3672
diff changeset
   374
	local node_obj = self.nodes[node];
77171fd1dc3c mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents: 3672
diff changeset
   375
	if not node_obj then
77171fd1dc3c mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents: 3672
diff changeset
   376
		return false, "item-not-found";
77171fd1dc3c mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents: 3672
diff changeset
   377
	end
77171fd1dc3c mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents: 3672
diff changeset
   378
	if not node_obj.subscribers[jid] then
77171fd1dc3c mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents: 3672
diff changeset
   379
		return false, "not-subscribed";
77171fd1dc3c mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents: 3672
diff changeset
   380
	end
8958
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   381
	local old_subscription = node_obj.subscribers[jid];
3698
77171fd1dc3c mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents: 3672
diff changeset
   382
	node_obj.subscribers[jid] = nil;
3938
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   383
	local normal_jid = self.config.normalize_jid(jid);
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   384
	local subs = self.subscriptions[normal_jid];
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   385
	if subs then
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   386
		local jid_subs = subs[jid];
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   387
		if jid_subs then
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   388
			jid_subs[node] = nil;
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   389
			if next(jid_subs) == nil then
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   390
				subs[jid] = nil;
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   391
			end
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   392
		end
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   393
		if next(subs) == nil then
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   394
			self.subscriptions[normal_jid] = nil;
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   395
		end
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   396
	end
8958
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   397
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   398
	if self.config.nodestore then
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   399
		local ok, err = save_node_to_store(self, node_obj);
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   400
		if not ok then
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   401
			node_obj.subscribers[jid] = old_subscription;
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   402
			self.subscriptions[normal_jid][jid][node] = old_subscription and true or nil;
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   403
			return ok, "internal-server-error";
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   404
		end
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   405
	end
ca6a09cf2829 util.pubsub: Store subscription changes
Kim Alvefur <zash@zash.se>
parents: 8957
diff changeset
   406
9233
711fea229e48 util.pubsub: Add 'service' field to all events
Matthew Wild <mwild1@gmail.com>
parents: 9223
diff changeset
   407
	self.events.fire_event("subscription-removed", { service = self, node = node, jid = jid, normalized_jid = normal_jid });
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   408
	return true;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   409
end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   410
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   411
function service:get_subscription(node, actor, jid) --> (true, subscription) or (false, err)
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   412
	-- Access checking
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   413
	local cap;
4100
69e3f1e7111e util.pubsub: Pass true instead of nil as the actor in a bunch of places, and fix a bunch of methods to not traceback on this (those with *_other capability checking).
Matthew Wild <mwild1@gmail.com>
parents: 4099
diff changeset
   414
	if actor == true or jid == actor or self:jids_equal(actor, jid) then
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   415
		cap = "get_subscription";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   416
	else
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   417
		cap = "get_subscription_other";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   418
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   419
	if not self:may(node, actor, cap) then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   420
		return false, "forbidden";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   421
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   422
	--
3626
444f965baed8 util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents: 3619
diff changeset
   423
	local node_obj = self.nodes[node];
3937
843ee23cc91a util.pubsub: Small code tidying for :get_subscription()
Matthew Wild <mwild1@gmail.com>
parents: 3936
diff changeset
   424
	if not node_obj then
843ee23cc91a util.pubsub: Small code tidying for :get_subscription()
Matthew Wild <mwild1@gmail.com>
parents: 3936
diff changeset
   425
		return false, "item-not-found";
3626
444f965baed8 util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents: 3619
diff changeset
   426
	end
3937
843ee23cc91a util.pubsub: Small code tidying for :get_subscription()
Matthew Wild <mwild1@gmail.com>
parents: 3936
diff changeset
   427
	return true, node_obj.subscribers[jid];
3626
444f965baed8 util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents: 3619
diff changeset
   428
end
444f965baed8 util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents: 3619
diff changeset
   429
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   430
function service:create(node, actor, options) --> ok, err
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   431
	-- Access checking
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   432
	if not self:may(node, actor, "create") then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   433
		return false, "forbidden";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   434
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   435
	--
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   436
	if self.nodes[node] then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   437
		return false, "conflict";
3672
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
   438
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5675
diff changeset
   439
9844
ec353524b739 util.pubsub: Validate node configuration on node creation (fixes #1328)
Kim Alvefur <zash@zash.se>
parents: 9820
diff changeset
   440
	local config = setmetatable(options or {}, {__index=self.node_defaults});
ec353524b739 util.pubsub: Validate node configuration on node creation (fixes #1328)
Kim Alvefur <zash@zash.se>
parents: 9820
diff changeset
   441
ec353524b739 util.pubsub: Validate node configuration on node creation (fixes #1328)
Kim Alvefur <zash@zash.se>
parents: 9820
diff changeset
   442
	if self.config.check_node_config then
ec353524b739 util.pubsub: Validate node configuration on node creation (fixes #1328)
Kim Alvefur <zash@zash.se>
parents: 9820
diff changeset
   443
		local ok = self.config.check_node_config(node, actor, config);
ec353524b739 util.pubsub: Validate node configuration on node creation (fixes #1328)
Kim Alvefur <zash@zash.se>
parents: 9820
diff changeset
   444
		if not ok then
ec353524b739 util.pubsub: Validate node configuration on node creation (fixes #1328)
Kim Alvefur <zash@zash.se>
parents: 9820
diff changeset
   445
			return false, "not-acceptable";
ec353524b739 util.pubsub: Validate node configuration on node creation (fixes #1328)
Kim Alvefur <zash@zash.se>
parents: 9820
diff changeset
   446
		end
ec353524b739 util.pubsub: Validate node configuration on node creation (fixes #1328)
Kim Alvefur <zash@zash.se>
parents: 9820
diff changeset
   447
	end
ec353524b739 util.pubsub: Validate node configuration on node creation (fixes #1328)
Kim Alvefur <zash@zash.se>
parents: 9820
diff changeset
   448
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   449
	self.nodes[node] = {
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   450
		name = node;
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   451
		subscribers = {};
9844
ec353524b739 util.pubsub: Validate node configuration on node creation (fixes #1328)
Kim Alvefur <zash@zash.se>
parents: 9820
diff changeset
   452
		config = config;
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   453
		affiliations = {};
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   454
	};
8505
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   455
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   456
	if self.config.nodestore then
8953
03ba5b4f131a util.pubsub: Fix applying per service node defaults when loading from nodestore
Kim Alvefur <zash@zash.se>
parents: 8944
diff changeset
   457
		local ok, err = save_node_to_store(self, self.nodes[node]);
8505
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   458
		if not ok then
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   459
			self.nodes[node] = nil;
8959
82f92af4b0f3 util.pubsub: Return error code known by pubsub.lib if persistent creation fails
Kim Alvefur <zash@zash.se>
parents: 8958
diff changeset
   460
			return ok, "internal-server-error";
8505
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   461
		end
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   462
	end
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   463
8214
5cbbe825d9d1 util.pubsub: Add a node parameter to itemstore().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7730
diff changeset
   464
	self.data[node] = self.config.itemstore(self.nodes[node].config, node);
9233
711fea229e48 util.pubsub: Add 'service' field to all events
Matthew Wild <mwild1@gmail.com>
parents: 9223
diff changeset
   465
	self.events.fire_event("node-created", { service = self, node = node, actor = actor });
8812
6cba2df3817c util.pubsub: Don't record the superuser as owner on creation
Kim Alvefur <zash@zash.se>
parents: 8697
diff changeset
   466
	if actor ~= true then
6cba2df3817c util.pubsub: Don't record the superuser as owner on creation
Kim Alvefur <zash@zash.se>
parents: 8697
diff changeset
   467
		local ok, err = self:set_affiliation(node, true, actor, "owner");
6cba2df3817c util.pubsub: Don't record the superuser as owner on creation
Kim Alvefur <zash@zash.se>
parents: 8697
diff changeset
   468
		if not ok then
6cba2df3817c util.pubsub: Don't record the superuser as owner on creation
Kim Alvefur <zash@zash.se>
parents: 8697
diff changeset
   469
			self.nodes[node] = nil;
6cba2df3817c util.pubsub: Don't record the superuser as owner on creation
Kim Alvefur <zash@zash.se>
parents: 8697
diff changeset
   470
			self.data[node] = nil;
6cba2df3817c util.pubsub: Don't record the superuser as owner on creation
Kim Alvefur <zash@zash.se>
parents: 8697
diff changeset
   471
			return ok, err;
6cba2df3817c util.pubsub: Don't record the superuser as owner on creation
Kim Alvefur <zash@zash.se>
parents: 8697
diff changeset
   472
		end
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   473
	end
8505
6c2c2fc4b8dd util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
Matthew Wild <mwild1@gmail.com>
parents: 8504
diff changeset
   474
8812
6cba2df3817c util.pubsub: Don't record the superuser as owner on creation
Kim Alvefur <zash@zash.se>
parents: 8697
diff changeset
   475
	return true;
3672
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
   476
end
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
   477
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   478
function service:delete(node, actor) --> ok, err
5320
518d864b2ab8 mod_pubsub, util.pubsub: Add delete action
Kim Alvefur <zash@zash.se>
parents: 5315
diff changeset
   479
	-- Access checking
518d864b2ab8 mod_pubsub, util.pubsub: Add delete action
Kim Alvefur <zash@zash.se>
parents: 5315
diff changeset
   480
	if not self:may(node, actor, "delete") then
518d864b2ab8 mod_pubsub, util.pubsub: Add delete action
Kim Alvefur <zash@zash.se>
parents: 5315
diff changeset
   481
		return false, "forbidden";
518d864b2ab8 mod_pubsub, util.pubsub: Add delete action
Kim Alvefur <zash@zash.se>
parents: 5315
diff changeset
   482
	end
518d864b2ab8 mod_pubsub, util.pubsub: Add delete action
Kim Alvefur <zash@zash.se>
parents: 5315
diff changeset
   483
	--
518d864b2ab8 mod_pubsub, util.pubsub: Add delete action
Kim Alvefur <zash@zash.se>
parents: 5315
diff changeset
   484
	local node_obj = self.nodes[node];
5675
e29ece65e3b0 util.pubsub: Check whether node exists, when deleting
Florian Zeitz <florob@babelmonkeys.de>
parents: 5628
diff changeset
   485
	if not node_obj then
e29ece65e3b0 util.pubsub: Check whether node exists, when deleting
Florian Zeitz <florob@babelmonkeys.de>
parents: 5628
diff changeset
   486
		return false, "item-not-found";
e29ece65e3b0 util.pubsub: Check whether node exists, when deleting
Florian Zeitz <florob@babelmonkeys.de>
parents: 5628
diff changeset
   487
	end
5320
518d864b2ab8 mod_pubsub, util.pubsub: Add delete action
Kim Alvefur <zash@zash.se>
parents: 5315
diff changeset
   488
	self.nodes[node] = nil;
8315
6fd36e73082b util.pubsub: Clear data on node deletion
Kim Alvefur <zash@zash.se>
parents: 8300
diff changeset
   489
	if self.data[node] and self.data[node].clear then
6fd36e73082b util.pubsub: Clear data on node deletion
Kim Alvefur <zash@zash.se>
parents: 8300
diff changeset
   490
		self.data[node]:clear();
6fd36e73082b util.pubsub: Clear data on node deletion
Kim Alvefur <zash@zash.se>
parents: 8300
diff changeset
   491
	end
5972
f365d3c8fd2c util.pubsub: Separate data from node configuration
Kim Alvefur <zash@zash.se>
parents: 5971
diff changeset
   492
	self.data[node] = nil;
8955
15bb54f96dd1 util.pubsub: Remove node from persistent storage on deletion
Kim Alvefur <zash@zash.se>
parents: 8954
diff changeset
   493
15bb54f96dd1 util.pubsub: Remove node from persistent storage on deletion
Kim Alvefur <zash@zash.se>
parents: 8954
diff changeset
   494
	if self.config.nodestore then
15bb54f96dd1 util.pubsub: Remove node from persistent storage on deletion
Kim Alvefur <zash@zash.se>
parents: 8954
diff changeset
   495
		local ok, err = delete_node_in_store(self, node);
15bb54f96dd1 util.pubsub: Remove node from persistent storage on deletion
Kim Alvefur <zash@zash.se>
parents: 8954
diff changeset
   496
		if not ok then
15bb54f96dd1 util.pubsub: Remove node from persistent storage on deletion
Kim Alvefur <zash@zash.se>
parents: 8954
diff changeset
   497
			self.nodes[node] = nil;
15bb54f96dd1 util.pubsub: Remove node from persistent storage on deletion
Kim Alvefur <zash@zash.se>
parents: 8954
diff changeset
   498
			return ok, err;
15bb54f96dd1 util.pubsub: Remove node from persistent storage on deletion
Kim Alvefur <zash@zash.se>
parents: 8954
diff changeset
   499
		end
15bb54f96dd1 util.pubsub: Remove node from persistent storage on deletion
Kim Alvefur <zash@zash.se>
parents: 8954
diff changeset
   500
	end
15bb54f96dd1 util.pubsub: Remove node from persistent storage on deletion
Kim Alvefur <zash@zash.se>
parents: 8954
diff changeset
   501
9233
711fea229e48 util.pubsub: Add 'service' field to all events
Matthew Wild <mwild1@gmail.com>
parents: 9223
diff changeset
   502
	self.events.fire_event("node-deleted", { service = self, node = node, actor = actor });
10522
9f50489c2033 util.pubsub: Factor out calling of broadcaster
Kim Alvefur <zash@zash.se>
parents: 9844
diff changeset
   503
	self:broadcast("delete", node, node_obj.subscribers, nil, actor, node_obj);
5320
518d864b2ab8 mod_pubsub, util.pubsub: Add delete action
Kim Alvefur <zash@zash.se>
parents: 5315
diff changeset
   504
	return true;
518d864b2ab8 mod_pubsub, util.pubsub: Add delete action
Kim Alvefur <zash@zash.se>
parents: 5315
diff changeset
   505
end
518d864b2ab8 mod_pubsub, util.pubsub: Add delete action
Kim Alvefur <zash@zash.se>
parents: 5315
diff changeset
   506
9203
249d90ff992e pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
Matthew Wild <mwild1@gmail.com>
parents: 9198
diff changeset
   507
-- Used to check that the config of a node is as expected (i.e. 'publish-options')
249d90ff992e pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
Matthew Wild <mwild1@gmail.com>
parents: 9198
diff changeset
   508
local function check_preconditions(node_config, required_config)
249d90ff992e pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
Matthew Wild <mwild1@gmail.com>
parents: 9198
diff changeset
   509
	if not (node_config and required_config) then
249d90ff992e pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
Matthew Wild <mwild1@gmail.com>
parents: 9198
diff changeset
   510
		return false;
249d90ff992e pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
Matthew Wild <mwild1@gmail.com>
parents: 9198
diff changeset
   511
	end
249d90ff992e pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
Matthew Wild <mwild1@gmail.com>
parents: 9198
diff changeset
   512
	for config_field, value in pairs(required_config) do
249d90ff992e pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
Matthew Wild <mwild1@gmail.com>
parents: 9198
diff changeset
   513
		if node_config[config_field] ~= value then
249d90ff992e pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
Matthew Wild <mwild1@gmail.com>
parents: 9198
diff changeset
   514
			return false;
249d90ff992e pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
Matthew Wild <mwild1@gmail.com>
parents: 9198
diff changeset
   515
		end
249d90ff992e pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
Matthew Wild <mwild1@gmail.com>
parents: 9198
diff changeset
   516
	end
249d90ff992e pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
Matthew Wild <mwild1@gmail.com>
parents: 9198
diff changeset
   517
	return true;
249d90ff992e pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
Matthew Wild <mwild1@gmail.com>
parents: 9198
diff changeset
   518
end
249d90ff992e pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
Matthew Wild <mwild1@gmail.com>
parents: 9198
diff changeset
   519
9519
038446c50630 util.pubsub: Allow publishing with a config that should be used as defaults only
Matthew Wild <mwild1@gmail.com>
parents: 9239
diff changeset
   520
function service:publish(node, actor, id, item, requested_config) --> ok, err
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   521
	-- Access checking
9132
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   522
	local may_publish = false;
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   523
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   524
	if self:may(node, actor, "publish") then
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   525
		may_publish = true;
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   526
	else
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   527
		local node_obj = self.nodes[node];
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   528
		local publish_model = node_obj and node_obj.config.publish_model;
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   529
		if publish_model == "open"
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   530
		or (publish_model == "subscribers" and node_obj.subscribers[actor]) then
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   531
			may_publish = true;
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   532
		end
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   533
	end
7721794e9e93 util.pubsub: Add support for publish_model config option
Matthew Wild <mwild1@gmail.com>
parents: 9120
diff changeset
   534
	if not may_publish then
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   535
		return false, "forbidden";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   536
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   537
	--
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   538
	local node_obj = self.nodes[node];
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   539
	if not node_obj then
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   540
		if not self.config.autocreate_on_publish then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   541
			return false, "item-not-found";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   542
		end
9519
038446c50630 util.pubsub: Allow publishing with a config that should be used as defaults only
Matthew Wild <mwild1@gmail.com>
parents: 9239
diff changeset
   543
		local ok, err = self:create(node, true, requested_config);
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   544
		if not ok then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   545
			return ok, err;
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   546
		end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   547
		node_obj = self.nodes[node];
9519
038446c50630 util.pubsub: Allow publishing with a config that should be used as defaults only
Matthew Wild <mwild1@gmail.com>
parents: 9239
diff changeset
   548
	elseif requested_config and not requested_config._defaults_only then
038446c50630 util.pubsub: Allow publishing with a config that should be used as defaults only
Matthew Wild <mwild1@gmail.com>
parents: 9239
diff changeset
   549
		-- Check that node has the requested config before we publish
038446c50630 util.pubsub: Allow publishing with a config that should be used as defaults only
Matthew Wild <mwild1@gmail.com>
parents: 9239
diff changeset
   550
		if not check_preconditions(node_obj.config, requested_config) then
038446c50630 util.pubsub: Allow publishing with a config that should be used as defaults only
Matthew Wild <mwild1@gmail.com>
parents: 9239
diff changeset
   551
			return false, "precondition-not-met";
038446c50630 util.pubsub: Allow publishing with a config that should be used as defaults only
Matthew Wild <mwild1@gmail.com>
parents: 9239
diff changeset
   552
		end
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   553
	end
8697
059183e5571e util.pubsub: Allow setting a callback for validating items to be published
Kim Alvefur <zash@zash.se>
parents: 8505
diff changeset
   554
	if not self.config.itemcheck(item) then
9210
76d593b35958 util.pubsub, pubsub.lib: Improve error on attempt to publish invalid item
Matthew Wild <mwild1@gmail.com>
parents: 9209
diff changeset
   555
		return nil, "invalid-item";
8697
059183e5571e util.pubsub: Allow setting a callback for validating items to be published
Kim Alvefur <zash@zash.se>
parents: 8505
diff changeset
   556
	end
5972
f365d3c8fd2c util.pubsub: Separate data from node configuration
Kim Alvefur <zash@zash.se>
parents: 5971
diff changeset
   557
	local node_data = self.data[node];
11569
087b275a9aee util.pubsub: Fix traceback if node data not initialized
Kim Alvefur <zash@zash.se>
parents: 10523
diff changeset
   558
	if not node_data then
087b275a9aee util.pubsub: Fix traceback if node data not initialized
Kim Alvefur <zash@zash.se>
parents: 10523
diff changeset
   559
		-- FIXME how is this possible?  #1657
087b275a9aee util.pubsub: Fix traceback if node data not initialized
Kim Alvefur <zash@zash.se>
parents: 10523
diff changeset
   560
		return nil, "internal-server-error";
087b275a9aee util.pubsub: Fix traceback if node data not initialized
Kim Alvefur <zash@zash.se>
parents: 10523
diff changeset
   561
	end
7698
56ce32cfd6d9 util.pubsub: Switch to use util.cache for item data
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
   562
	local ok = node_data:set(id, item);
56ce32cfd6d9 util.pubsub: Switch to use util.cache for item data
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
   563
	if not ok then
56ce32cfd6d9 util.pubsub: Switch to use util.cache for item data
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
   564
		return nil, "internal-server-error";
56ce32cfd6d9 util.pubsub: Switch to use util.cache for item data
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
   565
	end
8220
5f4e0d67b82a util.pubsub: Catch overriden id from storage
Kim Alvefur <zash@zash.se>
parents: 8215
diff changeset
   566
	if type(ok) == "string" then id = ok; end
9233
711fea229e48 util.pubsub: Add 'service' field to all events
Matthew Wild <mwild1@gmail.com>
parents: 9223
diff changeset
   567
	local event_data = { service = self, node = node, actor = actor, id = id, item = item };
9221
7e27dc4d100b util.pubsub: Fire item-published/<node> to allow for easier handling of per-node items
Matthew Wild <mwild1@gmail.com>
parents: 9210
diff changeset
   568
	self.events.fire_event("item-published/"..node, event_data);
7e27dc4d100b util.pubsub: Fire item-published/<node> to allow for easier handling of per-node items
Matthew Wild <mwild1@gmail.com>
parents: 9210
diff changeset
   569
	self.events.fire_event("item-published", event_data);
10522
9f50489c2033 util.pubsub: Factor out calling of broadcaster
Kim Alvefur <zash@zash.se>
parents: 9844
diff changeset
   570
	self:broadcast("items", node, node_obj.subscribers, item, actor, node_obj);
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   571
	return true;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   572
end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   573
10522
9f50489c2033 util.pubsub: Factor out calling of broadcaster
Kim Alvefur <zash@zash.se>
parents: 9844
diff changeset
   574
function service:broadcast(event, node, subscribers, item, actor, node_obj)
10523
641e3b7a6a39 util.pubsub: Pass subscribers trough a filter callback
Kim Alvefur <zash@zash.se>
parents: 10522
diff changeset
   575
	subscribers = self.config.subscriber_filter(subscribers, node, event);
10522
9f50489c2033 util.pubsub: Factor out calling of broadcaster
Kim Alvefur <zash@zash.se>
parents: 9844
diff changeset
   576
	return self.config.broadcaster(event, node, subscribers, item, actor, node_obj, self);
9f50489c2033 util.pubsub: Factor out calling of broadcaster
Kim Alvefur <zash@zash.se>
parents: 9844
diff changeset
   577
end
9f50489c2033 util.pubsub: Factor out calling of broadcaster
Kim Alvefur <zash@zash.se>
parents: 9844
diff changeset
   578
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   579
function service:retract(node, actor, id, retract) --> ok, err
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   580
	-- Access checking
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   581
	if not self:may(node, actor, "retract") then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   582
		return false, "forbidden";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   583
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   584
	--
3699
150e58d69e60 mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents: 3698
diff changeset
   585
	local node_obj = self.nodes[node];
7698
56ce32cfd6d9 util.pubsub: Switch to use util.cache for item data
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
   586
	if (not node_obj) or (not self.data[node]:get(id)) then
3699
150e58d69e60 mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents: 3698
diff changeset
   587
		return false, "item-not-found";
150e58d69e60 mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents: 3698
diff changeset
   588
	end
7698
56ce32cfd6d9 util.pubsub: Switch to use util.cache for item data
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
   589
	local ok = self.data[node]:set(id, nil);
56ce32cfd6d9 util.pubsub: Switch to use util.cache for item data
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
   590
	if not ok then
56ce32cfd6d9 util.pubsub: Switch to use util.cache for item data
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
   591
		return nil, "internal-server-error";
56ce32cfd6d9 util.pubsub: Switch to use util.cache for item data
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
   592
	end
9233
711fea229e48 util.pubsub: Add 'service' field to all events
Matthew Wild <mwild1@gmail.com>
parents: 9223
diff changeset
   593
	self.events.fire_event("item-retracted", { service = self, node = node, actor = actor, id = id });
3699
150e58d69e60 mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents: 3698
diff changeset
   594
	if retract then
10522
9f50489c2033 util.pubsub: Factor out calling of broadcaster
Kim Alvefur <zash@zash.se>
parents: 9844
diff changeset
   595
		self:broadcast("retract", node, node_obj.subscribers, retract, actor, node_obj);
3699
150e58d69e60 mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents: 3698
diff changeset
   596
	end
150e58d69e60 mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents: 3698
diff changeset
   597
	return true
150e58d69e60 mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents: 3698
diff changeset
   598
end
150e58d69e60 mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents: 3698
diff changeset
   599
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   600
function service:purge(node, actor, notify) --> ok, err
5312
fdcd2ac7c22d mod_pubsub, util.pubsub: Don't send purge notifications in an <items/> element
Florian Zeitz <florob@babelmonkeys.de>
parents: 5305
diff changeset
   601
	-- Access checking
fdcd2ac7c22d mod_pubsub, util.pubsub: Don't send purge notifications in an <items/> element
Florian Zeitz <florob@babelmonkeys.de>
parents: 5305
diff changeset
   602
	if not self:may(node, actor, "retract") then
fdcd2ac7c22d mod_pubsub, util.pubsub: Don't send purge notifications in an <items/> element
Florian Zeitz <florob@babelmonkeys.de>
parents: 5305
diff changeset
   603
		return false, "forbidden";
fdcd2ac7c22d mod_pubsub, util.pubsub: Don't send purge notifications in an <items/> element
Florian Zeitz <florob@babelmonkeys.de>
parents: 5305
diff changeset
   604
	end
fdcd2ac7c22d mod_pubsub, util.pubsub: Don't send purge notifications in an <items/> element
Florian Zeitz <florob@babelmonkeys.de>
parents: 5305
diff changeset
   605
	--
fdcd2ac7c22d mod_pubsub, util.pubsub: Don't send purge notifications in an <items/> element
Florian Zeitz <florob@babelmonkeys.de>
parents: 5305
diff changeset
   606
	local node_obj = self.nodes[node];
fdcd2ac7c22d mod_pubsub, util.pubsub: Don't send purge notifications in an <items/> element
Florian Zeitz <florob@babelmonkeys.de>
parents: 5305
diff changeset
   607
	if not node_obj then
fdcd2ac7c22d mod_pubsub, util.pubsub: Don't send purge notifications in an <items/> element
Florian Zeitz <florob@babelmonkeys.de>
parents: 5305
diff changeset
   608
		return false, "item-not-found";
fdcd2ac7c22d mod_pubsub, util.pubsub: Don't send purge notifications in an <items/> element
Florian Zeitz <florob@babelmonkeys.de>
parents: 5305
diff changeset
   609
	end
8300
ac5c90230c2c util.pubsub: Clear data store if it supports being cleared, otherwise fall back to creating a new one
Kim Alvefur <zash@zash.se>
parents: 8223
diff changeset
   610
	if self.data[node] and self.data[node].clear then
ac5c90230c2c util.pubsub: Clear data store if it supports being cleared, otherwise fall back to creating a new one
Kim Alvefur <zash@zash.se>
parents: 8223
diff changeset
   611
		self.data[node]:clear()
ac5c90230c2c util.pubsub: Clear data store if it supports being cleared, otherwise fall back to creating a new one
Kim Alvefur <zash@zash.se>
parents: 8223
diff changeset
   612
	else
ac5c90230c2c util.pubsub: Clear data store if it supports being cleared, otherwise fall back to creating a new one
Kim Alvefur <zash@zash.se>
parents: 8223
diff changeset
   613
		self.data[node] = self.config.itemstore(self.nodes[node].config, node);
ac5c90230c2c util.pubsub: Clear data store if it supports being cleared, otherwise fall back to creating a new one
Kim Alvefur <zash@zash.se>
parents: 8223
diff changeset
   614
	end
9233
711fea229e48 util.pubsub: Add 'service' field to all events
Matthew Wild <mwild1@gmail.com>
parents: 9223
diff changeset
   615
	self.events.fire_event("node-purged", { service = self, node = node, actor = actor });
5312
fdcd2ac7c22d mod_pubsub, util.pubsub: Don't send purge notifications in an <items/> element
Florian Zeitz <florob@babelmonkeys.de>
parents: 5305
diff changeset
   616
	if notify then
10522
9f50489c2033 util.pubsub: Factor out calling of broadcaster
Kim Alvefur <zash@zash.se>
parents: 9844
diff changeset
   617
		self:broadcast("purge", node, node_obj.subscribers, nil, actor, node_obj);
5312
fdcd2ac7c22d mod_pubsub, util.pubsub: Don't send purge notifications in an <items/> element
Florian Zeitz <florob@babelmonkeys.de>
parents: 5305
diff changeset
   618
	end
fdcd2ac7c22d mod_pubsub, util.pubsub: Don't send purge notifications in an <items/> element
Florian Zeitz <florob@babelmonkeys.de>
parents: 5305
diff changeset
   619
	return true
5305
391b72fede9f mod_pubsub, util.pubsub: Implement the purge action
Kim Alvefur <zash@zash.se>
parents: 5181
diff changeset
   620
end
391b72fede9f mod_pubsub, util.pubsub: Implement the purge action
Kim Alvefur <zash@zash.se>
parents: 5181
diff changeset
   621
9820
7f84d7f77a00 util.pubsub: Add support for requesting multiple specific items (needed for #1305)
Kim Alvefur <zash@zash.se>
parents: 9746
diff changeset
   622
function service:get_items(node, actor, ids) --> (true, { id, [id] = node }) or (false, err)
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   623
	-- Access checking
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   624
	if not self:may(node, actor, "get_items") then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   625
		return false, "forbidden";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   626
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   627
	--
3641
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3626
diff changeset
   628
	local node_obj = self.nodes[node];
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   629
	if not node_obj then
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   630
		return false, "item-not-found";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   631
	end
9820
7f84d7f77a00 util.pubsub: Add support for requesting multiple specific items (needed for #1305)
Kim Alvefur <zash@zash.se>
parents: 9746
diff changeset
   632
	if type(ids) == "string" then -- COMPAT see #1305
7f84d7f77a00 util.pubsub: Add support for requesting multiple specific items (needed for #1305)
Kim Alvefur <zash@zash.se>
parents: 9746
diff changeset
   633
		ids = { ids };
7f84d7f77a00 util.pubsub: Add support for requesting multiple specific items (needed for #1305)
Kim Alvefur <zash@zash.se>
parents: 9746
diff changeset
   634
	end
7f84d7f77a00 util.pubsub: Add support for requesting multiple specific items (needed for #1305)
Kim Alvefur <zash@zash.se>
parents: 9746
diff changeset
   635
	local data = {};
7f84d7f77a00 util.pubsub: Add support for requesting multiple specific items (needed for #1305)
Kim Alvefur <zash@zash.se>
parents: 9746
diff changeset
   636
	if ids then
7f84d7f77a00 util.pubsub: Add support for requesting multiple specific items (needed for #1305)
Kim Alvefur <zash@zash.se>
parents: 9746
diff changeset
   637
		for _, key in ipairs(ids) do
7f84d7f77a00 util.pubsub: Add support for requesting multiple specific items (needed for #1305)
Kim Alvefur <zash@zash.se>
parents: 9746
diff changeset
   638
			local value = self.data[node]:get(key);
7f84d7f77a00 util.pubsub: Add support for requesting multiple specific items (needed for #1305)
Kim Alvefur <zash@zash.se>
parents: 9746
diff changeset
   639
			if value then
7f84d7f77a00 util.pubsub: Add support for requesting multiple specific items (needed for #1305)
Kim Alvefur <zash@zash.se>
parents: 9746
diff changeset
   640
				data[#data+1] = key;
7f84d7f77a00 util.pubsub: Add support for requesting multiple specific items (needed for #1305)
Kim Alvefur <zash@zash.se>
parents: 9746
diff changeset
   641
				data[key] = value;
7f84d7f77a00 util.pubsub: Add support for requesting multiple specific items (needed for #1305)
Kim Alvefur <zash@zash.se>
parents: 9746
diff changeset
   642
			end
8319
8648cb171213 util.pubsub: Return item-not-found if a single item is requested, and not there
Kim Alvefur <zash@zash.se>
parents: 8315
diff changeset
   643
		end
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   644
	else
7698
56ce32cfd6d9 util.pubsub: Switch to use util.cache for item data
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
   645
		for key, value in self.data[node]:items() do
7729
29c20eefa306 util.pubsub: Fix item retrieval by including the item order as it was before using util.cache (thanks walduhu)
Kim Alvefur <zash@zash.se>
parents: 7706
diff changeset
   646
			data[#data+1] = key;
7698
56ce32cfd6d9 util.pubsub: Switch to use util.cache for item data
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
   647
			data[key] = value;
56ce32cfd6d9 util.pubsub: Switch to use util.cache for item data
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
   648
		end
3641
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3626
diff changeset
   649
	end
9820
7f84d7f77a00 util.pubsub: Add support for requesting multiple specific items (needed for #1305)
Kim Alvefur <zash@zash.se>
parents: 9746
diff changeset
   650
	return true, data;
3641
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3626
diff changeset
   651
end
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3626
diff changeset
   652
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   653
function service:get_last_item(node, actor) --> (true, id, node) or (false, err)
8379
eb6a9c314c86 util.pubsub: Add method for retreiving the last item (useful for sending on subscribe)
Kim Alvefur <zash@zash.se>
parents: 8346
diff changeset
   654
	-- Access checking
eb6a9c314c86 util.pubsub: Add method for retreiving the last item (useful for sending on subscribe)
Kim Alvefur <zash@zash.se>
parents: 8346
diff changeset
   655
	if not self:may(node, actor, "get_items") then
eb6a9c314c86 util.pubsub: Add method for retreiving the last item (useful for sending on subscribe)
Kim Alvefur <zash@zash.se>
parents: 8346
diff changeset
   656
		return false, "forbidden";
eb6a9c314c86 util.pubsub: Add method for retreiving the last item (useful for sending on subscribe)
Kim Alvefur <zash@zash.se>
parents: 8346
diff changeset
   657
	end
eb6a9c314c86 util.pubsub: Add method for retreiving the last item (useful for sending on subscribe)
Kim Alvefur <zash@zash.se>
parents: 8346
diff changeset
   658
	--
9204
1555ea0d6f61 util.pubsub: get_last_item(): Return error if node does not exist
Matthew Wild <mwild1@gmail.com>
parents: 9203
diff changeset
   659
9206
c65bfddd3cc5 Backed out changeset 27d800ddc3b0 (see below)
Matthew Wild <mwild1@gmail.com>
parents: 9205
diff changeset
   660
	-- Check node exists
c65bfddd3cc5 Backed out changeset 27d800ddc3b0 (see below)
Matthew Wild <mwild1@gmail.com>
parents: 9205
diff changeset
   661
	if not self.nodes[node] then
9204
1555ea0d6f61 util.pubsub: get_last_item(): Return error if node does not exist
Matthew Wild <mwild1@gmail.com>
parents: 9203
diff changeset
   662
		return false, "item-not-found";
1555ea0d6f61 util.pubsub: get_last_item(): Return error if node does not exist
Matthew Wild <mwild1@gmail.com>
parents: 9203
diff changeset
   663
	end
9207
c5a81acc1fc3 util.pubsub: Fix whitespace [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 9206
diff changeset
   664
9198
b6ffd4f951b9 util.pubsub: Add comment to clarify return values
Matthew Wild <mwild1@gmail.com>
parents: 9182
diff changeset
   665
	-- Returns success, id, item
9209
33ee40dc3e25 Pubsub: Add tests for :get_last_item() and fix for non-persistent nodes
Matthew Wild <mwild1@gmail.com>
parents: 9207
diff changeset
   666
	return true, self.data[node]:head();
8379
eb6a9c314c86 util.pubsub: Add method for retreiving the last item (useful for sending on subscribe)
Kim Alvefur <zash@zash.se>
parents: 8346
diff changeset
   667
end
eb6a9c314c86 util.pubsub: Add method for retreiving the last item (useful for sending on subscribe)
Kim Alvefur <zash@zash.se>
parents: 8346
diff changeset
   668
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   669
function service:get_nodes(actor) --> (true, map) or (false, err)
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   670
	-- Access checking
3917
263a133bdf5a util.pubsub: Fix nil global access in get_nodes()
Matthew Wild <mwild1@gmail.com>
parents: 3910
diff changeset
   671
	if not self:may(nil, actor, "get_nodes") then
3910
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   672
		return false, "forbidden";
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   673
	end
80fe910f912a util.pubsub: Too many changes to list or split sensibly. Added access control to all methods, with capabilities support. Renamed get() -> get_items() and changed it to return true, result on success. Support for autocreate_on_subscribe and autocreate_on_publish config options.
Matthew Wild <mwild1@gmail.com>
parents: 3909
diff changeset
   674
	--
3759
1f7305784e12 util.pubsub: Add service:get_nodes()
Matthew Wild <mwild1@gmail.com>
parents: 3699
diff changeset
   675
	return true, self.nodes;
1f7305784e12 util.pubsub: Add service:get_nodes()
Matthew Wild <mwild1@gmail.com>
parents: 3699
diff changeset
   676
end
1f7305784e12 util.pubsub: Add service:get_nodes()
Matthew Wild <mwild1@gmail.com>
parents: 3699
diff changeset
   677
9034
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   678
local function flatten_subscriptions(ret, serv, subs, node, node_obj)
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   679
	for subscribed_jid, subscribed_nodes in pairs(subs) do
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   680
		if node then -- Return only subscriptions to this node
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   681
			if subscribed_nodes[node] then
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   682
				ret[#ret+1] = {
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   683
					node = node;
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   684
					jid = subscribed_jid;
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   685
					subscription = node_obj.subscribers[subscribed_jid];
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   686
				};
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   687
			end
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   688
		else -- Return subscriptions to all nodes
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   689
			local nodes = serv.nodes;
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   690
			for subscribed_node in pairs(subscribed_nodes) do
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   691
				ret[#ret+1] = {
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   692
					node = subscribed_node;
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   693
					jid = subscribed_jid;
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   694
					subscription = nodes[subscribed_node].subscribers[subscribed_jid];
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   695
				};
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   696
			end
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   697
		end
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   698
	end
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   699
end
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   700
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   701
function service:get_subscriptions(node, actor, jid) --> (true, array) or (false, err)
3938
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   702
	-- Access checking
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   703
	local cap;
4100
69e3f1e7111e util.pubsub: Pass true instead of nil as the actor in a bunch of places, and fix a bunch of methods to not traceback on this (those with *_other capability checking).
Matthew Wild <mwild1@gmail.com>
parents: 4099
diff changeset
   704
	if actor == true or jid == actor or self:jids_equal(actor, jid) then
3938
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   705
		cap = "get_subscriptions";
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   706
	else
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   707
		cap = "get_subscriptions_other";
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   708
	end
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   709
	if not self:may(node, actor, cap) then
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   710
		return false, "forbidden";
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   711
	end
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   712
	--
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   713
	local node_obj;
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   714
	if node then
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   715
		node_obj = self.nodes[node];
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   716
		if not node_obj then
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   717
			return false, "item-not-found";
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   718
		end
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   719
	end
9034
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   720
	local ret = {};
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   721
	if jid == nil then
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   722
		for _, subs in pairs(self.subscriptions) do
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   723
			flatten_subscriptions(ret, self, subs, node, node_obj)
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   724
		end
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   725
		return true, ret;
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   726
	end
3938
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   727
	local normal_jid = self.config.normalize_jid(jid);
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   728
	local subs = self.subscriptions[normal_jid];
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   729
	-- We return the subscription object from the node to save
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   730
	-- a get_subscription() call for each node.
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   731
	if subs then
9034
d1a4b1b78695 util.pubsub: Support for returning *all* subscriptions
Kim Alvefur <zash@zash.se>
parents: 8959
diff changeset
   732
		flatten_subscriptions(ret, self, subs, node, node_obj)
3938
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   733
	end
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   734
	return true, ret;
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   735
end
a3ecaf46bd82 util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
Matthew Wild <mwild1@gmail.com>
parents: 3937
diff changeset
   736
3928
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   737
-- Access models only affect 'none' affiliation caps, service/default access level...
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   738
function service:set_node_capabilities(node, actor, capabilities) --> ok, err
3928
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   739
	-- Access checking
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   740
	if not self:may(node, actor, "configure") then
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   741
		return false, "forbidden";
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   742
	end
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   743
	--
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   744
	local node_obj = self.nodes[node];
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   745
	if not node_obj then
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   746
		return false, "item-not-found";
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   747
	end
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   748
	node_obj.capabilities = capabilities;
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   749
	return true;
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   750
end
4dfb345e33ec util.pubsub: Add service:set_node_capabilities()
Matthew Wild <mwild1@gmail.com>
parents: 3917
diff changeset
   751
9235
65c83bfcf2ee util.pubsub: Comments describing the return values of methods
Kim Alvefur <zash@zash.se>
parents: 9233
diff changeset
   752
function service:set_node_config(node, actor, new_config) --> ok, err
6440
3f1f11dfdf10 util.pubsub: Add support for node configuration
Kim Alvefur <zash@zash.se>
parents: 6439
diff changeset
   753
	if not self:may(node, actor, "configure") then
3f1f11dfdf10 util.pubsub: Add support for node configuration
Kim Alvefur <zash@zash.se>
parents: 6439
diff changeset
   754
		return false, "forbidden";
3f1f11dfdf10 util.pubsub: Add support for node configuration
Kim Alvefur <zash@zash.se>
parents: 6439
diff changeset
   755
	end
3f1f11dfdf10 util.pubsub: Add support for node configuration
Kim Alvefur <zash@zash.se>
parents: 6439
diff changeset
   756
3f1f11dfdf10 util.pubsub: Add support for node configuration
Kim Alvefur <zash@zash.se>
parents: 6439
diff changeset
   757
	local node_obj = self.nodes[node];
3f1f11dfdf10 util.pubsub: Add support for node configuration
Kim Alvefur <zash@zash.se>
parents: 6439
diff changeset
   758
	if not node_obj then
3f1f11dfdf10 util.pubsub: Add support for node configuration
Kim Alvefur <zash@zash.se>
parents: 6439
diff changeset
   759
		return false, "item-not-found";
3f1f11dfdf10 util.pubsub: Add support for node configuration
Kim Alvefur <zash@zash.se>
parents: 6439
diff changeset
   760
	end
3f1f11dfdf10 util.pubsub: Add support for node configuration
Kim Alvefur <zash@zash.se>
parents: 6439
diff changeset
   761
9120
a19fdc6e4f09 util.pubsub: Apply defaults metatable before config check (thanks pep.)
Kim Alvefur <zash@zash.se>
parents: 9110
diff changeset
   762
	setmetatable(new_config, {__index=self.node_defaults})
a19fdc6e4f09 util.pubsub: Apply defaults metatable before config check (thanks pep.)
Kim Alvefur <zash@zash.se>
parents: 9110
diff changeset
   763
9078
46d4322f7eed util.pubsub: Add support for a config validation function
Matthew Wild <mwild1@gmail.com>
parents: 9034
diff changeset
   764
	if self.config.check_node_config then
46d4322f7eed util.pubsub: Add support for a config validation function
Matthew Wild <mwild1@gmail.com>
parents: 9034
diff changeset
   765
		local ok = self.config.check_node_config(node, actor, new_config);
46d4322f7eed util.pubsub: Add support for a config validation function
Matthew Wild <mwild1@gmail.com>
parents: 9034
diff changeset
   766
		if not ok then
46d4322f7eed util.pubsub: Add support for a config validation function
Matthew Wild <mwild1@gmail.com>
parents: 9034
diff changeset
   767
			return false, "not-acceptable";
46d4322f7eed util.pubsub: Add support for a config validation function
Matthew Wild <mwild1@gmail.com>
parents: 9034
diff changeset
   768
		end
46d4322f7eed util.pubsub: Add support for a config validation function
Matthew Wild <mwild1@gmail.com>
parents: 9034
diff changeset
   769
	end
46d4322f7eed util.pubsub: Add support for a config validation function
Matthew Wild <mwild1@gmail.com>
parents: 9034
diff changeset
   770
8954
9baac001fccb util.pubsub: Persist nodes on configuration change
Kim Alvefur <zash@zash.se>
parents: 8953
diff changeset
   771
	local old_config = node_obj.config;
9120
a19fdc6e4f09 util.pubsub: Apply defaults metatable before config check (thanks pep.)
Kim Alvefur <zash@zash.se>
parents: 9110
diff changeset
   772
	node_obj.config = new_config;
8954
9baac001fccb util.pubsub: Persist nodes on configuration change
Kim Alvefur <zash@zash.se>
parents: 8953
diff changeset
   773
9baac001fccb util.pubsub: Persist nodes on configuration change
Kim Alvefur <zash@zash.se>
parents: 8953
diff changeset
   774
	if self.config.nodestore then
9baac001fccb util.pubsub: Persist nodes on configuration change
Kim Alvefur <zash@zash.se>
parents: 8953
diff changeset
   775
		local ok, err = save_node_to_store(self, node_obj);
9baac001fccb util.pubsub: Persist nodes on configuration change
Kim Alvefur <zash@zash.se>
parents: 8953
diff changeset
   776
		if not ok then
9baac001fccb util.pubsub: Persist nodes on configuration change
Kim Alvefur <zash@zash.se>
parents: 8953
diff changeset
   777
			node_obj.config = old_config;
9baac001fccb util.pubsub: Persist nodes on configuration change
Kim Alvefur <zash@zash.se>
parents: 8953
diff changeset
   778
			return ok, "internal-server-error";
9baac001fccb util.pubsub: Persist nodes on configuration change
Kim Alvefur <zash@zash.se>
parents: 8953
diff changeset
   779
		end
6440
3f1f11dfdf10 util.pubsub: Add support for node configuration
Kim Alvefur <zash@zash.se>
parents: 6439
diff changeset
   780
	end
8404
f1923a79c93d util.pubsub: Recreate itemstore if persist_items changes or resize it if max_items changes
Kim Alvefur <zash@zash.se>
parents: 8385
diff changeset
   781
9141
db47db788295 util.pubsub: Re-check all subscriptions on access_model change, unsubscribing those no longer allowed
Kim Alvefur <zash@zash.se>
parents: 9132
diff changeset
   782
	if old_config["access_model"] ~= node_obj.config["access_model"] then
db47db788295 util.pubsub: Re-check all subscriptions on access_model change, unsubscribing those no longer allowed
Kim Alvefur <zash@zash.se>
parents: 9132
diff changeset
   783
		for subscriber in pairs(node_obj.subscribers) do
db47db788295 util.pubsub: Re-check all subscriptions on access_model change, unsubscribing those no longer allowed
Kim Alvefur <zash@zash.se>
parents: 9132
diff changeset
   784
			if not self:may(node, subscriber, "be_subscribed") then
db47db788295 util.pubsub: Re-check all subscriptions on access_model change, unsubscribing those no longer allowed
Kim Alvefur <zash@zash.se>
parents: 9132
diff changeset
   785
				local ok, err = self:remove_subscription(node, true, subscriber);
db47db788295 util.pubsub: Re-check all subscriptions on access_model change, unsubscribing those no longer allowed
Kim Alvefur <zash@zash.se>
parents: 9132
diff changeset
   786
				if not ok then
db47db788295 util.pubsub: Re-check all subscriptions on access_model change, unsubscribing those no longer allowed
Kim Alvefur <zash@zash.se>
parents: 9132
diff changeset
   787
					node_obj.config = old_config;
db47db788295 util.pubsub: Re-check all subscriptions on access_model change, unsubscribing those no longer allowed
Kim Alvefur <zash@zash.se>
parents: 9132
diff changeset
   788
					return ok, err;
db47db788295 util.pubsub: Re-check all subscriptions on access_model change, unsubscribing those no longer allowed
Kim Alvefur <zash@zash.se>
parents: 9132
diff changeset
   789
				end
db47db788295 util.pubsub: Re-check all subscriptions on access_model change, unsubscribing those no longer allowed
Kim Alvefur <zash@zash.se>
parents: 9132
diff changeset
   790
			end
db47db788295 util.pubsub: Re-check all subscriptions on access_model change, unsubscribing those no longer allowed
Kim Alvefur <zash@zash.se>
parents: 9132
diff changeset
   791
		end
db47db788295 util.pubsub: Re-check all subscriptions on access_model change, unsubscribing those no longer allowed
Kim Alvefur <zash@zash.se>
parents: 9132
diff changeset
   792
	end
db47db788295 util.pubsub: Re-check all subscriptions on access_model change, unsubscribing those no longer allowed
Kim Alvefur <zash@zash.se>
parents: 9132
diff changeset
   793
8954
9baac001fccb util.pubsub: Persist nodes on configuration change
Kim Alvefur <zash@zash.se>
parents: 8953
diff changeset
   794
	if old_config["persist_items"] ~= node_obj.config["persist_items"] then
9baac001fccb util.pubsub: Persist nodes on configuration change
Kim Alvefur <zash@zash.se>
parents: 8953
diff changeset
   795
		self.data[node] = self.config.itemstore(self.nodes[node].config, node);
9baac001fccb util.pubsub: Persist nodes on configuration change
Kim Alvefur <zash@zash.se>
parents: 8953
diff changeset
   796
	elseif old_config["max_items"] ~= node_obj.config["max_items"] then
9baac001fccb util.pubsub: Persist nodes on configuration change
Kim Alvefur <zash@zash.se>
parents: 8953
diff changeset
   797
		self.data[node]:resize(self.nodes[node].config["max_items"]);
9baac001fccb util.pubsub: Persist nodes on configuration change
Kim Alvefur <zash@zash.se>
parents: 8953
diff changeset
   798
	end
8404
f1923a79c93d util.pubsub: Recreate itemstore if persist_items changes or resize it if max_items changes
Kim Alvefur <zash@zash.se>
parents: 8385
diff changeset
   799
6440
3f1f11dfdf10 util.pubsub: Add support for node configuration
Kim Alvefur <zash@zash.se>
parents: 6439
diff changeset
   800
	return true;
3f1f11dfdf10 util.pubsub: Add support for node configuration
Kim Alvefur <zash@zash.se>
parents: 6439
diff changeset
   801
end
3f1f11dfdf10 util.pubsub: Add support for node configuration
Kim Alvefur <zash@zash.se>
parents: 6439
diff changeset
   802
9542
b30455212f89 util.pubsub: Clarify comment about return value
Kim Alvefur <zash@zash.se>
parents: 9519
diff changeset
   803
function service:get_node_config(node, actor) --> (true, config) or (false, err)
9110
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   804
	if not self:may(node, actor, "get_configuration") then
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   805
		return false, "forbidden";
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   806
	end
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   807
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   808
	local node_obj = self.nodes[node];
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   809
	if not node_obj then
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   810
		return false, "item-not-found";
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   811
	end
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   812
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   813
	local config_table = {};
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   814
	for k, v in pairs(default_node_config) do
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   815
		config_table[k] = v;
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   816
	end
9147
b40efef8ec99 util.pubsub: Include node defaults from current service object
Kim Alvefur <zash@zash.se>
parents: 9141
diff changeset
   817
	for k, v in pairs(self.node_defaults) do
b40efef8ec99 util.pubsub: Include node defaults from current service object
Kim Alvefur <zash@zash.se>
parents: 9141
diff changeset
   818
		config_table[k] = v;
b40efef8ec99 util.pubsub: Include node defaults from current service object
Kim Alvefur <zash@zash.se>
parents: 9141
diff changeset
   819
	end
9110
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   820
	for k, v in pairs(node_obj.config) do
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   821
		config_table[k] = v;
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   822
	end
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   823
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   824
	return true, config_table;
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   825
end
6e42ef9c805c util.pubsub: Add method to retrieve node configuration
Matthew Wild <mwild1@gmail.com>
parents: 9109
diff changeset
   826
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6518
diff changeset
   827
return {
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6518
diff changeset
   828
	new = new;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6518
diff changeset
   829
};