summaryrefslogtreecommitdiff
path: root/plugins/YammerImport/yammerimporter.php
blob: 7710d41b52a990c088719d143282ae6ead292412 (plain)
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
<?php
/*
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2010, StatusNet, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Basic client class for Yammer's OAuth/JSON API.
 *
 * @package YammerImportPlugin
 * @author Brion Vibber <brion@status.net>
 */
class YammerImporter
{

    /**
     * Load or create an imported profile from Yammer data.
     * 
     * @param object $item loaded JSON data for Yammer importer
     * @return Profile
     */
    function importUserProfile($item)
    {
        $data = $this->prepUser($item);

        $profileId = $this->findImportedProfile($data['orig_id']);
        if ($profileId) {
            return Profile::staticGet('id', $profileId);
        } else {
            $user = User::register($data['options']);
            // @fixme set avatar!
            return $user->getProfile();
        }
    }

    /**
     * Load or create an imported group from Yammer data.
     *
     * @param object $item loaded JSON data for Yammer importer
     * @return User_group
     */
    function importGroup($item)
    {
        $data = $this->prepGroup($item);

        $groupId = $this->findImportedGroup($data['orig_id']);
        if ($groupId) {
            return User_group::staticGet('id', $groupId);
        } else {
            $group = User_group::register($data['options']);
            // @fixme set avatar!
            return $group;
        }
    }

    /**
     * Load or create an imported notice from Yammer data.
     *
     * @param object $item loaded JSON data for Yammer importer
     * @return Notice
     */
    function importNotice($item)
    {
        $data = $this->prepNotice($item);

        $noticeId = $this->findImportedNotice($data['orig_id']);
        if ($noticeId) {
            return Notice::staticGet('id', $noticeId);
        } else {
            $notice = Notice::saveNew($data['profile'],
                                      $data['content'],
                                      $data['source'],
                                      $data['options']);
            // @fixme attachments?
            return $notice;
        }
    }

    function prepUser($item)
    {
        if ($item['type'] != 'user') {
            throw new Exception('Wrong item type sent to Yammer user import processing.');
        }

        $origId = $item['id'];
        $origUrl = $item['url'];

        // @fixme check username rules?

        $options['nickname'] = $item['name'];
        $options['fullname'] = trim($item['full_name']);

        // We don't appear to have full bio avail here!
        $options['bio'] = $item['job_title'];

        // What about other data like emails?

        // Avatar... this will be the "_small" variant.
        // Remove that (pre-extension) suffix to get the orig-size image.
        $avatar = $item['mugshot_url'];

        // Warning: we don't have following state for other users?

        return array('orig_id' => $origId,
                     'orig_url' => $origUrl,
                     'avatar' => $avatar,
                     'options' => $options);

    }

    function prepGroup($item)
    {
        if ($item['type'] != 'group') {
            throw new Exception('Wrong item type sent to Yammer group import processing.');
        }

        $origId = $item['id'];
        $origUrl = $item['url'];

        $privacy = $item['privacy']; // Warning! only public groups in SN so far

        $options['nickname'] = $item['name'];
        $options['fullname'] = $item['full_name'];
        $options['created'] = $this->timestamp($item['created_at']);

        $avatar = $item['mugshot_url']; // as with user profiles...


        $options['mainpage'] = common_local_url('showgroup',
                                   array('nickname' => $options['nickname']));

        // @fixme what about admin user for the group?
        // bio? homepage etc? aliases?

        $options['local'] = true;
        return array('orig_id' => $origId,
                     'orig_url' => $origUrl,
                     'options' => $options);
    }

    function prepNotice($item)
    {
        if (isset($item['type']) && $item['type'] != 'message') {
            throw new Exception('Wrong item type sent to Yammer message import processing.');
        }

        $origId = $item['id'];
        $origUrl = $item['url'];

        $profile = $this->findImportedProfile($item['sender_id']);
        $content = $item['body']['plain'];
        $source = 'yammer';
        $options = array();

        if ($item['replied_to_id']) {
            $replyto = $this->findImportedNotice($item['replied_to_id']);
            if ($replyto) {
                $options['replyto'] = $replyto;
            }
        }
        $options['created'] = $this->timestamp($item['created_at']);

        // Parse/save rendered text?
        // Save liked info?
        // @todo attachments?

        return array('orig_id' => $origId,
                     'orig_url' => $origUrl,
                     'profile' => $profile,
                     'content' => $content,
                     'source' => $source,
                     'options' => $options);
    }

    function findImportedProfile($userId)
    {
        // @fixme
        return $userId;
    }

    function findImportedGroup($groupId)
    {
        // @fixme
        return $groupId;
    }

    function findImportedNotice($messageId)
    {
        // @fixme
        return $messageId;
    }

    /**
     * Normalize timestamp format.
     * @param string $ts
     * @return string
     */
    function timestamp($ts)
    {
        return common_sql_date(strtotime($ts));
    }
}