1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.messagestream.script

File MessageStreamScriptService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
28
8
1
184
90
13
0.46
3.5
8
1.62

Classes

Class Line # Actions
MessageStreamScriptService 42 28 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 11 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.messagestream.script;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25   
26    import org.xwiki.bridge.DocumentAccessBridge;
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.context.Execution;
29    import org.xwiki.messagestream.MessageStream;
30    import org.xwiki.messagestream.MessageStreamConfiguration;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.script.service.ScriptService;
33   
34    /**
35    * Service exposing the {@link MessageStream} functionality, allowing to post messages from the current user.
36    *
37    * @version $Id: 54536fb82c5b50951189471dfb98c6be10ca6369 $
38    */
39    @Component
40    @Named("messageStream")
41    @Singleton
 
42    public class MessageStreamScriptService implements ScriptService
43    {
44    /**
45    * The key under which the last encountered error is stored in the current execution context.
46    */
47    static final String ERROR_KEY = "scriptservice.messageStream.error";
48   
49    /**
50    * Provides access to the current context.
51    */
52    @Inject
53    protected Execution execution;
54   
55    /** The wrapped stream that is exposed in this service. */
56    @Inject
57    private MessageStream stream;
58   
59    @Inject
60    private MessageStreamConfiguration messageStreamConfiguration;
61   
62    @Inject
63    private DocumentAccessBridge documentAccessBridge;
64   
65    /**
66    * Post a message to the user's stream, visible to everyone.
67    *
68    * @param message the message to store
69    * @return {@code true} if the message was successfully posted, {@code false} otherwise
70    */
 
71  2 toggle public boolean postPublicMessage(String message)
72    {
73  2 try {
74  2 this.stream.postPublicMessage(message);
75  1 return true;
76    } catch (Exception e) {
77  1 setError(e);
78  1 return false;
79    }
80    }
81   
82    /**
83    * Post a message to the user's personal stream, displayed on his profile page and aggregated into their follower's
84    * streams.
85    *
86    * @param message the message to store
87    * @return {@code true} if the message was successfully posted, {@code false} otherwise
88    */
 
89  2 toggle public boolean postPersonalMessage(String message)
90    {
91  2 try {
92  2 this.stream.postPersonalMessage(message);
93  1 return true;
94    } catch (Exception e) {
95  1 setError(e);
96  1 return false;
97    }
98    }
99   
100    /**
101    * Post a private message to another user.
102    *
103    * @param message the message to send
104    * @param user the target user
105    * @return {@code true} if the message was successfully posted, {@code false} otherwise
106    */
 
107  2 toggle public boolean postDirectMessageToUser(String message, DocumentReference user)
108    {
109  2 try {
110  2 this.stream.postDirectMessageToUser(message, user);
111  1 return true;
112    } catch (Exception e) {
113  1 setError(e);
114  1 return false;
115    }
116    }
117   
118    /**
119    * Post a message to a specific group of users.
120    *
121    * @param message the message to send
122    * @param group the target group
123    * @return {@code true} if the message was successfully posted, {@code false} otherwise
124    */
 
125  2 toggle public boolean postMessageToGroup(String message, DocumentReference group)
126    {
127  2 try {
128  2 this.stream.postMessageToGroup(message, group);
129  1 return true;
130    } catch (Exception e) {
131  1 setError(e);
132  1 return false;
133    }
134    }
135   
136    /**
137    * Delete an existing message, identified by its unique ID, if the current user is the author of that message.
138    *
139    * @param id the unique ID of the message
140    * @return {@code true} if the message was successfully deleted, {@code false} otherwise
141    */
 
142  2 toggle public boolean deleteMessage(String id)
143    {
144  2 try {
145  2 this.stream.deleteMessage(id);
146  1 return true;
147    } catch (Exception e) {
148  1 setError(e);
149  1 return false;
150    }
151    }
152   
153    /**
154    * @return true if the Message Stream feature is active or false otherwise
155    * @since 8.4RC1
156    */
 
157  289 toggle public boolean isActive()
158    {
159  289 return messageStreamConfiguration.isActive(
160    documentAccessBridge.getCurrentDocumentReference().getWikiReference().getName()
161    );
162    }
163   
164    /**
165    * Get the error generated while performing the previously called action.
166    *
167    * @return the exception or {@code null} if no exception was thrown
168    */
 
169  4 toggle public Exception getLastError()
170    {
171  4 return (Exception) this.execution.getContext().getProperty(ERROR_KEY);
172    }
173   
174    /**
175    * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}.
176    *
177    * @param e the exception to store, can be {@code null} to clear the previously stored exception
178    * @see #getLastError()
179    */
 
180  5 toggle protected void setError(Exception e)
181    {
182  5 this.execution.getContext().setProperty(ERROR_KEY, e);
183    }
184    }