aboutsummaryrefslogtreecommitdiffstats
path: root/src/py/notice.py
blob: 564aa96b14ee290fb16a3b84877447d2a4dfb7d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from tojauth import TOJAuth
from asyncdb import AsyncDB
import mod
from imc.proxy import Proxy
import imc.proxy
import config

class Notice:
    _accessid = 5

    NOTICE_LIST_NUM = 10

    def __init__(self, mod_idendesc, get_link_fn):
        Notice.db = AsyncDB(config.CORE_DBNAME, config.CORE_DBUSER, 
                            config.CORE_DBPASSWORD)
        Notice._idendesc = mod_idendesc
        self.get_link = get_link_fn

        Proxy.instance.register_call(
            'core/notice/', 'list_notice', self.list_notice)
        Proxy.instance.register_call(
            'core/notice/', 'read_notice', self.read_notice)
        Proxy.instance.register_call(
            'core/notice/', 'del_notice', self.del_notice)
        Proxy.instance.register_call(
            'core/notice/', 'get_unseen_count', self.get_unseen_count)

    def unload(self):
        Proxy.instance.unregister_call(
            'core/notice/', 'list_notice')
        Proxy.instance.unregister_call(
            'core/notice/', 'read_notice')
        Proxy.instance.unregister_call(
            'core/notice/', 'del_notice')
        Proxy.instance.unregister_call(
            'core/notice/', 'get_unseen_count')

    @TOJAuth.check_access(_accessid, TOJAuth.ACCESS_EXECUTE)
    def send_notice(self, uid, title, content, noticemod_path, metadata):
        cur = self.db.cursor()

        noticemodid = None

        if noticemod_path != None: 
            sqlstr = ('SELECT "noticemodid" FROM "NOTICEMOD" WHERE "path" '
                      '= %s;')
            sqlarr = (noticemod_path, )
            cur.execute(sqlstr, sqlarr)

            for data in cur:
                noticemodid = data[0]

            if noticemodid == None:
                sqlstr = ('INSERT INTO "NOTICEMOD" ("path") VALUES (%s) '
                          'RETURNING "noticemodid";')
                sqlarr = (noticemod_path, )
                cur.execute(sqlstr, sqlarr)
                for data in cur:
                    noticemodid = data[0]

        sqlstr = ('INSERT INTO "NOTICE" ("uid", "title", "content", '
                  '"noticemodid", "metadata") VALUES (%s, %s, %s, %s, %s) '
                  'RETURNING "noticeid";')
        sqlarr = (uid, title, content, noticemodid, metadata)
        cur.execute(sqlstr, sqlarr)

        noticeid = None
        for data in cur:
            noticeid = data[0]

        unseen_count = self._get_unseen_count(uid)
        self.set_unseen_count(uid, unseen_count + 1)

        return noticeid

    @imc.async.caller
    def del_notice(self, noticeid):
        if(
            type(noticeid) != int
        ):
            return 'Eparameter'

        notice = self.get_notice(noticeid)
        if notice == None:
            return 'Enoticeid'

        with TOJAuth.change_current_iden(self._idendesc):
            self._del_notice(noticeid)
            self.notify_client(notice['uid'])

        return 'Success'

    @TOJAuth.check_access(_accessid, TOJAuth.ACCESS_EXECUTE)
    def _del_notice(self, noticeid):    
        cur = self.db.cursor()
        sqlstr = ('DELETE FROM "NOTICE" WHERE "noticeid" = %s;')
        sqlarr = (noticeid, )
        cur.execute(sqlstr, sqlarr)
    
    @imc.async.caller
    def list_notice(self, start_index = 0, list_num = NOTICE_LIST_NUM):
        if(
            type(start_index) != int or
            type(list_num) != int
        ):
            return 'Eparameter'

        uid = mod.UserMg.get_current_uid()
        if uid == None:
            return 'Euid'

        with TOJAuth.change_current_iden(self._idendesc):
            ret = self._list_notice(uid, start_index, list_num)

        return ret

    @TOJAuth.check_access(_accessid, TOJAuth.ACCESS_EXECUTE)
    def _list_notice(self, uid, start_index, list_num):
        cur = self.db.cursor()
        sqlstr = ('SELECT "noticeid", "title", "content", "time", '
                  '"noticemodid", "metadata", "unread" FROM "NOTICE" WHERE '
                  '"uid" = %s ORDER BY "noticeid" DESC LIMIT %s OFFSET %s;')
        sqlarr = (uid, list_num, start_index)
        cur.execute(sqlstr, sqlarr)

        ret = []
        for data in cur:
            obj = {}
            obj['noticeid'] = data[0]
            obj['title'] = data[1]
            obj['content'] = data[2]
            obj['time'] = data[3]
            obj['noticemodid'] = data[4]
            obj['metadata'] = data[5]
            obj['unread'] = data[6]
            ret.append(obj)

        self.set_unseen_count(uid, 0)

        return ret

    @imc.async.caller
    def read_notice(self, noticeid):
        if(
            type(noticeid) != int
        ):
            return 'Eparameter'

        notice = self.get_notice(noticeid)
        if notice == None:
            return 'Enoticeid'

        with TOJAuth.change_current_iden(self._idendesc):
            self.set_notice_unread(noticeid, False)

        return 'Success'

    @TOJAuth.check_access(_accessid, TOJAuth.ACCESS_EXECUTE)
    def set_notice_unread(self, noticeid, unread):
        cur = self.db.cursor()
        sqlstr = ('UPDATE "NOTICE" SET "unread" = %s WHERE "noticeid" = %s;')
        sqlarr = (unread, noticeid)
        cur.execute(sqlstr, sqlarr)

    @imc.async.caller
    def get_unseen_count(self):
        uid = mod.UserMg.get_current_uid()
        if uid == None:
            return 'Euid'

        with TOJAuth.change_current_iden(self._idendesc):
            unseen_count = self._get_unseen_count(uid)

        return {'unseen_count': unseen_count}

    @TOJAuth.check_access(_accessid, TOJAuth.ACCESS_EXECUTE)
    def _get_unseen_count(self, uid):
        cur = self.db.cursor()
        sqlstr = ('SELECT "unseen_count" FROM "NOTICE_UNSEEN" WHERE '
                  '"uid" = %s;')
        sqlarr = (uid, )
        cur.execute(sqlstr, sqlarr)

        unseen_count = None
        for data in cur:
            unseen_count = data[0]

        return unseen_count

    @TOJAuth.check_access(_accessid, TOJAuth.ACCESS_EXECUTE)
    def set_unseen_count(self, uid, unseen_count):
        old_unseen_count = self._get_unseen_count(uid)

        cur = self.db.cursor()
        sqlstr = ('UPDATE "NOTICE_UNSEEN" SET "unseen_count" = %s WHERE '
                  '"uid" = %s;')
        sqlarr = (unseen_count, uid)
        cur.execute(sqlstr, sqlarr)

        if old_unseen_count != unseen_count:
            self.notify_client(uid)

    @TOJAuth.check_access(_accessid, TOJAuth.ACCESS_EXECUTE)
    def create_unseen_count(self, uid):
        cur = self.db.cursor()
        sqlstr = ('INSERT INTO "NOTICE_UNSEEN" ("uid", "unseen_count") '
                  'VALUES (%s, %s);')
        sqlarr = (uid, 0)
        cur.execute(sqlstr, sqlarr)

    @TOJAuth.check_access(_accessid, TOJAuth.ACCESS_EXECUTE)
    def notify_client(self, uid):
        unseen_count = self._get_unseen_count(uid)

        for link in self.get_link('client', uid = uid):
            Proxy.instance.call_async(
                link + 'core/notice/', 'update_notice', 10000, None, 
                unseen_count
            )

    def get_notice(self, noticeid):
        cur = self.db.cursor()
        sqlstr = ('SELECT "noticeid", "uid", "title", "content", "time", '
                  '"noticemodid", "metadata", "unread" FROM "NOTICE" WHERE '
                  '"noticeid" = %s;')
        sqlarr = (noticeid, )
        cur.execute(sqlstr, sqlarr)

        ret = None
        for data in cur:
            ret = {}
            ret['noticeid'] = data[0]
            ret['uid'] = data[1]
            ret['title'] = data[2]
            ret['content'] = data[3]
            ret['time'] = data[4]
            ret['noticemodid'] = data[5]
            ret['metadata'] = data[6]
            ret['unread'] = data[7]

        if ret == None:
            return None

        uid = mod.UserMg.get_current_uid()
        if uid != ret['uid']:
            TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_EXECUTE)

        return ret