├── .gitignore ├── LICENSE ├── README.md ├── cache.go ├── go.mod ├── go.sum ├── main.go └── main.html /.gitignore: -------------------------------------------------------------------------------- 1 | /notionchanges 2 | /cache.json 3 | /space.id 4 | /notion.key 5 | *~ 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Unofficial recent changes lister for Notion.so sites 2 | 3 | Notion.so is a nice document database, but its notifications system is very 4 | bad: 5 | 6 | - By default it only shows you pages you've subscribed to. 7 | 8 | - If you subscribe to too many pages, or look at the "All" tab, the list of 9 | changes is very cluttered. 10 | 11 | - If someone works all day making 100 changes to a single page, and five 12 | other pages get one simple change each during the day, the 100 13 | notifications drown out the five notifications so you'll never see them. 14 | 15 | - The change view is in a tiny box that doesn't make full use of your 16 | browser window. 17 | 18 | - It's hard to tell which changes you have or haven't already looked at. 19 | 20 | - It's hard to tell which pages have changed recently and which haven't. 21 | 22 | This app, which uses the "unofficial" Notion.so api client from 23 | https://github.com/kjk/notionapi, tries to resolve all these issues in the 24 | simplest possible way: 25 | 26 | - In a single view, it shows each (somewhat recently modified) page in your 27 | notion database exactly once, along with who edited it most recently and 28 | which day it was edited. 29 | 30 | - Each page link contains a revision indicator so that your browser can tell 31 | if you've visited that revision before or not. The link colour changes if 32 | you have. This makes it easy to tell, at a glance, which revisions you've 33 | read or not read, and which things have changed since the last time you 34 | opened a page. 35 | 36 | - It's a totally dumb, flat, no-javascript html view that expands to fill 37 | the space available on your screen, and loads very fast. 38 | 39 | - That's it! 40 | 41 | 42 | ## Configuring notionchanges 43 | 44 | You need to provide two files, which must be in the current working 45 | directory at the time you start the `notionchanges` binary: 46 | 47 | - `notion.key`: a text file containing the `token_v2` cookie extracted from 48 | your notion session. The app uses this to access the notion API using your 49 | personal rights. You might need to refresh this file occasionally if you 50 | get logged out. 51 | 52 | - `space.id`: a text file containing the notion id of the "space" that forms the root of your 53 | notion database. You can find this by using Chrome's debugger to inspect 54 | the response to a `loadPageChunk` command in notion. (It generates at 55 | least one of these every time you load notion.) Look inside the returned 56 | `recordMap` object, then in the `space` object, and copy the uuid in 57 | there. [TODO: if none is configured, offer a list of spaces to choose 58 | from.] 59 | 60 | 61 | ## Running notionchanges 62 | 63 | To run the program, use these steps: 64 | 65 | - `git clone https://github.com/apenwarr/notionchanges` 66 | - `go run .` 67 | - Visit http://localhost:8187/ 68 | 69 | 70 | ## How do I control which users can see the page? 71 | 72 | It's not a good idea to publish the notionchanges view directly on the 73 | Internet, since it has no authentication. You can put it behind a proxy or 74 | control access using a VPN such as [Tailscale](https://tailscale.com/). 75 | 76 | 77 | ## How can I contribute? 78 | 79 | There are surely many ways to improve this project. 80 | 81 | Unfortunately I don't have time to fix most bugs or add new features. Sorry! 82 | But you can fork this repo and submit pull requests and I'll do my best to 83 | look at them and merge them if they're a good fit. 84 | -------------------------------------------------------------------------------- /cache.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "io/ioutil" 6 | "log" 7 | "os" 8 | 9 | "github.com/kjk/notionapi" 10 | ) 11 | 12 | const cacheFile = "cache.json" 13 | 14 | type Cache struct { 15 | client *notionapi.Client 16 | spaceID string 17 | 18 | ActivityIDs []string 19 | RecordMap notionapi.RecordMap 20 | } 21 | 22 | func NewCache(client *notionapi.Client, spaceID string) *Cache { 23 | rm := notionapi.RecordMap{ 24 | Activities: make(map[string]*notionapi.Record), 25 | Blocks: make(map[string]*notionapi.Record), 26 | Spaces: make(map[string]*notionapi.Record), 27 | Users: make(map[string]*notionapi.Record), 28 | Collections: make(map[string]*notionapi.Record), 29 | CollectionViews: make(map[string]*notionapi.Record), 30 | Comments: make(map[string]*notionapi.Record), 31 | Discussions: make(map[string]*notionapi.Record), 32 | } 33 | return &Cache{ 34 | client: client, 35 | spaceID: spaceID, 36 | 37 | ActivityIDs: []string{}, 38 | RecordMap: rm, 39 | } 40 | } 41 | 42 | func (c *Cache) Update() (changed bool) { 43 | next := "" 44 | n := 0 45 | 46 | nids := []string{} 47 | oids := c.ActivityIDs 48 | lookingFor := "" 49 | if len(oids) > 0 { 50 | lookingFor = oids[0] 51 | } 52 | 53 | limit := 1 54 | retrieve: 55 | for n < 1000 { 56 | log.Printf("Retrieving %-5d %q", limit, next) 57 | acts, err := c.client.GetActivityLog(c.spaceID, next, limit) 58 | if err != nil { 59 | log.Printf("GetActivityLog: %s\n", err) 60 | } 61 | 62 | mergeRecordMap(&c.RecordMap, acts.RecordMap) 63 | 64 | for _, aid := range acts.ActivityIDs { 65 | if aid == lookingFor { 66 | // caught up to old cached values 67 | nids = append(nids, oids...) 68 | break retrieve 69 | } else { 70 | nids = append(nids, aid) 71 | } 72 | n++ 73 | } 74 | 75 | next = acts.NextID 76 | if next == "" { 77 | break 78 | } 79 | limit = 20 80 | } 81 | 82 | c.ActivityIDs = nids 83 | return n > 0 84 | } 85 | 86 | func (c *Cache) Load() { 87 | b, err := ioutil.ReadFile(cacheFile) 88 | if err != nil { 89 | log.Printf("load cache: %v (ignored)", err) 90 | return 91 | } 92 | 93 | err = json.Unmarshal(b, c) 94 | if err != nil { 95 | log.Fatalf("unmarshal cache: %v", err) 96 | } 97 | 98 | err = notionapi.ParseRecordMap(&c.RecordMap) 99 | if err != nil { 100 | log.Fatalf("ParseRecordMap: %v", err) 101 | } 102 | } 103 | 104 | func (c *Cache) Save() { 105 | b, err := json.Marshal(c) 106 | if err != nil { 107 | log.Fatalf("cache marshal: %v", err) 108 | } 109 | newFile := cacheFile + ".new" 110 | ioutil.WriteFile(newFile, b, 0o666) 111 | err = os.Rename(newFile, cacheFile) 112 | if err != nil { 113 | log.Fatalf("rename %q -> %q: %v", newFile, cacheFile, err) 114 | } 115 | } 116 | 117 | func mergeRecords(into, from map[string]*notionapi.Record) { 118 | for k, v := range from { 119 | into[k] = v 120 | } 121 | } 122 | 123 | func mergeRecordMap(into, from *notionapi.RecordMap) { 124 | mergeRecords(into.Activities, from.Activities) 125 | mergeRecords(into.Blocks, from.Blocks) 126 | mergeRecords(into.Spaces, from.Spaces) 127 | mergeRecords(into.Users, from.Users) 128 | mergeRecords(into.Collections, from.Collections) 129 | mergeRecords(into.CollectionViews, from.CollectionViews) 130 | mergeRecords(into.Comments, from.Comments) 131 | mergeRecords(into.Discussions, from.Discussions) 132 | } 133 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/apenwarr/notionchanges 2 | 3 | go 1.13 4 | 5 | require github.com/kjk/notionapi v0.0.0-20200628022506-958cf2905933 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI= 2 | github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0= 3 | github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ= 4 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 5 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 6 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 7 | github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= 8 | github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= 9 | github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 10 | github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= 11 | github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= 12 | github.com/kjk/atomicfile v0.0.0-20190916063300-2d5c7d7d05bf/go.mod h1:+YlBbo63AHA3uS6tdRhd42B+I1lV7H7+aqDhwTRl5rs= 13 | github.com/kjk/caching_http_client v0.0.0-20190810075619-06ff809674f7/go.mod h1:uZMXWOA3unK8KYdy6aBIel64MMqZwsRFRKIvgSadLAU= 14 | github.com/kjk/notionapi v0.0.0-20200628022506-958cf2905933 h1:grY4TRLvgVVjSUafRG96jRUOx0j+TVwMDuB2Zds6sOE= 15 | github.com/kjk/notionapi v0.0.0-20200628022506-958cf2905933/go.mod h1:TN6VHOLMXTSoGSaXDKTqvw5Aos628adXJAXSKvjTTRI= 16 | github.com/kjk/siser v0.0.0-20190801014033-b3367920d7f2/go.mod h1:5VyY+9nF1wpSfWAIM/mIrd0fZCiA/6IK6CSf3PnjfMA= 17 | github.com/kjk/u v0.0.0-20191229080709-d1ac8976d53f/go.mod h1:5DUexog+kFLzpHxAQ7R9Of0N8DdhUjbpWGgnc41TMK4= 18 | github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 19 | github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= 20 | github.com/minio/minio-go/v6 v6.0.44/go.mod h1:qD0lajrGW49lKZLtXKtCB4X/qkMf0a5tBvN2PaZg7Gg= 21 | github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= 22 | github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= 23 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 24 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 25 | github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= 26 | github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= 27 | github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= 28 | github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= 29 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 30 | github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 31 | github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= 32 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 33 | github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= 34 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 35 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 36 | golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 37 | golang.org/x/crypto v0.0.0-20191227163750-53104e6ec876/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 38 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 39 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 40 | golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= 41 | golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 42 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 43 | golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 44 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 45 | golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 46 | golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 47 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 48 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 49 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 50 | golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 51 | gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= 52 | gopkg.in/ini.v1 v1.51.1/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= 53 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "html/template" 6 | "io/ioutil" 7 | "log" 8 | "net/http" 9 | "strings" 10 | "sync" 11 | "time" 12 | 13 | "github.com/kjk/notionapi" 14 | ) 15 | 16 | var keyFile = "notion.key" 17 | var spaceFile = "space.id" 18 | 19 | func lastEditor(cache *Cache, a *notionapi.Activity) (email string, lastEdit time.Time) { 20 | var last int64 21 | email = "" 22 | for _, e := range a.Edits { 23 | if e.Timestamp > last { 24 | last = e.Timestamp 25 | for _, auth := range e.Authors { 26 | u := cache.RecordMap.Users[auth.ID] 27 | if u != nil && u.User != nil { 28 | s := strings.Split(u.User.Email, "@") 29 | email = s[0] 30 | } 31 | } 32 | } 33 | } 34 | 35 | when := time.Unix(last/1000, (last%1000)*1000000) 36 | return email, when 37 | } 38 | 39 | func blockTitles(cache *Cache, id string) (titles []string, err error) { 40 | r := cache.RecordMap.Blocks[id] 41 | for r != nil { 42 | if r.Block != nil && r.Block.GetTitle() != nil { 43 | ts := r.Block.GetTitle() 44 | titles = append(titles, notionapi.TextSpansToString(ts)) 45 | } else if r.Collection != nil && r.Collection.Name != nil { 46 | ts := r.Collection.GetName() 47 | titles = append(titles, ts) 48 | } 49 | id = r.ID 50 | r = parentOf(cache, r) 51 | } 52 | if len(titles) > 0 { 53 | return titles, nil 54 | } 55 | 56 | if r == nil { 57 | return nil, fmt.Errorf("no block object for %q", id) 58 | } else if r.Block == nil { 59 | return nil, fmt.Errorf("no block sub-object for %q", id) 60 | } else { 61 | return nil, fmt.Errorf("title is empty for %q", id) 62 | } 63 | } 64 | 65 | type Page struct { 66 | ID string 67 | NavID string 68 | When time.Time 69 | Who string 70 | Event string 71 | 72 | // populated in second pass 73 | Permitted bool 74 | Title string 75 | Path []string 76 | PathLast string 77 | } 78 | 79 | func (p *Page) URL() string { 80 | return fmt.Sprintf("https://notion.so/%s?__stamp=%d", 81 | strings.ReplaceAll(p.NavID, "-", ""), 82 | p.When.Unix()) 83 | } 84 | 85 | func (p *Page) Date() string { 86 | return p.When.Format("2006-01-02") 87 | } 88 | 89 | func readString(filename string) string { 90 | b, err := ioutil.ReadFile(filename) 91 | if err != nil { 92 | log.Fatalf("%v", err) 93 | } 94 | return strings.TrimSpace(string(b)) 95 | } 96 | 97 | func parentOf(cache *Cache, r *notionapi.Record) *notionapi.Record { 98 | pid := "" 99 | if r == nil { 100 | return nil 101 | } else if r.Block != nil { 102 | pid = r.Block.ParentID 103 | } else if r.Collection != nil { 104 | pid = r.Collection.ParentID 105 | } else if r.CollectionView != nil { 106 | pid = r.CollectionView.ParentID 107 | } 108 | if pid == "" { 109 | return nil 110 | } 111 | if x, ok := cache.RecordMap.Blocks[pid]; ok { 112 | return x 113 | } 114 | if x, ok := cache.RecordMap.Collections[pid]; ok { 115 | return x 116 | } 117 | if x, ok := cache.RecordMap.CollectionViews[pid]; ok { 118 | return x 119 | } 120 | if x, ok := cache.RecordMap.Spaces[pid]; ok { 121 | return x 122 | } 123 | return nil 124 | } 125 | 126 | func checkPermitted(cache *Cache, id string) bool { 127 | r := cache.RecordMap.Blocks[id] 128 | for r != nil { 129 | if r.Block != nil && !r.Block.Alive { 130 | return false 131 | } else if r.Collection != nil && !r.Collection.Alive { 132 | return false 133 | } 134 | if r.Block != nil && r.Block.Permissions != nil { 135 | for _, p := range *r.Block.Permissions { 136 | if p.Type == "space_permission" { 137 | // visible to everyone in workspace, so it's 138 | // ok to reveal in the log. 139 | return true 140 | } 141 | } 142 | } 143 | 144 | r = parentOf(cache, r) 145 | } 146 | 147 | return false 148 | } 149 | 150 | func collectHistory(cache *Cache) []Page { 151 | pages := []Page{} 152 | seen := map[string]struct{}{} 153 | 154 | for _, actid := range cache.ActivityIDs { 155 | act := cache.RecordMap.Activities[actid].Activity 156 | if act == nil { 157 | log.Printf("missing activity: %v", actid) 158 | continue 159 | } 160 | 161 | nav := "" 162 | if act.NavigableBlockID != "" { 163 | nav = act.NavigableBlockID 164 | } else if act.CollectionRowID != "" { 165 | nav = act.CollectionRowID 166 | } else if act.CollectionID != "" { 167 | nav = act.ParentID 168 | } 169 | 170 | if _, ok := seen[nav]; ok { 171 | // already seen, earlier entry wins 172 | continue 173 | } 174 | 175 | email, when := lastEditor(cache, act) 176 | p := Page{ 177 | ID: act.ParentID, 178 | NavID: nav, 179 | When: when, 180 | Who: email, 181 | Event: act.Type, 182 | } 183 | pages = append(pages, p) 184 | seen[nav] = struct{}{} 185 | } 186 | 187 | for i := range pages { 188 | p := &pages[i] 189 | 190 | titles, err := blockTitles(cache, p.ID) 191 | if err != nil { 192 | p.Title = fmt.Sprintf("%s", err) 193 | } else { 194 | p.Title = titles[0] 195 | titles = titles[1:] 196 | if len(titles) > 0 { 197 | p.PathLast = titles[0] 198 | } 199 | for i := range titles { 200 | p.Path = append(p.Path, titles[len(titles)-1-i]) 201 | } 202 | } 203 | 204 | p.Permitted = checkPermitted(cache, p.ID) 205 | } 206 | 207 | return pages 208 | } 209 | 210 | func main() { 211 | client := ¬ionapi.Client{ 212 | AuthToken: readString(keyFile), 213 | } 214 | spaceID := readString(spaceFile) 215 | 216 | t, err := template.ParseFiles("main.html") 217 | if err != nil { 218 | log.Fatalf("main.html template: %v", err) 219 | } 220 | 221 | var mu sync.Mutex 222 | var lastUpdated time.Time 223 | 224 | cache := NewCache(client, spaceID) 225 | cache.Load() 226 | 227 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 228 | // It's too much work to figure out concurrency here, just 229 | // skip it. We don't expect a lot of parallel requests. 230 | mu.Lock() 231 | defer mu.Unlock() 232 | 233 | // Refresh template so we don't have to recompile every time 234 | // while debugging. 235 | t, err = template.ParseFiles("main.html") 236 | if err != nil { 237 | log.Fatalf("main.html template: %v", err) 238 | } 239 | 240 | if time.Since(lastUpdated) > time.Second*60 { 241 | changed := cache.Update() 242 | if changed { 243 | cache.Save() 244 | } 245 | lastUpdated = time.Now() 246 | } 247 | 248 | pages := collectHistory(cache) 249 | args := struct { 250 | Pages []Page 251 | }{ 252 | Pages: pages, 253 | } 254 | 255 | t.Execute(w, args) 256 | }) 257 | 258 | log.Fatal(http.ListenAndServe(":8187", nil)) 259 | } 260 | -------------------------------------------------------------------------------- /main.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |