Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate
1
'''
2
Created on 15 Sep 2012
3
4
@author: cgueret
5
'''
6
from semanticxo import graphstore, util
7
from rdflib.term import Literal
8
from sugar import profile
9
10
class Messages(object):
11
	
12
	def __init__(self):
13
		'''
14
		Constructor
15
		'''
16
		pass
17
18
	
19
	def post(self, message):
20
		'''
21
		Post a new public message
22
		'''
23
		# Get a grasp over the graph store
24
		gstore = graphstore.get_instance()
25
		
26
		# Create a new graph
27
		graph = gstore.create_graph()
28
		
29
		# Create a resource of type Message and populate it
30
		resource = graph.create_resource(uid=None, category='Message')
31
		resource.add("message", Literal(message))
32
		resource.add("author", Literal(profile.get_nick_name()))
33
		
34
		# Allow this graph to be shared with other XOs
35
		graph.add_share(util.public_uri())
36
		
37
		# Persist the graph
38
		gstore.persist_graph(graph)
39
40
	
41
	def get_messages(self):
42
		'''
43
		Get the list of messages currently stored locally
44
		'''
45
		# Array for the results
46
		messages = []
47
		
48
		# Get a grasp over the graph store
49
		gstore = graphstore.get_instance()
50
		
51
		# Get the list of graphs that contain messages
52
		graph_ids = gstore.get_graphs_list(restrict='Message')
53
		
54
		for graph_id in graph_ids[::-1]:
55
			
56
			# Load the graph
57
			graph = gstore.get_graph(graph_id)
58
			
59
			# Get the list of resources that are of type "Message"
60
			# (a single graph could be used to store several messages)
61
			resource_ids = graph.get_resources_list(restrict='Message')
62
			
63
			for resource_id in resource_ids:
64
				
65
				# Load the resource
66
				resource = graph.get_resource(resource_id)
67
				
68
				# Get the properties we are interested in
69
				message = resource.get("message")[0]
70
				author = resource.get("author")[0]
71
				
72
				# Push that to the result list
73
				messages.append("%s (from: %s)" % (message, author))
74
				
75
		# Return the messages
76
		return messages
77